Example #1
0
void test_example_awesome() {
    bcon_error_t ret;
    bson b[1];

    /* JSON {"BSON": ["awesome", 5.05, 1986]} */

    bcon awesome[] = { "BSON", "[", "awesome", BF(5.05), BI(1986), "]", BEND };
    test_bson_from_bcon( awesome, BCON_OK, BSON_VALID );

    if  (verbose )
        printf("\t--------\n");

    bson_init( b );
    bson_append_start_array( b, "BSON" );
    bson_append_string( b, "0", "awesome" );
    bson_append_double( b, "1", 5.05 );
    bson_append_int( b, "2", 1986 );
    bson_append_finish_array( b );
    ret = bson_finish( b );
    if ( verbose )
        bson_print( b );
    bson_destroy( b );
}
void
_mongoc_cursor_cursorid_init_with_reply (mongoc_cursor_t *cursor,
                                         bson_t          *reply,
                                         uint32_t         server_id)
{
   mongoc_cursor_cursorid_t *cid;

   cursor->sent = true;
   cursor->server_id = server_id;

   cid = (mongoc_cursor_cursorid_t *)cursor->iface_data;
   BSON_ASSERT (cid);

   bson_destroy (&cid->array);
   bson_steal (&cid->array, reply);

   if (!_mongoc_cursor_cursorid_start_batch (cursor)) {
      bson_set_error (&cursor->error,
                      MONGOC_ERROR_CURSOR,
                      MONGOC_ERROR_CURSOR_INVALID_CURSOR,
                      "Couldn't parse cursor document");
   }
}
Example #3
0
static void
test_binary (void)
{
   bson_subtype_t subtype;
   bson_uint32_t len;
   const bson_uint8_t *binary;

   bson_t *bcon = BCON_NEW (
      "foo", BCON_BIN (
         BSON_SUBTYPE_BINARY,
         (bson_uint8_t *)"deadbeef",
         8
         )
      );

   assert (BCON_EXTRACT (bcon, "foo", BCONE_BIN (subtype, binary, len)));

   assert (subtype == BSON_SUBTYPE_BINARY);
   assert (len == 8);
   assert (memcmp (binary, "deadbeef", 8) == 0);

   bson_destroy (bcon);
}
Example #4
0
void TestInsertDocument(CuTest* tc)
{
  db_connection* conn;
  bson* b;
  size_t i;
  
  conn = alloc_test_conn();
  b = make_sample_bson();

  for( i = 1; i < 11; ++i )
  {
    db_document_insert( conn, b );
    CuAssertIntEquals( tc, i, db_current_size( conn ) );
    CuAssertIntEquals( tc, bson_size(b) * i, conn->free_p );
  }

  bson_destroy( b );
  free( b );
  db_connection_destroy( conn );
  free( conn );
  
  delete_db();
}
Example #5
0
static int mongo_check_is_master( mongo *conn ) {
    bson out;
    bson_iterator it;
    bson_bool_t ismaster = 0;

    out.data = NULL;

    if ( mongo_simple_int_command( conn, "admin", "ismaster", 1, &out ) == MONGO_OK ) {
        if( bson_find( &it, &out, "ismaster" ) )
            ismaster = bson_iterator_bool( &it );
    } else {
        return MONGO_ERROR;
    }

    bson_destroy( &out );

    if( ismaster )
        return MONGO_OK;
    else {
        conn->err = MONGO_CONN_NOT_MASTER;
        return MONGO_ERROR;
    }
}
static void
test_extract_ctx (void)
{
   int32_t a, b, c;

   bson_t *bson = BCON_NEW (
      "a", BCON_INT32 (1),
      "b", BCON_INT32 (2),
      "c", BCON_INT32 (3)
      );

   test_extract_ctx_helper (bson, 3,
                            "a", BCONE_INT32 (a), NULL,
                            "b", BCONE_INT32 (b), NULL,
                            "c", BCONE_INT32 (c), NULL
                            );

   assert (a == 1);
   assert (b == 2);
   assert (c == 3);

   bson_destroy (bson);
}
Example #7
0
      ~BsonArray()
      {
         if (NULL != _array)
         {
            for (INT32 i = 0; i < _capacity; i++)
            {
               bson* obj = _array[i];
               if (NULL != obj)
               {
                  bson_destroy(obj);
                  SDB_OSS_FREE(obj);
               }
            }

            SDB_OSS_FREE(_array);
            _array = NULL;
         }

         _capacity = 0;
         _size = 0;
         _bsonSize = 0;
         _finished = FALSE;
      }
