Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Upload minimal working example

Change-Id: I7bf7a8581c4010237fc84178a98e13056c4642af
Reviewed-on: http://review.couchbase.org/21357
Tested-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
  • Loading branch information...
commit 545e4838b9e1aa47cf522b05385dbfcdceea62ba 1 parent 03d249d
@avsej avsej authored
View
1  .gitignore
@@ -41,6 +41,7 @@
/example/pillowfight/pillowfight
/example/syncmode/syncmode
/example/libeventdirect/eventloop
+/example/minimal/minimal
/gtest-1.6.0
/gtest-1.6.0.zip
/include/libcouchbase/configuration.h
View
5 Makefile.am
@@ -244,6 +244,10 @@ example_syncmode_syncmode_SOURCES = example/syncmode/main.c
example_syncmode_syncmode_DEPENDENCIES = libcouchbase.la
example_syncmode_syncmode_LDADD = libcouchbase.la
+example_minimal_minimal_SOURCES = example/minimal/minimal.c
+example_minimal_minimal_DEPENDENCIES = libcouchbase.la
+example_minimal_minimal_LDADD = libcouchbase.la
+
example_libeventdirect_eventloop_SOURCES = example/libeventdirect/main.c
example_libeventdirect_eventloop_DEPENDENCIES = libcouchbase.la
example_libeventdirect_eventloop_LDADD = libcouchbase.la -levent
@@ -254,6 +258,7 @@ example_libeventdirect_eventloop_LDADD = libcouchbase.la -levent
if BUILD_PLUGINS
if BUILD_EXAMPLES
noinst_PROGRAMS += example/syncmode/syncmode
+noinst_PROGRAMS += example/minimal/minimal
if HAVE_CXX
noinst_PROGRAMS += example/pillowfight/pillowfight
endif
View
17 example/README.markdown
@@ -28,3 +28,20 @@ pillowfight
This is an example that implements a small test program to show
you some of the functionalities in libcouchbase.
+
+minimal
+-------
+
+This is an minimal single-file example which works both unix-like and windows OS.
+It accepts three arguments: "host:port", "bucket" and "password".
+
+Build:
+
+ gcc -lcouchbase -o minimal minimal.c
+ cl /DWIN32 /Iinclude lib\libcouchbase.lib minimal.c
+
+Execute:
+
+ valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes ./minimal
+ ./minimal <host:port> <bucket> <passwd>
+ mininal.exe <host:port> <bucket> <passwd>
View
164 example/minimal/minimal.c
@@ -0,0 +1,164 @@
+/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+ * Copyright 2012 Couchbase, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * BUILD:
+ *
+ * gcc -lcouchbase -o minimal minimal.c
+ * cl /DWIN32 /Iinclude lib\libcouchbase.lib minimal.c
+ *
+ * RUN:
+ *
+ * valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes ./minimal
+ * ./minimal <host:port> <bucket> <passwd>
+ * mininal.exe <host:port> <bucket> <passwd>
+ */
+#include <stdio.h>
+#include <libcouchbase/couchbase.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#ifdef _WIN32
+#define PRIu64 "I64u"
+#else
+#include <inttypes.h>
+#endif
+
+static void
+error_callback(lcb_t instance, lcb_error_t error, const char *errinfo)
+{
+ fprintf(stderr, "ERROR: %s (0x%x), %s\n",
+ lcb_strerror(instance, error), error, errinfo);
+ exit(EXIT_FAILURE);
+}
+
+
+static void
+store_callback(lcb_t instance, const void *cookie, lcb_storage_t operation,
+ lcb_error_t error, const lcb_store_resp_t *item)
+{
+ if (error == LCB_SUCCESS) {
+ fprintf(stderr, "STORED \"");
+ fwrite(item->v.v0.key, sizeof(char), item->v.v0.nkey, stderr);
+ fprintf(stderr, "\" CAS: %"PRIu64"\n", item->v.v0.cas);
+ } else {
+ fprintf(stderr, "STORE ERROR: %s (0x%x)\n",
+ lcb_strerror(instance, error), error);
+ exit(EXIT_FAILURE);
+ }
+ (void)cookie;
+ (void)operation;
+}
+
+static void
+get_callback(lcb_t instance, const void *cookie, lcb_error_t error,
+ const lcb_get_resp_t *item)
+{
+ if (error == LCB_SUCCESS) {
+ fprintf(stderr, "GOT \"");
+ fwrite(item->v.v0.key, sizeof(char), item->v.v0.nkey, stderr);
+ fprintf(stderr, "\" CAS: %"PRIu64" FLAGS:0x%x SIZE:%lu\n",
+ item->v.v0.cas, item->v.v0.flags, (unsigned long)item->v.v0.nbytes);
+ fwrite(item->v.v0.bytes, sizeof(char), item->v.v0.nbytes, stderr);
+ } else {
+ fprintf(stderr, "GET ERROR: %s (0x%x)\n",
+ lcb_strerror(instance, error), error);
+ }
+ (void)cookie;
+}
+
+int
+main(int argc, char *argv[])
+{
+ lcb_error_t err;
+ lcb_t instance;
+ struct lcb_create_st create_options;
+ struct lcb_create_io_ops_st io_opts;
+
+ io_opts.version = 0;
+ io_opts.v.v0.type = LCB_IO_OPS_DEFAULT;
+ io_opts.v.v0.cookie = NULL;
+
+ memset(&create_options, 0, sizeof(create_options));
+ err = lcb_create_io_ops(&create_options.v.v0.io, &io_opts);
+ if (err != LCB_SUCCESS) {
+ fprintf(stderr, "Failed to create IO instance: %s\n",
+ lcb_strerror(NULL, err));
+ return 1;
+ }
+
+ if (argc > 1) {
+ create_options.v.v0.host = argv[1];
+ }
+ if (argc > 2) {
+ create_options.v.v0.user = argv[2];
+ create_options.v.v0.bucket = argv[2];
+ }
+ if (argc > 3) {
+ create_options.v.v0.passwd = argv[3];
+ }
+ err = lcb_create(&instance, &create_options);
+ if (err != LCB_SUCCESS) {
+ fprintf(stderr, "Failed to create libcouchbase instance: %s\n",
+ lcb_strerror(NULL, err));
+ return 1;
+ }
+ (void)lcb_set_error_callback(instance, error_callback);
+ /* Initiate the connect sequence in libcouchbase */
+ if ((err = lcb_connect(instance)) != LCB_SUCCESS) {
+ fprintf(stderr, "Failed to initiate connect: %s\n",
+ lcb_strerror(NULL, err));
+ lcb_destroy(instance);
+ return 1;
+ }
+ (void)lcb_set_get_callback(instance, get_callback);
+ (void)lcb_set_store_callback(instance, store_callback);
+ /* Run the event loop and wait until we've connected */
+ lcb_wait(instance);
+ {
+ lcb_store_cmd_t cmd;
+ const lcb_store_cmd_t * const commands[1] = { &cmd };
+ memset(&cmd, 0, sizeof(cmd));
+ cmd.v.v0.operation = LCB_SET;
+ cmd.v.v0.key = "foo";
+ cmd.v.v0.nkey = 3;
+ cmd.v.v0.bytes = "bar";
+ cmd.v.v0.nbytes = 3;
+ err = lcb_store(instance, NULL, 1, commands);
+ if (err != LCB_SUCCESS) {
+ fprintf(stderr, "Failed to get: %s\n", lcb_strerror(NULL, err));
+ return 1;
+ }
+ }
+ lcb_wait(instance);
+ {
+ lcb_get_cmd_t cmd;
+ const lcb_get_cmd_t * const commands[1] = { &cmd };
+ memset(&cmd, 0, sizeof(cmd));
+ cmd.v.v0.key = "foo";
+ cmd.v.v0.nkey = 3;
+ err = lcb_get(instance, NULL, 1, commands);
+ if (err != LCB_SUCCESS) {
+ fprintf(stderr, "Failed to get: %s\n", lcb_strerror(NULL, err));
+ return 1;
+ }
+ }
+ lcb_wait(instance);
+ lcb_destroy(instance);
+
+ return 0;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.