コード例 #1
0
ファイル: dbd_mongo.c プロジェクト: SteppeChange/coturn
static int mongo_add_origin(u08bits *origin, u08bits *realm)
{
	mongoc_collection_t * collection = mongo_get_collection("realm");

	if(!collection)
		return -1;
    
	int ret = -1;

	u08bits realm0[STUN_MAX_REALM_SIZE+1] = "\0";
	if(!realm) realm=realm0;
  
	bson_t query, doc, child;
	bson_init(&query);
	BSON_APPEND_UTF8(&query, "realm", (const char *)realm);
	bson_init(&doc);
	bson_append_document_begin(&doc, "$addToSet", -1, &child);
	BSON_APPEND_UTF8(&child, "origin", (const char *)origin);
	bson_append_document_end(&doc, &child);

	if (!mongoc_collection_update(collection, MONGOC_UPDATE_UPSERT, &query, &doc, NULL, NULL)) {
		TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error inserting/updating realm origin information\n");
	} else {
		ret = 0;
	}
	mongoc_collection_destroy(collection);
	bson_destroy(&query);
	bson_destroy(&doc);
	return ret;
}
コード例 #2
0
ファイル: dbd_mongo.c プロジェクト: SteppeChange/coturn
static int mongo_del_origin(u08bits *origin)
{
  mongoc_collection_t * collection = mongo_get_collection("realm"); 

  if(!collection)
    return -1;
    
  int ret = -1;
  
  bson_t query, doc, child;
  bson_init(&query);
  bson_init(&doc);
  bson_append_document_begin(&doc, "$pull", -1, &child);
  BSON_APPEND_UTF8(&child, "origin", (const char *)origin);
  bson_append_document_end(&doc, &child);

  if (!mongoc_collection_update(collection, MONGOC_UPDATE_MULTI_UPDATE, &query, &doc, NULL, NULL)) {
    TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error deleting origin information\n");
  } else {
    ret = 0;
  }
  mongoc_collection_destroy(collection);
  bson_destroy(&query);
  bson_destroy(&doc);
  return ret;
}
コード例 #3
0
ファイル: dbd_mongo.c プロジェクト: SteppeChange/coturn
static int mongo_set_oauth_key(oauth_key_data_raw *key) {

  mongoc_collection_t * collection = mongo_get_collection("oauth_key");

  if(!collection)
    return -1;

  bson_t query;
  bson_init(&query);
  BSON_APPEND_UTF8(&query, "kid", (const char *)key->kid);

  bson_t doc;
  bson_init(&doc);
  BSON_APPEND_UTF8(&doc, "kid", (const char *)key->kid);
  BSON_APPEND_UTF8(&doc, "as_rs_alg", (const char *)key->as_rs_alg);
  BSON_APPEND_UTF8(&doc, "ikm_key", (const char *)key->ikm_key);
  BSON_APPEND_INT64(&doc, "timestamp", (int64_t)key->timestamp);
  BSON_APPEND_INT32(&doc, "lifetime", (int32_t)key->lifetime);

  int ret = -1;

  if (!mongoc_collection_update(collection, MONGOC_UPDATE_UPSERT, &query, &doc, NULL, NULL)) {
    TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error inserting/updating oauth key information\n");
  } else {
    ret = 0;
  }
  mongoc_collection_destroy(collection);
  bson_destroy(&doc);
  bson_destroy(&query);
  return ret;
}
コード例 #4
0
ファイル: dbd_mongo.c プロジェクト: SteppeChange/coturn
static int mongo_set_user_key(u08bits *usname, u08bits *realm, const char *key) {
  mongoc_collection_t * collection = mongo_get_collection("turnusers_lt"); 

	if(!collection)
    return -1;
    
  bson_t query;
  bson_init(&query);
  BSON_APPEND_UTF8(&query, "name", (const char *)usname);
  BSON_APPEND_UTF8(&query, "realm", (const char *)realm);
      
  bson_t doc;
  bson_init(&doc);
  BSON_APPEND_UTF8(&doc, "name", (const char *)usname);
  BSON_APPEND_UTF8(&doc, "realm", (const char *)realm);
  BSON_APPEND_UTF8(&doc, "hmackey", (const char *)key);

  int ret = -1;
  
  if (!mongoc_collection_update(collection, MONGOC_UPDATE_UPSERT, &query, &doc, NULL, NULL)) {
    TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error inserting/updating user key information\n");
  } else {
    ret = 0;
  }
  mongoc_collection_destroy(collection);
  bson_destroy(&doc);
  bson_destroy(&query);
  return ret;
}
コード例 #5
0
ファイル: dbd_mongo.c プロジェクト: SteppeChange/coturn
static int mongo_set_admin_user(const u08bits *usname, const u08bits *realm, const password_t pwd)
{
	mongoc_collection_t * collection = mongo_get_collection("admin_user");

	if(!collection)
    return -1;

	bson_t query;
	bson_init(&query);
	BSON_APPEND_UTF8(&query, "name", (const char *)usname);

	bson_t doc;
	bson_init(&doc);
	BSON_APPEND_UTF8(&doc, "name", (const char *)usname);
	BSON_APPEND_UTF8(&doc, "realm", (const char *)realm);
	BSON_APPEND_UTF8(&doc, "password", (const char *)pwd);

	int ret = -1;

	if (!mongoc_collection_update(collection, MONGOC_UPDATE_UPSERT, &query, &doc, NULL, NULL)) {
		TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error inserting/updating admin user information\n");
	} else {
		ret = 0;
	}
	mongoc_collection_destroy(collection);
	bson_destroy(&doc);
	bson_destroy(&query);
	return ret;
}
コード例 #6
0
bool TMongoDriver::updateMulti(const QString &collection, const QVariantMap &criteria, const QVariantMap &object)
{
    if (!isOpen()) {
        return false;
    }

    errorCode = 0;
    errorString.clear();
    bson_error_t error;

    mongoc_collection_t *col = mongoc_client_get_collection(mongoClient, qPrintable(dbName), qPrintable(collection));
    bool res = mongoc_collection_update(col, MONGOC_UPDATE_MULTI_UPDATE,
                                        (bson_t *)TBson::toBson(criteria).data(),
                                        (bson_t *)TBson::toBson(object).data(), nullptr, &error);

    setLastCommandStatus(mongoc_collection_get_last_error(col));
    mongoc_collection_destroy(col);

    if (!res) {
        tSystemError("MongoDB UpdateMulti Error: %s", error.message);
        errorCode = error.code;
        errorString = QLatin1String(error.message);
    }
    return res;
}
コード例 #7
0
ファイル: collection.c プロジェクト: vessi/mruby-mongo
mrb_value mrb_mongo_collection_update(mrb_state *mrb, mrb_value self) {
  mrb_mongo_collection_data *data = DATA_PTR(self);
  mrb_value selector_hash, update_hash;
  mrb_bool upsert;
  bson_t *selector, *update;
  bson_error_t error;
  mongoc_update_flags_t flags;

  flags = MONGOC_UPDATE_MULTI_UPDATE;

  mrb_get_args(mrb, "HH|b", &selector_hash, &update_hash, &upsert);
  if (!mrb_hash_p(selector_hash)) {
    selector_hash = mrb_hash_new(mrb);
  }
  if (upsert) flags = flags | MONGOC_UPDATE_UPSERT;

  selector = bson_new();
  update = bson_new();
  mrb_hash_to_bson(mrb, selector_hash, selector);
  mrb_hash_to_bson(mrb, update_hash, update);

  if (!mongoc_collection_update(data->collection, flags, selector, update, NULL, &error)) {
    bson_destroy(selector);
    bson_destroy(update);
    mrb_raise(mrb, E_RUNTIME_ERROR, error.message);
  }

  bson_destroy(selector);
  bson_destroy(update);

  return mrb_nil_value();
}
コード例 #8
0
void Collection::CollectionImpl::Update( tlib::bson::object query, tlib::bson::object update, mongoc_update_flags_t flags )
{
	bson_error_t error;

	bool res = mongoc_collection_update( m_collection.get(), flags, query.get(), update.get(), NULL, &error );
	if(!res)
		throw MongoError( std::string(error.message).append(" in Collection::Update") );
}
コード例 #9
0
static void
test_mongoc_client_authenticate_failure (void)
{
    mongoc_collection_t *collection;
    mongoc_cursor_t *cursor;
    mongoc_client_t *client;
    const bson_t *doc;
    bson_error_t error;
    bool r;
    bson_t q;
    bson_t empty = BSON_INITIALIZER;

    /*
     * Try authenticating with that user.
     */
    bson_init(&q);
    client = mongoc_client_new(gTestUriWithBadPassword);
    collection = mongoc_client_get_collection(client, "test", "test");
    cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 1, 0,
                                    &q, NULL, NULL);
    r = mongoc_cursor_next(cursor, &doc);
    assert(!r);
    r = mongoc_cursor_error(cursor, &error);
    assert(r);
    assert(error.domain == MONGOC_ERROR_CLIENT);
    assert(error.code == MONGOC_ERROR_CLIENT_AUTHENTICATE);
    mongoc_cursor_destroy(cursor);

    /*
     * Try various commands while in the failed state to ensure we get the
     * same sort of errors.
     */
    r = mongoc_collection_insert (collection, 0, &empty, NULL, &error);
    assert (!r);
    assert (error.domain == MONGOC_ERROR_CLIENT);
    assert (error.code == MONGOC_ERROR_CLIENT_AUTHENTICATE);

    /*
     * Try various commands while in the failed state to ensure we get the
     * same sort of errors.
     */
    r = mongoc_collection_update (collection, 0, &q, &empty, NULL, &error);
    assert (!r);
    assert (error.domain == MONGOC_ERROR_CLIENT);
    assert (error.code == MONGOC_ERROR_CLIENT_AUTHENTICATE);

    mongoc_collection_destroy(collection);
    mongoc_client_destroy(client);
}
コード例 #10
0
/*
 * Update a document 'b' into MongoDB.
 */
