/[Frey]/trunk/lib/Frey/CouchAPI.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/Frey/CouchAPI.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1046 by dpavlin, Wed Apr 22 22:01:06 2009 UTC revision 1051 by dpavlin, Thu Apr 23 19:35:26 2009 UTC
# Line 1  Line 1 
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;
12    use strict;
13    
14  use JSON;  use JSON;
15  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
16  use URI::Escape;  use URI::Escape;
17    use File::Path qw(make_path remove_tree);
18    use Storable;
19    
20    our $VERSION = '0.1';
21    $VERSION .= ' on Frey ' . $Frey::VERSION;
22    
23    our $debug = $Frey::debug || 0;
24    
25  sub rewrite_urls {  sub rewrite_urls {
26          my ( $self, $tx ) = @_;          my ( $self, $tx ) = @_;
# Line 18  sub rewrite_urls { Line 36  sub rewrite_urls {
36          }          }
37  }  }
38    
39  my $path = '/data/webpac2/var/row';  our $config = {
40            path => '/data/webpac2/var/row',
41    };
42    
43    my $p = $config->{path};
44  my @all_dbs = map {  my @all_dbs = map {
45          s{^\Q$path\E/*}{};          s{^\Q$p\E/*}{};
46          $_;          $_;
47  } glob "$path/*/*";  } glob "$p/*/*";
48    
49    my $regex_dbs = '[a-z][a-z0-9_\$\(\)\+\-/]+';
50    
51    our $json = {};
52    our $status = 500;
53    
54    sub ok {
55            $json = { ok => JSON::true };
56            $status = 200;
57    }
58    
 my $regex_dbs = join('|', @all_dbs);  
59    
60  sub dispatch {  sub dispatch {
61          my ($self,$tx) = @_;          my ($self,$tx) = @_;
62    
63          my $url = $tx->req->url->to_string;          my $url = $tx->req->url->to_string;
64          $url = uri_unescape( $url );          $url = uri_unescape( $url );
65            my $method = $tx->req->method;
66          warn "INFO: using Apache CouchDB emulation API $url\n";          
   
         warn "## tx = ",dump( $tx );  
   
         my $json = {};  
   
67          if ( $url eq '/' ) {          if ( $url eq '/' ) {
68                  $json = {                  $json = {
69                          couchdb => "Emulated on Frey",                          couchdb => "Welcome",
70                          version => 0,                          version => "CouchAPI $VERSION",
71                  }                  }
72          } elsif ( $url eq '/_all_dbs' ) {          } elsif ( $url eq '/_all_dbs' ) {
73                  $json = [ @all_dbs ];                  $json = [ @all_dbs ];
74          } elsif ( $url =~ m{($regex_dbs)} ) {                  $status = 200;
75                  warn "# collecting docs from $path/$1/*\n";          } elsif ( $url =~ m{^/_config/?(.+)} ) {
76                  my @docs = glob "$path/$1/*";  
77                  $json = {                  $json = { CouchAPI => $config };
78                          db_name => $1,  
79                          doc_count => $#docs + 1,                  if ( $method eq 'PUT' ) {
80                          doc_del_count => 0,  
81                          update_seq => 0,                          my $part = $1;
82                          purge_seq => 0,                          warn "## part $part";
83                          capacity_running => JSON::false,  
84                          disk_size => 0,                          $part =~ s!^!->{'!;
85                          instance_start_time => time(),                          $part =~ s!/!'}->{'!;
86                  };                          $part =~ s/$/'}/;
87    
88                            my $data = $tx->req->content->file->slurp;
89                            $data = JSON->new->allow_nonref->decode( $data );
90                            warn "## data ",dump( $data );
91                            # poor man's transaction :-)
92                            my $code = "\$json$part = \$data; \$config$part = \$data;";
93                            eval $code;
94                            if ( $@ ) {
95                                    warn "ERROR: $code -> $@";
96                                    $status = 500;
97                            } else {
98                                    $status = 200;
99                            }
100    
101    warn "json ",dump( $json ), " config ", dump( $config );
102    
103                    } elsif ( $method eq 'GET' ) {
104                            $status = 200;
105                    } else {
106                            $status = 501;
107                    }
108    
109            } elsif ( $url =~ m{($regex_dbs)/$} ) {
110    
111    =head2 Database
112    
113    L<http://wiki.apache.org/couchdb/HTTP_database_API> except compaction
114    
115                  warn "## calculating disk_size\n";  =cut
116                  $json->{disk_size} += -s "$path/$1/$_" foreach $docs;  
117                    my $database = $1;
118                    my $dir = "$config->{path}/$database";
119    
120                    if ( $method eq 'GET' ) {
121                            $json = database_get( $database );
122                    } elsif ( $method eq 'DELETE' ) {
123                            if ( ! -e $dir ) {
124                                    $status = 404;
125                            } else {
126                                    remove_tree($dir) && ok || { $status = 501 };
127                            }
128                    } elsif ( $method eq 'PUT' ) {
129                            if ( ! -e $dir ) {
130                                    make_path($dir) && ok && warn "created $dir" || { $status = 501 };
131                            } else {
132                                    $status = 412;
133                            }
134                    }
135    
136            } elsif ( $url =~ m{($regex_dbs)/([^?]+)\??(.+)?$} ) {
137                    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;
146                    if ( $args ) {
147                            foreach my $a ( split(/[&;]/,$args) ) {
148                                    my ($n,$v) = split(/=/,$a);
149                                    $v =~ s{(["'])(.+)\1}{$2};
150                                    $arg->{$n} = $v;
151                            }
152                    }
153    
154                    my $path = $config->{path};
155                    warn "ERROR: path $path doesn't exist\n" unless -e $path;
156    
157                    my $p = "$path/$database/$id";
158                    warn "## database: $database id: $id -> $p ", dump( $arg ),"\n";
159    
160    
161                    if ( $id =~ m{_all_docs(\w+)?$} ) {
162    
163                            my $by = $1;
164                            my $offset = 0;
165                            my $startkey = delete $arg->{startkey};
166                            my $endkey   = delete $arg->{endkey};
167                            my $limit    = delete $arg->{limit};
168                            my $total_rows = 0;
169    
170                            my @docs = grep { length $_ } map {
171                                    return '' if defined $limit && $total_rows == $limit;
172            
173                                    s{^$path/$database/}{};
174                                    return '' if defined $endkey && $_ gt $endkey;
175    
176                                    if ( $startkey ) {
177                                            if ( $_ ge $startkey ) {
178                                                    $total_rows++;
179                                                    $_;
180                                            } else {
181                                                    $offset++;
182                                                    return '';
183                                            }
184                                    } else {
185                                            $total_rows++;
186                                            $_;
187                                    }
188    
189                            } glob( "$path/$database/*" );
190    
191                            warn "## docs $startkey -> $endkey limit $limit ", dump( @docs ); # if $debug;
192    
193                            $json = {
194                                    total_rows =>  $total_rows,
195                                    offset => $offset,
196                                    rows => [],
197                            };
198    
199                            my $rows;
200                            my @ids;
201    
202                            foreach my $id ( @docs ) {
203                                    warn "++ $id\n" if $debug;
204                                    my $p = "$path/$database/$id";
205                                    my $data = eval { Storable::retrieve( $p ) };
206                                    if ( $@ ) {
207                                            warn "ERROR: $p | $@\n";
208                                            next;
209                                    }
210                                    push @ids, $id;
211                                    $rows->{$id} = {
212                                            id => $id,
213                                            key => $id,
214                                            value => {
215                                                    rev => (stat($p))[9], # mtime
216                                            }
217                                    };
218                            }
219    
220                            my $descending = delete $arg->{descending};
221                            my @sorted = sort @ids;
222    
223                            warn "creating rows in ", $descending ? "descending" : "", " order\n";
224    
225                            foreach my $id ( $descending ? reverse @sorted : @sorted ) {
226                                    warn ">> $id ", $descending ? 'desc' : 'asc', "\n" if $debug;
227                                    push @{ $json->{rows} }, $rows->{$id};
228                            }
229    
230                    } elsif ( $method eq 'PUT' ) {
231            
232                            warn "## ",dump( $tx->req ) if $debug;
233    
234                            my $data = $tx->req->content->file->slurp;
235    
236                            Storable::store( from_json($data), $p );
237                            warn "store $p ", -s $p, " bytes: $data\n";
238                    } elsif ( $method eq 'GET' ) {
239                            if ( ! -e $p ) {
240                                    $status = 404;
241                            } else {
242                                    warn "retrive $p ", -s $p, " bytes\n";
243                                    $json = Storable::retrieve( $p );
244                            }
245                    } elsif ( $method eq 'DELETE' ) {
246                            if ( -e $p ) {
247                                    unlink $p || { $status = 501 };
248                            } else {
249                                    $status = 404;
250                            }
251                    } else {
252                            $status = 501;
253                    }
254    
255                    warn "WARNING: arg left from $url = ",dump( $arg ),$/ if keys %$arg;
256    
257            }
258    
259            if ( $status >= 400 && $status < 500 && ! defined $json) {
260                    $json = { error => 'not_found', reason => 'Missing' };
261                    warn "fake $status";
262          }          }
263    
264          $tx->res->code( 200 );          $tx->res->code( $status );
265          $tx->res->headers->content_type( 'text/json' );          $tx->res->headers->content_type( 'text/json' );
266          $tx->res->body( to_json $json );          my $body = to_json $json;
267            $tx->res->body( $body );
268            warn "CouchDB API: $method $url $status $body\n";
269          return $tx;          return $tx;
270    
271  }  }
272    
273    sub database_get {
274            my ($db_name) = @_;
275            my $path = $config->{path};
276            warn "# collecting docs from $path/$db_name/*\n";
277            my @docs = glob "$path/$db_name/*";
278            my $json = {
279                    db_name => $db_name,
280                    doc_count => $#docs + 1,
281                    doc_del_count => 0,
282                    update_seq => 0,
283                    purge_seq => 0,
284                    capacity_running => JSON::false,
285                    disk_size => 0,
286                    instance_start_time => time(),
287            };
288    
289            warn "## calculating disk_size\n";
290            $json->{disk_size} += -s $_ foreach @docs;
291            $status = 200;
292            return $json;
293    }
294    
295  1;  1;
296    __END__
297    
298    =head1 SEE ALSO
299    
300    L<http://wiki.apache.org/couchdb/Reference>
301    

Legend:
Removed from v.1046  
changed lines
  Added in v.1051

  ViewVC Help
Powered by ViewVC 1.1.26