package Trog::Data::FlatFile; use strict; use warnings; no warnings 'experimental'; use feature qw{signatures}; use Carp qw{confess}; use JSON::MaybeXS; use File::Slurper; use File::Copy; use Mojo::File; use parent qw{Trog::DataModule}; our $datastore = 'data/files'; sub lang { 'Perl Regex in Quotemeta' } sub help { 'https://perldoc.perl.org/functions/quotemeta.html' } =head1 Trog::Data::FlatFile This data model has multiple drawbacks, but is "good enough" for most low-content and few editor applications. You can only post once per second due to it storing each post as a file named after the timestamp. =cut our $parser = JSON::MaybeXS->new(); sub read ($self, $query={}) { #Optimize direct ID my @index; if ($query->{id}) { @index = ("$datastore/$query->{id}"); } else { @index = $self->_index(); } $query->{limit} //= 25; my @items; foreach my $item (@index) { next unless -f $item; my $slurped = eval { File::Slurper::read_text($item) }; if (!$slurped) { print "Failed to Read $item:\n$@\n"; next; } my $parsed = $parser->decode($slurped); #XXX this imposes an inefficiency in itself, get() will filter uselessly again here my @filtered = $self->filter($query,@$parsed); push(@items,@filtered) if @filtered; last if scalar(@items) == $query->{limit}; } return \@items; } sub _index ($self) { confess "Can't find datastore!" unless -d $datastore; opendir(my $dh, $datastore) or confess; my @index = grep { -f } map { "$datastore/$_" } readdir $dh; closedir $dh; return sort { $b cmp $a } @index; } sub write($self,$data) { foreach my $post (@$data) { my $file = "$datastore/$post->{id}"; my $update = [$post]; if (-f $file) { my $slurped = File::Slurper::read_text($file); my $parsed = $parser->decode($slurped); $update = [(@$parsed, $post)]; } open(my $fh, '>', $file) or confess; print $fh $parser->encode($update); close $fh; } } sub count ($self) { my @index = $self->_index(); return scalar(@index); } sub add ($self,@posts) { my $ctime = time(); @posts = map { $_->{id} //= $ctime; $_->{created} = $ctime; $_ } @posts; return $self->SUPER::add(@posts); } sub delete($self, @posts) { foreach my $update (@posts) { unlink "$datastore/$update->{id}" or confess; } return 0; } 1;