|
@@ -47,36 +47,67 @@ sub aliases {
|
|
|
|
|
|
|
|
sub add_post ($post,$data_obj) {
|
|
sub add_post ($post,$data_obj) {
|
|
|
my $dbh = _dbh();
|
|
my $dbh = _dbh();
|
|
|
|
|
+ build_index($data_obj,[$post]);
|
|
|
build_routes($data_obj,[$post]);
|
|
build_routes($data_obj,[$post]);
|
|
|
- return build_index($data_obj,[$post]);
|
|
|
|
|
|
|
+ return 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
sub remove_post ($post) {
|
|
sub remove_post ($post) {
|
|
|
my $dbh = _dbh();
|
|
my $dbh = _dbh();
|
|
|
- $dbh->do("DELETE FROM routes WHERE route=?", undef, $post->{local_href});
|
|
|
|
|
- return $dbh->do("DELETE FROM posts_index WHERE post_id=?", undef, $post->{id});
|
|
|
|
|
|
|
+
|
|
|
|
|
+ # Deleting the post will cascade to the post index & primary route, which cascades to the aliases
|
|
|
|
|
+ $dbh->do("DELETE FROM post WHERE uuid=?", undef, $post->{id});
|
|
|
|
|
+
|
|
|
|
|
+ # Now that we've wasted the routes and post, let's reap any dangling tags or callbacks.
|
|
|
|
|
+ # We won't ever reap methods, because they're just HTTP methods in an enum table.
|
|
|
|
|
+ $dbh->do("DELETE from callbacks WHERE id NOT IN (SELECT DISTINCT callback_id FROM routes)");
|
|
|
|
|
+ $dbh->do("DELETE from tag WHERE id NOT IN (SELECT DISTINCT tag_id FROM posts_index)");
|
|
|
|
|
+ return 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
sub build_index($data_obj,$posts=[]) {
|
|
sub build_index($data_obj,$posts=[]) {
|
|
|
my $dbh = _dbh();
|
|
my $dbh = _dbh();
|
|
|
$posts = $data_obj->read({ limit => 0, acls => ['admin'] }) unless @$posts;
|
|
$posts = $data_obj->read({ limit => 0, acls => ['admin'] }) unless @$posts;
|
|
|
|
|
|
|
|
|
|
+ # First, slap in the UUIDs
|
|
|
|
|
+ my @uuids = map { $_->{id} } @$posts;
|
|
|
|
|
+ Trog::SQLite::bulk_insert($dbh,'post',['uuid'],'IGNORE', @uuids);
|
|
|
|
|
+ my $pids = _id_for_uuid($dbh,@uuids);
|
|
|
|
|
+ foreach my $post (@$posts) {
|
|
|
|
|
+ $post->{post_id} = $pids->{$post->{id}}{id};
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ # Slap in the tags
|
|
|
my @tags = uniq map { @{$_->{tags}} } @$posts;
|
|
my @tags = uniq map { @{$_->{tags}} } @$posts;
|
|
|
Trog::SQLite::bulk_insert($dbh,'tag', ['name'], 'IGNORE', @tags);
|
|
Trog::SQLite::bulk_insert($dbh,'tag', ['name'], 'IGNORE', @tags);
|
|
|
|
|
+ #TODO restrict query to only the specific tags we care about
|
|
|
my $t = $dbh->selectall_hashref("SELECT id,name FROM tag", 'name');
|
|
my $t = $dbh->selectall_hashref("SELECT id,name FROM tag", 'name');
|
|
|
foreach my $k (keys(%$t)) { $t->{$k} = $t->{$k}->{id} };
|
|
foreach my $k (keys(%$t)) { $t->{$k} = $t->{$k}->{id} };
|
|
|
|
|
|
|
|
|
|
+ # Finally, index the posts
|
|
|
Trog::SQLite::bulk_insert($dbh,'posts_index',[qw{post_id post_time tag_id}], 'IGNORE', map {
|
|
Trog::SQLite::bulk_insert($dbh,'posts_index',[qw{post_id post_time tag_id}], 'IGNORE', map {
|
|
|
my $subj = $_;
|
|
my $subj = $_;
|
|
|
- map { ( $subj->{id}, $subj->{created}, $t->{$_} ) } @{$subj->{tags}}
|
|
|
|
|
|
|
+ map { ( $subj->{post_id}, $subj->{created}, $t->{$_} ) } @{$subj->{tags}}
|
|
|
} @$posts );
|
|
} @$posts );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+sub _id_for_uuid($dbh,@uuids) {
|
|
|
|
|
+ my $bind = join(',', (map { '?' } @uuids));
|
|
|
|
|
+ Trog::SQLite::bulk_insert($dbh,'post',['uuid'],'IGNORE', @uuids);
|
|
|
|
|
+ return $dbh->selectall_hashref("SELECT id,uuid FROM post WHERE uuid IN ($bind)", 'uuid', {}, @uuids);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
# It is important we use get() instead of read() because of incomplete data.
|
|
# It is important we use get() instead of read() because of incomplete data.
|
|
|
sub build_routes($data_obj,$posts=[]) {
|
|
sub build_routes($data_obj,$posts=[]) {
|
|
|
my $dbh = _dbh();
|
|
my $dbh = _dbh();
|
|
|
@$posts = $data_obj->get( limit => 0, acls => ['admin'] ) unless @$posts;
|
|
@$posts = $data_obj->get( limit => 0, acls => ['admin'] ) unless @$posts;
|
|
|
|
|
|
|
|
|
|
+ my @uuids = map { $_->{id} } @$posts;
|
|
|
|
|
+ my $pids = _id_for_uuid($dbh,@uuids);
|
|
|
|
|
+ foreach my $post (@$posts) {
|
|
|
|
|
+ $post->{post_id} = $pids->{$post->{id}}{id};
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
# Ensure the callbacks we need are installed
|
|
# Ensure the callbacks we need are installed
|
|
|
Trog::SQLite::bulk_insert($dbh,'callbacks', [qw{callback}], 'IGNORE', (uniq map { $_->{callback} } @$posts) );
|
|
Trog::SQLite::bulk_insert($dbh,'callbacks', [qw{callback}], 'IGNORE', (uniq map { $_->{callback} } @$posts) );
|
|
|
|
|
|
|
@@ -90,8 +121,8 @@ sub build_routes($data_obj,$posts=[]) {
|
|
|
$_
|
|
$_
|
|
|
} @$posts;
|
|
} @$posts;
|
|
|
|
|
|
|
|
- my @routes = map { ($_->{local_href}, $_->{method_id}, $_->{callback_id} ) } @$posts;
|
|
|
|
|
- Trog::SQLite::bulk_insert($dbh,'routes', [qw{route method_id callback_id}], 'IGNORE', @routes);
|
|
|
|
|
|
|
+ my @routes = map { ($_->{post_id}, $_->{local_href}, $_->{method_id}, $_->{callback_id} ) } @$posts;
|
|
|
|
|
+ Trog::SQLite::bulk_insert($dbh,'routes', [qw{post_id route method_id callback_id}], 'IGNORE', @routes);
|
|
|
|
|
|
|
|
# Now, compile the post aliases
|
|
# Now, compile the post aliases
|
|
|
my %routes_actual = routes();
|
|
my %routes_actual = routes();
|