From fd841e416881cc0392e61ec312c1870f3a0004bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Tue, 2 Dec 2014 10:06:21 +0100 Subject: Initial import of libmongo-client version 0.1.8-2 --- docs/tutorial/examples/GNUmakefile | 36 +++ docs/tutorial/examples/tut_bson_build.c | 81 ++++++ docs/tutorial/examples/tut_bson_build.json | 16 ++ docs/tutorial/examples/tut_bson_traverse.c | 123 ++++++++++ docs/tutorial/examples/tut_hl_client.c | 107 ++++++++ docs/tutorial/examples/tut_json2bson.c | 132 ++++++++++ docs/tutorial/examples/tut_mongo_sync.c | 273 +++++++++++++++++++++ docs/tutorial/examples/tut_mongo_sync_cmd_create.c | 82 +++++++ docs/tutorial/examples/tut_mongo_sync_cmd_custom.c | 81 ++++++ .../examples/tut_mongo_sync_cmd_index_create.c | 54 ++++ 10 files changed, 985 insertions(+) create mode 100644 docs/tutorial/examples/GNUmakefile create mode 100644 docs/tutorial/examples/tut_bson_build.c create mode 100644 docs/tutorial/examples/tut_bson_build.json create mode 100644 docs/tutorial/examples/tut_bson_traverse.c create mode 100644 docs/tutorial/examples/tut_hl_client.c create mode 100644 docs/tutorial/examples/tut_json2bson.c create mode 100644 docs/tutorial/examples/tut_mongo_sync.c create mode 100644 docs/tutorial/examples/tut_mongo_sync_cmd_create.c create mode 100644 docs/tutorial/examples/tut_mongo_sync_cmd_custom.c create mode 100644 docs/tutorial/examples/tut_mongo_sync_cmd_index_create.c (limited to 'docs/tutorial/examples') diff --git a/docs/tutorial/examples/GNUmakefile b/docs/tutorial/examples/GNUmakefile new file mode 100644 index 0000000..01b5363 --- /dev/null +++ b/docs/tutorial/examples/GNUmakefile @@ -0,0 +1,36 @@ +# NOTE: This Makefile assumes that a recent enough version of +# libmongo-client is installed! +# +# It will NOT work in the build directory, without an installed +# libmongo-client library. + +TUTORIAL_PROGRAMS = tut/bson_build \ + tut/bson_traverse \ + tut/mongo_sync \ + tut/mongo_sync_cmd_create \ + tut/mongo_sync_cmd_custom \ + tut/mongo_sync_cmd_index_create \ + tut/hl_client \ + tut/json2bson + +LMC_CFLAGS = $(shell pkg-config --cflags libmongo-client) +LMC_LIBS = $(shell pkg-config --libs libmongo-client) + +JSON_C_CFLAGS = $(shell pkg-config --cflags json) +JSON_C_LIBS = $(shell pkg-config --libs json) + +TUT_CFLAGS = ${LMC_CFLAGS} +TUT_LIBS = ${LMC_LIBS} + +all: ${TUTORIAL_PROGRAMS} +clean: + rm -f ${TUTORIAL_PROGRAMS} + -rmdir tut/ + +tut/json2bson: TUT_CFLAGS += ${JSON_C_CFLAGS} +tut/json2bson: TUT_LIBS += ${JSON_C_LIBS} +${TUTORIAL_PROGRAMS}: tut/%: tut_%.c + @install -d tut + ${CC} ${TUT_CFLAGS} ${CFLAGS} $< ${TUT_LIBS} ${LDFLAGS} -o $@ + +.PHONY: all clean diff --git a/docs/tutorial/examples/tut_bson_build.c b/docs/tutorial/examples/tut_bson_build.c new file mode 100644 index 0000000..2624310 --- /dev/null +++ b/docs/tutorial/examples/tut_bson_build.c @@ -0,0 +1,81 @@ +#include + +#include +#include + +int +main (void) +{ + bson *b_new, *b_builder, *b_builder_full; + bson *page1, *page2, *pages; + + page1 = bson_new (); + bson_append_string (page1, "title", "BSON tutorial", -1); + bson_append_string (page1, "content", "...", -1); + bson_append_int32 (page1, "importance", 1); + bson_finish (page1); + + page2 = bson_new (); + bson_append_string (page2, "title", "Some other thing", -1); + bson_append_string (page2, "content", "...", -1); + bson_append_int32 (page2, "importance", 0); + bson_finish (page2); + + pages = bson_new (); + bson_append_document (pages, "1", page1); + bson_append_document (pages, "2", page2); + bson_finish (pages); + + b_new = bson_new (); + bson_append_string (b_new, "author", "Gergely Nagy", -1); + bson_append_array (b_new, "pages", pages); + bson_append_boolean (b_new, "inline", TRUE); + bson_finish (b_new); + + b_builder = bson_build (BSON_TYPE_STRING, "author", "Gergely Nagy", -1, + BSON_TYPE_ARRAY, "pages", pages, + BSON_TYPE_BOOLEAN, "inline", TRUE, + BSON_TYPE_NONE); + bson_finish (b_builder); + + b_builder_full = bson_build_full + (BSON_TYPE_STRING, "author", FALSE, "Gergely Nagy", -1, + BSON_TYPE_ARRAY, "pages", TRUE, + bson_build_full (BSON_TYPE_DOCUMENT, "1", TRUE, + bson_build (BSON_TYPE_STRING, "title", "BSON tutorial", -1, + BSON_TYPE_STRING, "content", "...", -1, + BSON_TYPE_INT32, "importance", 1, + BSON_TYPE_NONE), + BSON_TYPE_DOCUMENT, "2", TRUE, + bson_build (BSON_TYPE_STRING, "title", "Some other thing", -1, + BSON_TYPE_STRING, "content", "...", -1, + BSON_TYPE_INT32, "importance", 0, + BSON_TYPE_NONE), + BSON_TYPE_NONE), + BSON_TYPE_BOOLEAN, "inline", FALSE, TRUE, + BSON_TYPE_NONE); + bson_finish (b_builder_full); + + if (bson_size (b_new) != bson_size (b_builder) || + bson_size (b_new) != bson_size (b_builder_full)) + { + fprintf (stderr, "There's something fishy: the three BSON objects have different sizes"); + return 1; + } + + if (memcmp (bson_data (b_new), bson_data (b_builder), bson_size (b_new)) != 0 || + memcmp (bson_data (b_new), bson_data (b_builder_full), bson_size (b_new)) != 0) + { + fprintf (stderr, "The BSON objects do not match. Something smells."); + return 1; + } + + bson_free (b_builder_full); + bson_free (b_builder); + bson_free (b_new); + bson_free (pages); + bson_free (page2); + bson_free (page1); + + return 0; +} diff --git a/docs/tutorial/examples/tut_bson_build.json b/docs/tutorial/examples/tut_bson_build.json new file mode 100644 index 0000000..078cf53 --- /dev/null +++ b/docs/tutorial/examples/tut_bson_build.json @@ -0,0 +1,16 @@ +{ + author: "Gergely Nagy", + pages: [ + { + title: "BSON tutorial", + content: "...", + importance: 1 + }, + { + title: "Some other thing", + content: "...", + importance: 0 + } + ], + inline: true +} diff --git a/docs/tutorial/examples/tut_bson_traverse.c b/docs/tutorial/examples/tut_bson_traverse.c new file mode 100644 index 0000000..4be7b1d --- /dev/null +++ b/docs/tutorial/examples/tut_bson_traverse.c @@ -0,0 +1,123 @@ +#include + +#include +#include + +bson * +tut_bson (void) +{ + bson *b; + + b = bson_build_full + (BSON_TYPE_STRING, "author", FALSE, "Gergely Nagy", -1, + BSON_TYPE_ARRAY, "pages", TRUE, + bson_build_full (BSON_TYPE_DOCUMENT, "1", TRUE, + bson_build (BSON_TYPE_STRING, "title", "BSON tutorial", -1, + BSON_TYPE_STRING, "content", "...", -1, + BSON_TYPE_INT32, "importance", 1, + BSON_TYPE_NONE), + BSON_TYPE_DOCUMENT, "2", TRUE, + bson_build (BSON_TYPE_STRING, "title", "Some other thing", -1, + BSON_TYPE_STRING, "content", "...", -1, + BSON_TYPE_INT32, "importance", 0, + BSON_TYPE_NONE), + BSON_TYPE_NONE), + BSON_TYPE_BOOLEAN, "inline", FALSE, TRUE, + BSON_TYPE_NONE); + bson_finish (b); + + return b; +} + +int +main (void) +{ + bson *doc; + bson_cursor *c, *c_arr, *c_page; + + bson *v_doc, *v_array; + gboolean v_bool; + const gchar *v_str; + + doc = tut_bson (); + + c = bson_find (doc, "author"); + bson_cursor_get_string (c, &v_str); + printf ("Author: %s\n", v_str); + + bson_cursor_next (c); + bson_cursor_next (c); + + bson_cursor_get_boolean (c, &v_bool); + printf ("inline: %s\n", (v_bool) ? "TRUE" : "FALSE"); + + bson_cursor_free (c); + + c = bson_find (doc, "author"); + bson_cursor_get_string (c, &v_str); + bson_cursor_free (c); + c = bson_find (doc, "inline"); + bson_cursor_get_boolean (c, &v_bool); + bson_cursor_free (c); + + printf ("Author: %s; inline: %s; (bson_find)\n", + v_str, (v_bool) ? "TRUE" : "FALSE"); + + c = bson_find (doc, "author"); + bson_cursor_get_string (c, &v_str); + while (bson_cursor_next (c)) + { + if (strcmp (bson_cursor_key (c), "inline") == 0) + { + bson_cursor_get_boolean (c, &v_bool); + break; + } + } + bson_cursor_free (c); + + printf ("Author: %s; inline: %s; (bson_cursor_next)\n", + v_str, (v_bool) ? "TRUE" : "FALSE"); + + c = bson_find (doc, "author"); + bson_cursor_get_string (c, &v_str); + bson_cursor_find_next (c, "inline"); + bson_cursor_get_boolean (c, &v_bool); + bson_cursor_free (c); + + printf ("Author: %s; inline: %s; (bson_cursor_find_next)\n", + v_str, (v_bool) ? "TRUE" : "FALSE"); + + c = bson_find (doc, "pages"); + bson_cursor_find (c, "inline"); + bson_cursor_get_boolean (c, &v_bool); + bson_cursor_find (c, "author"); + bson_cursor_get_string (c, &v_str); + bson_cursor_free (c); + + printf ("Author: %s; inline: %s; (bson_cursor_find)\n", + v_str, (v_bool) ? "TRUE" : "FALSE"); + + c = bson_cursor_new (doc); + while (bson_cursor_next (c)) + { + printf ("Key: %s; type=%s\n", bson_cursor_key (c), + bson_cursor_type_as_string (c)); + } + bson_cursor_free (c); + + c = bson_find (doc, "pages"); + bson_cursor_get_array (c, &v_array); + c_arr = bson_find (v_array, "2"); + bson_cursor_get_document (c_arr, &v_doc); + c_page = bson_find (v_doc, "title"); + bson_cursor_get_string (c_page, &v_str); + + bson_cursor_free (c_page); + bson_cursor_free (c_arr); + bson_cursor_free (c); + + printf ("Title of the 2nd page in the pages array: %s\n", v_str); + + bson_free (doc); + return 0; +} diff --git a/docs/tutorial/examples/tut_hl_client.c b/docs/tutorial/examples/tut_hl_client.c new file mode 100644 index 0000000..68ceb8f --- /dev/null +++ b/docs/tutorial/examples/tut_hl_client.c @@ -0,0 +1,107 @@ +#include + +#include +#include +#include +#include + +static void +do_inserts (mongo_sync_connection *conn) +{ + bson *base; + gint i; + + base = bson_build + (BSON_TYPE_STRING, "tutorial-program", "tut_hl_client.c", -1, + BSON_TYPE_INT32, "the answer to life, the universe and everything", 42, + BSON_TYPE_NONE); + bson_finish (base); + + for (i = 0; i < 1000; i++) + { + bson *n; + + n = bson_new_from_data (bson_data (base), bson_size (base) - 1); + bson_append_int32 (n, "counter", i); + bson_finish (n); + + if (!mongo_sync_cmd_insert (conn, "lmc.tutorial", n, NULL)) + { + fprintf (stderr, "Error inserting document %d: %s\n", i, + strerror (errno)); + exit (1); + } + bson_free (n); + } + bson_free (base); +} + +static void +do_query (mongo_sync_connection *conn) +{ + mongo_sync_cursor *c; + bson *query; + gchar *error = NULL; + + query = bson_build + (BSON_TYPE_STRING, "tutorial-program", "tut_hl_client.c", -1, + BSON_TYPE_NONE); + bson_finish (query); + + c = mongo_sync_cursor_new (conn, "lmc.tutorial", + mongo_sync_cmd_query (conn, "lmc.tutorial", 0, + 0, 10, query, NULL)); + if (!c) + { + fprintf (stderr, "Error creating the query cursor: %s\n", + strerror (errno)); + exit (1); + } + bson_free (query); + + while (mongo_sync_cursor_next (c)) + { + bson *b = mongo_sync_cursor_get_data (c); + bson_cursor *bc; + gint32 cnt; + + if (!b) + { + int e = errno; + + mongo_sync_cmd_get_last_error (conn, "lmc", &error); + fprintf (stderr, "Error retrieving cursor data: %s\n", + (error) ? error : strerror (e)); + exit (1); + } + + bc = bson_find (b, "counter"); + bson_cursor_get_int32 (bc, &cnt); + printf ("\rCounter: %d", cnt); + + bson_cursor_free (bc); + bson_free (b); + } + printf ("\n"); + + mongo_sync_cursor_free (c); +} + +int +main (void) +{ + mongo_sync_connection *conn; + + conn = mongo_sync_connect ("localhost", 27017, FALSE); + if (!conn) + { + fprintf (stderr, "Connection failed: %s\n", strerror (errno)); + return 1; + } + + do_inserts (conn); + do_query (conn); + + mongo_sync_disconnect (conn); + return 0; +} diff --git a/docs/tutorial/examples/tut_json2bson.c b/docs/tutorial/examples/tut_json2bson.c new file mode 100644 index 0000000..3ad5b9a --- /dev/null +++ b/docs/tutorial/examples/tut_json2bson.c @@ -0,0 +1,132 @@ +#define __STRICT_ANSI__ 1 + +#include +#include + +#include +#include +#include + +static bson *json_to_bson (struct json_object *json); + +static void +json_key_to_bson_key (bson *b, void *val, + const gchar *key) +{ + switch (json_object_get_type (val)) + { + case json_type_boolean: + bson_append_boolean (b, key, json_object_get_boolean (val)); + break; + case json_type_double: + bson_append_double (b, key, json_object_get_double (val)); + break; + case json_type_int: + bson_append_int32 (b, key, json_object_get_int (val)); + break; + case json_type_string: + bson_append_string (b, key, json_object_get_string (val), -1); + break; + case json_type_object: + { + bson *sub; + + sub = json_to_bson (val); + bson_append_document (b, key, sub); + bson_free (sub); + break; + } + case json_type_array: + { + gint pos; + bson *sub; + + sub = bson_new (); + + for (pos = 0; pos < json_object_array_length (val); pos++) + { + gchar *nk = g_strdup_printf ("%d", pos); + + json_key_to_bson_key (sub, json_object_array_get_idx (val, pos), + nk); + g_free (nk); + } + bson_finish (sub); + + bson_append_array (b, key, sub); + bson_free (sub); + break; + } + default: + break; + } +} + +static void +json_to_bson_foreach (bson *b, struct json_object *json) +{ + json_object_object_foreach (json, key, val) + { + json_key_to_bson_key (b, val, key); + } +} + +static bson * +json_to_bson (struct json_object *json) +{ + bson *b; + + b = bson_new (); + json_to_bson_foreach (b, json); + bson_finish (b); + + return b; +} + +int +main (int argc, char **argv) +{ + GIOChannel *input; + GString *json_str; + GError *error = NULL; + struct json_tokener *tokener; + + input = g_io_channel_unix_new (0); + + json_str = g_string_new (NULL); + tokener = json_tokener_new (); + + while (g_io_channel_read_line_string (input, json_str, + NULL, &error) == G_IO_STATUS_NORMAL) + { + struct json_object *json; + bson *bson; + + json_tokener_reset (tokener); + + json = json_tokener_parse_ex (tokener, json_str->str, json_str->len); + if (!json) + { + fprintf (stderr, "Error parsing json: %s\n", json_str->str); + break; + } + + if (json_object_get_type (json) != json_type_object) + { + fprintf (stderr, + "Error: json's top-level object is not object: %s\n", + json_str->str); + json_object_put (json); + break; + } + + bson = json_to_bson (json); + json_object_put (json); + + write (1, bson_data (bson), bson_size (bson)); + + bson_free (bson); + } + + return 0; +} diff --git a/docs/tutorial/examples/tut_mongo_sync.c b/docs/tutorial/examples/tut_mongo_sync.c new file mode 100644 index 0000000..ff27560 --- /dev/null +++ b/docs/tutorial/examples/tut_mongo_sync.c @@ -0,0 +1,273 @@ +#include +#include +#include +#include +#include + +void +tut_sync_connect (void) +{ + mongo_sync_connection *conn; + + conn = mongo_sync_connect ("localhost", 27017, TRUE); + if (!conn) + { + perror ("mongo_sync_connect()"); + exit (1); + } + mongo_sync_disconnect (conn); +} + +void +tut_sync_connect_replica (void) +{ + mongo_sync_connection *conn; + + conn = mongo_sync_connect ("mongo-master", 27017, TRUE); + if (!conn) + { + perror ("mongo_sync_connect()"); + return; + } + + if (!mongo_sync_conn_set_auto_reconnect (conn, TRUE)) + { + perror ("mongo_sync_conn_set_auto_reconnect()"); + return; + } + + if (!mongo_sync_conn_seed_add (conn, "mongo-replica", 27017)) + { + perror ("mongo_sync_conn_seed_add()"); + return; + } + if (!mongo_sync_conn_seed_add (conn, "mongo-replica-2", 27017)) + { + perror ("mongo_sync_conn_seed_add()"); + return; + } + + mongo_sync_disconnect (conn); +} + +void +tut_sync_insert (void) +{ + mongo_sync_connection *conn; + bson *doc1, *doc2, *doc3; + + conn = mongo_sync_connect ("localhost", 27017, FALSE); + if (!conn) + { + perror ("mongo_sync_connect()"); + exit (1); + } + + doc1 = bson_build (BSON_TYPE_STRING, "hello", "world", -1, + BSON_TYPE_INT32, "the_final_answer", 42, + BSON_TYPE_BOOLEAN, "yes?", FALSE, + BSON_TYPE_INT32, "n", 1, + BSON_TYPE_NONE); + bson_finish (doc1); + + if (!mongo_sync_cmd_insert (conn, "tutorial.docs", doc1, NULL)) + { + perror ("mongo_sync_cmd_insert()"); + exit (1); + } + + doc2 = bson_build (BSON_TYPE_INT32, "n", 2, + BSON_TYPE_BOOLEAN, "yes?", FALSE, + BSON_TYPE_STRING, "hello", "dolly", -1, + BSON_TYPE_NONE); + bson_finish (doc2); + + doc3 = bson_build (BSON_TYPE_INT32, "n", 3, + BSON_TYPE_STRING, "hello", "nurse", -1, + BSON_TYPE_BOOLEAN, "yes?", TRUE, + BSON_TYPE_NONE); + bson_finish (doc3); + + if (!mongo_sync_cmd_insert (conn, "tutorial.docs", doc2, doc3, NULL)) + { + perror ("mongo_sync_cmd_insert()"); + exit (1); + } + + bson_free (doc3); + bson_free (doc2); + bson_free (doc1); + + mongo_sync_disconnect (conn); +} + +void +tut_sync_query_simple (void) +{ + mongo_sync_connection *conn; + mongo_packet *p; + mongo_sync_cursor *cursor; + bson *query; + gint i = 0; + + conn = mongo_sync_connect ("localhost", 27017, FALSE); + if (!conn) + { + perror ("mongo_sync_connect()"); + exit (1); + } + + query = bson_new (); + bson_finish (query); + + p = mongo_sync_cmd_query (conn, "tutorial.docs", 0, + 0, 10, query, NULL); + if (!p) + { + perror ("mongo_sync_cmd_query()"); + exit (1); + } + bson_free (query); + + cursor = mongo_sync_cursor_new (conn, "tutorial.docs", p); + if (!cursor) + { + perror ("mongo_sync_cursor_new()"); + exit (1); + } + + while (mongo_sync_cursor_next (cursor)) + { + bson *result = mongo_sync_cursor_get_data (cursor); + bson_cursor *c; + + if (!result) + { + perror ("mongo_sync_cursor_get_data()"); + exit (1); + } + + printf ("Keys in document #%d:\n", i); + c = bson_cursor_new (result); + while (bson_cursor_next (c)) + printf ("\t%s\n", bson_cursor_key (c)); + + i++; + bson_cursor_free (c); + bson_free (result); + } + + mongo_sync_cursor_free (cursor); + mongo_sync_disconnect (conn); +} + +void +tut_sync_query_complex (void) +{ + mongo_sync_connection *conn; + mongo_packet *p; + mongo_sync_cursor *cursor; + bson *query, *select; + gint i = 0; + + conn = mongo_sync_connect ("localhost", 27017, FALSE); + if (!conn) + { + perror ("mongo_sync_connect()"); + exit (1); + } + + query = bson_build_full (BSON_TYPE_DOCUMENT, "$query", TRUE, + bson_build (BSON_TYPE_BOOLEAN, "yes?", FALSE, + BSON_TYPE_NONE), + BSON_TYPE_DOCUMENT, "$orderby", TRUE, + bson_build (BSON_TYPE_INT32, "n", 1, + BSON_TYPE_NONE), + BSON_TYPE_NONE); + bson_finish (query); + + select = bson_build (BSON_TYPE_INT32, "hello", 1, + BSON_TYPE_INT32, "n", 1, + BSON_TYPE_INT32, "yes?", 1, + BSON_TYPE_NONE); + bson_finish (select); + + p = mongo_sync_cmd_query (conn, "tutorial.docs", 0, + 0, 10, query, select); + if (!p) + { + perror ("mongo_sync_cmd_query()"); + exit (1); + } + bson_free (query); + bson_free (select); + + cursor = mongo_sync_cursor_new (conn, "tutorial.docs", p); + if (!cursor) + { + perror ("mongo_sync_cursor_new()"); + exit (1); + } + + while (mongo_sync_cursor_next (cursor)) + { + const char *hello; + gint32 n; + gboolean yes; + + bson *result; + bson_cursor *c; + + result = mongo_sync_cursor_get_data (cursor); + if (!result) + { + perror ("mongo_sync_cursor_get_data()"); + exit (1); + } + + c = bson_find (result, "hello"); + if (!bson_cursor_get_string (c, &hello)) + { + perror ("bson_cursor_get_string()"); + exit (1); + } + bson_cursor_free (c); + + c = bson_find (result, "n"); + if (!bson_cursor_get_int32 (c, &n)) + { + perror ("bson_cursor_get_int32()"); + exit (1); + } + bson_cursor_free (c); + + c = bson_find (result, "yes?"); + if (!bson_cursor_get_boolean (c, &yes)) + { + perror ("bson_cursor_get_boolean()"); + exit (1); + } + bson_cursor_free (c); + + printf ("Document #%d: hello=%s; n=%d; yes?=%s\n", + i, hello, n, (yes) ? "TRUE" : "FALSE"); + + bson_free (result); + i++; + } + + mongo_sync_cursor_free (cursor); + mongo_sync_disconnect (conn); +} + +int +main (int argc, char *argv[]) +{ + tut_sync_connect (); + tut_sync_connect_replica (); + tut_sync_insert (); + tut_sync_query_simple (); + tut_sync_query_complex (); + + return 0; +} diff --git a/docs/tutorial/examples/tut_mongo_sync_cmd_create.c b/docs/tutorial/examples/tut_mongo_sync_cmd_create.c new file mode 100644 index 0000000..9b31c91 --- /dev/null +++ b/docs/tutorial/examples/tut_mongo_sync_cmd_create.c @@ -0,0 +1,82 @@ +#include + +#include +#include + +static void +print_coll_info (bson *info) +{ + bson_cursor *c = NULL; + bson *options = NULL; + + const gchar *name; + gboolean capped = FALSE; + gint64 size = -1; + gint64 max = -1; + + c = bson_find (info, "name"); + bson_cursor_get_string (c, &name); + bson_cursor_find (c, "options"); + + bson_cursor_get_document (c, &options); + + printf ("Options for %s:\n", name); + + bson_cursor_free (c); + bson_free (info); + + c = bson_find (options, "capped"); + bson_cursor_get_boolean (c, &capped); + bson_cursor_free (c); + + c = bson_find (options, "size"); + bson_cursor_get_int64 (c, &size); + bson_cursor_free (c); + + c = bson_find (options, "max"); + bson_cursor_get_int64 (c, &max); + bson_cursor_free (c); + + bson_free (options); + + printf ("\tCapped: %s\n", (capped) ? "yes" : "no"); + if (size > 0) + printf ("\tSize : %lu\n", size); + if (max > 0) + printf ("\tMax : %lu\n", max); + printf ("\n"); +} + +int +main (void) +{ + mongo_sync_connection *conn; + + conn = mongo_sync_connect ("localhost", 27017, FALSE); + if (!conn) + { + fprintf (stderr, "Connection failed: %s\n", strerror (errno)); + return 1; + } + + mongo_sync_cmd_create (conn, "lmc", "cmd_create", MONGO_COLLECTION_DEFAULTS); + print_coll_info (mongo_sync_cmd_exists (conn, "lmc", "cmd_create")); + + mongo_sync_cmd_create (conn, "lmc", "cmd_create_capped", + MONGO_COLLECTION_CAPPED, 655360); + print_coll_info (mongo_sync_cmd_exists (conn, "lmc", "cmd_create_capped")); + + mongo_sync_cmd_create (conn, "lmc", "cmd_create_capped_max", + MONGO_COLLECTION_CAPPED | MONGO_COLLECTION_CAPPED_MAX, + 655360, 100); + print_coll_info (mongo_sync_cmd_exists (conn, "lmc", + "cmd_create_capped_max")); + + mongo_sync_cmd_create (conn, "lmc", "cmd_create_sized", + MONGO_COLLECTION_SIZED, 655360); + print_coll_info (mongo_sync_cmd_exists (conn, "lmc", "cmd_create_sized")); + + mongo_sync_disconnect (conn); + + return 0; +} diff --git a/docs/tutorial/examples/tut_mongo_sync_cmd_custom.c b/docs/tutorial/examples/tut_mongo_sync_cmd_custom.c new file mode 100644 index 0000000..4e48b18 --- /dev/null +++ b/docs/tutorial/examples/tut_mongo_sync_cmd_custom.c @@ -0,0 +1,81 @@ +#include + +#include +#include +#include +#include + +int +main (void) +{ + mongo_sync_connection *conn; + mongo_packet *p; + mongo_sync_cursor *cursor; + bson *eval; + + conn = mongo_sync_connect ("localhost", 27017, FALSE); + if (!conn) + { + perror ("mongo_sync_connect()"); + exit (1); + } + + eval = bson_build_full (BSON_TYPE_JS_CODE, "$eval", FALSE, + "function(x){return x + 4.2;}", -1, + BSON_TYPE_ARRAY, "args", TRUE, + bson_build (BSON_TYPE_INT32, "0", 1, + BSON_TYPE_NONE), + BSON_TYPE_NONE); + bson_finish (eval); + + p = mongo_sync_cmd_custom (conn, "test", eval); + + if (!p) + { + gchar *error = NULL; + + mongo_sync_cmd_get_last_error (conn, "test", &error); + fprintf (stderr, "Can't run db.eval: %s\n", error); + g_free (error); + + exit (1); + } + + cursor = mongo_sync_cursor_new (conn, "test", p); + + if (!cursor) + { + perror ("mongo_sync_cursor_new()"); + exit (1); + } + + while (mongo_sync_cursor_next (cursor)) + { + bson *result; + bson_cursor *c; + gdouble r; + + result = mongo_sync_cursor_get_data (cursor); + if (!result) + { + perror ("mongo_sync_cursor_get_data()"); + exit (1); + } + + c = bson_find (result, "retval"); + if (!bson_cursor_get_double (c, &r)) + { + perror ("bson_cursor_get_double()"); + exit (1); + } + bson_cursor_free (c); + bson_free (result); + + printf ("Result: %2.1f\n", r); + } + + mongo_sync_cursor_free (cursor); + mongo_sync_disconnect (conn); + + return 0; +} diff --git a/docs/tutorial/examples/tut_mongo_sync_cmd_index_create.c b/docs/tutorial/examples/tut_mongo_sync_cmd_index_create.c new file mode 100644 index 0000000..0e2f0b5 --- /dev/null +++ b/docs/tutorial/examples/tut_mongo_sync_cmd_index_create.c @@ -0,0 +1,54 @@ +#include + +#include +#include + +static void +create_and_verify_index(mongo_sync_connection *conn, + bson *index) +{ + if (!mongo_sync_cmd_index_create (conn, "lmc.indexed", index, + MONGO_INDEX_UNIQUE | MONGO_INDEX_DROP_DUPS | + MONGO_INDEX_SPARSE)) + { + gchar *error = NULL; + int e = errno; + + mongo_sync_cmd_get_last_error (conn, "lmc.indexed", &error); + fprintf (stderr, "Can't create indexes: %s\n", error ? error : strerror (e)); + g_free (error); + } + else + printf ("Index successfully created!\n"); +} + +int +main (void) +{ + mongo_sync_connection *conn; + bson *invalid_index, *index; + + invalid_index = bson_build (BSON_TYPE_STRING, "name", "", -1, + BSON_TYPE_NONE); + bson_finish (invalid_index); + + index = bson_build (BSON_TYPE_INT32, "name", 1, + BSON_TYPE_NONE); + bson_finish (index); + + conn = mongo_sync_connect ("localhost", 27017, FALSE); + if (!conn) + { + fprintf (stderr, "Connection failed: %s\n", strerror (errno)); + return 1; + } + + create_and_verify_index (conn, invalid_index); + create_and_verify_index (conn, index); + + bson_free (invalid_index); + bson_free (index); + mongo_sync_disconnect (conn); + + return 0; +} -- cgit v1.2.3