libcouchbase 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 430532d85eb75b3478daa697098512f585b7cd54
4
- data.tar.gz: 89c63ea2b2de27e46d47e8d5007f4b8229332a34
3
+ metadata.gz: 6d524a31db2216cde317e1ccf74030b5fbd1ca60
4
+ data.tar.gz: 194c1907de0856d710203559b769cf6982cf7924
5
5
  SHA512:
6
- metadata.gz: 3e1fc5f17546cdb22d93c5ca76da96562c5fb2b866d997065ac4c7d50cf1574c53280268b1266ce6573bdb0d552b8678514fd0d0a7efa04c2c34e9a332e9a619
7
- data.tar.gz: 9ded77701bbd289afc6986d0bbe15064999430e821b9df41bbee56c397f21186b36a874e79797e0cf260202317fa12a4118ac2ff8067d5fd0afa2cc262cae8a7
6
+ metadata.gz: fd3fdf3e6f2c7e9f69d948368c2c5132b99633bcdad8c3f8357ad4128b88834f35024a80f14f57e61f00aef4efc6cde98f6ca0761b7d11ed28f303beaf387a3b
7
+ data.tar.gz: 4c4c5d0ae0af7c8e489af2bf9617124e7ee8ca39251294e2b2e78200116ebdb7b07c952c36b01a74646ab52fc20c9d01d6ae6e6ccc29fe32244aaf93c527ea2e
@@ -3,7 +3,7 @@ use strict;
3
3
  use warnings;
4
4
  use Digest::MD5 qw(md5_hex);
5
5
 
6
- my $HDR = "** $0:";
6
+ my $HDR = "** $0 ($$):";
7
7
  $\="\n";
8
8
 
9
9
  my $DT_SRC = shift @ARGV;
@@ -16,23 +16,36 @@ if (!scalar @O_FILES) {
16
16
  exec($CMD,@ARGV);
17
17
  }
18
18
 
19
+ # Copy .o files to a temporary location before DTrace messes with them
20
+ chomp(my $tmpdir = `mktemp -d -t $$`);
21
+ if (system("tar cf - @O_FILES | tar xf - -C $tmpdir") != 0) {
22
+ system("rm -r $tmpdir");
23
+ exit(1);
24
+ }
25
+
19
26
  my $ss = join('_', @O_FILES);
20
27
  my $hexstr = md5_hex($ss);
21
28
 
22
- my $INSTRUMENTED = "generated/probes_${hexstr}.o";
29
+ # From now, we work with files in the temporary location, update @ARGV
30
+ map { $_ =~ s,.+\.o$,$tmpdir/$&, } @ARGV;
31
+
32
+ my $INSTRUMENTED = "generated/probes_${hexstr}_$$.o";
23
33
  # Run DTrace instrumentation. Assuming running from build directory:
24
34
  my @args = (
25
35
  'dtrace', '-C', '-G',
26
36
  '-s', $DT_SRC,
27
37
  '-o', $INSTRUMENTED,
28
- @O_FILES);
38
+ grep { $_ =~ /\.o$/ } @ARGV);
29
39
 
30
40
  print "$HDR: Creating instrumented DTrace object: @args";
31
41
  if (system(@args) != 0) {
42
+ system("rm -r $tmpdir");
32
43
  exit(1);
33
44
  }
34
45
 
35
46
  unshift @ARGV, $CMD;
36
47
  push @ARGV, $INSTRUMENTED;
37
48
  print "$HDR: Linking with instrumented DTrace object: @ARGV";
38
- exit(system(@ARGV));
49
+ my $rc = system(@ARGV);
50
+ system("rm -r $tmpdir");
51
+ exit($rc);
@@ -723,7 +723,9 @@ cliopts_parse_options(cliopts_entry *entries,
723
723
  }
724
724
 
725
725
  print_help(&ctx, settings);
726
- exit(0);
726
+ if (!settings->help_noexit) {
727
+ exit(0);
728
+ }
727
729
 