bool
MongoUpdate(MONGO_CONN* conn, char* database, char *collection, BSON* b, BSON* op)
{
	mongoc_collection_t *c = NULL;
	bson_error_t error;
	bool r = false;

	c = mongoc_client_get_collection (conn, database, collection);

	r = mongoc_collection_update(c, MONGOC_UPDATE_NONE, b, op, NULL, &error);
	mongoc_collection_destroy(c);
	if (!r)
		ereport(ERROR, (errmsg("failed to update row"),
						errhint("Mongo error: \"%s\"", error.message)));
	return true;
}
コード例 #11
0
ファイル: dbd_mongo.c プロジェクト: SteppeChange/coturn
static int mongo_set_permission_ip(const char *kind, u08bits *realm, const char* ip, int del)
{
	char sub_collection_name[129];
	snprintf(sub_collection_name,sizeof(sub_collection_name)-1,"%s_peer_ip",kind);

	mongoc_collection_t * collection = mongo_get_collection("realm");

	if(!collection)
		return -1;

	int ret = -1;

	u08bits realm0[STUN_MAX_REALM_SIZE+1] = "\0";
	if(!realm) realm=realm0;

	bson_t query, doc, child;
	bson_init(&query);
	BSON_APPEND_UTF8(&query, "realm", (const char *)realm);
	bson_init(&doc);
	if(del) {
		bson_append_document_begin(&doc, "$pull", -1, &child);
	} else {
		bson_append_document_begin(&doc, "$addToSet", -1, &child);
	}
	BSON_APPEND_UTF8(&child, sub_collection_name, (const char *)ip);
	bson_append_document_end(&doc, &child);

	mongoc_update_flags_t flags = MONGOC_UPDATE_NONE;

	if(del) {
		flags = MONGOC_UPDATE_MULTI_UPDATE;
	} else {
		flags = MONGOC_UPDATE_UPSERT;
	}

	if (!mongoc_collection_update(collection, flags, &query, &doc, NULL, NULL)) {
		TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error inserting permission ip information\n");
	} else {
		ret = 0;
	}
	mongoc_collection_destroy(collection);
	bson_destroy(&query);
	bson_destroy(&doc);
	return ret;
}
コード例 #12
0
ファイル: collection.c プロジェクト: FrissAnalytics/mongolite
SEXP R_mongo_collection_update(SEXP ptr_col, SEXP ptr_selector, SEXP ptr_update, SEXP upsert, SEXP multiple){
  mongoc_collection_t *col = r2col(ptr_col);
  bson_t *selector = r2bson(ptr_selector);
  bson_t *update = r2bson(ptr_update);

  //set update flags
  mongoc_update_flags_t flags = MONGOC_UPDATE_NONE;
  if(asLogical(upsert))
    flags = flags + MONGOC_UPDATE_UPSERT;
  if(asLogical(multiple))
    flags = flags + MONGOC_UPDATE_MULTI_UPDATE;

  bson_error_t err;
  if(!mongoc_collection_update(col, flags, selector, update, NULL, &err))
    stop(err.message);

  return ScalarLogical(1);
}
コード例 #13
0
/**
 * _mongoc_gridfs_file_flush_page:
 *
 *    Unconditionally flushes the file's current page to the database.
 *    The page to flush is determined by page->n.
 *
 * Side Effects:
 *
 *    On success, file->page is properly destroyed and set to NULL.
 *
 * Returns:
 *
 *    True on success; false otherwise.
 */
static bool
_mongoc_gridfs_file_flush_page (mongoc_gridfs_file_t *file)
{
   bson_t *selector, *update;
   bool r;
   const uint8_t *buf;
   uint32_t len;

   ENTRY;
   BSON_ASSERT (file);
   BSON_ASSERT (file->page);

   buf = _mongoc_gridfs_file_page_get_data (file->page);
   len = _mongoc_gridfs_file_page_get_len (file->page);

   selector = bson_new ();

   bson_append_value (selector, "files_id", -1, &file->files_id);
   bson_append_int32 (selector, "n", -1, file->n);

   update = bson_sized_new (file->chunk_size + 100);

   bson_append_value (update, "files_id", -1, &file->files_id);
   bson_append_int32 (update, "n", -1, file->n);
   bson_append_binary (update, "data", -1, BSON_SUBTYPE_BINARY, buf, len);

   r = mongoc_collection_update (file->gridfs->chunks,
                                 MONGOC_UPDATE_UPSERT,
                                 selector,
                                 update,
                                 NULL,
                                 &file->error);

   bson_destroy (selector);
   bson_destroy (update);

   if (r) {
      _mongoc_gridfs_file_page_destroy (file->page);
      file->page = NULL;
      r = mongoc_gridfs_file_save (file);
   }

   RETURN (r);
}
コード例 #14
0
ファイル: dbd_mongo.c プロジェクト: SteppeChange/coturn
static int mongo_set_realm_option_one(u08bits *realm, unsigned long value, const char* opt) {
  mongoc_collection_t * collection = mongo_get_collection("realm"); 

	if(!collection)
    return -1;
    
  bson_t query, doc, child;
  bson_init(&query);
  BSON_APPEND_UTF8(&query, "realm", (const char *)realm);
  bson_init(&doc);
  
  size_t klen = 9 + strlen(opt);
  char * _k = (char *)turn_malloc(klen);
  strcpy(_k, "options.");
  strcat(_k, opt);
  
  if (value > 0) {
    bson_append_document_begin(&doc, "$set", -1, &child);
    BSON_APPEND_INT32(&child, _k, (int32_t)value);
    bson_append_document_end(&doc, &child);
  } else {
    bson_append_document_begin(&doc, "$unset", -1, &child);
    BSON_APPEND_INT32(&child, _k, 1);
    bson_append_document_end(&doc, &child);
  }
  turn_free(_k,klen);
  
  int ret = -1;
  
  if (!mongoc_collection_update(collection, MONGOC_UPDATE_MULTI_UPDATE, &query, &doc, NULL, NULL)) {
    TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error deleting origin information\n");
  } else {
    ret = 0;
  }
  mongoc_collection_destroy(collection);
  bson_destroy(&query);
  bson_destroy(&doc);
  return ret;
}
コード例 #15
0
/*
 * Update some rows in the specified table
 * _h: structure representing database connection
 * _k: key names
 * _o: operators
 * _v: values of the keys that must match
 * _uk: updated columns
 * _uv: updated values of the columns
 * _n: number of key=value pairs
 * _un: number of columns to update
 */
