Guest User

Untitled

a guest
Sep 25th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # This feature demonstrates several aspects of dealing with Couch using the
  3. # Perl API
  4.  
  5. use strict;
  6. use warnings;
  7. use blib;
  8. use Couchbase::Client;
  9. use Couchbase::Client::IDXConst;
  10. use Data::Dumper::Concise;
  11. use JSON::XS;
  12. use Log::Fu;
  13. use Carp qw(confess);
  14. $SIG{__DIE__} = \&confess;
  15.  
  16. # Create the client
  17. my $cbo = Couchbase::Client->new({
  18. username=> "Administrator",
  19. password=> "123456",
  20. bucket => "membase0",
  21. server => "10.0.0.99:8091"
  22. });
  23.  
  24. # Create some sample data to work with
  25. my @posts = (
  26. [ "bought-a-cat" => {
  27. title => "bought-a-cat",
  28. body => "I went to the pet store earlier and brought home a little kitty",
  29. date => "2009/01/30 18:04:11"
  30. }],
  31. [ "biking" => {
  32. title => "Biking",
  33. body => "My biggest hobby is mountainbiking. The other day...",
  34. date => "2009/01/30 18:04:11"
  35. }],
  36. [ "hello-world" => {
  37. title => "Hello World",
  38. body => "Well hello and welcome to my new blog...",
  39. date => "2009/01/15 15:52:20"
  40. }]
  41. );
  42.  
  43. foreach (0..200) {
  44. my $k = "Post_$_";
  45. push @posts, [$k, {
  46. title => "Title_$k",
  47. body => "Body_$k",
  48. date => "2012/03/23 13:53:00",
  49. counter => 0
  50. }];
  51. }
  52.  
  53. # store all the posts, while checking for errors
  54. {
  55. my $results = $cbo->couch_set_multi(@posts);
  56. my @errkeys = grep { !$results->{$_}->is_ok } keys %$results;
  57. if (@errkeys) {
  58. die ("Store did not succeed! Errored keys: ".join(",", @errkeys));
  59. }
  60. }
  61.  
  62.  
  63. #create a design doc
  64. {
  65. my $design_json = {
  66. _id => "_design/blog",
  67. language => "javascript",
  68. views => {
  69. recent_posts => {
  70. "map" => 'function(doc) '.
  71. '{ if(doc.date&&doc.title)' .
  72. ' { emit(doc.date,doc.title); } }'
  73. }
  74. }
  75. };
  76. my $retval = $cbo->couch_design_put($design_json);
  77. log_infof("Path=%s, Return HTTP=%d, (Ok=%d)",
  78. $retval->path, $retval->http_code, $retval->is_ok);
  79. if (!$retval->is_ok) {
  80. log_errf("Couldn't save design doc: %s", Dumper($retval->value));
  81. }
  82. }
  83.  
  84. # Get the design document again..
  85. my $Design;
  86. {
  87. # re-get our design document, to make sure it still exists..
  88. $Design = $cbo->couch_design_get("blog");
  89. log_infof("Got design. Path=%s, HTTP=%d (Ok=%d)",
  90. $Design->path, $Design->http_code, $Design->is_ok);
  91.  
  92. }
  93.  
  94.  
  95. # let's get the path for the view. this is nice if we intend to perform lower
  96. # level operations
  97.  
  98. my $view = $Design->get_view_path("recent_posts");
  99. log_info("View path is $view");
  100.  
  101.  
  102. # fetch all the results at once. Might be memory-hungry!
  103. {
  104. my $resultset = $Design->get_view_results("recent_posts");
  105. if (!$resultset->is_ok) {
  106. die "Got resultset error: ". $resultset->errstr;
  107. }
  108. eval {
  109. log_infof("Got %d rows", scalar @{$resultset->rows} );
  110. }; if ($@) {
  111. print Dumper($resultset);
  112. die $@;
  113. }
  114. }
  115.  
  116. # We can be more efficient by using an iterator to incrementally fetch the results
  117. {
  118. my $iter = $Design->get_view_iterator("recent_posts", ForUpdate => 1);
  119. log_infof("Have iterator. Path: %s", $iter->path);
  120. my $rescount = 0;
  121. while (my $row = $iter->next) {
  122. $rescount++;
  123. # Display our progress
  124. print "+";
  125.  
  126. # Check that we actually have a valid resultset
  127. die "Expected doc!" unless $row->doc;
  128.  
  129. # Get the old value. We compare this later
  130. my $old_val = $row->doc->{counter} || 0;
  131.  
  132. # Increment the value:
  133. $row->doc->{counter}++;
  134.  
  135. # And save it to couchbase
  136. my $memd_rv = $row->save();
  137.  
  138. # Check that the save was OK
  139. if (!$memd_rv->is_ok) {
  140. log_errf("Couldn't save doc! %s", $memd_rv->errstr);
  141. }
  142.  
  143. #Get the saved object again
  144. my $new_row = $cbo->couch_doc_get($row->id);
  145.  
  146. # Some more error checking..
  147. if (!$new_row) {
  148. die("Grrrr!");
  149. }
  150.  
  151. if (!$new_row->is_ok) {
  152. log_errf("Couldn't get new row: %s", $new_row->errstr);
  153. next;
  154. }
  155. if (!$new_row->value) {
  156. log_err("Status is ok but new row is not here!");
  157. print Dumper($new_row);
  158. next;
  159. }
  160.  
  161. # After all the above annoying error checking codes, we should now be
  162. # certain that the current value of 'counter' is 1 more than the cached
  163. # value (from the row returned by the original iterator)
  164. if ($new_row->value->{counter} != $old_val+1) {
  165. die("Didn't get expected updates...");
  166. }
  167. }
  168. print "\n";
  169.  
  170. log_infof("Got a total of %d/%d rows", $rescount, $iter->count);
  171.  
  172. log_infof("Error string (if any) %s", $iter->info->errstr || "<NO ERROR>");
  173. }
Add Comment
Please, Sign In to add comment