1 |
package Frey::CouchAPI; |
package Frey::CouchAPI; |
2 |
|
|
3 |
|
=head1 DESCRIPTION |
4 |
|
|
5 |
|
This is REST wrapper using L<Mojo::Transaction> to implement Apache's CouchDB API |
6 |
|
|
7 |
|
=head1 Supported HTTP API |
8 |
|
|
9 |
|
=cut |
10 |
|
|
11 |
use warnings; |
use warnings; |
12 |
use strict; |
use strict; |
13 |
|
|
108 |
|
|
109 |
} elsif ( $url =~ m{($regex_dbs)/$} ) { |
} elsif ( $url =~ m{($regex_dbs)/$} ) { |
110 |
|
|
111 |
|
=head2 Database |
112 |
|
|
113 |
|
L<http://wiki.apache.org/couchdb/HTTP_database_API> except compaction |
114 |
|
|
115 |
|
=cut |
116 |
|
|
117 |
my $database = $1; |
my $database = $1; |
118 |
my $dir = "$config->{path}/$database"; |
my $dir = "$config->{path}/$database"; |
119 |
|
|
136 |
} elsif ( $url =~ m{($regex_dbs)/([^?]+)\??(.+)?$} ) { |
} elsif ( $url =~ m{($regex_dbs)/([^?]+)\??(.+)?$} ) { |
137 |
my ($database,$id,$args) = ($1,$2,$3); |
my ($database,$id,$args) = ($1,$2,$3); |
138 |
|
|
139 |
|
=head2 Document |
140 |
|
|
141 |
|
L<http://wiki.apache.org/couchdb/HTTP_Document_API> |
142 |
|
|
143 |
|
=cut |
144 |
|
|
145 |
my $arg; |
my $arg; |
146 |
if ( $args ) { |
if ( $args ) { |
147 |
foreach my $a ( split(/[&;]/,$args) ) { |
foreach my $a ( split(/[&;]/,$args) ) { |
155 |
warn "ERROR: path $path doesn't exist\n" unless -e $path; |
warn "ERROR: path $path doesn't exist\n" unless -e $path; |
156 |
|
|
157 |
my $p = "$path/$database/$id"; |
my $p = "$path/$database/$id"; |
158 |
warn "## database: $database id: $id -> $p [$args]\n"; |
warn "## database: $database id: $id -> $p ", dump( $arg ),"\n"; |
159 |
|
|
160 |
|
|
161 |
if ( $id =~ m{_all_docs(\w+)?$} ) { |
if ( $id =~ m{_all_docs(\w+)?$} ) { |
163 |
my $by = $1; |
my $by = $1; |
164 |
my $offset = 0; |
my $offset = 0; |
165 |
my $startkey = delete $arg->{startkey}; |
my $startkey = delete $arg->{startkey}; |
166 |
warn "STARTKEY: $startkey\n"; |
my $endkey = delete $arg->{endkey}; |
167 |
|
my $limit = delete $arg->{limit}; |
168 |
my $total_rows = 0; |
my $total_rows = 0; |
169 |
|
|
170 |
my @docs = grep { length $_ } map { |
my @docs = grep { length $_ } map { |
171 |
|
return '' if defined $limit && $total_rows == $limit; |
172 |
|
|
173 |
s{^$path/$database/}{}; |
s{^$path/$database/}{}; |
174 |
|
return '' if defined $endkey && $_ gt $endkey; |
175 |
|
|
176 |
if ( $startkey ) { |
if ( $startkey ) { |
177 |
if ( $_ >= $startkey ) { |
if ( $_ ge $startkey ) { |
178 |
$total_rows++; |
$total_rows++; |
179 |
$_; |
$_; |
180 |
} else { |
} else { |
181 |
$offset++; |
$offset++; |
182 |
|
return ''; |
183 |
} |
} |
184 |
} else { |
} else { |
185 |
$total_rows++; |
$total_rows++; |
186 |
$_; |
$_; |
187 |
} |
} |
188 |
|
|
189 |
} glob( "$path/$database/*" ); |
} glob( "$path/$database/*" ); |
190 |
|
|
191 |
warn "## docs ", dump( @docs ) if $debug; |
warn "## docs $startkey -> $endkey limit $limit ", dump( @docs ); # if $debug; |
192 |
|
|
193 |
$json = { |
$json = { |
194 |
total_rows => $total_rows, |
total_rows => $total_rows, |
293 |
} |
} |
294 |
|
|
295 |
1; |
1; |
296 |
|
__END__ |
297 |
|
|
298 |
|
=head1 SEE ALSO |
299 |
|
|
300 |
|
L<http://wiki.apache.org/couchdb/Reference> |
301 |
|
|