Created
January 9, 2012 19:24
-
-
Save ingenthr/1584482 to your computer and use it in GitHub Desktop.
hello couchbase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* -*- 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. | |
| */ | |
| /** | |
| * Example "hello Couchbase" libcouchbase program. | |
| * | |
| * @author Matt Ingenthron | |
| */ | |
| #include <getopt.h> | |
| #include <stdio.h> | |
| #include <ctype.h> | |
| #include <stdlib.h> | |
| #include <errno.h> | |
| #include <string.h> | |
| #include <libcouchbase/couchbase.h> | |
| /* the callback invoked by the library when receiving an error */ | |
| static void error_callback(libcouchbase_t instance, | |
| libcouchbase_error_t error, | |
| const char *errinfo) | |
| { | |
| (void)instance; | |
| fprintf(stderr, "Error %d", error); | |
| if (errinfo) { | |
| fprintf(stderr, ": %s", errinfo); | |
| } | |
| fprintf(stderr, "\n"); | |
| exit(EXIT_FAILURE); | |
| } | |
| /* the callback invoked by the library when receiving a get response */ | |
| static void get_callback(libcouchbase_t instance, | |
| const void *cookie, | |
| libcouchbase_error_t error, | |
| const void *key, size_t nkey, | |
| const void *bytes, size_t nbytes, | |
| uint32_t flags, uint64_t cas) | |
| { | |
| (void)instance; (void)cookie; (void)cas; | |
| fwrite(bytes, 1, nbytes, stdout); | |
| fprintf(stderr, "Get callback received %s\n", response_string); | |
| } | |
| int main(int argc, char **argv) | |
| { | |
| libcouchbase_t instance; /* our libcouchbase instance */ | |
| libcouchbase_error_t oprc; /* for checking various responses */ | |
| const char *host = "localhost:8091"; | |
| const char *username = NULL; | |
| const char *passwd = NULL; | |
| const char *bucket = "default"; | |
| instance = libcouchbase_create(host, username, | |
| passwd, bucket, NULL); | |
| if (instance == NULL) { | |
| fprintf(stderr, "Failed to create libcouchbase instance\n"); | |
| return 1; | |
| } | |
| (void)libcouchbase_set_error_callback(instance, error_callback); | |
| (void)libcouchbase_set_get_callback(instance, get_callback); | |
| if (libcouchbase_connect(instance) != LIBCOUCHBASE_SUCCESS) { | |
| fprintf(stderr, "Failed to connect libcouchbase instance to server\n"); | |
| return 1; | |
| } | |
| /* Wait for the connect to compelete */ | |
| libcouchbase_wait(instance); | |
| /* A simple document we want to create */ | |
| char *key = "hello"; | |
| char *doc = "{ \"message\": \"world\" }"; | |
| /* Store doc to in the system */ | |
| oprc = libcouchbase_store(instance, | |
| NULL, | |
| LIBCOUCHBASE_SET, | |
| key, /* the key or _id of the document */ | |
| strlen(key), /* the key length */ | |
| doc, | |
| strlen(doc), /* length of */ | |
| 0, /* flags, */ | |
| 0, /* expiration */ | |
| 0); /* and CAS values, see API reference */ | |
| if (oprc != LIBCOUCHBASE_SUCCESS) { | |
| fprintf(stderr, "Failed to create hello store operation.\n"); | |
| return 1; | |
| } | |
| /* Wait for the operation to compelete */ | |
| libcouchbase_wait(instance); | |
| oprc = libcouchbase_get_last_error(instance); | |
| if (oprc == LIBCOUCHBASE_SUCCESS) { | |
| fprintf(stderr, "Successfully set hello.\n"); | |
| } else { | |
| fprintf(stderr, "Could not set hello. Error received is %d\n", oprc); | |
| return 1; | |
| } | |
| /* Now go and get "hello" */ | |
| const char* keys[1]; | |
| size_t nkey[1]; | |
| keys[0] = key; | |
| nkey[0] = strlen(key); | |
| oprc = libcouchbase_mget(instance, | |
| NULL, | |
| 1, | |
| (const void*const*)keys, | |
| nkey, | |
| NULL); | |
| if (oprc != LIBCOUCHBASE_SUCCESS) { | |
| fprintf(stderr, "Failed to create hello mget operation.\n"); | |
| return 1; | |
| } | |
| /* Wait for the operation to compelete */ | |
| libcouchbase_wait(instance); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment