CouchDB _all_docs importer
authorDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 14 Dec 2010 21:24:25 +0000 (22:24 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 14 Dec 2010 21:24:25 +0000 (22:24 +0100)
lib/MojoFacets/Data.pm
lib/MojoFacets/Import/CouchDB.pm [new file with mode: 0644]

index 478ebaa..d5b4cf5 100644 (file)
@@ -19,6 +19,7 @@ use Digest::MD5;
 use MojoFacets::Import::File;
 use MojoFacets::Import::HTMLTable;
 use MojoFacets::Import::CSV;
 use MojoFacets::Import::File;
 use MojoFacets::Import::HTMLTable;
 use MojoFacets::Import::CSV;
+use MojoFacets::Import::CouchDB;
 
 our $loaded;
 our $filters;
 
 our $loaded;
 our $filters;
@@ -41,10 +42,7 @@ sub index {
                } elsif ( -d $file && $file =~ m/\.html$/ ) {
                        $file =~ s/$data_dir\/*//;
                        push @files, $file;
                } elsif ( -d $file && $file =~ m/\.html$/ ) {
                        $file =~ s/$data_dir\/*//;
                        push @files, $file;
-               } elsif ( -f $file && $file =~ m/\.csv$/i ) {
-                       $file =~ s/$data_dir\/*//;
-                       push @files, $file;
-               } elsif ( -f $file && $file =~ m/\.storable/i ) {
+               } elsif ( -f $file && $file =~ m/\.(csv|storabe|couchdb)$/i ) {
                        $file =~ s/$data_dir\/*//;
                        push @files, $file;
                } else {
                        $file =~ s/$data_dir\/*//;
                        push @files, $file;
                } else {
@@ -190,6 +188,8 @@ sub _load_path {
        if ( -f $full_path ) {
                if ( $full_path =~ m/.csv/i ) {
                        $data = MojoFacets::Import::CSV->new( full_path => $full_path )->data;
        if ( -f $full_path ) {
                if ( $full_path =~ m/.csv/i ) {
                        $data = MojoFacets::Import::CSV->new( full_path => $full_path )->data;
+               } elsif ( $full_path =~ m/.couchdb/i ) {
+                       $data = MojoFacets::Import::CouchDB->new( full_path => $full_path )->data;
                } elsif ( $full_path =~ m/.storable/ ) {
                        warn "open $full_path ", -s $full_path, " bytes";
                        open(my $pipe, "<", $full_path) || die $!;
                } elsif ( $full_path =~ m/.storable/ ) {
                        warn "open $full_path ", -s $full_path, " bytes";
                        open(my $pipe, "<", $full_path) || die $!;
diff --git a/lib/MojoFacets/Import/CouchDB.pm b/lib/MojoFacets/Import/CouchDB.pm
new file mode 100644 (file)
index 0000000..670152a
--- /dev/null
@@ -0,0 +1,42 @@
+package MojoFacets::Import::CouchDB;
+
+use warnings;
+use strict;
+
+use base 'Mojo::Base';
+
+use File::Slurp;
+use Data::Dump qw(dump);
+use JSON;
+use Mojo::Client;
+
+__PACKAGE__->attr('path');
+__PACKAGE__->attr('full_path');
+
+sub data {
+       my $self = shift;
+
+       my $path = $self->path;
+
+       # we could use Mojo::JSON here, but it's too slow
+#      $data = from_json read_file $path;
+       my $url = read_file $self->full_path;
+       $url =~ s{/\s*$}{}s;
+
+       warn "# CouchDB URL: $url";
+
+       my $json = Mojo::Client->new->get( "$url/_all_docs?include_docs=true" )->res->json;
+
+       my $data;
+
+       if ( ref $json->{rows} eq 'ARRAY' ) {
+               foreach my $doc ( @{$json->{rows}} ) {
+                       push @{ $data->{items} }, $doc->{doc};
+               }
+       }
+
+       return $data;
+
+}
+
+1