FlatFile.pm 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package Trog::Data::FlatFile;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures};
  6. use Carp qw{confess};
  7. use JSON::MaybeXS;
  8. use File::Slurper;
  9. use File::Copy;
  10. use Mojo::File;
  11. use parent qw{Trog::DataModule};
  12. our $datastore = 'data/files';
  13. sub lang { 'Perl Regex in Quotemeta' }
  14. sub help { 'https://perldoc.perl.org/functions/quotemeta.html' }
  15. =head1 Trog::Data::FlatFile
  16. This data model has multiple drawbacks, but is "good enough" for most low-content and few editor applications.
  17. You can only post once per second due to it storing each post as a file named after the timestamp.
  18. =cut
  19. our $parser = JSON::MaybeXS->new();
  20. sub read ($self, $query={}) {
  21. #Optimize direct ID
  22. my @index;
  23. if ($query->{id}) {
  24. @index = ("$datastore/$query->{id}");
  25. } else {
  26. @index = $self->_index();
  27. }
  28. $query->{limit} //= 25;
  29. my @items;
  30. foreach my $item (@index) {
  31. next unless -f $item;
  32. my $slurped = eval { File::Slurper::read_text($item) };
  33. if (!$slurped) {
  34. print "Failed to Read $item:\n$@\n";
  35. next;
  36. }
  37. my $parsed = $parser->decode($slurped);
  38. #XXX this imposes an inefficiency in itself, get() will filter uselessly again here
  39. my @filtered = $self->filter($query,@$parsed);
  40. push(@items,@filtered) if @filtered;
  41. last if scalar(@items) == $query->{limit};
  42. }
  43. return \@items;
  44. }
  45. sub _index ($self) {
  46. confess "Can't find datastore!" unless -d $datastore;
  47. opendir(my $dh, $datastore) or confess;
  48. my @index = grep { -f } map { "$datastore/$_" } readdir $dh;
  49. closedir $dh;
  50. return sort { $b cmp $a } @index;
  51. }
  52. sub write($self,$data) {
  53. foreach my $post (@$data) {
  54. my $file = "$datastore/$post->{id}";
  55. my $update = [$post];
  56. if (-f $file) {
  57. my $slurped = File::Slurper::read_text($file);
  58. my $parsed = $parser->decode($slurped);
  59. $update = [(@$parsed, $post)];
  60. }
  61. open(my $fh, '>', $file) or confess;
  62. print $fh $parser->encode($update);
  63. close $fh;
  64. }
  65. }
  66. sub count ($self) {
  67. my @index = $self->_index();
  68. return scalar(@index);
  69. }
  70. sub add ($self,@posts) {
  71. my $ctime = time();
  72. @posts = map {
  73. $_->{id} //= $ctime;
  74. $_->{created} = $ctime;
  75. $_
  76. } @posts;
  77. return $self->SUPER::add(@posts);
  78. }
  79. sub delete($self, @posts) {
  80. foreach my $update (@posts) {
  81. unlink "$datastore/$update->{id}" or confess;
  82. }
  83. return 0;
  84. }
  85. 1;