int db_mongodb_update(const db1_con_t* _h, const db_key_t* _k,
		const db_op_t* _o, const db_val_t* _v, const db_key_t* _uk,
		const db_val_t* _uv, const int _n, const int _un)
{
	int i;
	km_mongodb_con_t *mgcon;
	mongoc_client_t *client;
	mongoc_collection_t *collection = NULL;
	bson_error_t error;
	bson_t *mdoc = NULL;
	bson_t *udoc = NULL, *sdoc = NULL;
	char *cname;
	char b1;

	mgcon = MONGODB_CON(_h);
	if(mgcon==NULL || mgcon->id== NULL || mgcon->con==NULL) {
		LM_ERR("connection to server is null\n");
		return -1;
	}
	client = mgcon->con;
	if(CON_TABLE(_h)->s==NULL) {
		LM_ERR("collection (table) name not set\n");
		return -1;
	}
	b1 = '\0';
	if(CON_TABLE(_h)->s[CON_TABLE(_h)->len]!='\0') {
		b1 = CON_TABLE(_h)->s[CON_TABLE(_h)->len];
		CON_TABLE(_h)->s[CON_TABLE(_h)->len] = '\0';
	}
	cname = CON_TABLE(_h)->s;
	collection = mongoc_client_get_collection(client, mgcon->id->database,
			cname);
	if(collection==NULL) {
		LM_ERR("cannot get collection (table): %s\n", cname);
		if(b1 != '\0') CON_TABLE(_h)->s[CON_TABLE(_h)->len] = b1;
		return -1;
	}
	if(b1 != '\0') CON_TABLE(_h)->s[CON_TABLE(_h)->len] = b1;

	udoc = bson_new();
	if(udoc==NULL) {
		LM_ERR("cannot initialize update bson document\n");
		goto error;
	}
	sdoc = bson_new();
	if(sdoc==NULL) {
		LM_ERR("cannot initialize update bson document\n");
		goto error;
	}
	mdoc = bson_new();
	if(mdoc==NULL) {
		LM_ERR("cannot initialize match bson document\n");
		goto error;
	}

	for(i = 0; i < _un; i++) {
		if(db_mongodb_bson_add(sdoc, _uk[i], _uv+i, i)<0)
			goto error;
	}
	if(!bson_append_document(udoc, "$set", 4, sdoc)) {
                LM_ERR("failed to append document to bson document\n");
                goto error;
        }

	if(_o==NULL) {
		for(i = 0; i < _n; i++) {
			if(db_mongodb_bson_add(mdoc, _k[i], _v+i, i)<0)
				goto error;
		}
	} else {
		for(i = 0; i < _n; i++) {
			if(!strcmp(_o[i], OP_EQ)) {
				if(db_mongodb_bson_add(mdoc, _k[i], _v+i, i)<0)
					goto error;
			} else {
				if(db_mongodb_bson_filter_add(mdoc, _k, _o, _v, i)<0)
					goto error;
			}
		}
	}

	if (!mongoc_collection_update (collection, MONGOC_UPDATE_NONE, mdoc,
				udoc, NULL, &error)) {
		LM_ERR("failed to update in collection: %s\n", error.message);
		goto error;
	}
	bson_destroy (mdoc);
	bson_destroy (udoc);
	bson_destroy (sdoc);
	mongoc_collection_destroy (collection);
	
	return 0;
error:
	if(mdoc) bson_destroy (mdoc);
	if(udoc) bson_destroy (udoc);
	if(sdoc) bson_destroy (sdoc);
	if(collection) mongoc_collection_destroy (collection);
	return -1;
}
コード例 #16
0
    /*
    public function update(array $criteria,
                             array $new_object,
                             array $options = array()): mixed;
     */
    static Variant HHVM_METHOD(MongoCollection, update, Array criteria, Array new_object, Array options) {
        mongoc_collection_t *collection;
        bson_t selector; //selector is the criteria (which document to update)
        bson_t update; //update is the new_object containing the new data 
        const bson_t * collection_error; //update is the new_object containing the new data 
        bson_error_t error;
        collection = get_collection(this_);

        encodeToBSON(criteria, &selector);
        encodeToBSON(new_object, &update);
        
        //先定义一些默认的参数
        mongoc_update_flags_t update_flag = MONGOC_UPDATE_NONE;
        //todo
        int w_flag = MONGOC_WRITE_CONCERN_W_DEFAULT;
        
        
        //如果传递了参数
        if(!options.empty()){
            //printf("multiple = %s\r\n",options[String("multiple")].toBoolean() ? "true":"false");
            if(options[String("multiple")].toBoolean()==true){
                update_flag = MONGOC_UPDATE_MULTI_UPDATE;
            }
            if(options[String("upsert")].toBoolean()==true){
                update_flag = MONGOC_UPDATE_UPSERT;
            }
        }
        mongoc_write_concern_t *write_concern;
        write_concern = mongoc_write_concern_new();
        mongoc_write_concern_set_w(write_concern, w_flag);
   
        bool ret = mongoc_collection_update(collection, update_flag, &selector, &update, write_concern, &error);
        bson_destroy(&update);
        bson_destroy(&selector);
        if (!ret) {
            mongoThrow<MongoCursorException>((const char *) error.message);
        }
        collection_error = mongoc_collection_get_last_error(collection);
        //char *str;
        //if (collection_error) {
            //for debug
            //str = bson_as_json(collection_error, NULL);
            //printf("debug = %s\r\n",str);
            //bson_free(str);
        //}
        Array collectionReturn = Array();
        Array ouput = Array();
        collectionReturn = cbson_loads(collection_error);
        mongoc_collection_destroy(collection);
        ouput.add(String("ok"),1);
        ouput.add(String("nModified"),collectionReturn[String("nModified")]);
        ouput.add(String("n"),collectionReturn[String("nMatched")]);
        ouput.add(String("updatedExisting"),collectionReturn[String("nMatched")].toInt64() > 0 ?true:false);//todo 
        
        ouput.add(String("err"),collectionReturn[String("writeErrors")]);
        ouput.add(String("errmsg"),collectionReturn[String("writeErrors")]);
        
        const StaticString s_MongoTimestamp("MongoTimestamp");
        Array mongotimestampParam = Array();
        ObjectData * data = create_object(&s_MongoTimestamp,mongotimestampParam);
        ouput.add(String("lastOp"), data);
        ouput.add(String("mongoRaw"),collectionReturn);
        
        
        return ouput;
        //return ret;

        
    }
