Esempio n. 1
0
static void
test_bool (void)
{
   bson_bool_t b;

   bson_t *bcon = BCON_NEW ("foo", BCON_BOOL (TRUE));

   assert (BCON_EXTRACT (bcon, "foo", BCONE_BOOL (b)));

   assert (b == TRUE);

   bson_destroy (bcon);
}
Esempio n. 2
0
static void
test_bool (void)
{
   bool b;

   bson_t *bcon = BCON_NEW ("foo", BCON_BOOL (true));

   BSON_ASSERT (BCON_EXTRACT (bcon, "foo", BCONE_BOOL (b)));

   BSON_ASSERT (b == true);

   bson_destroy (bcon);
}
Esempio n. 3
0
static void
test_value_basic (void)
{
   static const uint8_t raw[16] = { 0 };
   const bson_value_t *value;
   bson_value_t copy;
   bson_iter_t iter;
   bson_oid_t oid;
   bson_t other = BSON_INITIALIZER;
   bson_t *doc;
   bson_t sub = BSON_INITIALIZER;
   bool r;
   int i;

   bson_oid_init (&oid, NULL);

   doc = BCON_NEW ("double", BCON_DOUBLE (123.4),
                   "utf8", "this is my string",
                   "document", BCON_DOCUMENT (&sub),
                   "array", BCON_DOCUMENT (&sub),
                   "binary", BCON_BIN (BSON_SUBTYPE_BINARY, raw, sizeof raw),
                   "undefined", BCON_UNDEFINED,
                   "oid", BCON_OID (&oid),
                   "bool", BCON_BOOL (true),
                   "datetime", BCON_DATE_TIME (12345678),
                   "null", BCON_NULL,
                   "regex", BCON_REGEX ("^hello", "i"),
                   "dbpointer", BCON_DBPOINTER ("test.test", &oid),
                   "code", BCON_CODE ("var a = function() {}"),
                   "symbol", BCON_SYMBOL ("my_symbol"),
                   "codewscope", BCON_CODEWSCOPE ("var a = 1;", &sub),
                   "int32", BCON_INT32 (1234),
                   "timestamp", BCON_TIMESTAMP (1234, 4567),
                   "int64", BCON_INT32 (4321),
                   "maxkey", BCON_MAXKEY,
                   "minkey", BCON_MINKEY);

   r = bson_iter_init (&iter, doc);
   assert (r);

   for (i = 0; i < 20; i++) {
      r = bson_iter_next (&iter);
      assert (r);

      value = bson_iter_value (&iter);
      assert (value);

      bson_value_copy (value, &copy);

      r = bson_append_value (&other, bson_iter_key (&iter), -1, &copy);
      assert (r);

      bson_value_destroy (&copy);
   }

   r = bson_iter_next (&iter);
   assert (!r);

   bson_destroy (doc);
   bson_destroy (&other);
}
Esempio n. 4
0
void configure_sharding() {
	mongoc_client_t *conn;
	mongoc_collection_t *collection;
	mongoc_collection_t *config;
	bson_error_t error;
	bson_t *command;
	bson_t reply;
	char *str;
	const char *process;
	mongoc_cursor_t *cursor;
	const bson_t *doc;
    bson_t *update = NULL;
    bson_t *query = NULL;

	conn = mongoc_client_new(DEFAULT_URI);
	collection = mongoc_client_get_collection(conn, "admin", "test");

	//Work out if we arer talking to a mongod or a mongos
	command = BCON_NEW("serverStatus", BCON_INT32(1));
	if (mongoc_collection_command_simple(collection, command, NULL, &reply,
			&error)) {
		process = get_bson_string(&reply, "process");
		printf("Loader is talking to %s\n", process);
	} else {
		fprintf(stderr, "Failed to run command: %s\n", error.message);
		exit(1);
	}
	bson_destroy(command);
	bson_destroy(&reply);
	mongoc_collection_destroy(collection);
	//Could be mongos or mongos.exe
	if (strncmp(process, "mongos", 6) != 0) {
		mongoc_client_destroy(conn);
		return;
	}

	collection = mongoc_client_get_collection(conn, "config", "shards");

	query = bson_new();
	cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 0, 0,
			query, NULL, NULL);

	int shardcount = 0;
	while (mongoc_cursor_next(cursor, &doc)) {
		shardnames[shardcount] = strdup(get_bson_string(doc, "_id"));
		shardcount++;
	}
	nshards = shardcount;
	bson_destroy(query);
	mongoc_cursor_destroy(cursor);

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

	//Work out if we arer talking to a mongod or a mongos
	command = BCON_NEW("collMod", BCON_UTF8(NAMESPACE),"noPadding",BCON_BOOL(getenv("PACKTIGHT")?true:false));
	if (mongoc_collection_command_simple(collection, command, NULL, &reply,
			&error)) {

	} else {
		fprintf(stderr, "Failed to disable padding on db (is this WiredTiger?): %s\n", error.message);

	}
	bson_destroy(command);
	bson_destroy(&reply);


	//Work out if we arer talking to a mongod or a mongos
	command = BCON_NEW("enableSharding", BCON_UTF8(DATA_DB));
	if (mongoc_collection_command_simple(collection, command, NULL, &reply,
			&error)) {

	} else {
		fprintf(stderr, "Failed to enable sharding on db: %s\n", error.message);

	}
	bson_destroy(command);
	bson_destroy(&reply);

	//Work out if we arer talking to a mongod or a mongos
	command =
			BCON_NEW("shardCollection", BCON_UTF8(NAMESPACE),"key","{","_id",BCON_INT32(1),"}");
	if (mongoc_collection_command_simple(collection, command, NULL, &reply,
			&error)) {

	} else {
		fprintf(stderr, "Failed to shard colleciton: %s\n", error.message);

	}
	bson_destroy(command);
	bson_destroy(&reply);



	mongoc_collection_destroy(collection);

	collection = mongoc_client_get_collection(conn, "config", "collections");

    query = BCON_NEW ("_id", BCON_UTF8 (NAMESPACE));
    update = BCON_NEW ("$set", "{","noBalance", BCON_BOOL(false),"}");

    if (!mongoc_collection_update (collection, MONGOC_UPDATE_NONE, query, update, NULL, &error)) {
        printf ("Failed to turn off autobalancing for collection%s\n", error.message);
    }

	bson_destroy(query);
	bson_destroy(update);

	mongoc_client_destroy(conn);

}