TagIndex.pm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package Trog::SQLite::TagIndex;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures};
  6. use List::Util qw{uniq};
  7. use Trog::SQLite;
  8. =head1 Trog::SQLite::TagIndex
  9. An SQLite3 index of posts by tag and date.
  10. Used to speed up the flat-file data model.
  11. Also used to retrieve cached routes from posts.
  12. =head1 FUNCTIONS
  13. =cut
  14. sub posts_for_tags (@tags) {
  15. my $dbh = _dbh();
  16. my $clause = @tags ? "WHERE tag IN (".join(',' ,(map {'?'} @tags)).")" : '';
  17. my $rows = $dbh->selectall_arrayref("SELECT DISTINCT id FROM posts $clause ORDER BY created DESC",{ Slice => {} }, @tags);
  18. return () unless ref $rows eq 'ARRAY' && @$rows;
  19. return map { $_->{id} } @$rows;
  20. }
  21. sub routes {
  22. my $dbh = _dbh();
  23. my $rows = $dbh->selectall_arrayref("SELECT id, route, method, callback FROM all_routes",{ Slice => {} });
  24. return () unless ref $rows eq 'ARRAY' && @$rows;
  25. my %routes = map { $_->{route} => $_ } @$rows;
  26. return %routes;
  27. }
  28. sub aliases {
  29. my $dbh = _dbh();
  30. my $rows = $dbh->selectall_arrayref("SELECT actual,alias FROM aliases", { Slice => {} });
  31. return () unless ref $rows eq 'ARRAY' && @$rows;
  32. my %aliases = map { $_->{alias} => $_->{actual} } @$rows;
  33. return %aliases;
  34. }
  35. sub add_post ($post,$data_obj) {
  36. my $dbh = _dbh();
  37. build_routes($data_obj,[$post]);
  38. return build_index($data_obj,[$post]);
  39. }
  40. sub remove_post ($post) {
  41. my $dbh = _dbh();
  42. $dbh->do("DELETE FROM routes WHERE route=?", undef, $post->{local_href});
  43. return $dbh->do("DELETE FROM posts_index WHERE post_id=?", undef, $post->{id});
  44. }
  45. sub build_index($data_obj,$posts=[]) {
  46. my $dbh = _dbh();
  47. $posts = $data_obj->read({ limit => 0, acls => ['admin'] }) unless @$posts;
  48. my @tags = uniq map { @{$_->{tags}} } @$posts;
  49. Trog::SQLite::bulk_insert($dbh,'tag', ['name'], 'IGNORE', @tags);
  50. my $t = $dbh->selectall_hashref("SELECT id,name FROM tag", 'name');
  51. foreach my $k (keys(%$t)) { $t->{$k} = $t->{$k}->{id} };
  52. Trog::SQLite::bulk_insert($dbh,'posts_index',[qw{post_id post_time tag_id}], 'IGNORE', map {
  53. my $subj = $_;
  54. map { ( $subj->{id}, $subj->{created}, $t->{$_} ) } @{$subj->{tags}}
  55. } @$posts );
  56. }
  57. # It is important we use get() instead of read() because of incomplete data.
  58. sub build_routes($data_obj,$posts=[]) {
  59. my $dbh = _dbh();
  60. @$posts = $data_obj->get( limit => 0, acls => ['admin'] ) unless @$posts;
  61. # Ensure the callbacks we need are installed
  62. Trog::SQLite::bulk_insert($dbh,'callbacks', [qw{callback}], 'IGNORE', (uniq map { $_->{callback} } @$posts) );
  63. my $m = $dbh->selectall_hashref("SELECT id, method FROM methods", 'method');
  64. foreach my $k (keys(%$m)) { $m->{$k} = $m->{$k}->{id} };
  65. my $c = $dbh->selectall_hashref("SELECT id, callback FROM callbacks", 'callback');
  66. foreach my $k (keys(%$c)) { $c->{$k} = $c->{$k}->{id} };
  67. @$posts = map {
  68. $_->{method_id} = $m->{$_->{method}};
  69. $_->{callback_id} = $c->{$_->{callback}};
  70. $_
  71. } @$posts;
  72. my @routes = map { ($_->{local_href}, $_->{method_id}, $_->{callback_id} ) } @$posts;
  73. Trog::SQLite::bulk_insert($dbh,'routes', [qw{route method_id callback_id}], 'IGNORE', @routes);
  74. # Now, compile the post aliases
  75. my %routes_actual = routes();
  76. foreach my $post (@$posts) {
  77. next unless (ref $post->{aliases} eq 'ARRAY') && @{$post->{aliases}};
  78. my $route = $post->{local_href};
  79. Trog::SQLite::bulk_insert($dbh, 'post_aliases', [qw{route_id alias}], 'IGNORE', map { ($routes_actual{$route}{id}, $_) } @{$post->{aliases}} );
  80. }
  81. }
  82. # Ensure the db schema is OK, and give us a handle
  83. sub _dbh {
  84. my $file = 'schema/flatfile.schema';
  85. my $dbname = "data/posts.db";
  86. return Trog::SQLite::dbh($file,$dbname);
  87. }
  88. 1;