George S. Baugh 5 ani în urmă
părinte
comite
e8cb5df691
2 a modificat fișierele cu 34 adăugiri și 2 ștergeri
  1. 1 1
      Makefile
  2. 33 1
      lib/Trog/Data/FlatFile.pm

+ 1 - 1
Makefile

@@ -15,7 +15,7 @@ reset-dummy-data:
 
 .PHONY: depend
 depend:
-	sudo apt install -y sqlite3 libsqlite3-dev libdbd-sqlite3-perl cpanminus starman  libcal-dav-perl libtext-xslate-perl libserver-starter-perl libplack-perl libcal-dav-perl libconfig-tiny-perl libdatetime-format-http-perl libjson-maybexs-perl libuuid-tiny-perl libcapture-tiny-perl libconfig-simple-perl libdbi-perl libfile-slurper-perl libfile-touch-perl libfile-copy-recursive-perl libxml-rss-perl libmodule-install-perl
+	sudo apt install -y sqlite3 libsqlite3-dev libdbd-sqlite3-perl cpanminus starman  libcal-dav-perl libtext-xslate-perl libserver-starter-perl libplack-perl libcal-dav-perl libconfig-tiny-perl libdatetime-format-http-perl libjson-maybexs-perl libuuid-tiny-perl libcapture-tiny-perl libconfig-simple-perl libdbi-perl libfile-slurper-perl libfile-touch-perl libfile-copy-recursive-perl libxml-rss-perl libmodule-install-perl libio-aio-perl
 	sudo cpanm Mojo::File Date::Format WWW::SitemapIndex::XML WWW::Sitemap::XML HTTP::Body Pod::Html URL::Encode
 	wget -O www/scripts/fgEmojiPicker.js https://github.com/woody180/vanilla-javascript-emoji-picker/raw/master/fgEmojiPicker.js
 	wget -O www/scripts/full-emoji-list.json https://github.com/woody180/vanilla-javascript-emoji-picker/raw/master/full-emoji-list.json

+ 33 - 1
lib/Trog/Data/FlatFile.pm

@@ -11,6 +11,7 @@ use JSON::MaybeXS;
 use File::Slurper;
 use File::Copy;
 use Mojo::File;
+use IO::AIO 2;
 
 use parent qw{Trog::DataModule};
 
@@ -37,9 +38,40 @@ sub read ($self, $query={}) {
     }
     $query->{limit} //= 25;
 
+    my $done = 0;
+    my $grp = aio_group sub {
+        $done = 1;
+    };
+    #TODO up the limit of group appropriately
+
+    my $contents = {};
+    my $num_read = 0;
+    @index = grep { -f } @index;
+
     my @items;
+    feed $grp sub {
+        my $file = shift @index or return;
+        add $grp (aio_slurp $file, 0, 0, $contents->{$file}, sub {
+
+            #Don't waste any time if we dont have to
+            return if scalar(@items) >= $query->{limit};
+
+            my $parsed  = $parser->decode($contents->{$file});
+
+            #XXX this imposes an inefficiency in itself, get() will filter uselessly again here later
+            my @filtered = $self->filter($query,@$parsed);
+            push(@items,@filtered) if @filtered;
+        });
+    };
+    while (@index && !$done) {
+        IO::AIO::poll_cb();
+        last if scalar(@items) == $query->{limit};
+    }
+    $grp->cancel();
+    @items = sort {$b->{created} <=> $a->{created} } @items;
+    return \@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";