728
730
  } else if (curmode == MODE_RESTARGS) {
729
731
  ii++;
@@ -776,3 +778,85 @@ cliopts_parse_options(cliopts_entry *entries,
776
778
  }
777
779
  return ret;
778
780
  }
781
+
782
+ CLIOPTS_API
783
+ int cliopts_split_args(char *args, int *argc, char ***argv)
784
+ {
785
+ char *p = args;
786
+ char *current = NULL;
787
+ int skip = 0;
788
+
789
+ *argc = 0;
790
+ *argv = NULL;
791
+
792
+ #define ch(x) (*(x + skip))
793
+
794
+ while (1) {
795
+
796
+ while (ch(p) && isspace(ch(p))) {
797
+ p++;
798
+ }
799
+ if (*p) {
800
+ int insq = 0;
801
+ int done = 0;
802
+
803
+ if (current == NULL) {
804
+ current = p;
805
+ }
806
+ while (!done) {
807
+ if (insq) {
808
+ if (ch(p) == '\\' && ch(p + 1) == '\'') {
809
+ skip++;
810
+ } else if (ch(p) == '\'') {
811
+ if (ch(p + 1) && !isspace(ch(p + 1))) {
812
+ // do not consider single quote inside word as terminating
813
+ } else {
814
+ *p = '\0';
815
+ p++;
816
+ done = 1;
817
+ }
818
+ } else if (!*p) {
819
+ cliopt_debug("unterminated single quote");
820
+ goto err;
821
+ }
822
+ } else {
823
+ switch (ch(p)) {
824
+ case '\'':
825
+ if (current == p) {
826
+ current++;
827
+ insq = 1;
828
+ }
829
+ break;
830
+ case ' ':
831
+ case '\n':
832
+ case '\r':
833
+ case '\t':
834
+ *p = '\0';
835
+ p++;
836
+ case '\0':
837
+ done = 1;
838
+ break;
839
+ }
840
+ }
841
+ if (skip) {
842
+ *p = ch(p);
843
+ }
844
+ if (*p && !done) {
845
+ p++;
846
+ }
847
+ }
848
+ *argv = realloc(*argv, (*argc + 1) * sizeof(char *));
849
+ (*argv)[*argc] = current;
850
+ (*argc)++;
851
+ current = NULL;
852
+ } else {
853
+ return 0;
854
+ }
855
+ }
856
+
857
+ err:
858
+ free(*argv);
859
+ *argc = 0;
860
+ *argv = NULL;
861
+ return 1;
862
+ }
@@ -98,6 +98,8 @@ struct cliopts_extra_settings {
98
98
  int error_nohelp;
99
99
  /** Don't interpret --help or -? as help flags */
100
100
  int help_noflag;
101
+ /** Don't exit on --help or -? */
102
+ int help_noexit;
101
103
  /** Program name (defaults to argv[0]) */
102
104
  const char *progname;
103
105
  /** Usage string (defaults to "[OPTIONS..]") */
@@ -161,6 +163,14 @@ cliopts_parse_options(cliopts_entry *entries,
161
163
  char **argv,
162
164
  int *lastidx,
163
165
  struct cliopts_extra_settings *settings);
166
+
167
+ /**
168
+ * Split string for arguments, handling single quotes for grouping
169
+ */
170
+ CLIOPTS_API
171
+ int
172
+ cliopts_split_args(char *args, int *argc, char ***argv);
173
+
164
174
  #ifdef __cplusplus
165
175
  }
166
176
 
@@ -260,6 +270,22 @@ public:
260
270
  doCopy(other);
261
271
  }
262
272
 
273
+ ~TOption() {
274
+ freeInnerVal();
275
+ }
276
+
277
+ /**
278
+ * Reset result to the default value for the option
279
+ * @return the option object, for method chaining.
280
+ */
281
+ inline Ttype& reset() {
282
+ freeInnerVal();
283
+ innerVal = createDefault();
284
+ priv = Tpriv();
285
+ found = 0;
286
+ return *this;
287
+ }
288
+
263
289
  /**
264
290
  * Set the default value for the option
265
291
  * @param val the default value
@@ -323,6 +349,17 @@ protected:
323
349
  /** Called from within copy constructor */
324
350
  inline void doCopy(TOption&) {}
325
351
 
352
+ inline void freeInnerVal() {
353
+ switch (ktype) {
354
+ case CLIOPTS_ARGT_LIST:
355
+ cliopts_list_clear((cliopts_list *)&innerVal);
356
+ break;
357
+ default:
358
+ /* nothing to do */
359
+ break;
360
+ }
361
+ }
362
+
326
363
  /** Create the default value for the option */
327
364
  static inline Taccum createDefault() { return Taccum(); }
328
365
  };
@@ -431,6 +468,14 @@ public:
431
468
 
432
469
  void addOption(Option& opt) { options.push_back(&opt); }
433
470
 
