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

Legend:
Removed from v.1047  
changed lines
  Added in v.1052

  ViewVC Help
Powered by ViewVC 1.1.26