/[Frey]/branches/zimbardo/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 /branches/zimbardo/lib/Frey/CouchAPI.pm

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

revision 1057 by dpavlin, Thu Apr 23 22:18:46 2009 UTC revision 1062 by dpavlin, Mon Apr 27 16:23:52 2009 UTC
# Line 23  use URI::Escape; Line 23  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.2';  our $VERSION = '0.3';
27  $VERSION .= " (Frey $Frey::VERSION)" if $Frey::VERSION;  $VERSION .= " (Frey $Frey::VERSION)" if $Frey::VERSION;
28    
29  our $debug = $Frey::debug || 0;  our $debug = $Frey::debug || 0;
# Line 43  sub rewrite_urls { Line 43  sub rewrite_urls {
43  }  }
44    
45  our $config = {  our $config = {
46          path => '/data/webpac2/var/row',          database => {
47                    path => '/data/webpac2/var/row',
48                    name_glob => '/*/*',
49            }
50  };  };
51    
 my $p = $config->{path};  
 my @all_dbs = map {  
         s{^\Q$p\E/*}{};  
         $_;  
 } glob "$p/*/*";  
   
52  my $regex_dbs = '[a-z][a-z0-9_\$\(\)\+\-/]+';  my $regex_dbs = '[a-z][a-z0-9_\$\(\)\+\-/]+';
53    
54  our $json = {};  our $json = {};
# Line 65  sub ok { Line 62  sub ok {
62    
63  sub file_rev { (stat($_[0]))[9] } # mtime  sub file_rev { (stat($_[0]))[9] } # mtime
64    
65    sub data_from_tx {
66            my $tx = shift;
67            my $data = $tx->req->content->file->slurp;
68            $data = JSON->new->allow_nonref->decode( $data );
69            warn "## data ",dump( $data );
70            return $data;
71    }
72    
73  sub dispatch {  sub dispatch {
74          my ($self,$tx) = @_;          my ($self,$tx) = @_;
75    
# Line 73  sub dispatch { Line 78  sub dispatch {
78          my $url = $tx->req->url->to_string;          my $url = $tx->req->url->to_string;
79          $url = uri_unescape( $url );          $url = uri_unescape( $url );
80          my $method = $tx->req->method;          my $method = $tx->req->method;
81          my $path = $config->{path};          my $path = $config->{database}->{path};
82                    
83          if ( $url eq '/' ) {          if ( $url eq '/' ) {
84                  $json = {                  $json = {
# Line 82  sub dispatch { Line 87  sub dispatch {
87                  };                  };
88                  $status = 200;                  $status = 200;
89          } elsif ( $url eq '/_all_dbs' ) {          } elsif ( $url eq '/_all_dbs' ) {
90                  $json = [ @all_dbs ];                  $json = [
91                            map {
92                                    s{^\Q$path\E/*}{};
93                                    $_;
94                            } glob $path . $config->{database}->{name_glob}
95                    ];
96                  $status = 200;                  $status = 200;
97          } elsif ( $url =~ m{^/_config/?(.+)} ) {          } elsif ( $url =~ m{^/_config/?(.+)} ) {
98    
99                  $json = { CouchAPI => $config };                  $json = $config;
100    
101                  if ( $method eq 'PUT' ) {                  if ( $method eq 'PUT' ) {
102    
103                          my $part = $1;                          my $part = $1;
104                          warn "## part $part";                          my $data = data_from_tx( $tx );
105                            warn "## part $part = $data\n";
106    
                         $part =~ s!^!->{'!;  
107                          $part =~ s!/!'}->{'!;                          $part =~ s!/!'}->{'!;
                         $part =~ s/$/'}/;  
108    
                         my $data = $tx->req->content->file->slurp;  
                         $data = JSON->new->allow_nonref->decode( $data );  
                         warn "## data ",dump( $data );  
109                          # poor man's transaction :-)                          # poor man's transaction :-)
110                          my $code = "\$json$part = \$data; \$config$part = \$data;";                          my $code = "\$config->{'$part'} = \$data;";
111                          eval $code;                          eval $code;
112                          if ( $@ ) {                          if ( $@ ) {
113                                  warn "ERROR: $code -> $@";                                  warn "ERROR: $code -> $@";
# Line 110  sub dispatch { Line 116  sub dispatch {
116                                  $status = 200;                                  $status = 200;
117                          }                          }
118    
119  warn "json ",dump( $json ), " config ", dump( $config );                          warn "# config after $code is ",dump( $config ),$/;
120    
121                  } elsif ( $method eq 'GET' ) {                  } elsif ( $method eq 'GET' ) {
122                          $status = 200;                          $status = 200;
# Line 118  warn "json ",dump( $json ), " config ", Line 124  warn "json ",dump( $json ), " config ",
124                          $status = 501;                          $status = 501;
125                  }                  }
126    
         } elsif ( $url =~ m{($regex_dbs)/$} ) {  
   
127  =head2 Database  =head2 Database
128    
129  L<http://wiki.apache.org/couchdb/HTTP_database_API> except compaction  L<http://wiki.apache.org/couchdb/HTTP_database_API> except compaction
130    
131  =cut  =cut
132    
133            } elsif (
134                       $url =~ m{($regex_dbs)/$}
135                    # DELETE doesn't have trailing slash
136                    || $method eq 'DELETE' && $url =~ m{($regex_dbs)$}
137            ) {
138    
139                  my $database = $1;                  my $database = $1;
140    
141                  my $dir = "$path/$database";                  my $dir = "$path/$database";
# Line 157  L<http://wiki.apache.org/couchdb/HTTP_da Line 167  L<http://wiki.apache.org/couchdb/HTTP_da
167                          }                          }
168                  }                  }
169    
170                    warn "## database $database $status ",dump( $json );
171    
172          } elsif ( $url =~ m{($regex_dbs)/([^?]+)\??(.+)?$} ) {          } elsif ( $url =~ m{($regex_dbs)/([^?]+)\??(.+)?$} ) {
173                  my ($database,$id,$args) = ($1,$2,$3);                  my ($database,$id,$args) = ($1,$2,$3);
174    
# Line 186  L<http://wiki.apache.org/couchdb/HTTP_Do Line 198  L<http://wiki.apache.org/couchdb/HTTP_Do
198                          my $by = $1;                          my $by = $1;
199                          my $offset = 0;                          my $offset = 0;
200                          my $startkey = delete $arg->{startkey};                          my $startkey = delete $arg->{startkey};
201                               $startkey ||= delete $arg->{startkey_docid}; # XXX key == id
202                          my $endkey   = delete $arg->{endkey};                          my $endkey   = delete $arg->{endkey};
203                          my $limit    = delete $arg->{limit};                          my $limit    = delete $arg->{limit};
204                            my $skip     = delete $arg->{skip};
205                          my $total_rows = 0;                          my $total_rows = 0;
206                            my $collected_rows = 0;
207    
208                          my @docs = grep { length $_ } map {                          my @docs = grep { length $_ } map {
209    
210                                  if ( $limit > 0 && $total_rows == $limit ) {                                  $total_rows++;
211            
212                                    if ( $limit > 0 && $collected_rows == $limit ) {
213                                          '';                                          '';
214                                  } else {                                  } else {
215                    
# Line 202  L<http://wiki.apache.org/couchdb/HTTP_Do Line 219  L<http://wiki.apache.org/couchdb/HTTP_Do
219                                                  '';                                                  '';
220                                          } elsif ( $startkey ) {                                          } elsif ( $startkey ) {
221                                                  if ( $_ ge $startkey ) {                                                  if ( $_ ge $startkey ) {
222                                                          $total_rows++;                                                          $collected_rows++;
223                                                          $_;                                                          $_;
224                                                  } else {                                                  } else {
225                                                          $offset++;                                                          $offset++;
226                                                          '';                                                          '';
227                                                  }                                                  }
228                                          } else {                                          } else {
229                                                  $total_rows++;                                                  $collected_rows++;
230                                                  $_;                                                  $_;
231                                          }                                          }
232                                  }                                  }
233    
234                          } glob( "$path/$database/*" );                          } glob( "$path/$database/*" );
235    
236                            $offset += $skip if $skip;
237    
238                          warn "## docs $startkey -> $endkey limit $limit ", dump( @docs ); # if $debug;                          warn "## docs $startkey -> $endkey limit $limit ", dump( @docs ); # if $debug;
239    
# Line 260  L<http://wiki.apache.org/couchdb/HTTP_Do Line 278  L<http://wiki.apache.org/couchdb/HTTP_Do
278    
279                  } elsif ( $method eq 'PUT' ) {                  } elsif ( $method eq 'PUT' ) {
280                    
281                          warn "## ",dump( $tx->req ); # if $debug;                          warn "## ",dump( $tx->req ) if $debug;
282    
283                          my $data = $tx->req->content->file->slurp;                          my $data = $tx->req->content->file->slurp;
284    
285                            my $db_path = "$path/$database";
286                            make_path $db_path unless -e $db_path;
287    
288                          Storable::store( from_json($data), $p );                          Storable::store( from_json($data), $p );
289                          my $rev = file_rev $p;                          my $rev = file_rev $p;
290                          warn "store $p $rev size ", -s $p, " bytes | $data\n";                          warn "store $p $rev size ", -s $p, " bytes | $data\n";
# Line 298  L<http://wiki.apache.org/couchdb/HTTP_Do Line 319  L<http://wiki.apache.org/couchdb/HTTP_Do
319                                  $status = 404;                                  $status = 404;
320                          }                          }
321                  } elsif ( $method eq 'POST' ) {                  } elsif ( $method eq 'POST' ) {
322                            my $data = data_from_tx( $tx );
323    
324                            # FIXME implement real view server and return 200
325                          $json = { total_rows => 0, offset => 0 };                          $json = { total_rows => 0, offset => 0 };
326                          $status = 202; # FIXME implement real view server and return 200                          $status = 202;
327    
328                  } else {                  } else {
329                          $status = 501;                          $status = 501;
330                  }                  }
331    
332                  warn "WARNING: arg left from $url = ",dump( $arg ),$/ if keys %$arg;                  if ( keys %$arg ) {
333                            warn "WARNING: arg left from $url = ",dump( $arg ),$/;
334                            $status = 501;
335                    }
336    
337          }          }
338    
# Line 331  L<http://wiki.apache.org/couchdb/HTTP_Do Line 359  L<http://wiki.apache.org/couchdb/HTTP_Do
359    
360  sub database_get {  sub database_get {
361          my ($db_name) = @_;          my ($db_name) = @_;
362          my $path = $config->{path};          my $path = $config->{database}->{path} || die;
363          warn "# collecting docs from $path/$db_name/*\n";          warn "# collecting docs from $path/$db_name/*\n";
364          my @docs = glob "$path/$db_name/*";          my @docs = glob "$path/$db_name/*";
365          my $json = {          my $json = {

Legend:
Removed from v.1057  
changed lines
  Added in v.1062

  ViewVC Help
Powered by ViewVC 1.1.26