コード例 #17
0
ファイル: mongoproxy.cpp プロジェクト: flycloud123/darkforce
bool dbproxy::update (std::string collection, mongoc_update_flags_t flags, const bson_t * selector, const bson_t * update, const mongoc_write_concern_t * write_concern, bson_error_t * error) {
    mongoc_collection_t * _c = get_collection(collection);
    return mongoc_collection_update(_c, flags, selector, update, write_concern, error);
}
コード例 #18
0
static void
test_update (void)
{
   mongoc_collection_t *collection;
   mongoc_client_t *client;
   bson_context_t *context;
   bson_error_t error;
   bool r;
   bson_oid_t oid;
   unsigned i;
   bson_t b;
   bson_t q;
   bson_t u;
   bson_t set;

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

   collection = mongoc_client_get_collection(client, "test", "test");
   ASSERT (collection);

   context = bson_context_new(BSON_CONTEXT_NONE);
   ASSERT (context);

   for (i = 0; i < 10; i++) {
      bson_init(&b);
      bson_oid_init(&oid, context);
      bson_append_oid(&b, "_id", 3, &oid);
      bson_append_utf8(&b, "utf8", 4, "utf8 string", 11);
      bson_append_int32(&b, "int32", 5, 1234);
      bson_append_int64(&b, "int64", 5, 12345678);
      bson_append_bool(&b, "bool", 4, 1);

      r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &b, NULL, &error);
      if (!r) {
         MONGOC_WARNING("%s\n", error.message);
      }
      ASSERT (r);

      bson_init(&q);
      bson_append_oid(&q, "_id", 3, &oid);

      bson_init(&u);
      bson_append_document_begin(&u, "$set", 4, &set);
      bson_append_utf8(&set, "utf8", 4, "updated", 7);
      bson_append_document_end(&u, &set);

      r = mongoc_collection_update(collection, MONGOC_UPDATE_NONE, &q, &u, NULL, &error);
      if (!r) {
         MONGOC_WARNING("%s\n", error.message);
      }
      ASSERT (r);

      bson_destroy(&b);
      bson_destroy(&q);
      bson_destroy(&u);
   }

   bson_init(&q);
   bson_init(&u);
   BSON_APPEND_INT32 (&u, "abcd", 1);
   BSON_APPEND_INT32 (&u, "$hi", 1);
   r = mongoc_collection_update(collection, MONGOC_UPDATE_NONE, &q, &u, NULL, &error);
   ASSERT (!r);
   ASSERT (error.domain == MONGOC_ERROR_BSON);
   ASSERT (error.code == MONGOC_ERROR_BSON_INVALID);
   bson_destroy(&q);
   bson_destroy(&u);

   bson_init(&q);
   bson_init(&u);
   BSON_APPEND_INT32 (&u, "a.b.c.d", 1);
   r = mongoc_collection_update(collection, MONGOC_UPDATE_NONE, &q, &u, NULL, &error);
   ASSERT (!r);
   ASSERT (error.domain == MONGOC_ERROR_BSON);
   ASSERT (error.code == MONGOC_ERROR_BSON_INVALID);
   bson_destroy(&q);
   bson_destroy(&u);

   mongoc_collection_destroy(collection);
   bson_context_destroy(context);
   mongoc_client_destroy(client);
}
コード例 #19
0
ファイル: fastload.c プロジェクト: johnlpage/BigAggBlog
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);

}
コード例 #20
0
ファイル: server.c プロジェクト: lngz/2929protocal
int check_state(unsigned char * terminal_id)
{

    bson_oid_t oid;
    bson_t *doc = NULL;
    bson_t *update = NULL;
    bson_t *query = NULL;
    int count;
    int found = 0;

 
    found = mongoc_collection_count (collection_state, MONGOC_QUERY_NONE, query, 0, 0, NULL, &error);

    if (found == 0) {
        doc = bson_new ();
        bson_oid_init (&oid, NULL);
        BSON_APPEND_INT32 (doc, "id", get_product_id(terminal_id));
        BSON_APPEND_INT32 (doc, "count", 0);

        if (!mongoc_collection_insert (collection_state, MONGOC_INSERT_NONE, doc, NULL, &error)) {
            printf ("%s\n", error.message);
        }

    }

    query = bson_new ();
    BSON_APPEND_INT32 (query, "id", get_product_id(terminal_id));

    cursor = mongoc_collection_find (collection_state, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);

    //while (mongoc_cursor_next (cursor, &doc)) {
    //   mongoc_cursor_next (cursor, &result);
    while (mongoc_cursor_next (cursor, (const  bson_t **) &doc)) {
        bson_iter_t iter;
        bson_iter_t sub_iter;
        // str = bson_as_json (doc, NULL);
        // fprintf (stderr, "%s\n", str);
        // bson_free (str);

        if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "count", &sub_iter)) {
            // fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter));
            // printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter));
            count = (int)bson_iter_int32 (&sub_iter);
        }
    }
    fprintf (stderr,"Found count %d .\n", count);

    count++;
    
    if (count > MAX_STANDBY) 
    {
        count = 1;
        update = bson_new ();
        BSON_APPEND_INT32 (update, "id",get_product_id(terminal_id));
        BSON_APPEND_INT32 (update, "count", count);


        if (!mongoc_collection_update (collection_state, MONGOC_UPDATE_NONE, query, update, NULL, &error)) {
            printf ("%s\n", error.message);
        }
    }
    else
    {
        update = bson_new ();
        BSON_APPEND_INT32 (update, "id",get_product_id(terminal_id));
        BSON_APPEND_INT32 (update, "count", count);

        if (!mongoc_collection_update (collection_state, MONGOC_UPDATE_NONE, query, update, NULL, &error)) {
            printf ("%s\n", error.message);
        }
    }

    return count;


}
コード例 #21
0
/** save a gridfs file */
bool
mongoc_gridfs_file_save (mongoc_gridfs_file_t *file)
{
   bson_t *selector, *update, child;
   const char *md5;
   const char *filename;
   const char *content_type;
   const bson_t *aliases;
   const bson_t *metadata;
   bool r;

   ENTRY;

   if (!file->is_dirty) {
      return 1;
   }

   if (file->page && _mongoc_gridfs_file_page_is_dirty (file->page)) {
      _mongoc_gridfs_file_flush_page (file);
   }

   md5 = mongoc_gridfs_file_get_md5 (file);
   filename = mongoc_gridfs_file_get_filename (file);
   content_type = mongoc_gridfs_file_get_content_type (file);
   aliases = mongoc_gridfs_file_get_aliases (file);
   metadata = mongoc_gridfs_file_get_metadata (file);

   selector = bson_new ();
   bson_append_value (selector, "_id", -1, &file->files_id);

   update = bson_new ();
   bson_append_document_begin (update, "$set", -1, &child);
   bson_append_int64 (&child, "length", -1, file->length);
   bson_append_int32 (&child, "chunkSize", -1, file->chunk_size);
   bson_append_date_time (&child, "uploadDate", -1, file->upload_date);

   if (md5) {
      bson_append_utf8 (&child, "md5", -1, md5, -1);
   }

   if (filename) {
      bson_append_utf8 (&child, "filename", -1, filename, -1);
   }

   if (content_type) {
      bson_append_utf8 (&child, "contentType", -1, content_type, -1);
   }

   if (aliases) {
      bson_append_array (&child, "aliases", -1, aliases);
   }

   if (metadata) {
      bson_append_document (&child, "metadata", -1, metadata);
   }

   bson_append_document_end (update, &child);

   r = mongoc_collection_update (file->gridfs->files, MONGOC_UPDATE_UPSERT,
                                 selector, update, NULL, &file->error);

   file->failed = !r;


   bson_destroy (selector);
   bson_destroy (update);

   file->is_dirty = 0;

   RETURN (r);
}
コード例 #22
0
ファイル: main.c プロジェクト: ablawat/technologie-nosql
int main()
{
    char uri_string[]      = "mongodb://localhost:27017/";
    char db_name[]         = "test";
    char collection_name[] = "test";
    
    mongoc_client_t     *client;
    mongoc_collection_t *collection;
    mongoc_cursor_t     *cursor;
    
    linked_list_tag_t **tags;
    
    const bson_t *document;
    bson_t *query;
    bson_t *update;
    
    json_object *json;
    enum json_type json_type;
    
    size_t len;
    
    char *str;
    char *tags_str;
    
    char **strings;
    char **pointer;
    
    //
    
    
    mongoc_init();
    
    client = mongoc_client_new(uri_string);
    collection = mongoc_client_get_collection(client, db_name, collection_name);
    
    query = bson_new();
    
    cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);
    
    while (mongoc_cursor_next(cursor, &document))
    {
        str  = bson_as_json(document, NULL);
        json = json_tokener_parse(str);
        
        json_object_object_foreach(json, key, val)
        {
            if (strcmp(key, "tags") == 0)
            {
                json_type = json_object_get_type(val);
                
                if (json_type == json_type_string)
                {
                    tags_str = strdup(json_object_get_string(val));
                    tags = tags_split(tags_str, " ");
                    free(tags_str);
                    
                    //linked_list_tag_print(tags);
                    
                    update = create_bson_update(tags);
                    
                    //printf("%s\n", bson_as_json(update, NULL));
                    
                    linked_list_tag_clear(tags);
                    free(tags);
                }
            }
            else if (strcmp(key, "id") == 0)
            {
                query = create_bson_selector(json_object_get_int(val));
                
                printf("%s\n", bson_as_json(query, NULL));
            }
        }
        
        mongoc_collection_update(collection, MONGOC_QUERY_NONE, query, update, NULL, NULL);
        
        bson_destroy(update);
        bson_destroy(query);
        bson_free(str);
        json_object_put(json);
    }
    
    mongoc_cursor_destroy(cursor);
    mongoc_collection_destroy(collection);
    mongoc_client_destroy(client);
    
    return 0;
}