Beispiel #1
0
SEXP mongo_index_create(SEXP mongo_conn, SEXP ns, SEXP key, SEXP options) {
    mongo* conn = _checkMongo(mongo_conn);
    const char* _ns = CHAR(STRING_ELT(ns, 0));
    int _options = 0;
    int i;
    int len = LENGTH(options);
    for (i = 0; i < len; i++)
        _options |= INTEGER(options)[i];
    bson* _key;
    bson b;
    int keyIsBSON = _isBSON(key);
    if (keyIsBSON)
        _key = _checkBSON(key);
    else {
        _key = &b;
        len = LENGTH(key);
        bson_init(&b);
        for (i = 0; i < len; i++)
            bson_append_int(&b, CHAR(STRING_ELT(key, i)), 1);
        bson_finish(&b);
    }
    bson out;
    int success = mongo_create_index(conn, _ns, _key, _options, &out);
    if (!keyIsBSON)
        bson_destroy(&b);
    if (success == MONGO_OK) {
        bson_destroy(&out);
        return R_NilValue;
    }
    SEXP ret = _mongo_bson_create(&out);
    bson_destroy(&out);
    UNPROTECT(3);
    return ret;
}
void test_index_helper( mongo *conn ) {
    int ret;

    bson b, out;
    bson_iterator it;

    bson_init( &b );
    bson_append_int( &b, "foo", -1 );
    bson_finish( &b );

    mongo_create_index( conn, "test.bar", &b, NULL, MONGO_INDEX_SPARSE | MONGO_INDEX_UNIQUE, &out );

    bson_destroy( &b );
    bson_destroy( &out );

    bson_init( &b );
    bson_append_start_object( &b, "key" );
    bson_append_int( &b, "foo", -1 );
    bson_append_finish_object( &b );

    bson_finish( &b );

    ret = mongo_find_one( conn, "test.system.indexes", &b, NULL, &out );
    ASSERT( ret == MONGO_OK );

    bson_print( &out );

    bson_iterator_init( &it, &out );

    ASSERT( bson_find( &it, &out, "unique" ) );
    ASSERT( bson_find( &it, &out, "sparse" ) );

    bson_destroy( &b );
    bson_destroy( &out );
}
Beispiel #3
0
/* Create index on smsc and ts fields, as these are used for retrieving the DLR */
static void dlr_mongodb_ensure_index(void)
{
    DBPoolConn *pconn;
    mongo_connection *conn = NULL;
    bson_buffer bb;
    bson key;

    pconn = dbpool_conn_consume(pool);
    if (pconn == NULL) {
        return;
    }
    conn = (mongo_connection*)pconn->conn;

    bson_buffer_init(&bb);
    bson_append_int(&bb, octstr_get_cstr(fields->field_smsc), 1);
    bson_append_int(&bb, octstr_get_cstr(fields->field_ts), 1);
    bson_from_buffer(&key, &bb);

    MONGO_TRY {
        mongo_create_index(conn, mongodb_namespace, &key, 0, NULL);
    } MONGO_CATCH {
        mongodb_error("dlr_mongodb_ensure_index", conn->exception.type);
    }

    dbpool_conn_produce(pconn);
    bson_destroy(&key);
}
Beispiel #4
0
bson_bool_t mongo_create_simple_index( mongo *conn, const char *ns, const char *field, int options, bson *out ) {
    bson b;
    bson_bool_t success;

    bson_init( &b );
    bson_append_int( &b, field, 1 );
    bson_finish( &b );

    success = mongo_create_index( conn, ns, &b, options, out );
    bson_destroy( &b );
    return success;
}
void test_index_helper_invalid( mongo *conn ) {
    bson b, out;

    bson_init( &b );
    bson_append_int( &b, "foo", -1 );
    bson_finish( &b );

    ASSERT( MONGO_ERROR == mongo_create_index( conn, "testbar", &b, NULL, MONGO_INDEX_SPARSE | MONGO_INDEX_UNIQUE, &out ));

    bson_destroy( &b );
    bson_destroy( &out );
}
Beispiel #6
0
static void tutorial_index( mongo *conn ) {
  bson key[1];

  bson_init( key );
  bson_append_int( key, "name", 1 );
  bson_finish( key );

  mongo_create_index( conn, "tutorial.persons", key, NULL, 0, NULL );

  bson_destroy( key );

  printf( "simple index created on \"name\"\n" );

  bson_init( key );
  bson_append_int( key, "age", 1 );
  bson_append_int( key, "name", 1 );
  bson_finish( key );

  mongo_create_index( conn, "tutorial.persons", key, NULL, 0, NULL );

  bson_destroy( key );

  printf( "compound index created on \"age\", \"name\"\n" );
}
Beispiel #7
0
int gridfs_init( mongo *client, const char *dbname, const char *prefix,
    gridfs *gfs ) {

    int options;
    bson b;
    bson_bool_t success;

    gfs->client = client;

    /* Allocate space to own the dbname */
    gfs->dbname = ( const char * )bson_malloc( strlen( dbname )+1 );
    strcpy( ( char * )gfs->dbname, dbname );

    /* Allocate space to own the prefix */
    if ( prefix == NULL ) prefix = "fs";
    gfs->prefix = ( const char * )bson_malloc( strlen( prefix )+1 );
    strcpy( ( char * )gfs->prefix, prefix );

    /* Allocate space to own files_ns */
    gfs->files_ns =
        ( const char * ) bson_malloc ( strlen( prefix )+strlen( dbname )+strlen( ".files" )+2 );
    strcpy( ( char * )gfs->files_ns, dbname );
    strcat( ( char * )gfs->files_ns, "." );
    strcat( ( char * )gfs->files_ns, prefix );
    strcat( ( char * )gfs->files_ns, ".files" );

    /* Allocate space to own chunks_ns */
    gfs->chunks_ns = ( const char * ) bson_malloc( strlen( prefix ) + strlen( dbname )
                     + strlen( ".chunks" ) + 2 );
    strcpy( ( char * )gfs->chunks_ns, dbname );
    strcat( ( char * )gfs->chunks_ns, "." );
    strcat( ( char * )gfs->chunks_ns, prefix );
    strcat( ( char * )gfs->chunks_ns, ".chunks" );

    bson_init( &b );
    bson_append_int( &b, "filename", 1 );
    bson_finish( &b );
    options = 0;
    success = ( mongo_create_index( gfs->client, gfs->files_ns, &b, options, NULL ) == MONGO_OK );
    bson_destroy( &b );
    if ( !success ) {
        bson_free( ( char * )gfs->dbname );
        bson_free( ( char * )gfs->prefix );
        bson_free( ( char * )gfs->files_ns );
        bson_free( ( char * )gfs->chunks_ns );
        return MONGO_ERROR;
    }

    bson_init( &b );
    bson_append_int( &b, "files_id", 1 );
    bson_append_int( &b, "n", 1 );
    bson_finish( &b );
    options = MONGO_INDEX_UNIQUE;
    success = ( mongo_create_index( gfs->client, gfs->chunks_ns, &b, options, NULL ) == MONGO_OK );
    bson_destroy( &b );
    if ( !success ) {
        bson_free( ( char * )gfs->dbname );
        bson_free( ( char * )gfs->prefix );
        bson_free( ( char * )gfs->files_ns );
        bson_free( ( char * )gfs->chunks_ns );
        return MONGO_ERROR;
    }

    return MONGO_OK;
}
Beispiel #8
0
int gridfs_init(mongo_connection * client, const char * dbname,
                const char * prefix, gridfs* gfs)
{
    int options;
    bson_buffer bb;
    bson b;
    bson out;
    bson_bool_t success;

    gfs->client = client;

    /* Allocate space to own the dbname */
    gfs->dbname = (const char *)malloc(strlen(dbname)+1);
    if (gfs->dbname == NULL) {
        return FALSE;
    }
    strcpy((char*)gfs->dbname, dbname);

    /* Allocate space to own the prefix */
    if (prefix == NULL) prefix = "fs";
    gfs->prefix = (const char *)malloc(strlen(prefix)+1);
    if (gfs->prefix == NULL) {
        free((char*)gfs->dbname);
        return FALSE;
    }
    strcpy((char *)gfs->prefix, prefix);

    /* Allocate space to own files_ns */
    gfs->files_ns =
        (const char *) malloc (strlen(prefix)+strlen(dbname)+strlen(".files")+2);
    if (gfs->files_ns == NULL) {
        free((char*)gfs->dbname);
        free((char*)gfs->prefix);
        return FALSE;
    }
    strcpy((char*)gfs->files_ns, dbname);
    strcat((char*)gfs->files_ns, ".");
    strcat((char*)gfs->files_ns, prefix);
    strcat((char*)gfs->files_ns, ".files");

    /* Allocate space to own chunks_ns */
    gfs->chunks_ns = (const char *) malloc(strlen(prefix) + strlen(dbname)
                                           + strlen(".chunks") + 2);
    if (gfs->chunks_ns == NULL) {
        free((char*)gfs->dbname);
        free((char*)gfs->prefix);
        free((char*)gfs->files_ns);
        return FALSE;
    }
    strcpy((char*)gfs->chunks_ns, dbname);
    strcat((char*)gfs->chunks_ns, ".");
    strcat((char*)gfs->chunks_ns, prefix);
    strcat((char*)gfs->chunks_ns, ".chunks");

    bson_buffer_init(&bb);
    bson_append_int(&bb, "filename", 1);
    bson_from_buffer(&b, &bb);
    options = 0;
    success = mongo_create_index(gfs->client, gfs->files_ns, &b, options, &out);
    bson_destroy(&b);
    if (!success) {
        free((char*)gfs->dbname);
        free((char*)gfs->prefix);
        free((char*)gfs->files_ns);
        free((char*)gfs->chunks_ns);
        return FALSE;
    }

    bson_buffer_init(&bb);
    bson_append_int(&bb, "files_id", 1);
    bson_append_int(&bb, "n", 1);
    bson_from_buffer(&b, &bb);
    options = MONGO_INDEX_UNIQUE;
    success = mongo_create_index(gfs->client, gfs->chunks_ns, &b, options, &out);
    bson_destroy(&b);
    if (!success) {
        free((char*)gfs->dbname);
        free((char*)gfs->prefix);
        free((char*)gfs->files_ns);
        free((char*)gfs->chunks_ns);
        return FALSE;
    }

    return TRUE;
}