Merge branch 'master' of github.com:dpavlin/angular-mojolicious
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 15 Oct 2011 09:28:07 +0000 (11:28 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 15 Oct 2011 09:28:07 +0000 (11:28 +0200)
couchdb-view-server.pl [new file with mode: 0755]
t/couchdb-view-server.json [new file with mode: 0644]

diff --git a/couchdb-view-server.pl b/couchdb-view-server.pl
new file mode 100755 (executable)
index 0000000..cc64570
--- /dev/null
@@ -0,0 +1,68 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+
+# http://wiki.apache.org/couchdb/View_server
+#
+# /etc/couchdb/local.ini add:
+#
+# [query_servers]
+# perl = /usr/bin/perl /srv/angular-mojolicious/couchdb-view-server.pl
+#
+# example view:
+#
+# sub { [ undef, shift ] }
+
+use JSON::XS;
+use IO::Handle;
+use Data::Dump qw(dump);
+
+my $j = JSON::XS->new;
+
+my $in  = IO::Handle->new_from_fd(\*STDIN, 'r');
+my $out = IO::Handle->new_from_fd(\*STDOUT, 'w');
+$out->autoflush(1);
+
+open(my $l_fh, '>>', "/tmp/couchdb-perl-view.log");
+$l_fh->autoflush(1);
+
+sub _debug {
+       print $l_fh "@_\n";
+}
+
+sub _log {
+       $out->print($j->encode([ 'log' => @_ ]), "\n");
+}
+
+our @fun;
+
+while(defined(my $line = $in->getline)) {
+       chomp $line;
+       _debug $line;
+       my $input = $j->decode($line);
+       my ($cmd, @args) = @$input;
+
+       if ( $cmd eq 'reset' ) {
+               @fun = ();
+               $out->print("true\n");
+       } elsif ( $cmd eq 'add_fun' ) {
+               push @fun, eval $args[0];
+               if ( $@ ) {
+                       $out->print( qq|{"error": "$!", "reason": "$@"}\n| );
+               } else {
+                       $out->print("true\n");
+               }
+       } elsif ( $cmd eq 'map_doc' ) {
+               my @results;
+               foreach my $fun ( @fun ) {
+                       my $d = eval { $fun->(@args) };
+                       _log $@ if $@;
+                       push @results, [$d];
+               }
+               my $json = $j->utf8->encode( \@results );
+               $out->print("$json\n");
+               _debug "# $json";
+       } else {
+               _log "$cmd unimplemented", dump( $input );
+       }
+}
diff --git a/t/couchdb-view-server.json b/t/couchdb-view-server.json
new file mode 100644 (file)
index 0000000..b6bf218
--- /dev/null
@@ -0,0 +1,3 @@
+["reset",{"reduce_limit":true,"timeout":5000}]
+["add_fun","sub {\n [ shift->{_id}, 1 ];\n}"]
+["map_doc",{"_id":"cb71ad755be66b467ae5e51e4100e027","_rev":"3-11fd6bead394b463531889babc686ac4","trigger":{"active":["t61p:11026","1317673926.10924","1317673926.15453","0"],"output":"","command":"notify-send \"CouchDB trigger notify example\""}}]