Пример #1
0
int
main (int   argc,
      char *argv[])
{

    bson_t * query;


    /*
    time_t oid_thinks_time;  //what time does the OID think it is
    bson_oid_t oid;
    bson_oid_t *oid_pointer = &oid;
    bson_oid_init (&oid, NULL);  // get a standard ObjectId
    oid_thinks_time = bson_oid_get_time_t (&oid); //It was just made
    printf ("The OID was generated at %u\n", (unsigned) oid_thinks_time); //prove it



    time_t  ts = time(NULL);  //make a new time
    struct tm * timeinfo = localtime(&ts);
    timeinfo->tm_year = 2014-1900;  //-1900 because time.h
    timeinfo->tm_mon  = 12 - 1;     // time.h off by one (starts at 0)
    timeinfo->tm_mday = 25;
    ts = mktime(timeinfo);          // create the time
    u_int32_t ts_uint = (uint32_t)ts;
    ts_uint = BSON_UINT32_TO_BE (ts_uint); //BSON wants big endian time
    memcpy (&oid_pointer->bytes[0], &ts_uint, sizeof (ts_uint));  //overwrite the first 4 bytes with user selected time
    oid_thinks_time = bson_oid_get_time_t (&oid);
    printf ("The OID was fixed to time %u\n", (unsigned) oid_thinks_time);//prove it
*/

    query = BCON_NEW ("ping", BCON_INT32 (1));
    query = BCON_NEW("$and","[",
                     "{", "a", BCON_INT32(1), "}",
                     "{", "yyyyyyy", "{", "$ne", BCON_UTF8("xxxxxxxxxx"), "}", "}","]");

    bson_t *doc;

    query = BCON_NEW (
                    "$and", "[", "{", "_id", BCON_INT32(1), "}",
                                 "{", "yyyyyy", "{", "$ne", BCON_UTF8 ("xxxxxxx"), "}", "}","]"
                    );
    query = BCON_NEW("$and","[",
                     "{", "timestamp", "{", "$eq", BCON_INT64(1111111111112), "}", "}",
                     "{", "timestamp", "{", "$gte", BCON_INT64(1111111111111), "}", "}",
                     "{", "timestamp", "{", "$lt", BCON_INT64(1111111111110), "}", "}","]");
    //prove it looks right

    size_t s;
    char * as_json;
    as_json = bson_as_json(query, &s);
    printf("%s\n", as_json);
    return 0;
}
Пример #2
0
static void
bulk_collation (mongoc_collection_t *collection)
{
   mongoc_bulk_operation_t *bulk;
   bson_t *opts;
   bson_t *doc;
   bson_t *selector;
   bson_t *update;
   bson_error_t error;
   bson_t reply;
   char *str;
   uint32_t ret;

   /* insert {_id: "one"} and {_id: "One"} */
   bulk = mongoc_collection_create_bulk_operation_with_opts (
      collection, NULL);
   doc = BCON_NEW ("_id", BCON_UTF8 ("one"));
   mongoc_bulk_operation_insert (bulk, doc);
   bson_destroy (doc);

   doc = BCON_NEW ("_id", BCON_UTF8 ("One"));
   mongoc_bulk_operation_insert (bulk, doc);
   bson_destroy (doc);

   /* "One" normally sorts before "one"; make "one" come first */
   opts = BCON_NEW ("collation",
                    "{",
                    "locale",
                    BCON_UTF8 ("en_US"),
                    "caseFirst",
                    BCON_UTF8 ("lower"),
                    "}");

   /* set x=1 on the document with _id "One", which now sorts after "one" */
   update = BCON_NEW ("$set", "{", "x", BCON_INT64 (1), "}");
   selector = BCON_NEW ("_id", "{", "$gt", BCON_UTF8 ("one"), "}");
   mongoc_bulk_operation_update_one_with_opts (
      bulk, selector, update, opts, &error);

   ret = mongoc_bulk_operation_execute (bulk, &reply, &error);

   str = bson_as_canonical_extended_json (&reply, NULL);
   printf ("%s\n", str);
   bson_free (str);

   if (!ret) {
      printf ("Error: %s\n", error.message);
   }

   bson_destroy (&reply);
   bson_destroy (update);
   bson_destroy (selector);
   bson_destroy (opts);
   mongoc_bulk_operation_destroy (bulk);
}
Пример #3
0
static void
test_int64 (void)
{
   int64_t i64;

   bson_t *bcon = BCON_NEW ("foo", BCON_INT64 (10));

   assert (BCON_EXTRACT (bcon, "foo", BCONE_INT64 (i64)));

   assert (i64 == 10);

   bson_destroy (bcon);
}
Пример #4
0
static void
test_mongoc_matcher_eq_int64 (void)
{
   bson_t *spec;
   bson_t *doc;
   bson_error_t error;
   mongoc_matcher_t *matcher;
   bool r;

   spec = BCON_NEW ("hello", BCON_INT64 (1234));
   matcher = mongoc_matcher_new (spec, &error);
   BSON_ASSERT (matcher);
   r = mongoc_matcher_match (matcher, spec);
   BSON_ASSERT (r);
   bson_destroy (spec);
   mongoc_matcher_destroy (matcher);

   spec = BCON_NEW ("hello", BCON_INT64 (1234));
   doc = BCON_NEW ("hello", BCON_INT64 (1234));
   matcher = mongoc_matcher_new (spec, &error);
   BSON_ASSERT (matcher);
   r = mongoc_matcher_match (matcher, doc);
   BSON_ASSERT (r);
   bson_destroy (spec);
   bson_destroy (doc);
   mongoc_matcher_destroy (matcher);

   spec = BCON_NEW ("hello", BCON_INT64 (1234));
   doc = BCON_NEW ("hello", BCON_INT32 (4321));
   matcher = mongoc_matcher_new (spec, &error);
   BSON_ASSERT (matcher);
   r = mongoc_matcher_match (matcher, doc);
   BSON_ASSERT (!r);
   bson_destroy (spec);
   bson_destroy (doc);
   mongoc_matcher_destroy (matcher);
}
Пример #5
0
static void
test_int64 (void)
{
   bson_t bcon, expected;

   bson_init (&bcon);
   bson_init (&expected);

   bson_append_int64 (&expected, "foo", -1, 100);

   BCON_APPEND (&bcon, "foo", BCON_INT64 (100));

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Пример #6
0
void aggregate_match(aggregation_state_t *state, char* query) {
    //TODO: Run a find on the database/collection with that query.
    bson_t *doc1 = BCON_NEW("zip", BCON_UTF8("10001"), "state", BCON_UTF8("NY"), "population", BCON_INT64(5));
    bson_t *doc2 = BCON_NEW("zip", BCON_UTF8("10002"), "state", BCON_UTF8("NY"), "population", BCON_INT64(5));
    bson_t *doc3 = BCON_NEW("zip", BCON_UTF8("08520"), "state", BCON_UTF8("NJ"), "population", BCON_INT64(5));

    bson_t *result_docs = malloc(3 * sizeof(bson_t));
    result_docs[0] = *doc1;
    result_docs[1] = *doc2;
    result_docs[2] = *doc3;

    (*state).docs = result_docs;
    (*state).docs_len = 3;
}
Пример #7
0
int carve_chunk( mongoc_client_t *conn,   mongoc_collection_t *collection) {
	bson_error_t error;
	bson_t reply;
	unsigned long long seqno = -1;
	char *str;
	mongoc_collection_t *admin;

	bson_t *command;
	//Get a sequence value using find and modify on a record
	if (mongoc_collection_find_and_modify(collection,
	BCON_NEW("_id", BCON_UTF8("sequence")), NULL,
	BCON_NEW ("$inc", "{", "count", BCON_INT32 (1), "}"), NULL, false, true,
			true, &reply, &error)) {

		seqno = get_bson_int(&reply, "value.count");
		printf("Got Sequence number %llu\n", seqno);
	} else {
		fprintf(stderr, "Cannot get sequence %s\n", error.message);
	}


	//Shard our collection if it isnt already
	admin = mongoc_client_get_collection(conn, "admin", "test");

	//Work out if we arer talking to a mongod or a mongos
	int done = 0;
	while (!done) {
		done = 1;
		command =
				BCON_NEW("split", BCON_UTF8(NAMESPACE),"middle","{","_id",BCON_INT64(seqno<<32),"}");
		if (mongoc_collection_command_simple(admin, command, NULL, &reply,
				&error)) {
			fprintf(stderr, "Split chunk: OK\n");
		} else {
			//	fprintf(stderr, "Failed to split chunk (trying again): %s\n",
			//			error.message);
			if (strcmp(error.message, "split failed") == 0) {
				sleep(1);
				done = 0;
			}
		}
		bson_destroy(command);
		bson_destroy(&reply);
	}

	done = 0;
	while (!done) {
		done = 1;
		command =
				BCON_NEW("moveChunk", BCON_UTF8(NAMESPACE),"find","{","_id",BCON_INT64(seqno<<32),"}","to",BCON_UTF8(shardnames[seqno%nshards]));
		if (mongoc_collection_command_simple(admin, command, NULL, &reply,
				&error)) {
			fprintf(stderr, "Move chunk to %llu: OK\n", seqno % nshards);
		} else {
			//	fprintf(stderr, "Failed to move chunk to %llu(trying again): %s\n",
			//			seqno % nshards, error.message);
			if (strcmp(error.message, "move failed") == 0) {
				sleep(1);
				done = 0;
			}
		}
		bson_destroy(command);
		bson_destroy(&reply);
	}

	mongoc_collection_destroy(admin);
	return seqno;
}