Exemplo n.º 1
0
static void *
background_mongoc_collection_find_and_modify (void *data)
{
   future_t *future = (future_t *) data;
   future_value_t return_value;

   return_value.type = future_value_bool_type;

   future_value_set_bool (
      &return_value,
      mongoc_collection_find_and_modify (
         future_value_get_mongoc_collection_ptr (future_get_param (future, 0)),
         future_value_get_const_bson_ptr (future_get_param (future, 1)),
         future_value_get_const_bson_ptr (future_get_param (future, 2)),
         future_value_get_const_bson_ptr (future_get_param (future, 3)),
         future_value_get_const_bson_ptr (future_get_param (future, 4)),
         future_value_get_bool (future_get_param (future, 5)),
         future_value_get_bool (future_get_param (future, 6)),
         future_value_get_bool (future_get_param (future, 7)),
         future_value_get_bson_ptr (future_get_param (future, 8)),
         future_value_get_bson_error_ptr (future_get_param (future, 9))
      ));

   future_resolve (future, return_value);

   return NULL;
}
Exemplo n.º 2
0
int
main (int   argc,
      char *argv[])
{
   mongoc_collection_t *collection;
   mongoc_client_t *client;
   bson_error_t error;
   bson_t *query;
   bson_t *update;
   bson_t reply;
   char *str;

   mongoc_init ();

   client = mongoc_client_new ("mongodb://127.0.0.1:27017/");
   collection = mongoc_client_get_collection (client, "test", "test");

   /*
    * Build our query, {"cmpxchg": 1}
    */
   query = BCON_NEW ("cmpxchg", BCON_INT32 (1));

   /*
    * Build our update. {"$set": {"cmpxchg": 2}}
    */
   update = BCON_NEW ("$set", "{", "cmpxchg", BCON_INT32 (2), "}");

   /*
    * Submit the findAndModify.
    */
   if (!mongoc_collection_find_and_modify (collection, query, NULL, update, NULL, false, false, true, &reply, &error)) {
      fprintf (stderr, "find_and_modify() failure: %s\n", error.message);
      return 1;
   }

   /*
    * Print the result as JSON.
    */
   str = bson_as_json (&reply, NULL);
   printf ("%s\n", str);
   bson_free (str);

   /*
    * Cleanup.
    */
   bson_destroy (query);
   bson_destroy (update);
   bson_destroy (&reply);
   mongoc_collection_destroy (collection);
   mongoc_client_destroy (client);

   mongoc_cleanup ();

   return 0;
}
Exemplo n.º 3
0
void updateAlarm(mongoc_collection_t *coll,char* se_id,int interval,int ialarm){
    time_t timep;
    bson_t *query;
    bson_oid_t se_oid;
    bson_oid_init_from_string(&se_oid, se_id);
    time(&timep);
    bson_error_t error;
    bson_t reply;
    query = bson_new ();
    BSON_APPEND_OID (query, "_id",&se_oid);
    bson_t *update = BCON_NEW ("$set", "{", "alarmtime",BCON_DOUBLE(timep),"interval",BCON_INT32(interval),"balarm",BCON_INT32(ialarm),"}");

    if (!mongoc_collection_find_and_modify (coll, query, NULL, update, NULL, false, false, true, &reply, &error)) {
      //fprintf (stderr, "find_and_modify() failure: %s\n", error.message);
    }
    bson_destroy (query);
    bson_destroy (update);
}
static void
test_find_and_modify (void)
{
   mongoc_collection_t *collection;
   mongoc_client_t *client;
   bson_error_t error;
   bson_iter_t iter;
   bson_iter_t citer;
   bson_t *update;
   bson_t doc = BSON_INITIALIZER;
   bson_t reply;
   bool r;

   client = mongoc_client_new (gTestUri);
   ASSERT (client);

   collection = get_test_collection (client, "test_find_and_modify");
   ASSERT (collection);

   BSON_APPEND_INT32 (&doc, "superduper", 77889);

   r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &doc, NULL, &error);
   assert (r);

   update = BCON_NEW ("$set", "{",
                         "superduper", BCON_INT32 (1234),
                      "}");

   r = mongoc_collection_find_and_modify (collection,
                                          &doc,
                                          NULL,
                                          update,
                                          NULL,
                                          false,
                                          false,
                                          true,
                                          &reply,
                                          &error);
   assert (r);

   assert (bson_iter_init_find (&iter, &reply, "value"));
   assert (BSON_ITER_HOLDS_DOCUMENT (&iter));
   assert (bson_iter_recurse (&iter, &citer));
   assert (bson_iter_find (&citer, "superduper"));
   assert (BSON_ITER_HOLDS_INT32 (&citer));
   assert (bson_iter_int32 (&citer) == 1234);

   assert (bson_iter_init_find (&iter, &reply, "lastErrorObject"));
   assert (BSON_ITER_HOLDS_DOCUMENT (&iter));
   assert (bson_iter_recurse (&iter, &citer));
   assert (bson_iter_find (&citer, "updatedExisting"));
   assert (BSON_ITER_HOLDS_BOOL (&citer));
   assert (bson_iter_bool (&citer));

   bson_destroy (&reply);
   bson_destroy (update);

   r = mongoc_collection_drop (collection, &error);
   assert (r);

   mongoc_collection_destroy (collection);
   mongoc_client_destroy (client);
   bson_destroy (&doc);
}
Exemplo n.º 5
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;
}
Exemplo n.º 6
0
/**
 * Main module procedure.
 * @param[in] instance Module instance.
 * @param[in] request Radius request.
 * @return
 */
