Skip to content

Commit

Permalink
n1ql: primary index creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mnunberg committed Jun 3, 2016
1 parent 506cbe1 commit 8573d8f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,16 @@ Go |
node.js |
Java |
PHP

### Creating N1QL Primary Index

This should show how to create the primary N1QL index on a bucket, ignoring
whether it exists or not

[C](c/n1ql-create-primary-index.cc) |
[Python](python/n1ql-create-primary-index.py) |
.NET |
Go |
node.js |
Java |
PHP
1 change: 1 addition & 0 deletions c/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ subdoc-retrieving
fts-basic
create-remove-bucket
flush
n1ql-create-primary-index
2 changes: 1 addition & 1 deletion c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ PROGS=connecting updating retrieving query-create-index query-criteria \
query-placeholders counter expiration \
query-consistency cas durability bulk-get bulk-store \
connecting-ssl subdoc-retrieving subdoc-updating fts-basic \
create-remove-bucket flush
create-remove-bucket flush n1ql-create-primary-index

all: $(PROGS)

Expand Down
41 changes: 41 additions & 0 deletions c/n1ql-create-primary-index.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <libcouchbase/couchbase.h>
#include <libcouchbase/ixmgmt.h>

static void
ixmgmt_callback(lcb_t, int, const lcb_RESPN1XMGMT *resp)
{
if (resp->rc == LCB_SUCCESS) {
printf("Operation successful!\n");
} else if (resp->rc == LCB_KEY_EEXISTS) {
printf("Index already exists!\n");
} else {
printf("Operation failed: %s\n", lcb_strerror(NULL, resp->rc));
}
}


int main(int, char **)
{
lcb_t instance;
lcb_create(&instance, NULL);
lcb_connect(instance);
lcb_wait(instance);

if (lcb_get_bootstrap_status(instance) != LCB_SUCCESS) {
printf("Couldn't bootstrap: %s\n",
lcb_strerror(NULL, lcb_get_bootstrap_status(instance)));
exit(EXIT_FAILURE);
}

const char *bktname;
lcb_cntl(instance, LCB_CNTL_GET, LCB_CNTL_BUCKETNAME, &bktname);

lcb_CMDN1XMGMT cmd = { { 0 } };
cmd.spec.flags = LCB_N1XSPEC_F_PRIMARY;
cmd.spec.keyspace = bktname;
cmd.spec.nkeyspace = strlen(bktname);
cmd.callback = ixmgmt_callback;
lcb_n1x_create(instance, NULL, &cmd);
lcb_wait(instance);
lcb_destroy(instance);
}
7 changes: 7 additions & 0 deletions python/n1ql-create-primary-index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python

from couchbase.bucket import Bucket

cb = Bucket('couchbase://localhost/default')
manager = cb.bucket_manager()
manager.n1ql_index_create_primary(ignore_exists=True)

0 comments on commit 8573d8f

Please sign in to comment.