Example #8
0
static void
insert_test_docs (mongoc_collection_t *collection)
{
   mongoc_write_concern_t *write_concern;
   bson_error_t error;
   bson_oid_t oid;
   bson_t b;
   int i;

   write_concern = mongoc_write_concern_new();
   mongoc_write_concern_set_w(write_concern, 3);

   {
      const bson_t *wc;
      char *str;

      wc = _mongoc_write_concern_get_gle(write_concern);
      str = bson_as_json(wc, NULL);
      fprintf(stderr, "Write Concern: %s\n", str);
      bson_free(str);
   }

   for (i = 0; i < 200; i++) {
      bson_init(&b);
      bson_oid_init(&oid, NULL);
      bson_append_oid(&b, "_id", 3, &oid);

      ASSERT_OR_PRINT (mongoc_collection_insert(collection,
                                                MONGOC_INSERT_NONE,
                                                &b,
                                                write_concern,
                                                &error), error);
      bson_destroy(&b);
   }

   mongoc_write_concern_destroy(write_concern);
}
Example #9
0
static void
_append_write_err_legacy (mongoc_write_result_t *result,
                          const char *err,
                          mongoc_error_domain_t domain,
                          int32_t code,
                          uint32_t offset)
{
   bson_t holder, write_errors, child;
   bson_iter_t iter;

   BSON_ASSERT (code > 0);

   if (!result->error.domain) {
      bson_set_error (&result->error, domain, (uint32_t) code, "%s", err);
   }

   /* stop processing, if result->ordered */
   result->failed = true;

   bson_init (&holder);
   bson_append_array_begin (&holder, "0", 1, &write_errors);
   bson_append_document_begin (&write_errors, "0", 1, &child);

   /* set error's "index" to 0; fixed up in _mongoc_write_result_merge_arrays */
   bson_append_int32 (&child, "index", 5, 0);
   bson_append_int32 (&child, "code", 4, code);
   bson_append_utf8 (&child, "errmsg", 6, err, -1);
   bson_append_document_end (&write_errors, &child);
   bson_append_array_end (&holder, &write_errors);
   bson_iter_init (&iter, &holder);
   bson_iter_next (&iter);

   _mongoc_write_result_merge_arrays (
      offset, result, &result->writeErrors, &iter);

   bson_destroy (&holder);
}
bool
mongoc_gridfs_file_remove (mongoc_gridfs_file_t *file,
                           bson_error_t         *error)
{
   bson_t sel = BSON_INITIALIZER;
   bool ret = false;

   BSON_ASSERT (file);

   BSON_APPEND_VALUE (&sel, "_id", &file->files_id);

   if (!mongoc_collection_remove (file->gridfs->files,
                                  MONGOC_REMOVE_SINGLE_REMOVE,
                                  &sel,
                                  NULL,
                                  error)) {
      goto cleanup;
   }

   bson_reinit (&sel);
   BSON_APPEND_VALUE (&sel, "files_id", &file->files_id);

   if (!mongoc_collection_remove (file->gridfs->chunks,
                                  MONGOC_REMOVE_NONE,
                                  &sel,
                                  NULL,
                                  error)) {
      goto cleanup;
   }

   ret = true;

cleanup:
   bson_destroy (&sel);

   return ret;
}
Example #11
0
void *
json_to_bson(text *json)
{
	bson	b;
	void   *result;
	JsonLexContext *lex = makeJsonLexContext(json, true);
	jsonSemAction sem;
	json_to_bson_state state;

	memset(&state, 0, sizeof(json_to_bson_state));
	state.bson = &b;
	state.lex = lex;

	memset(&sem, 0, sizeof(sem));
	sem.semstate = (void *) &state;
	sem.object_start = jbson_object_start;
	sem.object_end = jbson_object_end;
	sem.array_start = jbson_array_start;
	sem.array_end = jbson_array_end;
	sem.array_element_start = jbson_array_element_start;
	sem.scalar = jbson_scalar;
	sem.object_field_start = jbson_object_field_start;

	bson_init(&b);
	pg_parse_json(lex, &sem);

	pfree(lex->strval->data);
	pfree(lex->strval);
	pfree(lex);

	bson_finish(&b);
	result = palloc(bson_size(&b));
	memcpy(result, b.data, bson_size(&b));
	bson_destroy(&b);

	return result;
}
Example #12
0
static bool
auto_ismaster (request_t *request,
               void *data)
{
   const char *response_json = (const char *) data;
   char *quotes_replaced;
   bson_t response;
   bson_error_t error;

   if (!request->is_command || strcasecmp (request->command_name, "ismaster")) {
      return false;
   }

   quotes_replaced = single_quotes_to_double (response_json);

   if (!bson_init_from_json (&response, quotes_replaced, -1, &error)) {
      fprintf (stderr, "%s\n", error.message);
      fflush (stderr);
      abort ();
   }

   if (mock_server_get_rand_delay (request->server)) {
      _mongoc_usleep ((int64_t) (rand () % 10) * 1000);
   }

   mock_server_replies (request,
                        MONGOC_REPLY_NONE,
                        0,
                        0,
                        1,
                        response_json);

   bson_destroy (&response);
   bson_free (quotes_replaced);
   request_destroy (request);
   return true;
}
Example #13
0
int main() {
  mongo conn[1];
  int status = mongo_client( conn, "127.0.0.1", 27017 );

  if( status != MONGO_OK ) {
      switch ( conn->err ) {
        case MONGO_CONN_NO_SOCKET:  printf( "no socket\n" ); return 1;
        case MONGO_CONN_FAIL:       printf( "connection failed\n" ); return 1;
        case MONGO_CONN_NOT_MASTER: printf( "not master\n" ); return 1;
      }
  }

  bson b[1];

  bson_init( b );
  bson_append_new_oid( b, "_id" );
  bson_append_string( b, "name", "Joe" );
  bson_append_int( b, "age", 33 );
  bson_finish( b );

  mongo_insert( conn, "tutorial.persons", b, 0 );

  bson_destroy( b );

 
  tutorial_insert_batch(conn);

  tutorial_empty_query(conn);
  tutorial_simple_query(conn);
  tutorial_update(conn);
  tutorial_index(conn);

  mongo_destroy( conn );

  return 0;
}
Example #14
0
MONGO_EXPORT int mongo_create_index( mongo *conn, const char *ns, const bson *key, int options, bson *out ) {
    bson b;
    bson_iterator it;
    char name[255] = {'_'};
    int i = 1;
    char idxns[1024];

    bson_iterator_init( &it, key );
    while( i < 255 && bson_iterator_next( &it ) ) {
        strncpy( name + i, bson_iterator_key( &it ), 255 - i );
        i += strlen( bson_iterator_key( &it ) );
    }
    name[254] = '\0';

    bson_init( &b );
    bson_append_bson( &b, "key", key );
    bson_append_string( &b, "ns", ns );
    bson_append_string( &b, "name", name );
    if ( options & MONGO_INDEX_UNIQUE )
        bson_append_bool( &b, "unique", 1 );
    if ( options & MONGO_INDEX_DROP_DUPS )
        bson_append_bool( &b, "dropDups", 1 );
    if ( options & MONGO_INDEX_BACKGROUND )
        bson_append_bool( &b, "background", 1 );
    if ( options & MONGO_INDEX_SPARSE )
        bson_append_bool( &b, "sparse", 1 );
    bson_finish( &b );

    strncpy( idxns, ns, 1024-16 );
    strcpy( strchr( idxns, '.' ), ".system.indexes" );
    mongo_insert( conn, idxns, &b );
    bson_destroy( &b );

    *strchr( idxns, '.' ) = '\0'; /* just db not ns */
    return mongo_cmd_get_last_error( conn, idxns, out );
}
Example #15
0
static int mongo_cmd_get_error_helper( mongo *conn, const char *db,
                                       bson *realout, const char *cmdtype ) {

    bson out = {NULL,0};
    bson_bool_t haserror = 0;

    /* Reset last error codes. */
    conn->lasterrcode = 0;
    bson_free( conn->lasterrstr );
    conn->lasterrstr = NULL;

    /* If there's an error, store its code and string in the connection object. */
    if( mongo_simple_int_command( conn, db, cmdtype, 1, &out ) == MONGO_OK ) {
        bson_iterator it;
        haserror = ( bson_find( &it, &out, "err" ) != BSON_NULL );
        if( haserror ) {
            conn->lasterrstr = ( char * )bson_malloc( bson_iterator_string_len( &it ) );
            if( conn->lasterrstr ) {
                strcpy( conn->lasterrstr, bson_iterator_string( &it ) );
            }

            if( bson_find( &it, &out, "code" ) != BSON_NULL )
                conn->lasterrcode = bson_iterator_int( &it );
        }
    }

    if( realout )
        *realout = out; /* transfer of ownership */
    else
        bson_destroy( &out );

    if( haserror )
        return MONGO_ERROR;
    else
        return MONGO_OK;
}
static void
mongoc_uri_parse_tags (mongoc_uri_t *uri, /* IN */
                       const char   *str, /* IN */
                       bson_t       *doc) /* IN */
{
   const char *end_keyval;
   const char *end_key;
   bson_t b;
   char *keyval;
   char *key;
   char keystr[32];
   int i;

   bson_init(&b);

again:
   if ((keyval = scan_to_unichar(str, ',', &end_keyval))) {
      if ((key = scan_to_unichar(keyval, ':', &end_key))) {
         bson_append_utf8(&b, key, -1, end_key + 1, -1);
         bson_free(key);
      }
      bson_free(keyval);
      str = end_keyval + 1;
      goto again;
   } else {
      if ((key = scan_to_unichar(str, ':', &end_key))) {
         bson_append_utf8(&b, key, -1, end_key + 1, -1);
         bson_free(key);
      }
   }

   i = bson_count_keys(doc);
   bson_snprintf(keystr, sizeof keystr, "%u", i);
   bson_append_document(doc, keystr, -1, &b);
   bson_destroy(&b);
}
Example #17
0
CHAR * json2rawcbson ( const CHAR *str )
{
   bson obj ;
   CHAR *p = NULL ;
   bson_init ( &obj ) ;
   if ( jsonToBson2 ( &obj, str, 0, 1 ) )
   {
      if ( obj.data )
      {
         INT32 bsonsize = *((INT32*)obj.data) ;
         if ( 0 < bsonsize )
         {
            p = (CHAR*)malloc ( bsonsize ) ;
            if ( p )
            {
               memset ( p, 0, bsonsize ) ;
               memcpy ( p, obj.data, bsonsize ) ;
            }
         }
      }
   }
   bson_destroy ( &obj ) ;
   return p ;
}
Example #18
0
static void
_mongoc_client_killcursors_command (mongoc_cluster_t       *cluster,
                                    mongoc_server_stream_t *server_stream,
                                    int64_t                 cursor_id,
                                    const char             *db,
                                    const char             *collection)
{
   bson_t command = BSON_INITIALIZER;
   bson_t child;

   bson_append_utf8 (&command, "killCursors", 11, collection, -1);
   bson_append_array_begin (&command, "cursors", 7, &child);
   bson_append_int64 (&child, "0", 1, cursor_id);
   bson_append_array_end (&command, &child);

   /* Find, getMore And killCursors Commands Spec: "The result from the
    * killCursors command MAY be safely ignored."
    */
   mongoc_cluster_run_command (cluster, server_stream->stream,
                               MONGOC_QUERY_SLAVE_OK, db, &command,
                               NULL, NULL);

   bson_destroy (&command);
}
Example #19
0
static int __check_auth_reply(void)
{
    int         res;
    modem_ret_t ret;
    ssize_t     len;
    uint8_t     *data;
    bson        b;

    ret = modem_get_packet(TCS_SERVICE_PROF, &data, &len);
    if (ret != MODEM_RET_OK) {
        printf("Error getting server packet=%d\n", ret);
        return -1;
    }

    printf("Decoding BSON data of size=%ld\n", len);

    bson_init_unfinished_data(&b, (char *)data + 2, len - 2, 0);
    bson_print(&b);

    bson_destroy(&b);
    free(data);

    return 0;
}
Example #20
0
MONGO_EXPORT void gridfile_get_chunk( gridfile *gfile, int n, bson* out ) {
    bson query;
    
    bson_iterator it;
    bson_oid_t id;
    int result;

    bson_init( &query );
    bson_find( &it, gfile->meta, "_id" );
    id = *bson_iterator_oid( &it );
    bson_append_oid( &query, "files_id", &id );
    bson_append_int( &query, "n", n );
    bson_finish( &query );

    result = (mongo_find_one(gfile->gfs->client,
                             gfile->gfs->chunks_ns,
                             &query, NULL, out ) == MONGO_OK );
    bson_destroy( &query );
    if (!result) {
        bson empty;
        bson_empty(&empty);
        bson_copy(out, &empty);
    }
}
Example #21
0
int database_add_orphan_transaction(struct database* db, unsigned char const* hash, struct transaction const* tx)
{
    mongoc_collection_t* collection = mongoc_client_get_collection(db->client, database_name(db), "transactions");
    
    // Convert the transaction to a bson document
    bson_t* doc = bson_new();
    transaction_bson(tx, doc);

    // Set the hash
    BSON_APPEND_BINARY(doc, "hash", BSON_SUBTYPE_BINARY, (uint8_t*)hash, 32);

    // Give it a new id
    bson_oid_t oid;
    bson_oid_init(&oid, NULL);
    BSON_APPEND_OID(doc, "_id", &oid);

    // Orphan -> height = -1
    BSON_APPEND_INT32(doc, "height", -1);

#if 0
    // Print json
    char* str = bson_as_json(doc, NULL);
    printf("%s\n", str);
    bson_free(str);
#endif

    // Perform insert
    bson_error_t error;
    if(mongoc_collection_insert(collection, MONGOC_INSERT_NONE, doc, NULL, &error) == 0) {
        printf("MongoDB error: %s\n", error.message);
    }

    bson_destroy(doc);
    mongoc_collection_destroy(collection);
    return 0;
}
Example #22
0
int be_mongo_superuser(void *conf, const char *username)
{
	struct mongo_backend *handle = (struct mongo_backend *) conf;
	mongoc_collection_t *collection;
	mongoc_cursor_t *cursor;
	bson_error_t error;
	const bson_t *doc;
	int result = 0;

	bson_t query;
	bson_iter_t iter;
	bson_init (&query);
	bson_append_utf8(&query, handle->user_username_prop, -1, username, -1);

	collection = mongoc_client_get_collection(handle->client, handle->database, handle->user_coll);

	cursor = mongoc_collection_find_with_opts(collection, &query, NULL, NULL);

	if (!mongoc_cursor_error (cursor, &error) &&
		mongoc_cursor_next (cursor, &doc)) {
		bson_iter_init(&iter, doc);
		if (bson_iter_find(&iter, handle->user_superuser_prop)) {
			result = bson_iter_as_bool(&iter) ? 1 : 0;
		}
	}

	if (mongoc_cursor_error (cursor, &error)) {
		fprintf(stderr, "Cursor Failure: %s\n", error.message);
	}

	bson_destroy (&query);
	mongoc_cursor_destroy (cursor);
	mongoc_collection_destroy (collection);

	return (result) ? BACKEND_ALLOW : BACKEND_DEFER;
}
static mongoc_cursor_state_t
_get_next_batch (mongoc_cursor_t *cursor)
{
   data_cmd_t *data = (data_cmd_t *) cursor->impl.data;
   bson_t getmore_cmd;
   getmore_type_t getmore_type = _getmore_type (cursor);

   switch (getmore_type) {
   case GETMORE_CMD:
      _mongoc_cursor_prepare_getmore_command (cursor, &getmore_cmd);
      _mongoc_cursor_response_refresh (
         cursor, &getmore_cmd, NULL /* opts */, &data->response);
      bson_destroy (&getmore_cmd);
      data->reading_from = CMD_RESPONSE;
      return IN_BATCH;
   case OP_GETMORE:
      _mongoc_cursor_op_getmore (cursor, &data->response_legacy);
      data->reading_from = OP_GETMORE_RESPONSE;
      return IN_BATCH;
   case UNKNOWN:
   default:
      return DONE;
   }
}
Example #24
0
static void
test_bson_as_json_double (void)
{
   size_t len;
   bson_t *b;
   char *str;

   b = bson_new();
   assert(bson_append_double(b, "foo", -1, 123.456));
   assert(bson_append_double(b, "bar", -1, 3));
   assert(bson_append_double(b, "baz", -1, -1));
   assert(bson_append_double(b, "quux", -1, 1.0001));
   assert(bson_append_double(b, "huge", -1, 1e99));
   str = bson_as_json(b, &len);
   ASSERT_CMPSTR (str,
                  "{"
                  " \"foo\" : 123.456,"
                  " \"bar\" : 3,"
                  " \"baz\" : -1,"
                  " \"quux\" : 1.0001,"
                  " \"huge\" : 1e+99 }");
   bson_free(str);
   bson_destroy(b);
}
Example #25
0
static void
test_inline_doc (void)
{
   int32_t a, b;

   bson_t *bcon = BCON_NEW (
      "foo", "{",
         "b", BCON_INT32 (2),
         "a", BCON_INT32 (1),
      "}"
   );

   assert (BCON_EXTRACT (bcon,
      "foo", "{",
         "a", BCONE_INT32 (a),
         "b", BCONE_INT32 (b),
      "}"
   ));

   assert (a == 1);
   assert (b == 2);

   bson_destroy (bcon);
}
Example #26
0
static int Encode(lua_State *L){
    bson* b = bson_encode(L);
    if(b == NULL){
        LOG_ERROR("null");
        return -1;
    } 
    bson_print(b);
    bson* bson_field = (bson*) lua_newuserdata(L,sizeof(bson));
    if(bson_field == NULL)
    {
        LOG_ERROR("null");
        return -1;
    }
    int ret = bson_copy(bson_field,b);
    if(ret != 0)
    {
        LOG_ERROR("copy fail");
    }
    //bson_print(bson_field);
    luaL_getmetatable(L,"bson");
    lua_setmetatable(L,-2);
    bson_destroy(b);
    return 1;
}
Example #27
0
INT32 migExport::_run( const CHAR *pCSName, const CHAR *pCLName, INT32 &total )
{
   INT32 rc = SDB_OK ;
   const CHAR *pTemp = NULL ;
   bson obj ;
   bson_iterator it ;
   bson_type type ;

   bson_init( &obj ) ;
   if ( ( pCSName == NULL && pCLName == NULL ) ||
        ( pCSName == NULL && pCLName != NULL ) )
   {
      //never cs cl
      rc = _getCSList() ;
      if ( rc )
      {
         PD_LOG ( PDERROR, "Failed to get collection space list, rc = %d",
                  rc ) ;
         goto error ;
      }
      while( TRUE )
      {
         rc = sdbNext( _gCSList, &obj ) ;
         if ( rc )
         {
            if ( SDB_DMS_EOC != rc )
            {
               PD_LOG ( PDERROR, "Failed to get collection space list, rc = %d",
                        rc ) ;
               goto error ;
            }
            else
            {
               rc = SDB_OK ;
               goto done ;
            }
         }
         type = bson_find( &it, &obj, "Name" ) ;
         if ( type != BSON_STRING )
         {
            rc = SDB_SYS ;
            PD_LOG ( PDERROR, "List collection space does not string, rc = %d",
                     rc ) ;
            goto error ;
         }
         pTemp = bson_iterator_string( &it ) ;
         rc = _run( pTemp, pCLName, total ) ;
         if ( rc )
         {
            PD_LOG ( PDERROR, "Faild to call _run, rc = %d", rc ) ;
            goto error ;
         }
      }
   }
   else if ( pCSName != NULL && pCLName == NULL )
   {
      //cs
      rc = _getCLList() ;
      if ( rc )
      {
         PD_LOG ( PDERROR, "Failed to get collection list, rc = %d",
                  rc ) ;
         goto error ;
      }
      while ( TRUE )
      {
         rc = sdbNext( _gCLList, &obj ) ;
         if ( rc )
         {
            if ( SDB_DMS_EOC != rc )
            {
               PD_LOG ( PDERROR, "Failed to get collection list, rc = %d",
                        rc ) ;
               goto error ;
            }
            else
            {
               rc = SDB_OK ;
               goto done ;
            }
         }
         type = bson_find( &it, &obj, "Name" ) ;
         if ( type != BSON_STRING )
         {
            rc = SDB_SYS ;
            PD_LOG ( PDERROR, "List collection does not string, rc = %d",
                     rc ) ;
            goto error ;
         }
         pTemp = bson_iterator_string( &it ) ;
         rc = _run( pCSName, pTemp, total ) ;
         if ( rc )
         {
            PD_LOG ( PDERROR, "Faild to call _run, rc = %d", rc ) ;
            goto error ;
         }
      }
   }
   else
   {
      //cs and cl
      rc = _exportCL( pCSName, pCLName, total ) ;
      if ( rc )
      {
         PD_LOG ( PDERROR, "Faild to call _export, rc = %d", rc ) ;
         goto error ;
      }
   }
done:
   bson_destroy ( &obj ) ;
   return rc ;
error:
   goto done ;
}
Example #28
0
void gridfile_destroy( gridfile *gfile )

