TEST(collectonspace,sdbCreateCollection) { sdbConnectionHandle connection = 0 ; sdbCSHandle collectionspace = 0 ; sdbCollectionHandle collection = 0 ; INT32 rc = SDB_OK ; rc = initEnv( HOST, SERVER, USER, PASSWD ) ; ASSERT_EQ( SDB_OK, rc ) ; rc = sdbConnect ( HOST, SERVER, USER, PASSWD, &connection ) ; ASSERT_EQ( SDB_OK, rc ) ; rc = getCollectionSpace ( connection, COLLECTION_SPACE_NAME, &collectionspace ) ; ASSERT_EQ( SDB_OK, rc ) ; rc = sdbDropCollection ( collectionspace, COLLECTION_NAME ) ; ASSERT_EQ( SDB_OK, rc ) ; rc = sdbCreateCollection ( collectionspace, COLLECTION_NAME, &collection ) ; ASSERT_EQ( SDB_OK, rc ) ; sdbDisconnect ( connection ) ; sdbReleaseCollection ( collection ) ; sdbReleaseCS ( collectionspace ) ; sdbReleaseConnection ( connection ) ; }
TEST(collectonspace,sdbCreateCollection1_without_options) { sdbConnectionHandle connection = 0 ; sdbCSHandle collectionspace = 0 ; sdbCollectionHandle collection = 0 ; INT32 rc = SDB_OK ; bson option ; rc = initEnv( HOST, SERVER, USER, PASSWD ) ; ASSERT_EQ( SDB_OK, rc ) ; rc = sdbConnect ( HOST, SERVER, USER, PASSWD, &connection ) ; ASSERT_EQ( SDB_OK, rc ) ; rc = getCollectionSpace ( connection, COLLECTION_SPACE_NAME, &collectionspace ) ; ASSERT_EQ( SDB_OK, rc ) ; rc = sdbDropCollection ( collectionspace, COLLECTION_NAME ) ; ASSERT_EQ( SDB_OK, rc ) ; bson_init( &option ); bson_append_int( &option, "ReplSize", 0 ); bson_finish( &option ) ; rc = sdbCreateCollection1 ( collectionspace, COLLECTION_NAME, &option, &collection ) ; ASSERT_EQ( SDB_OK, rc ) ; bson_destroy( &option ) ; sdbDisconnect ( connection ) ; sdbReleaseCollection ( collection ) ; sdbReleaseCS ( collectionspace ) ; sdbReleaseConnection ( connection ) ; }
migExport::~migExport () { if ( _isOpen ) { ossClose ( _file ) ; } if ( _gCursor ) { sdbCloseCursor( _gCursor ) ; sdbReleaseCursor( _gCursor ) ; } if ( _gCSList ) { sdbCloseCursor( _gCSList ) ; sdbReleaseCursor( _gCSList ) ; } if ( _gCLList ) { sdbCloseCursor( _gCLList ) ; sdbReleaseCursor( _gCLList ) ; } if ( _gCollection ) { sdbReleaseCollection ( _gCollection ) ; } if ( _gCollectionSpace ) { sdbReleaseCS ( _gCollectionSpace ) ; } if ( _gConnection ) { sdbDisconnect ( _gConnection ) ; sdbReleaseConnection( _gConnection ) ; } SAFE_OSS_FREE( _pBuffer ) ; }
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 ; }
INT32 main ( INT32 argc, CHAR **argv ) { // initialize local variables CHAR *pHostName = NULL ; CHAR *pServiceName = NULL ; CHAR *pUsr = NULL ; CHAR *pPasswd = NULL ; // define a connetion handle; use to connect to database sdbConnectionHandle connection = 0 ; // define a collection space handle sdbCSHandle collectionspace = 0 ; // define a collection handle sdbCollectionHandle collection = 0 ; // define a cursor handle for query sdbCursorHandle cursor = 0 ; sdbCursorHandle cursor1 = 0 ; // define local variables // initialize them before use INT32 rc = SDB_OK ; bson obj ; bson rule ; bson record ; bson updatecondition ; bson tmp ; bson_iterator it ; // verify syntax if ( 5 != argc ) { displaySyntax ( (CHAR*)argv[0] ) ; exit ( 0 ) ; } // read argument pHostName = (CHAR*)argv[1] ; pServiceName = (CHAR*)argv[2] ; pUsr = (CHAR*)argv[3] ; pPasswd = (CHAR*)argv[4] ; // connect to database rc = sdbConnect ( pHostName, pServiceName, pUsr, pPasswd, &connection ) ; if( rc!=SDB_OK ) { printf("Failed to connet to database, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } // create collection space rc = sdbCreateCollectionSpace ( connection, COLLECTION_SPACE_NAME, SDB_PAGESIZE_4K, &collectionspace ) ; if( rc!=SDB_OK ) { printf("Failed to create collection space, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } // create collection in a specified colletion space. rc = sdbCreateCollection ( collectionspace, COLLECTION_NAME, &collection ) ; if( rc!=SDB_OK ) { printf("Failed to create collection, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } // prepare record bson_init( &record ) ; bson_append_int( &record, "age", 10 ) ; rc = bson_finish( &record ) ; CHECK_RC ( rc, "Failed to build bson" ) ; // insert record into database rc = sdbInsert( collection, &record ) ; if( rc!=SDB_OK ) { printf("Failed to insert, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } bson_destroy( &record ) ; // query all the record in this collection rc = sdbQuery(collection, NULL, NULL, NULL, NULL, 0, -1, &cursor ) ; if( rc!=SDB_OK ) { printf("Failed to query, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } // get the record from cursor bson_init(&obj) ; bson_init(&tmp) ; rc=sdbNext( cursor, &obj ) ; if ( rc!= SDB_OK ) { printf("Failed to get next, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } rc = bson_copy( &tmp, &obj ) ; CHECK_RC ( rc, "Failed to copy bson" ) ; printf("Before update, the record is:\n") ; bson_print( &tmp ) ; // set the update condition using "_id" bson_find( &it, &obj, "_id" ) ; bson_init( &updatecondition ) ; bson_append_element( &updatecondition, NULL, &it ) ; rc = bson_finish( &updatecondition ) ; CHECK_RC ( rc, "Failed to build bson" ) ; // set the update rule bson_init( &rule ) ; bson_append_start_object ( &rule, "$set" ) ; bson_append_int ( &rule, "age", 99 ) ; bson_append_finish_object ( &rule ) ; rc = bson_finish ( &rule ) ; CHECK_RC ( rc, "Failed to build bson" ) ; // update rc = sdbUpdate(collection, &rule, &updatecondition, NULL ) ; if ( rc!=SDB_OK ) { printf("Failed to update, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } bson_destroy(&obj) ; bson_destroy(&rule) ; bson_destroy(&updatecondition) ; // query all the record in this collection again rc = sdbQuery(collection, NULL, NULL, NULL, NULL, 0, -1, &cursor1 ) ; if( rc!=SDB_OK ) { printf("Failed to query, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } // get record from cursor1 bson_init(&obj) ; rc=sdbNext( cursor1, &obj ) ; if ( rc!= SDB_OK ) { printf("Failed to get next, rc = %d" OSS_NEWLINE, rc ) ; goto error ; } printf("after update, the record is:\n") ; bson_print( &obj ) ; bson_destroy(&obj) ; done: // drop collection space rc = sdbDropCollectionSpace( connection, COLLECTION_SPACE_NAME ) ; if ( rc != SDB_OK ) { printf("Failed to drop collection space, rc = %d" OSS_NEWLINE, rc ) ; } // disconnect the connection sdbDisconnect ( connection ) ; // release the local variables sdbReleaseCursor ( cursor ) ; sdbReleaseCursor ( cursor1 ) ; sdbReleaseCollection ( collection ) ; sdbReleaseCS ( collectionspace ) ; sdbReleaseConnection ( connection ) ; return 0; error: goto done ; }
INT32 main ( INT32 argc, CHAR **argv ) { // initialize local variables CHAR *pHostName = NULL ; CHAR *pServiceName = NULL ; CHAR *pUsr = NULL ; CHAR *pPasswd = NULL ; // define a connetion handle; use to connect to database sdbConnectionHandle connection = 0 ; // define a collection space handle sdbCSHandle collectionspace = 0 ; // define a collection handle sdbCollectionHandle collection = 0 ; // define a cursor handle for query sdbCursorHandle cursor = 0 ; // define local variables // initialize them before use INT32 rc = SDB_OK ; INT32 count = 0 ; bson obj ; bson rule ; bson objList [ NUM_RECORD ] ; // verify syntax if ( 5 != argc ) { displaySyntax ( (CHAR*)argv[0] ) ; exit ( 0 ) ; } // read argument pHostName = (CHAR*)argv[1] ; pServiceName = (CHAR*)argv[2] ; pUsr = (CHAR*)argv[3] ; pPasswd = (CHAR*)argv[4] ; // connect to database rc = sdbConnect ( pHostName, pServiceName, pUsr, pPasswd, &connection ) ; CHECK_RC ( rc, "Failed to connet to database" ) ; // create collection space rc = sdbCreateCollectionSpace ( connection, COLLECTION_SPACE_NAME, SDB_PAGESIZE_4K, &collectionspace ) ; CHECK_RC ( rc, "Failed to create collection space" ) ; // create collection in a specified colletion space. // Here,we build it up in the new collection. rc = sdbCreateCollection ( collectionspace, COLLECTION_NAME, &collection ) ; CHECK_RC ( rc, "Failed to create collection" ) ; // create record list using objList createRecordList ( &objList[0], NUM_RECORD ) ; // insert obj and free memory that allocated by createRecordList for ( count = 0; count < NUM_RECORD; count++ ) { // all the contents of inserted record are the same except _id rc = sdbInsert ( collection, &objList[count] ) ; if ( rc ) { printf ( "Failed to insert record, rc = %d" OSS_NEWLINE, rc ) ; } bson_destroy ( &objList[count] ) ; } // query all the record in this collection // and return the result by the cursor handle rc = sdbQuery(collection, NULL, NULL, NULL, NULL, 0, -1, &cursor ) ; CHECK_RC ( rc, "Failed to query" ) ; // get all the qureied records bson_init(&obj); while( !( rc=sdbNext( cursor, &obj ) ) ) { bson_print( &obj ) ; bson_destroy(&obj) ; bson_init(&obj); } bson_destroy(&obj) ; if( rc==SDB_DMS_EOC ) { printf("All the record had been list." OSS_NEWLINE ) ; } else if( rc!=SDB_OK ) { CHECK_RC ( rc, "Failed to get the record" ) ; } // drop the specified collection rc = sdbDropCollection( collectionspace,COLLECTION_NAME ) ; CHECK_RC ( rc, "Failed to drop the specified collection" ) ; // drop the specified collection space rc = sdbDropCollectionSpace( connection,COLLECTION_SPACE_NAME ) ; CHECK_RC ( rc, "Failed to drop the specified collection space" ) ; done: // disconnect the connection sdbDisconnect ( connection ) ; // release the local variables sdbReleaseCursor ( cursor ) ; sdbReleaseCollection ( collection ) ; sdbReleaseCS ( collectionspace ) ; sdbReleaseConnection ( connection ) ; return 0; error: goto done ; }
INT32 main ( INT32 argc, CHAR **argv ) { // initialize local variables CHAR *pHostName = NULL ; CHAR *pServiceName = NULL ; CHAR *pUsr = NULL ; CHAR *pPasswd = NULL ; // define a connetion handle; use to connect to database sdbConnectionHandle connection = 0 ; // define a collection space handle sdbCSHandle collectionspace = 0 ; // define a collection handle sdbCollectionHandle collection = 0 ; // define a cursor handle for query sdbCursorHandle cursor = 0 ; // define local variables // initialize them before use bson obj ; bson rule ; INT32 rc = SDB_OK ; // read argument pHostName = (CHAR*)argv[1] ; pServiceName = (CHAR*)argv[2] ; pUsr = (CHAR*)argv[3] ; pPasswd = (CHAR*)argv[4] ; // verify syntax if ( 5 != argc ) { displaySyntax ( (CHAR*)argv[0] ) ; exit ( 0 ) ; } // connect to database rc = sdbConnect ( pHostName, pServiceName, pUsr, pPasswd, &connection ) ; CHECK_RC ( rc, "Failed to connet to database" ) ; // create collection space rc = sdbCreateCollectionSpace ( connection, COLLECTION_SPACE_NAME, SDB_PAGESIZE_4K, &collectionspace ) ; CHECK_RC ( rc, "Failed to create collection space" ) ; // create collection in a specified colletion space. rc = sdbCreateCollection ( collectionspace, COLLECTION_NAME, &collection ) ; CHECK_RC ( rc, "Failed to create collection" ) ; // insert records to the collection bson_init ( &obj ) ; // build a English record createEnglishRecord ( &obj ) ; // insert rc = sdbInsert ( collection, &obj ) ; CHECK_RC ( rc, "Failed to insert record" ) ; // query the records // the result is in the cursor handle rc = sdbQuery(collection, NULL, NULL, NULL, NULL, 0, -1, &cursor ) ; CHECK_RC ( rc, "Failed to query" ) ; // update the record // let's set the rule and query condition first // here,we make the condition to be NULL // so all the records will be update bson_init ( &rule ) ; bson_append_start_object ( &rule, "$set" ) ; bson_append_int ( &rule, "age", 19 ) ; bson_append_finish_object ( &rule ) ; bson_finish ( &rule ) ; CHECK_RC ( rc, "Failed to build bson" ) ; printf ( "The update rule is:" ) ; bson_print( &rule ) ; // update rc = sdbUpdate ( collection, &rule, NULL, NULL ) ; CHECK_RC ( rc, "Failed to update the record" ) ; // free memory after finish using bson bson_destroy ( &rule ) ; printf ( "Success to update!" OSS_NEWLINE ) ; // drop the specified collection rc = sdbDropCollection ( collectionspace,COLLECTION_NAME ) ; CHECK_RC ( rc, "Failed to drop the specified collection" ) ; // drop the specified collection space rc = sdbDropCollectionSpace ( connection,COLLECTION_SPACE_NAME ) ; CHECK_RC ( rc, "Failed to drop the specified collection" ) ; done: // disconnect the connection sdbDisconnect ( connection ) ; // release the local variables sdbReleaseCursor ( cursor ) ; sdbReleaseCollection ( collection ) ; sdbReleaseCS ( collectionspace ) ; sdbReleaseConnection ( connection ) ; return 0; error: goto done ; }
INT32 main ( INT32 argc, CHAR **argv ) { // initialize local variables CHAR *pHostName = NULL ; CHAR *pServiceName = NULL ; CHAR *pUsr = NULL ; CHAR *pPasswd = NULL ; // define handles sdbConnectionHandle db = SDB_INVALID_HANDLE ; sdbCSHandle cs = SDB_INVALID_HANDLE ; sdbCollectionHandle cl = SDB_INVALID_HANDLE ; sdbLobHandle lob = SDB_INVALID_HANDLE ; INT32 rc = SDB_OK ; CHAR buf[BUFSIZE] = { 'a' } ; bson_oid_t oid ; // verify syntax if ( 5 != argc ) { displaySyntax ( (CHAR*)argv[0] ) ; exit ( 0 ) ; } // read argument pHostName = (CHAR*)argv[1] ; pServiceName = (CHAR*)argv[2] ; pUsr = (CHAR*)argv[3] ; pPasswd = (CHAR*)argv[4] ; // connect to database rc = sdbConnect ( pHostName, pServiceName, pUsr, pPasswd, &db ) ; CHECK_RC ( rc, "Failed to connet to database" ) ; // create collection space rc = sdbCreateCollectionSpace ( db, COLLECTION_SPACE_NAME, SDB_PAGESIZE_4K, &cs ) ; CHECK_RC ( rc, "Failed to create collection space" ) ; // create collection rc = sdbCreateCollection ( cs, COLLECTION_NAME, &cl ) ; CHECK_RC ( rc, "Failed to create collection" ) ; // create a large object and than write something to it // we need to gen an oid for the creating large object bson_oid_gen( &oid ) ; rc = sdbOpenLob( cl, &oid, SDB_LOB_CREATEONLY, &lob ) ; CHECK_RC ( rc, "Failed to create large object" ) ; // write something to the newly created large object rc = sdbWriteLob( lob, buf, BUFSIZE ) ; CHECK_RC ( rc, "Failed to write to large object" ) ; // close the newly created large object rc = sdbCloseLob( &lob ) ; CHECK_RC ( rc, "Failed to close large object" ) ; printf( "Success to create a large object and write something to it!\n" ) ; // drop the collection rc = sdbDropCollection( cs,COLLECTION_NAME ) ; CHECK_RC ( rc, "Failed to drop the specified collection" ) ; // drop the collection space rc = sdbDropCollectionSpace( db,COLLECTION_SPACE_NAME ) ; CHECK_RC ( rc, "Failed to drop the specified collection space" ) ; done: // disconnect the connection sdbDisconnect ( db ) ; // release the handles // sdbReleaseLob ( lob ) ; sdbReleaseCollection ( cl ) ; sdbReleaseCS ( cs ) ; sdbReleaseConnection ( db ) ; return 0; error: goto done ; }