Permalink
Browse files

Added preliminary view code

  • Loading branch information...
1 parent 1591b6d commit e1da175201bc95e2dff1f738f5ce76b3166c3033 @mnunberg committed Apr 10, 2012
View
@@ -4,25 +4,34 @@ MANIFEST.SKIP
Makefile.PL
README.pod
-PLCB_ConfUtil.pm
-PLCB_Config.pm
-build_libraries.pl
+lib/Couchbase/Client/README.pod
-inc/Devel/CheckLib.pm
+
+
+################################################################################
+### Base Perl API ###
+################################################################################
lib/Couchbase/Client.pm
lib/Couchbase/Client/Return.pm
lib/Couchbase/Client/Errors.pm
lib/Couchbase/Client/IDXConst.pm
-lib/Couchbase/Client/README.pod
-
+################################################################################
+### Perl Memcached Compat ###
+################################################################################
lib/Couchbase/Client/Compat.pm
+################################################################################
+### Async Perl API ###
+################################################################################
lib/Couchbase/Client/Async.pm
lib/Couchbase/Client/Async/Event.pm
lib/Couchbase/Client/Async/Request.pm
+################################################################################
+### Testing API ###
+################################################################################
lib/Couchbase/MockServer.pm
lib/Couchbase/Test/ClientSync.pm
lib/Couchbase/Test/Common.pm
@@ -32,32 +41,87 @@ lib/Couchbase/Test/Settings.pm
lib/Couchbase/Test/Interop.pm
lib/Couchbase/Test/Netfail.pm
-xs/Client.xs
-xs/Client_multi.xs
-xs/Async.xs
+################################################################################
+### Views Perl API ###
+################################################################################
+lib/Couchbase/Client/Iterator.pm
+lib/Couchbase/Couch/Base.pm
+lib/Couchbase/Couch/Design.pm
+lib/Couchbase/Couch/Handle.pm
+lib/Couchbase/Couch/HandleInfo.pm
+
+################################################################################
+################################################################################
+### Common C Support ###
+################################################################################
+################################################################################
xs/perl-couchbase.h
-xs/perl-couchbase-async.h
xs/plcb-util.h
xs/plcb-return.h
+xs/plcb-convert.h
+xs/plcb-commands.h
+xs/typemap
xs/callbacks.c
xs/convert.c
xs/ctor.c
+
+################################################################################
+### Basic C Client Support ###
+################################################################################
+xs/Client.xs
+xs/Client_multi.xs
+xs/Iterator_get.xs
+
+################################################################################
+### Async C Support ###
+################################################################################
+xs/perl-couchbase-async.h
+xs/Async.xs
xs/async_base.c
xs/async_callbacks.c
xs/async_events.c
+xs/iterator.c
-constants/idx_constants.pl
-constants/error_constants.pl
-constants/print_constants.pl
+################################################################################
+### Views C Support ###
+################################################################################
+xs/perl-couchbase-couch.h
+xs/couch_handle.c
+xs/Couch_request_handle.xs
+################################################################################
+### Examples ###
+################################################################################
+eg/couch_demo.pl
+eg/memd_iter.pl
+eg/memd_multi.pl
+eg/memd_basic.pl
+eg/memd_async.pl
+
+
+################################################################################
+### Test Skeleton ###
+################################################################################
t/00-load.t
t/01-main.t
t/02-compat.t
t/tmp/CouchbaseMock.jar
+# libcouchbase
+
+inc/Devel/CheckLib.pm
+
+PLCB_ConfUtil.pm
+PLCB_Config.pm
+build_libraries.pl
+
+constants/idx_constants.pl
+constants/error_constants.pl
+constants/print_constants.pl
+
src/Makefile.PL
src/libcouchbase.pm
View
3 TODO
@@ -0,0 +1,3 @@
+Refactor modules into strictly client modules, test modules, etc. etc.
+Decouple Async module
+Implement stat tests
View
@@ -0,0 +1,126 @@
+#!/usr/bin/perl
+# This feature demonstrates several aspects of dealing with Couch using the
+# Perl API
+
+use strict;
+use warnings;
+use blib;
+use Couchbase::Client;
+use Couchbase::Client::IDXConst;
+use Data::Dumper::Concise;
+use JSON::XS;
+use Log::Fu;
+use Carp qw(confess);
+$SIG{__DIE__} = \&confess;
+
+# Create the client
+my $cbo = Couchbase::Client->new({
+ username=> "Administrator",
+ password=> "123456",
+ bucket => "membase0",
+ server => "10.0.0.99:8091"
+});
+
+# Create some sample data to work with
+my @posts = (
+ [ "bought-a-cat" => {
+ title => "bought-a-cat",
+ body => "I went to the pet store earlier and brought home a little kitty",
+ date => "2009/01/30 18:04:11"
+ }],
+ [ "biking" => {
+ title => "Biking",
+ body => "My biggest hobby is mountainbiking. The other day...",
+ date => "2009/01/30 18:04:11"
+ }],
+ [ "hello-world" => {
+ title => "Hello World",
+ body => "Well hello and welcome to my new blog...",
+ date => "2009/01/15 15:52:20"
+ }]
+);
+
+foreach (0..3000) {
+ my $k = "Post_$_";
+ push @posts, [$k, { title => "Title_$k", body => "Body_$k", date => "2012/03/23 13:53:00" }];
+}
+
+# store all the posts, while checking for errors
+{
+ my $results = $cbo->couch_set_multi(@posts);
+ my @errkeys = grep { !$results->{$_}->is_ok } keys %$results;
+ if (@errkeys) {
+ die ("Store did not succeed! Errored keys: ".join(",", @errkeys));
+ }
+}
+
+
+#create a design doc
+{
+ my $design_json = {
+ _id => "_design/blog",
+ language => "javascript",
+ views => {
+ recent_posts => {
+ "map" => 'function(doc) '.
+ '{ if(doc.date&&doc.title)' .
+ ' { emit(doc.date,doc.title); } }'
+ }
+ }
+ };
+ my $retval = $cbo->couch_design_put($design_json);
+ log_infof("Path=%s, Return HTTP=%d, (Ok=%d)",
+ $retval->path, $retval->http_code, $retval->is_ok);
+ if (!$retval->is_ok) {
+ log_errf("Couldn't save design doc: %s", Dumper($retval->value));
+ }
+}
+
+# Get the design document again..
+my $Design;
+{
+ # re-get our design document, to make sure it still exists..
+ $Design = $cbo->couch_design_get("blog");
+ log_infof("Got design. Path=%s, HTTP=%d (Ok=%d)",
+ $Design->path, $Design->http_code, $Design->is_ok);
+
+}
+
+
+# let's get the path for the view. this is nice if we intend to perform lower
+# level operations
+
+my $view = $Design->get_view_path("recent_posts");
+log_info("View path is $view");
+
+
+# fetch all the results at once. Might be memory-hungry!
+{
+ my $resultset = $Design->get_view_results("recent_posts");
+ if (!$resultset->is_ok) {
+ die "Got resultset error: ". $resultset->errstr;
+ }
+ eval {
+ log_infof("Got %d rows", scalar @{$resultset->rows} );
+ }; if ($@) {
+ print Dumper($resultset);
+ die $@;
+ }
+}
+
+# We can be more efficient by using an iterator to incrementally fetch the results
+{
+ my $iter = $Design->get_view_iterator("recent_posts");
+ log_infof("Have iterator. Path: %s", $iter->path);
+ my $rescount = 0;
+ while (my $row = $iter->next) {
+ $rescount++;
+ print "+";
+ die "Ooops" unless exists $row->{id};
+ }
+ print "\n";
+
+ log_infof("Got a total of %d/%d rows", $rescount, $iter->count);
+
+ log_infof("Error string (if any) %s", $iter->info->errstr || "<NO ERROR>");
+}
File renamed without changes.
File renamed without changes.
View
@@ -0,0 +1,70 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use blib;
+use Couchbase::Client;
+use Log::Fu;
+use Data::Dumper;
+use Time::HiRes qw(time sleep);
+
+my $cbo = Couchbase::Client->new({
+ username => "Administrator",
+ password => "123456",
+ bucket => "membase0",
+ server => "10.0.0.99:8091"
+});
+
+my @Keys = map { "IterKey$_" } (0..20);
+
+# Store the key. $results is the results of the store operation
+my $results = $cbo->set_multi( map { [ $_, scalar reverse($_) ] } @Keys );
+
+
+# set the timeout, so we can demonstrate!
+my $Begin = time();
+$cbo->timeout(60); # 60 seconds
+
+# get_iterator creates a new iterator object
+my $iterator = $cbo->get_iterator(@Keys);
+my $iterator2 = $cbo->get_iterator(@Keys);
+
+printf("Starting Query..\n");
+
+# Continue fetching the 'next' result. ->next returns nothing
+# once there are no more results
+while (my ($key,$ret) = $iterator->next) {
+ printf("Elapsed: %d, Got return for %s (Value=%s)\n",
+ time() - $Begin, $key, $ret->value);
+ sleep(1.5);
+ ($key,$ret) = $iterator2->next();
+ printf("Got result for key %s from second iterator\n", $key);
+}
+
+printf("All values done. Waited %d secs\n", time() - $Begin);
+
+__END__
+
+Starting Query..
+Elapsed: 0, Got return for IterKey0 (Value=0yeKretI)
+Elapsed: 1, Got return for IterKey19 (Value=91yeKretI)
+Elapsed: 3, Got return for IterKey16 (Value=61yeKretI)
+Elapsed: 4, Got return for IterKey15 (Value=51yeKretI)
+Elapsed: 6, Got return for IterKey13 (Value=31yeKretI)
+Elapsed: 7, Got return for IterKey10 (Value=01yeKretI)
+Elapsed: 9, Got return for IterKey9 (Value=9yeKretI)
+Elapsed: 10, Got return for IterKey6 (Value=6yeKretI)
+Elapsed: 12, Got return for IterKey5 (Value=5yeKretI)
+Elapsed: 13, Got return for IterKey3 (Value=3yeKretI)
+Elapsed: 15, Got return for IterKey20 (Value=02yeKretI)
+Elapsed: 16, Got return for IterKey18 (Value=81yeKretI)
+Elapsed: 18, Got return for IterKey17 (Value=71yeKretI)
+Elapsed: 19, Got return for IterKey14 (Value=41yeKretI)
+Elapsed: 21, Got return for IterKey12 (Value=21yeKretI)
+Elapsed: 22, Got return for IterKey11 (Value=11yeKretI)
+Elapsed: 24, Got return for IterKey8 (Value=8yeKretI)
+Elapsed: 25, Got return for IterKey7 (Value=7yeKretI)
+Elapsed: 27, Got return for IterKey4 (Value=4yeKretI)
+Elapsed: 28, Got return for IterKey2 (Value=2yeKretI)
+Elapsed: 30, Got return for IterKey1 (Value=1yeKretI)
+All values done. Waited 31 secs
+
Oops, something went wrong.

0 comments on commit e1da175

Please sign in to comment.