static rlm_rcode_t mod_proc(void *instance, REQUEST *request) {
  rlm_mongodb_t *inst = instance;
  rlm_mongodb_conn_t *conn = NULL;
  rlm_rcode_t code = RLM_MODULE_FAIL;
  bson_error_t error;

  conn = fr_connection_get(inst->pool);
  if (!conn) {
    goto end;
  }

  if (inst->action == RLM_MONGODB_GET) {
    // TODO: implement me!
    code = RLM_MODULE_FAIL;
  } else {
    mongoc_collection_t *mongo_collection = NULL;
    char *db = NULL, *collection = NULL, *query = NULL, *sort = NULL, *update = NULL;
    bson_t *bson_query = NULL, *bson_sort = NULL, *bson_update = NULL;

    if (tmpl_aexpand(request, &db, request, inst->cfg.db, NULL, NULL) < 0) {
      ERROR("failed to substitute attributes for db '%s'", inst->cfg.db->name);
      goto end_set;
    }

    if (tmpl_aexpand(request, &collection, request, inst->cfg.collection, NULL, NULL) < 0) {
      ERROR("failed to substitute attributes for collection '%s'", inst->cfg.collection->name);
      goto end_set;
    }

    ssize_t query_len = tmpl_aexpand(request, &query, request, inst->cfg.search_query, NULL, NULL);
    if (query_len < 0) {
      ERROR("failed to substitute attributes for search query '%s'", inst->cfg.search_query->name);
      goto end_set;
    }
    bson_query = bson_new_from_json((uint8_t *) query, query_len, &error);
    if (!bson_query) {
      RERROR("JSON->BSON conversion failed for search query '%s': %d.%d %s",
             query, error.domain, error.code, error.message);
      goto end_set;
    }

    ssize_t sort_len = tmpl_aexpand(request, &sort, request, inst->cfg.sort_query, NULL, NULL);
    if (query_len < 0) {
      ERROR("failed to substitute attributes for sort query '%s'", inst->cfg.sort_query->name);
      goto end_set;
    }
    if (sort_len) {
      bson_sort = bson_new_from_json((uint8_t *) sort, sort_len, &error);
      if (!bson_sort) {
        RERROR("JSON->BSON conversion failed for sort query '%s': %d.%d %s",
               sort, error.domain, error.code, error.message);
        goto end_set;
      }
    }

    ssize_t update_len = tmpl_aexpand(request, &update, request, inst->cfg.update_query, NULL, NULL);
    if (query_len < 0) {
      ERROR("failed to substitute attributes for update query '%s'", inst->cfg.update_query->name);
      goto end_set;
    }
    if (update_len) {
      bson_update = bson_new_from_json((uint8_t *) update, update_len, &error);
      if (!bson_update) {
        RERROR("JSON->BSON conversion failed for update query '%s': %d.%d %s",
               update, error.domain, error.code, error.message);
        goto end_set;
      }
    }

    mongo_collection = mongoc_client_get_collection(conn->client, db, collection);
    if (!mongo_collection) {
      RERROR("failed to get collection %s/%s", db, collection);
      goto end_set;
    }

    bool ok = mongoc_collection_find_and_modify(mongo_collection, bson_query, bson_sort,
                                                bson_update, NULL, inst->cfg.remove,
                                                inst->cfg.upsert, false, NULL, &error);

    code = ok ? RLM_MODULE_OK : RLM_MODULE_FAIL;

    end_set:
    if (mongo_collection) mongoc_collection_destroy(mongo_collection);
    if (bson_query) bson_destroy(bson_query);
    if (bson_sort) bson_destroy(bson_sort);
    if (bson_update) bson_destroy(bson_update);
  }

  end:
  if (conn) fr_connection_release(inst->pool, conn);
  return code;
}
Exemplo n.º 7
0
bool dbproxy::find_and_modify(std::string collection, const bson_t * query, const bson_t * sort, const bson_t * update, const bson_t * fields, bool _remove, bool upsert, bool _new, bson_t * reply, bson_error_t * error) {
    mongoc_collection_t * _c = get_collection(collection);
    return mongoc_collection_find_and_modify(_c, query, sort, update, fields, _remove, upsert, _new, reply, error);
}