1 package MojoFacets::Import::CouchDB;
9 use Data::Dump qw(dump);
13 __PACKAGE__->attr('path');
14 __PACKAGE__->attr('full_path');
16 sub ext { '.couchdb' };
19 my ($flat,$data,$prefix) = @_;
20 if ( ref $data eq '' ) {
21 push @{ $$flat->{$prefix} }, $data;
22 } elsif ( ref $data eq 'HASH' ) {
23 foreach my $key ( keys %$data ) {
24 my $full_prefix = $prefix ? $prefix . '_' : '';
26 flatten( $flat, $data->{$key}, $full_prefix );
28 } elsif ( ref $data eq 'ARRAY' ) {
29 foreach my $el ( @$data ) {
30 flatten( $flat, $el, $prefix );
32 } elsif ( ref $data eq 'Mojo::JSON::_Bool' ) {
33 push @{ $$flat->{$prefix} }, $data;
35 die "unsupported ",ref($data)," from ",dump($data);
42 my $path = $self->path;
44 my $url = read_file $self->full_path;
47 $url .= '/_all_docs?include_docs=true' unless $url =~ m/\?/;
49 warn "# CouchDB URL: $url";
51 # fix "Maximum message size exceeded"
52 $ENV{MOJO_MAX_MESSAGE_SIZE} = 64 * 1024 * 1024;
54 my $tx = Mojo::UserAgent->new->get($url);
57 if ( my $res = $tx->success) {
65 if ( ref $json->{rows} eq 'ARRAY' ) {
66 foreach my $doc ( @{$json->{rows}} ) {
67 if ( exists $doc->{id} && exists $doc->{doc} ) {
68 next if $doc->{id} =~ m{^_design/}; # $doc->{id} == $doc->{doc}->{_id}
70 flatten( \$flat, $doc->{doc}, '' );
71 push @{ $data->{items} }, $flat;
73 push @{ $data->{items} }, $doc;
77 die "can't find rows in ",dump( $res->body );