int database_create_transaction_indexes(struct database* db) { bson_t* keys; mongoc_index_opt_t opt; bson_error_t error; mongoc_collection_t* collection = mongoc_client_get_collection(db->client, database_name(db), "transactions"); // transactions.hash { keys = bson_new(); BSON_APPEND_INT32(keys, "hash", 1); mongoc_index_opt_init(&opt); opt.unique = 1; if(mongoc_collection_create_index(collection, keys, &opt, &error) == 0) { printf("index creation error (transaction.hash)\n"); } bson_destroy(keys); } // (transaction.inputs.output_reference.hash, transaction.inputs.output_reference.index) [non-unique] { keys = bson_new(); BSON_APPEND_INT32(keys, "inputs.output_reference.hash", 1); BSON_APPEND_INT32(keys, "inputs.output_reference.index", 1); mongoc_index_opt_init(&opt); opt.unique = 0; if(mongoc_collection_create_index(collection, keys, &opt, &error) == 0) { printf("index creation error (inputs.output_reference)\n"); } bson_destroy(keys); } mongoc_collection_destroy(collection); return 0; }
static void test_index (void) { mongoc_collection_t *collection; mongoc_database_t *database; mongoc_client_t *client; mongoc_index_opt_t opt; bson_error_t error; bool r; bson_t keys; mongoc_index_opt_init(&opt); client = mongoc_client_new(gTestUri); ASSERT (client); database = get_test_database (client); ASSERT (database); collection = get_test_collection (client, "test_index"); ASSERT (collection); bson_init(&keys); bson_append_int32(&keys, "hello", -1, 1); r = mongoc_collection_create_index(collection, &keys, &opt, &error); ASSERT (r); r = mongoc_collection_create_index(collection, &keys, &opt, &error); ASSERT (r); r = mongoc_collection_drop_index(collection, "hello_1", &error); ASSERT (r); bson_destroy(&keys); r = mongoc_collection_drop (collection, &error); ASSERT (r); mongoc_collection_destroy(collection); mongoc_database_destroy(database); mongoc_client_destroy(client); }
SEXP R_mongo_collection_create_index(SEXP ptr_col, SEXP ptr_bson) { mongoc_collection_t *col = r2col(ptr_col); bson_t *b = r2bson(ptr_bson); const mongoc_index_opt_t *options = mongoc_index_opt_get_default(); bson_error_t err; if(!mongoc_collection_create_index(col, b, options, &err)) stop(err.message); return ScalarLogical(1); }
void Collection::CollectionImpl::CreateIndex( tlib::bson::object key, bool unique ) { bson_error_t error; mongoc_index_opt_t opt; mongoc_index_opt_init(&opt); opt.unique = unique; bool res = mongoc_collection_create_index( m_collection.get(), key.get(), &opt, &error ); if(!res) throw MongoError( std::string(error.message).append(" in Collection::CreateIndex") ); }
/** * _mongoc_gridfs_ensure_index: * * ensure gridfs indexes * * Ensure fast searches for chunks via [ files_id, n ] * Ensure fast searches for files via [ filename ] */ static bool _mongoc_gridfs_ensure_index (mongoc_gridfs_t *gridfs, bson_error_t *error) { bson_t keys; mongoc_index_opt_t opt; bool r; ENTRY; bson_init (&keys); bson_append_int32 (&keys, "files_id", -1, 1); bson_append_int32 (&keys, "n", -1, 1); mongoc_index_opt_init (&opt); opt.unique = 1; r = mongoc_collection_create_index (gridfs->chunks, &keys, &opt, error); bson_destroy (&keys); if (!r) { RETURN (r); } bson_init (&keys); bson_append_int32 (&keys, "filename", -1, 1); bson_append_int32 (&keys, "uploadDate", -1, 1); opt.unique = 0; r = mongoc_collection_create_index (gridfs->files, &keys, &opt, error); bson_destroy (&keys); if (!r) { RETURN (r); } RETURN (1); }
static int init() { str_t tmp = empty_str; str_t host = conf_lookup(&robot.conf, str_from("DB_HOST")).string; str_t name = conf_lookup(&robot.conf, str_from("DB_NAME")).string; size_t i; int rc = 1; str_cat(&tmp, "mongodb://"); str_ncat(&tmp, host.ptr, host.len); robot.mongoc_client = mongoc_client_new(tmp.ptr); if (robot.mongoc_client == NULL) { rc = 0; fprintf(stderr, "mongoc_client_new(\"%s\") error!!!!\n", tmp.ptr); goto end; } robot.mongoc_database = mongoc_client_get_database(robot.mongoc_client, name.ptr); if (robot.mongoc_database == NULL) { rc = 0; fprintf(stderr, "mongoc_client_get_database(\"%s\") error!!!!\n", name.ptr); goto end; } { mongoc_collection_t* message_collection; mongoc_index_opt_t opt; bson_error_t error; bson_t keys; message_collection = mongoc_database_get_collection(robot.mongoc_database, "message"); if (message_collection == NULL) { rc = 0; fprintf(stderr, "mongoc_database_get_collection(\"message\") error!!!!\n"); goto end; } mongoc_index_opt_init(&opt); // from+type 做联合索引 bson_init(&keys); BSON_APPEND_INT32(&keys, "from", 1); BSON_APPEND_INT32(&keys, "type", 1); if (!mongoc_collection_create_index(message_collection, &keys, &opt, &error)) MONGOC_WARNING("%s\n", error.message); bson_destroy(&keys); // time 做逆序索引 bson_init(&keys); BSON_APPEND_INT32(&keys, "time", -1); if (!mongoc_collection_create_index(message_collection, &keys, &opt, &error)) MONGOC_WARNING("%s\n", error.message); bson_destroy(&keys); // content 做全文索引 bson_init(&keys); BSON_APPEND_UTF8(&keys, "content", "text"); if (!mongoc_collection_create_index(message_collection, &keys, &opt, &error)) MONGOC_WARNING("%s\n", error.message); bson_destroy(&keys); } { mongoc_collection_t* unprocessed_collection; mongoc_index_opt_t opt; bson_error_t error; bson_t keys; unprocessed_collection = mongoc_database_get_collection(robot.mongoc_database, "unprocessed"); if (unprocessed_collection == NULL) { rc = 0; fprintf(stderr, "mongoc_database_get_collection(\"unprocessed\") error!!!!\n"); goto end; } mongoc_index_opt_init(&opt); // time 做逆序索引 bson_init(&keys); BSON_APPEND_INT32(&keys, "time", -1); if (!mongoc_collection_create_index(unprocessed_collection, &keys, &opt, &error)) MONGOC_WARNING("%s\n", error.message); bson_destroy(&keys); } for (i = 0; modules[i]; ++i) { if (modules[i]->module_init) { rc = modules[i]->module_init(); if (!rc) goto end; } } end: str_free(tmp); return rc; }
bool dbproxy::create_index(std::string collection_name, const bson_t * keys, const mongoc_index_opt_t *opt, bson_error_t * error) { mongoc_collection_t * _c = get_collection(collection_name); return mongoc_collection_create_index(_c, keys, opt, error); }