TEST(cursor,sdbCursor_get_over_then_close) { INT32 rc = SDB_OK ; rc = initEnv( HOST, SERVER, USER, PASSWD ) ; ASSERT_EQ( SDB_OK, rc ) ; sdbConnectionHandle connection = 0 ; sdbCollectionHandle collection = 0 ; sdbCursorHandle cursor = 0 ; INT32 NUM = 1 ; bson obj ; bson obj1 ; bson obj2 ; rc = sdbConnect ( HOST, SERVER, USER, PASSWD, &connection ) ; ASSERT_EQ( SDB_OK, rc ) ; rc = getCollection ( connection, COLLECTION_FULL_NAME, &collection ) ; ASSERT_EQ( SDB_OK, rc ) ; insertRecords ( collection, NUM ) ; rc = sdbQuery( collection, NULL, NULL, NULL, NULL, 0, -1, &cursor ) ; ASSERT_EQ( SDB_OK, rc ) ; bson_init(&obj); bson_init(&obj1); bson_init(&obj2); rc = sdbNext ( cursor, &obj ) ; CHECK_MSG("%s%d\n","rc = ",rc) ; ASSERT_EQ( SDB_OK, rc ) ; printf( "SdbNext record is:\n" ) ; bson_print( &obj ) ; printf("\n") ; rc = sdbNext ( cursor, &obj1 ) ; CHECK_MSG("%s%d\n","rc = ",rc) ; ASSERT_EQ( SDB_DMS_EOC, rc ) ; rc = sdbNext ( cursor, &obj1 ) ; CHECK_MSG("%s%d\n","rc = ",rc) ; ASSERT_EQ( SDB_DMS_CONTEXT_IS_CLOSE, rc ) ; bson_destroy( &obj ) ; bson_destroy( &obj1 ) ; bson_destroy( &obj2 ) ; rc = sdbCloseCursor( cursor ) ; CHECK_MSG("%s%d\n","rc = ",rc) ; ASSERT_EQ( SDB_OK, rc ) ; sdbDisconnect ( connection ) ; sdbReleaseCursor ( cursor ) ; sdbReleaseCollection ( collection ) ; 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 ) ; }
TEST(cursor,sdbCloseCursor) { INT32 rc = SDB_OK ; rc = initEnv( HOST, SERVER, USER, PASSWD ) ; ASSERT_EQ( SDB_OK, rc ) ; sdbConnectionHandle connection = 0 ; sdbCollectionHandle collection = 0 ; sdbCursorHandle cursor = 0 ; INT32 NUM = 10 ; bson obj ; bson obj1 ; rc = sdbConnect ( HOST, SERVER, USER, PASSWD, &connection ) ; ASSERT_EQ( SDB_OK, rc ) ; rc = getCollection ( connection, COLLECTION_FULL_NAME, &collection ) ; ASSERT_EQ( SDB_OK, rc ) ; insertRecords ( collection, NUM ) ; rc = sdbQuery( collection, NULL, NULL, NULL, NULL, 0, -1, &cursor ) ; bson_init(&obj); rc = sdbCurrent( cursor, &obj ) ; ASSERT_EQ( SDB_OK, rc ) ; printf( "Current record is:\n" ) ; bson_print( &obj ) ; printf("\n") ; bson_destroy( &obj ) ; rc = sdbCloseCursor( cursor ) ; ASSERT_EQ( SDB_OK, rc ) ; bson_init( &obj1 ) ; rc = sdbCurrent( cursor, &obj1 ) ; ASSERT_EQ( rc, SDB_DMS_CONTEXT_IS_CLOSE ) ; sdbDisconnect ( connection ) ; sdbReleaseCursor ( cursor ) ; sdbReleaseCollection ( collection ) ; sdbReleaseConnection ( connection ) ; }
INT32 expCLExporter::run( UINT64 &exportedCount, UINT64 &failCount ) { INT32 rc = SDB_OK ; sdbCursorHandle hCusor = SDB_INVALID_HANDLE ; sdbCollectionHandle hCL = SDB_INVALID_HANDLE ; const CHAR *buf = NULL ; UINT32 size = 0 ; rc = _query( hCL, hCusor ) ; if ( SDB_OK != rc ) { PD_LOG ( PDERROR, "Failed to query collection %s.%s, rc = %d", _cl.csName.c_str(), _cl.clName.c_str(), rc ); goto error ; } rc = _out.open() ; if ( SDB_OK != rc ) { PD_LOG( PDERROR, "Failed to open output, rc = %d", rc ) ; goto error ; } rc = _convertor.init() ; if ( SDB_OK != rc ) { PD_LOG( PDERROR, "Failed to init convertor, rc = %d", rc ) ; goto error ; } rc = _convertor.head( buf, size ) ; if ( SDB_OK != rc ) { PD_LOG( PDERROR, "Failed to get head of convertor, rc = %d", rc ) ; goto error ; } if ( size > 0 ) { rc = _out.output( buf, size ); if ( SDB_OK != rc ) { PD_LOG( PDERROR, "Failed to output the head, rc = %d", rc ) ; goto error ; } buf = _options.delRecord().c_str() ; size = (UINT32)_options.delRecord().size() ; rc = _out.output( buf, size ); if ( SDB_OK != rc ) { PD_LOG( PDERROR, "Failed to output the record delimiter, rc = %d", rc ) ; goto error ; } } rc = _exportRecords( hCusor, exportedCount, failCount ) ; if ( SDB_OK != rc ) { PD_LOG( PDERROR, "Failed to export records, rc = %d", rc ) ; goto error ; } rc = _convertor.tail( buf, size ) ; if ( SDB_OK != rc ) { PD_LOG( PDERROR, "Failed to get tail of convertor, rc = %d", rc ) ; goto error ; } if ( size > 0 ) { rc = _out.output( buf, size ); if ( SDB_OK != rc ) { PD_LOG( PDERROR, "Failed to output the tail, rc = %d", rc ) ; goto error ; } } done : _out.close() ; if ( SDB_INVALID_HANDLE != hCL ) { sdbReleaseCollection(hCL) ; } if ( SDB_INVALID_HANDLE != hCusor ) { sdbCloseCursor(hCusor) ; sdbReleaseCursor(hCusor) ; } return rc ; error : goto done ; }
INT32 RecordSharding::init(const vector<Host>& hosts, const string& user, const string& password, const string& csname, const string& clname, BOOLEAN useSSL) { INT32 rc = SDB_OK; sdbConnectionHandle conn = SDB_INVALID_HANDLE; sdbCursorHandle cursor = SDB_INVALID_HANDLE; bson cataObj; INT32 cataCount = 0; SDB_ASSERT(!_inited, "alreay inited"); _hosts = &hosts; _user = user; _password = password; _csname = csname; _clname = clname; _useSSL = useSSL; bson_init(&cataObj); for (vector<Host>::const_iterator it = hosts.begin(); it != hosts.end(); it++) { const Host& host = *it; if (SDB_INVALID_HANDLE != cursor) { sdbCloseCursor(cursor); sdbReleaseCursor(cursor); cursor = SDB_INVALID_HANDLE; } if (SDB_INVALID_HANDLE != conn) { sdbDisconnect(conn); sdbReleaseConnection(conn); conn = SDB_INVALID_HANDLE; } if (_useSSL) { rc = sdbSecureConnect(host.hostname.c_str(), host.svcname.c_str(), _user.c_str(), _password.c_str(), &conn); } else { rc = sdbConnect(host.hostname.c_str(), host.svcname.c_str(), _user.c_str(), _password.c_str(), &conn); } if (SDB_OK != rc) { PD_LOG(PDWARNING, "failed to connect to server %s:%s, rc=%d, usessl=%d", host.hostname.c_str(), host.svcname.c_str(), rc, _useSSL); rc = SDB_OK; continue; } rc = sdbGetSnapshot(conn, SDB_SNAP_CATALOG, NULL, NULL, NULL, &cursor); if (SDB_OK != rc) { if (SDB_INVALID_HANDLE != cursor) { sdbCloseCursor(cursor); sdbReleaseCursor(cursor); cursor = SDB_INVALID_HANDLE; } if (SDB_RTN_COORD_ONLY == rc) { PD_LOG(PDWARNING, "%s:%s is not coordinator", host.hostname.c_str(), host.svcname.c_str()); rc = SDB_OK; continue; } PD_LOG(PDWARNING, "failed to get coordinator group from %s:%s, rc = %d", host.hostname.c_str(), host.svcname.c_str(), rc); rc = SDB_OK; continue; } break; } if (SDB_INVALID_HANDLE == cursor) { rc = SDB_OK; PD_LOG(PDWARNING, "failed to get coordinator group"); goto done; } for(;;) { rc = sdbNext(cursor, &cataObj); if (SDB_OK != rc) { if (SDB_DMS_EOC == rc) { rc = SDB_OK; break; } PD_LOG(PDERROR, "failed to get cataObj from cursor, rc=%d", rc); goto error; } rc = _cataAgent.updateCatalog(bson_data(&cataObj)); if (SDB_OK != rc) { PD_LOG(PDERROR, "failed to update catalog agent, rc=%d", rc); goto error; } cataCount++; } if (0 == cataCount) { _inited = TRUE; goto done; } _collectionName = _csname + "." + _clname; rc = _cataAgent.getCataInfo(_collectionName, _cataInfo); if (SDB_OK != rc) { PD_LOG(PDERROR, "failed to get catalog info, rc=%d", rc); goto error; } _isMainCL = _cataInfo.isMainCL(); if (_cataInfo.isMainCL()) { vector<string> subCLList; INT32 subGroupNum = 0; rc = _cataInfo.getSubCLList(subCLList); if (SDB_OK != rc) { PD_LOG(PDERROR, "failed to get group by record, rc=%d", rc); goto error; } for (vector<string>::iterator it = subCLList.begin(); it != subCLList.end(); it++) { CataInfo cataInfo; rc = _cataAgent.getCataInfo(*it, cataInfo); if (SDB_OK != rc) { PD_LOG(PDERROR, "failed to get catalog info, rc=%d", rc); goto error; } subGroupNum += cataInfo.getGroupNum(); _subCataInfo[*it] = cataInfo; } _groupNum = subGroupNum; } else { _groupNum = _cataInfo.getGroupNum(); } _inited = TRUE; done: bson_destroy(&cataObj); if (SDB_INVALID_HANDLE != cursor) { sdbCloseCursor(cursor); sdbReleaseCursor(cursor); cursor = SDB_INVALID_HANDLE; } if (SDB_INVALID_HANDLE != conn) { sdbDisconnect(conn); sdbReleaseConnection(conn); conn = SDB_INVALID_HANDLE; } return rc; error: goto done; }