471
+ /**
472
+ * Resets internal state.
473
+ */
474
+ void reset() {
475
+ options.clear();
476
+ restargs.clear();
477
+ }
478
+
434
479
  /**
435
480
  * Parses the options from the commandline
436
481
  * @param argc number of arguments
@@ -5,6 +5,7 @@
5
5
  #include <libcouchbase/api3.h>
6
6
  #include <assert.h>
7
7
  #include <string.h>
8
+ #include <cstdlib>
8
9
  #include <string>
9
10
  #include <vector>
10
11
 
@@ -47,6 +48,16 @@ int main(int argc, char **argv) {
47
48
  } else {
48
49
  crst.v.v3.connstr = DEFAULT_CONNSTR;
49
50
  }
51
+ if (argc > 2) {
52
+ crst.v.v3.username = argv[2];
53
+ } else {
54
+ crst.v.v3.username = "Administrator";
55
+ }
56
+ if (argc > 3) {
57
+ crst.v.v3.passwd = argv[3];
58
+ } else {
59
+ crst.v.v3.passwd = "password";
60
+ }
50
61
 
51
62
  lcb_t instance;
52
63
  lcb_error_t rc = lcb_create(&instance, &crst);
@@ -57,6 +57,16 @@ int main(int argc, char **argv)
57
57
  } else {
58
58
  crst.v.v3.connstr = DEFAULT_CONNSTR;
59
59
  }
60
+ if (argc > 2) {
61
+ crst.v.v3.username = argv[2];
62
+ } else {
63
+ crst.v.v3.username = "Administrator";
64
+ }
65
+ if (argc > 3) {
66
+ crst.v.v3.passwd = argv[3];
67
+ } else {
68
+ crst.v.v3.passwd = "password";
69
+ }
60
70
 
61
71
  lcb_t instance;
62
72
  lcb_error_t rc = lcb_create(&instance, &crst);
@@ -0,0 +1,287 @@
1
+ /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
+ /*
3
+ * Copyright 2017 Couchbase, Inc.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ #include <unistd.h>
19
+ #include <stdlib.h>
20
+
21
+ #include <libcouchbase/couchbase.h>
22
+ #include <libcouchbase/n1ql.h>
23
+ #include <assert.h>
24
+ #include <string.h>
25
+
26
+ static void generic_callback(lcb_t instance, int type, const lcb_RESPBASE *rb)
27
+ {
28
+ if (rb->rc != LCB_SUCCESS && rb->rc != LCB_SUBDOC_MULTI_FAILURE) {
29
+ printf("Failure: 0x%x, %s\n", rb->rc, lcb_strerror(instance, rb->rc));
30
+ return;
31
+ }
32
+
33
+ switch (type) {
34
+ case LCB_CALLBACK_SDLOOKUP: {
35
+ lcb_SDENTRY ent;
36
+ size_t iter = 0;
37
+ size_t index = 0;
38
+ const lcb_RESPSUBDOC *resp = (const lcb_RESPSUBDOC *)rb;
39
+
40
+ while (lcb_sdresult_next(resp, &ent, &iter)) {
41
+ if (index == 0 && ent.status != LCB_SUCCESS) {
42
+ // attribute does not exist, skip response
43
+ return;
44
+ }
45
+ if (index == 1) {
46
+ printf(" * %.*s: %.*s%%\n", (int)resp->nkey, resp->key, (int)ent.nvalue, ent.value);
47
+ }
48
+ index++;
49
+ }
50
+ break;
51
+ }
52
+ case LCB_CALLBACK_SDMUTATE: {
53
+ lcb_SDENTRY ent;
54
+ size_t iter = 0;
55
+ size_t oix = 0;
56
+ const lcb_RESPSUBDOC *resp = (const lcb_RESPSUBDOC *)rb;
57
+ while (lcb_sdresult_next(resp, &ent, &iter)) {
58
+ size_t index = oix++;
59
+ if (type == LCB_CALLBACK_SDMUTATE) {
60
+ index = ent.index;
61
+ }
62
+ printf("[%lu]: 0x%x. %.*s\n", index, ent.status, (int)ent.nvalue, ent.value);
63
+ }
64
+ break;
65
+ }
66
+ }
67
+ }
68
+
69
+ static void n1qlrow_callback(lcb_t instance, int type, const lcb_RESPN1QL *resp)
70
+ {
71
+ if (resp->rc != LCB_SUCCESS) {
72
+ printf("Failure: 0x%x, %s\n", resp->rc, lcb_strerror(instance, resp->rc));
73
+ printf("HTTP status: %d\n", (int)resp->htresp->htstatus);
74
+ {
75
+ const char *const *hdr = resp->htresp->headers;
76
+ for (; hdr && *hdr; hdr++) {
77
+ printf("%s", *hdr);
78
+ if (hdr + 1) {
79
+ printf(" = %s", *(++hdr));
80
+ }
81
+ printf("\n");
82
+ }
83
+ }
84
+ printf("%.*s\n", (int)resp->nrow, resp->row);
85
+ return;
86
+ }
87
+
88
+ char *start = "{\"docID\":\"";
89
+ char *stop = "\"";
90
+
91
+ char *key = strnstr(resp->row, start, resp->nrow);
92
+ if (key == NULL || key != resp->row) {
93
+ return;
94
+ }
95
+ key += strlen(start);
96
+ char *z = strnstr(key, stop, resp->nrow - (resp->row - key));
97
+ if (z == NULL) {
98
+ return;
99
+ }
100
+ *z = '\0';
101
+
102
+ lcb_sched_enter(instance);
103
+ {
104
+ lcb_error_t rc;
105
+ lcb_CMDSUBDOC cmd = {};
106
+ lcb_SDSPEC specs[2] = {};
107
+ char *path = "discounts.jsmith123";
108
+
109
+ LCB_CMD_SET_KEY(&cmd, key, strlen(key));
110
+
111
+ specs[0].sdcmd = LCB_SDCMD_EXISTS;
112
+ specs[0].options = LCB_SDSPEC_F_XATTRPATH;
113
+ LCB_SDSPEC_SET_PATH(&specs[0], path, strlen(path));
114
+
115
+ specs[1].sdcmd = LCB_SDCMD_GET;
116
+ specs[1].options = LCB_SDSPEC_F_XATTRPATH;
117
+ LCB_SDSPEC_SET_PATH(&specs[1], path, strlen(path));
118
+
119
+ cmd.specs = specs;
120
+ cmd.nspecs = 2;
121
+ rc = lcb_subdoc3(instance, NULL, &cmd);
122
+ assert(rc == LCB_SUCCESS);
123
+ }
124
+ lcb_sched_leave(instance);
125
+ }
126
+
127
+ // #define DEFAULT_CONNSTR "couchbase://localhost/travel-sample"
128
+ #define DEFAULT_CONNSTR "couchbase://192.168.1.194/travel-sample"
129
+
130
+ static lcb_t connect_as(char *username, char *password)
131
+ {
132
+ struct lcb_create_st crst = {.version = 3};
133
+
134
+ crst.v.v3.connstr = DEFAULT_CONNSTR;
135
+ crst.v.v3.username = username;
136
+ crst.v.v3.passwd = password;
137
+
138
+ lcb_t instance;
139
+ lcb_error_t rc;
140
+
141
+ rc = lcb_create(&instance, &crst);
142
+ assert(rc == LCB_SUCCESS);
143
+ rc = lcb_connect(instance);
144
+ assert(rc == LCB_SUCCESS);
145
+ lcb_wait(instance);
146
+ rc = lcb_get_bootstrap_status(instance);
147
+ assert(rc == LCB_SUCCESS);
148
+
149
+ lcb_install_callback3(instance, LCB_CALLBACK_DEFAULT, generic_callback);
150
+
151
+ return instance;
152
+ }
153
+
154
+ int main()
155
+ {
156
+ lcb_error_t rc;
157
+ lcb_t instance;
158
+
159
+ instance = connect_as("Administrator", "password");
160
+
161
+ // Add key-value pairs to hotel_10138, representing traveller-Ids and associated discount percentages
162
+ {
163
+ lcb_CMDSUBDOC cmd = {};
164
+ lcb_SDSPEC specs[4] = {};
165
+ char *key = "hotel_10138";
166
+
167
+ LCB_CMD_SET_KEY(&cmd, key, strlen(key));
168
+ {
169
+ char *path = "discounts.jsmith123";
170
+ char *val = "20";
171
+
172
+ specs[0].sdcmd = LCB_SDCMD_DICT_UPSERT;
173
+ specs[0].options = LCB_SDSPEC_F_MKINTERMEDIATES | LCB_SDSPEC_F_XATTRPATH;
174
+ LCB_SDSPEC_SET_PATH(&specs[0], path, strlen(path));
175
+ LCB_CMD_SET_VALUE(&specs[0], val, strlen(val));
176
+ }
177
+ {
178
+ char *path = "discounts.pjones356";
179
+ char *val = "30";
180
+
181
+ specs[1].sdcmd = LCB_SDCMD_DICT_UPSERT;
182
+ specs[1].options = LCB_SDSPEC_F_MKINTERMEDIATES | LCB_SDSPEC_F_XATTRPATH;
183
+ LCB_SDSPEC_SET_PATH(&specs[1], path, strlen(path));
184
+ LCB_CMD_SET_VALUE(&specs[1], val, strlen(val));
185
+ }
186
+ // The following lines, "insert" and "remove", simply demonstrate insertion and
187
+ // removal of the same path and value
188
+ {
189
+ char *path = "discounts.jbrown789";
190
+ char *val = "25";
191
+
192
+ specs[2].sdcmd = LCB_SDCMD_DICT_ADD;
193
+ specs[2].options = LCB_SDSPEC_F_MKINTERMEDIATES | LCB_SDSPEC_F_XATTRPATH;
194
+ LCB_SDSPEC_SET_PATH(&specs[2], path, strlen(path));
195
+ LCB_CMD_SET_VALUE(&specs[2], val, strlen(val));
196
+ }
197
+ {
198
+ char *path = "discounts.jbrown789";
199
+
200
+ specs[3].sdcmd = LCB_SDCMD_REMOVE;
201
+ specs[3].options = LCB_SDSPEC_F_XATTRPATH;
202
+ LCB_SDSPEC_SET_PATH(&specs[3], path, strlen(path));
203
+ }
204
+
205
+ cmd.specs = specs;
206
+ cmd.nspecs = 4;
207
+ rc = lcb_subdoc3(instance, NULL, &cmd);
208
+ assert(rc == LCB_SUCCESS);
209
+ }
210
+
211
+ // Add key - value pairs to hotel_10142, again representing traveller - Ids and associated discount percentages
212
+ {
213
+ lcb_CMDSUBDOC cmd = {};
214
+ lcb_SDSPEC specs[2] = {};
215
+ char *key = "hotel_10142";
216
+
217
+ LCB_CMD_SET_KEY(&cmd, key, strlen(key));
218
+ {
219
+ char *path = "discounts.jsmith123";
220
+ char *val = "15";
221
+
222
+ specs[0].sdcmd = LCB_SDCMD_DICT_UPSERT;
223
+ specs[0].options = LCB_SDSPEC_F_MKINTERMEDIATES | LCB_SDSPEC_F_XATTRPATH;
224
+ LCB_SDSPEC_SET_PATH(&specs[0], path, strlen(path));
225
+ LCB_CMD_SET_VALUE(&specs[0], val, strlen(val));
226
+ }
227
+ {
228
+ char *path = "discounts.pjones356";
229
+ char *val = "10";
230
+
231
+ specs[1].sdcmd = LCB_SDCMD_DICT_UPSERT;
232
+ specs[1].options = LCB_SDSPEC_F_MKINTERMEDIATES | LCB_SDSPEC_F_XATTRPATH;
233
+ LCB_SDSPEC_SET_PATH(&specs[1], path, strlen(path));
234
+ LCB_CMD_SET_VALUE(&specs[1], val, strlen(val));
235
+ }
236
+
237
+ cmd.specs = specs;
238
+ cmd.nspecs = 2;
239
+ rc = lcb_subdoc3(instance, NULL, &cmd);
240
+ assert(rc == LCB_SUCCESS);
241
+ }
242
+
243
+ lcb_wait(instance);
244
+
245
+ // Create a user and assign roles. This user will search for their available discounts.
246
+ {
247
+ lcb_CMDHTTP cmd = {};
248
+ char *path = "/settings/rbac/users/local/jsmith123";
249
+ char *payload = "password=jsmith123pwd&name=John+Smith"
250
+ "&roles=data_reader[travel-sample],query_select[travel-sample],data_writer[travel-sample]";
251
+
252
+ cmd.type = LCB_HTTP_TYPE_MANAGEMENT;
253
+ cmd.method = LCB_HTTP_METHOD_PUT;
254
+ LCB_CMD_SET_KEY(&cmd, path, strlen(path));
255
+ cmd.body = payload;
256
+ cmd.nbody = strlen(payload);
257
+ cmd.content_type = "application/x-www-form-urlencoded";
258
+
259
+ lcb_http3(instance, NULL, &cmd);
260
+ lcb_wait(instance);
261
+ }
262
+
263
+ lcb_destroy(instance);
264
+
265
+ // reconnect using new user
266
+ instance = connect_as("jsmith123", "jsmith123pwd");
267
+
268
+ // Perform a N1QL Query to return document IDs from the bucket. These IDs will be
269
+ // used to reference each document in turn, and check for extended attributes
270
+ // corresponding to discounts.
271
+ {
272
+ lcb_CMDN1QL cmd = {};
273
+ lcb_N1QLPARAMS *params = lcb_n1p_new();
274
+ char *query = "SELECT id, meta(`travel-sample`).id AS docID FROM `travel-sample`";
275
+
276
+ lcb_n1p_setstmtz(params, query);
277
+ rc = lcb_n1p_mkcmd(params, &cmd);
278
+ assert(rc == LCB_SUCCESS);
279
+ cmd.callback = n1qlrow_callback;
280
+
281
+ printf("User \"jsmith123\" has discounts in the hotels below:\n");
282
+ lcb_n1ql_query(instance, NULL, &cmd);
283
+ lcb_wait(instance);
284
+ }
285
+
286
+ lcb_destroy(instance);
287
+ }