Guest User

Untitled

a guest
Sep 25th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 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..3000) {
  44. my $k = "Post_$_";
  45. push @posts, [$k, { title => "Title_$k", body => "Body_$k", date => "2012/03/23 13:53:00" }];
  46. }
  47.  
  48. # store all the posts, while checking for errors
  49. {
  50. my $results = $cbo->couch_set_multi(@posts);
  51. my @errkeys = grep { !$results->{$_}->is_ok } keys %$results;
  52. if (@errkeys) {
  53. die ("Store did not succeed! Errored keys: ".join(",", @errkeys));
  54. }
  55. }
  56.  
  57.  
  58. #create a design doc
  59. {
  60. my $design_json = {
  61. _id => "_design/blog",
  62. language => "javascript",
  63. views => {
  64. recent_posts => {
  65. "map" => 'function(doc) '.
  66. '{ if(doc.date&&doc.title)' .
  67. ' { emit(doc.date,doc.title); } }'
  68. }
  69. }
  70. };
  71. my $retval = $cbo->couch_design_put($design_json);
  72. log_infof("Path=%s, Return HTTP=%d, (Ok=%d)",
  73. $retval->path, $retval->http_code, $retval->is_ok);
  74. if (!$retval->is_ok) {
  75. log_errf("Couldn't save design doc: %s", Dumper($retval->value));
  76. }
  77. }
  78.  
  79. # Get the design document again..
  80. my $Design;
  81. {
  82. # re-get our design document, to make sure it still exists..
  83. $Design = $cbo->couch_design_get("blog");
  84. log_infof("Got design. Path=%s, HTTP=%d (Ok=%d)",
  85. $Design->path, $Design->http_code, $Design->is_ok);
  86.  
  87. }
  88.  
  89.  
  90. # let's get the path for the view. this is nice if we intend to perform lower
  91. # level operations
  92.  
  93. my $view = $Design->get_view_path("recent_posts");
  94. log_info("View path is $view");
  95.  
  96.  
  97. # fetch all the results at once. Might be memory-hungry!
  98. {
  99. my $resultset = $Design->get_view_results("recent_posts");
  100. if (!$resultset->is_ok) {
  101. die "Got resultset error: ". $resultset->errstr;
  102. }
  103. eval {
  104. log_infof("Got %d rows", scalar @{$resultset->rows} );
  105. }; if ($@) {
  106. print Dumper($resultset);
  107. die $@;
  108. }
  109. }
  110.  
  111. # We can be more efficient by using an iterator to incrementally fetch the results
  112. {
  113. my $iter = $Design->get_view_iterator("recent_posts");
  114. log_infof("Have iterator. Path: %s", $iter->path);
  115. my $rescount = 0;
  116. while (my $row = $iter->next) {
  117. $rescount++;
  118. print "+";
  119. die "Ooops" unless exists $row->{id};
  120. }
  121. print "\n";
  122.  
  123. log_infof("Got a total of %d/%d rows", $rescount, $iter->count);
  124.  
  125. log_infof("Error string (if any) %s", $iter->info->errstr || "<NO ERROR>");
  126. }
Add Comment
Please, Sign In to add comment