{
    bson_destroy( gfile->meta );
    bson_free( gfile->meta );
}
Example #29
0
INT32 migExport::_exportCL( const CHAR *pCSName,
                            const CHAR *pCLName,
                            INT32 &total )
{
   INT32 rc = SDB_OK ;
   INT32 clTotal = 0 ;
   bson obj ;

   bson_init( &obj ) ;
   _gCollection = 0 ;
   _gCollectionSpace = 0 ;

   rc = _getCS( pCSName ) ;
   if ( rc )
   {
      PD_LOG ( PDERROR, "Failed to get collection space, rc = %d",
               rc ) ;
      goto error ;
   }
   rc = _getCL( pCLName ) ;
   if ( rc )
   {
      PD_LOG ( PDERROR, "Failed to get collection, rc = %d",
               rc ) ;
      goto error ;
   }
   rc = _query() ;
   if ( rc )
   {
      if ( SDB_DMS_EOC == rc )
      {
         rc = SDB_OK ;
         goto done ;
      }
      else
      {
         PD_LOG ( PDERROR, "Failed to get record, rc = %d",
                  rc ) ;
         goto error ;
      }
   }
   while ( TRUE )
   {
      rc = sdbNext( _gCursor, &obj ) ;
      if ( rc )
      {
         if ( SDB_DMS_EOC != rc )
         {
            PD_LOG ( PDERROR, "Failed to get collection list, rc = %d",
                     rc ) ;
            goto error ;
         }
         else
         {
            rc = SDB_OK ;
            goto done ;
         }
      }
      rc = _writeRecord( &obj ) ;
      if ( rc )
      {
         PD_LOG ( PDERROR, "Failed to write record to file, rc = %d",
                  rc ) ;
         goto error ;
      }
      bson_destroy ( &obj ) ;
      ++clTotal ;
   }
done:
   total += clTotal ;
   bson_destroy ( &obj ) ;
   if ( _gCollection )
   {
      sdbReleaseCollection ( _gCollection ) ;
   }
   if ( _gCollectionSpace )
   {
      sdbReleaseCS ( _gCollectionSpace ) ;
   }
   _gCollection = 0 ;
   _gCollectionSpace = 0 ;
   PD_LOG ( PDEVENT, "%s.%s export record %d in file",pCSName, pCLName, clTotal ) ;
   return rc ;
error:
   goto done ;
}
Example #30
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;
}