// PD_TRACE_DECLARE_FUNCTION ( SDB_RTNLOCALLOBSTREAM__PREPARE, "_rtnLocalLobStream::_prepare" ) INT32 _rtnLocalLobStream::_prepare( const CHAR *fullName, const bson::OID &oid, INT32 mode, _pmdEDUCB *cb ) { INT32 rc = SDB_OK ; PD_TRACE_ENTRY( SDB_RTNLOCALLOBSTREAM__PREPARE ) ; dmsStorageUnitID suID = DMS_INVALID_CS ; const CHAR *clName = NULL ; _dmsCB = sdbGetDMSCB() ; if ( SDB_LOB_MODE_READ != mode ) { rc = _dmsCB->writable( cb ) ; if ( rc ) { PD_LOG( PDERROR, "Database is not writable, rc = %d", rc ) ; goto error ; } _writeDMS = TRUE ; } rc = rtnResolveCollectionNameAndLock( fullName, _dmsCB, &_su, &clName, suID ) ; if ( SDB_OK != rc ) { PD_LOG( PDERROR, "Failed to resolve collection name:%s", fullName ) ; goto error ; } rc = _su->data()->getMBContext( &_mbContext, clName, -1 ) ; if ( SDB_OK != rc ) { PD_LOG( PDERROR, "Failed to resolve collection name:%s", clName ) ; goto error ; } rc = _getAccessPrivilege( fullName, oid, mode ) ; if ( SDB_OK != rc ) { PD_LOG( PDERROR, "Failed to get lob privilege:%d", rc ) ; goto error ; } done: PD_TRACE_EXITRC( SDB_RTNLOCALLOBSTREAM__PREPARE, rc ) ; return rc ; error: _closeInner( cb ) ; goto done ; }
// PD_TRACE_DECLARE_FUNCTION ( SDB_RTNTRAVERSALQUERY, "rtnTraversalQuery" ) INT32 rtnTraversalQuery ( const CHAR *pCollectionName, const BSONObj &key, const CHAR *pIndexName, INT32 dir, pmdEDUCB *cb, SDB_DMSCB *dmsCB, SDB_RTNCB *rtnCB, SINT64 &contextID, rtnContextData **ppContext, BOOLEAN enablePrefetch ) { INT32 rc = SDB_OK ; PD_TRACE_ENTRY ( SDB_RTNTRAVERSALQUERY ) ; SDB_ASSERT ( pCollectionName, "collection name can't be NULL" ) ; SDB_ASSERT ( pIndexName, "index name can't be NULL" ) ; SDB_ASSERT ( cb, "cb can't be NULL" ) ; SDB_ASSERT ( dmsCB, "dmsCB can't be NULL" ) ; SDB_ASSERT ( rtnCB, "rtnCB can't be NULL" ) ; SDB_ASSERT ( dir == 1 || dir == -1, "dir must be 1 or -1" ) ; dmsStorageUnitID suID = DMS_INVALID_CS ; dmsStorageUnit *su = NULL ; rtnContextData *context = NULL ; const CHAR *pCollectionShortName = NULL ; optAccessPlan *plan = NULL ; dmsMBContext *mbContext = NULL ; rtnPredicateList *predList = NULL ; rtnIXScanner *scanner = NULL ; BSONObj hint ; BSONObj dummy ; rc = rtnResolveCollectionNameAndLock ( pCollectionName, dmsCB, &su, &pCollectionShortName, suID ) ; PD_RC_CHECK ( rc, PDERROR, "Failed to resolve collection name %s", pCollectionName ) ; rc = su->data()->getMBContext( &mbContext, pCollectionShortName, -1 ) ; PD_RC_CHECK( rc, PDERROR, "Failed to get dms mb context, rc: %d", rc ) ; rc = rtnCB->contextNew ( RTN_CONTEXT_DATA, (rtnContext**)&context, contextID, cb ) ; PD_RC_CHECK ( rc, PDERROR, "Failed to create new context, %d", rc ) ; SDB_ASSERT ( context, "context can't be NULL" ) ; try { hint = BSON( "" << pIndexName ) ; } catch ( std::exception &e ) { PD_RC_CHECK ( SDB_SYS, PDERROR, "Failed to construct hint object: %s", e.what() ) ; } plan = SDB_OSS_NEW optAccessPlan( su, pCollectionShortName, dummy, dummy, hint ) ; if ( !plan ) { rc = SDB_OOM ; goto error ; } rc = plan->optimize() ; PD_RC_CHECK( rc, PDERROR, "Plan optimize failed, rc: %d", rc ) ; PD_CHECK ( plan->getScanType() == IXSCAN && !plan->isAutoGen(), SDB_INVALIDARG, error, PDERROR, "Unable to generate access plan by index %s", pIndexName ) ; rc = mbContext->mbLock( SHARED ) ; PD_RC_CHECK( rc, PDERROR, "dms mb context lock failed, rc: %d", rc ) ; { dmsRecordID rid ; if ( -1 == dir ) { rid.resetMax() ; } else { rid.resetMin () ; } ixmIndexCB indexCB ( plan->getIndexCBExtent(), su->index(), NULL ) ; PD_CHECK ( indexCB.isInitialized(), SDB_SYS, error, PDERROR, "unable to get proper index control block" ) ; if ( indexCB.getLogicalID() != plan->getIndexLID() ) { PD_LOG( PDERROR, "Index[extent id: %d] logical id[%d] is not " "expected[%d]", plan->getIndexCBExtent(), indexCB.getLogicalID(), plan->getIndexLID() ) ; rc = SDB_IXM_NOTEXIST ; goto error ; } predList = plan->getPredList() ; SDB_ASSERT ( predList, "predList can't be NULL" ) ; predList->setDirection ( dir ) ; scanner = SDB_OSS_NEW rtnIXScanner ( &indexCB, predList, su, cb ) ; PD_CHECK ( scanner, SDB_OOM, error, PDERROR, "Unable to allocate memory for scanner" ) ; rc = scanner->relocateRID ( key, rid ) ; PD_CHECK ( SDB_OK == rc, rc, error, PDERROR, "Failed to relocate key to the specified location: %s, " "rc = %d", key.toString().c_str(), rc ) ; } mbContext->mbUnlock() ; rc = context->openTraversal( su, mbContext, plan, scanner, cb, dummy, -1, 0 ) ; PD_RC_CHECK( rc, PDERROR, "Open context traversal faield, rc: %d", rc ) ; mbContext = NULL ; plan = NULL ; suID = DMS_INVALID_CS ; scanner = NULL ; su = NULL ; if ( cb->getMonConfigCB()->timestampON ) { context->getMonCB()->recordStartTimestamp() ; } if ( ppContext ) { *ppContext = context ; } if ( enablePrefetch ) { context->enablePrefetch ( cb ) ; } done : PD_TRACE_EXITRC ( SDB_RTNTRAVERSALQUERY, rc ) ; return rc ; error : if ( su && mbContext ) { su->data()->releaseMBContext( mbContext ) ; } if ( plan ) { plan->release() ; } if ( scanner ) { SDB_OSS_DEL scanner ; } if ( DMS_INVALID_CS != suID ) { dmsCB->suUnlock( suID ) ; } if ( -1 != contextID ) { rtnCB->contextDelete ( contextID, cb ) ; contextID = -1 ; } goto done ; }
// PD_TRACE_DECLARE_FUNCTION ( SDB_RTNQUERY, "rtnQuery" ) INT32 rtnQuery ( const CHAR *pCollectionName, const BSONObj &selector, const BSONObj &matcher, const BSONObj &orderBy, const BSONObj &hint, SINT32 flags, pmdEDUCB *cb, SINT64 numToSkip, SINT64 numToReturn, SDB_DMSCB *dmsCB, SDB_RTNCB *rtnCB, SINT64 &contextID, rtnContextBase **ppContext, BOOLEAN enablePrefetch ) { INT32 rc = SDB_OK ; PD_TRACE_ENTRY ( SDB_RTNQUERY ) ; dmsStorageUnitID suID = DMS_INVALID_CS ; contextID = -1 ; SDB_ASSERT ( pCollectionName, "collection name can't be NULL" ) ; SDB_ASSERT ( cb, "educb can't be NULL" ) ; SDB_ASSERT ( dmsCB, "dmsCB can't be NULL" ) ; SDB_ASSERT ( rtnCB, "rtnCB can't be NULL" ) ; dmsStorageUnit *su = NULL ; dmsMBContext *mbContext = NULL ; rtnContextData *dataContext = NULL ; const CHAR *pCollectionShortName = NULL ; rtnAccessPlanManager *apm = NULL ; optAccessPlan *plan = NULL ; BSONObj hintTmp = hint ; BSONObj blockObj ; BSONObj *pBlockObj = NULL ; const CHAR *indexName = NULL ; const CHAR *scanType = NULL ; INT32 indexLID = DMS_INVALID_EXTENT ; INT32 direction = 0 ; if ( FLG_QUERY_EXPLAIN & flags ) { rc = rtnExplain( pCollectionName, selector, matcher, orderBy, hint, flags, numToSkip, numToReturn, cb, dmsCB, rtnCB, contextID, ppContext ) ; if ( SDB_OK != rc ) { PD_LOG( PDERROR, "failed to explain query:%d", rc ) ; goto error ; } else { goto done ; } } rc = rtnResolveCollectionNameAndLock ( pCollectionName, dmsCB, &su, &pCollectionShortName, suID ) ; PD_RC_CHECK( rc, PDERROR, "Failed to resolve collection name %s", pCollectionName ) ; rc = su->data()->getMBContext( &mbContext, pCollectionShortName, -1 ) ; PD_RC_CHECK( rc, PDERROR, "Failed to get dms mb context, rc: %d", rc ) ; rc = rtnCB->contextNew ( ( flags & FLG_QUERY_PARALLED ) ? RTN_CONTEXT_PARADATA : RTN_CONTEXT_DATA, (rtnContext**)&dataContext, contextID, cb ) ; PD_RC_CHECK( rc, PDERROR, "Failed to create new data context" ) ; if ( Object == hint.getField( FIELD_NAME_META ).type() ) { BSONObjBuilder build ; rc = _rtnParseQueryMeta( hint.getField( FIELD_NAME_META ).embeddedObject(), scanType, indexName, indexLID, direction, blockObj ) ; PD_RC_CHECK( rc, PDERROR, "Failed to parase query meta[%s], rc: %d", hint.toString().c_str(), rc ) ; pBlockObj = &blockObj ; if ( indexName ) { build.append( "", indexName ) ; } else { build.appendNull( "" ) ; } hintTmp = build.obj () ; } apm = su->getAPM() ; SDB_ASSERT ( apm, "apm shouldn't be NULL" ) ; rc = apm->getPlan ( matcher, orderBy, // orderBy hintTmp, // hint pCollectionShortName, &plan ) ; if ( rc ) { PD_LOG ( PDERROR, "Failed to get access plan for %s, context %lld, " "rc: %d", pCollectionName, contextID, rc ) ; goto error ; } else if ( ( flags & FLG_QUERY_FORCE_HINT ) && !hintTmp.isEmpty() && plan->isHintFailed() ) { PD_LOG( PDERROR, "Query used force hint[%s] failed", hintTmp.toString().c_str() ) ; rc = SDB_RTN_INVALID_HINT ; goto error ; } if ( pBlockObj ) { if ( !indexName && TBSCAN != plan->getScanType() ) { PD_LOG( PDERROR, "Scan type[%d] must be TBSCAN", plan->getScanType() ) ; rc = SDB_SYS ; goto error ; } else if ( indexName && ( IXSCAN != plan->getScanType() || indexLID != plan->getIndexLID() ) ) { PD_LOG( PDERROR, "Scan type[%d] error or indexLID[%d] is the " "same with [%d]", plan->getScanType(), plan->getIndexLID(), indexLID ) ; rc = SDB_IXM_NOTEXIST ; goto error ; } } if ( flags & FLG_QUERY_STRINGOUT ) { dataContext->getSelector().setStringOutput( TRUE ) ; } rc = dataContext->open( su, mbContext, plan, cb, selector, plan->sortRequired() ? -1 : numToReturn, plan->sortRequired() ? 0 : numToSkip, pBlockObj, direction ) ; PD_RC_CHECK( rc, PDERROR, "Open data context failed, rc: %d", rc ) ; suID = DMS_INVALID_CS ; plan = NULL ; mbContext = NULL ; if ( cb->getMonConfigCB()->timestampON ) { dataContext->getMonCB()->recordStartTimestamp() ; } if ( dataContext->getPlan()->sortRequired() ) { rc = rtnSort ( (rtnContext**)&dataContext, orderBy, cb, numToSkip, numToReturn, rtnCB, contextID ) ; PD_RC_CHECK( rc, PDERROR, "Failed to sort, rc: %d", rc ) ; } if ( ppContext ) { *ppContext = dataContext ; } if ( enablePrefetch ) { dataContext->enablePrefetch ( cb ) ; } done : PD_TRACE_EXITRC ( SDB_RTNQUERY, rc ) ; return rc ; error : if ( su && mbContext ) { su->data()->releaseMBContext( mbContext ) ; } if ( plan ) { plan->release() ; } if ( DMS_INVALID_CS != suID ) { dmsCB->suUnlock( suID ) ; } if ( -1 != contextID ) { rtnCB->contextDelete ( contextID, cb ) ; contextID = -1 ; } goto done ; }
// PD_TRACE_DECLARE_FUNCTION ( SDB_RTNINSERT2, "rtnInsert" ) INT32 rtnInsert ( const CHAR *pCollectionName, BSONObj &objs, INT32 objNum, INT32 flags, pmdEDUCB *cb, SDB_DMSCB *dmsCB, SDB_DPSCB *dpsCB, INT16 w ) { INT32 rc = SDB_OK ; PD_TRACE_ENTRY ( SDB_RTNINSERT2 ) ; SDB_ASSERT ( pCollectionName, "collection name can't be NULL" ) ; SDB_ASSERT ( cb, "educb can't be NULL" ) ; SDB_ASSERT ( dmsCB, "dmsCB can't be NULL" ) ; dmsStorageUnit *su = NULL ; dmsStorageUnitID suID = DMS_INVALID_CS ; const CHAR *pCollectionShortName = NULL ; UINT32 insertCount = 0 ; BOOLEAN writable = FALSE ; ossValuePtr pDataPos = 0 ; rc = dmsCB->writable( cb ) ; if ( rc ) { PD_LOG ( PDERROR, "Database is not writable, rc = %d", rc ) ; goto error; } writable = TRUE; rc = rtnResolveCollectionNameAndLock ( pCollectionName, dmsCB, &su, &pCollectionShortName, suID ) ; if ( rc ) { PD_LOG ( PDERROR, "Failed to resolve collection name %s", pCollectionName ) ; goto error ; } if ( objs.isEmpty () ) { PD_LOG ( PDERROR, "Insert record can't be empty" ) ; rc = SDB_INVALIDARG ; goto error ; } pDataPos = (ossValuePtr)objs.objdata() ; for ( INT32 i = 0 ; i < objNum ; ++i ) { if ( ++insertCount > RTN_INSERT_ONCE_NUM ) { insertCount = 0 ; if ( cb->isInterrupted() ) { rc = SDB_APP_INTERRUPT ; goto error ; } } try { BSONObj record ( (const CHAR*)pDataPos ) ; rc = su->insertRecord ( pCollectionShortName, record, cb, dpsCB ) ; if ( rc ) { if ( ( SDB_IXM_DUP_KEY == rc ) && ( FLG_INSERT_CONTONDUP & flags ) ) { rc = SDB_OK ; } else { PD_LOG ( PDERROR, "Failed to insert record %s into " "collection: %s, rc: %d", record.toString().c_str(), pCollectionName, rc ) ; goto error ; } } pDataPos += ossAlignX ( (ossValuePtr)record.objsize(), 4 ) ; } catch ( std::exception &e ) { PD_LOG ( PDERROR, "Failed to convert to BSON and insert to " "collection: %s", e.what() ) ; rc = SDB_INVALIDARG ; goto error ; } } done : if ( DMS_INVALID_CS != suID ) { dmsCB->suUnlock ( suID ) ; } if ( writable ) { dmsCB->writeDown( cb ); } if ( cb ) { if ( SDB_OK == rc && dpsCB ) { rc = dpsCB->completeOpr( cb, w ) ; } } PD_TRACE_EXITRC ( SDB_RTNINSERT2, rc ) ; return rc ; error : goto done ; }