Guest User

Untitled

a guest
Oct 1st, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 0 0
  1. /* gcc example.c -o example $(pkg-config --cflags --libs libmongoc-1.0) */
  2.  
  3. /* ./example-client [CONNECTION_STRING [COLLECTION_NAME]] */
  4.  
  5. #include <mongoc.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. #include <unistd.h>
  10.  
  11. #define TEST_NUM 1000000
  12.  
  13. int
  14. main (int   argc,
  15.       char *argv[])
  16. {
  17.    int i = 0;
  18.    int fail = 0;
  19.    
  20.    mongoc_client_t *client;
  21.    mongoc_collection_t *collection;
  22.    mongoc_cursor_t *cursor;
  23.    bson_error_t error;
  24.    const bson_t *doc;
  25.    const char *uristr = "mongodb://127.0.0.1/";
  26.    const char *collection_name = "test";
  27.    bson_t query;
  28.    char *str;
  29.  
  30.    mongoc_init ();
  31.  
  32.    for (i = 0; i < TEST_NUM; i++){
  33.       printf("%d/%d/%d\n", i, fail, TEST_NUM);
  34.  
  35.       client = mongoc_client_new (uristr);
  36.  
  37.       if (!client) {
  38.          fprintf (stderr, "Failed to parse URI.\n");
  39.          return EXIT_FAILURE;
  40.       }
  41.  
  42.       bson_init (&query);
  43.  
  44.       collection = mongoc_client_get_collection (client, "test", collection_name);
  45.       cursor = mongoc_collection_find (collection,
  46.                                        MONGOC_QUERY_NONE,
  47.                                        0,
  48.                                        0,
  49.                                        0,
  50.                                        &query,
  51.                                        NULL,  /* Fields, NULL for all. */
  52.                                        NULL); /* Read Prefs, NULL for default */
  53.  
  54.       while (mongoc_cursor_next (cursor, &doc)) {
  55.          str = bson_as_json (doc, NULL);
  56.          fprintf (stdout, "%s\n", str);
  57.          bson_free (str);
  58.       }
  59.  
  60.       if (mongoc_cursor_error (cursor, &error)) {
  61.          fprintf (stderr, "Cursor Failure: %s\n", error.message);
  62.          fail++;
  63.          //return EXIT_FAILURE;
  64.       }
  65.  
  66.       bson_destroy (&query);
  67.       mongoc_cursor_destroy (cursor);
  68.       mongoc_collection_destroy (collection);
  69.       mongoc_client_destroy (client);
  70.  
  71.       usleep(1000);
  72.    }
  73.  
  74.    mongoc_cleanup ();
  75.    
  76.    return EXIT_SUCCESS;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment