/**************************************************************************** Desc : Retrieves the current transaction number of a database Notes: This routine should only be called only from within an update transaction since read transactions are not assigned a transaction number. ****************************************************************************/ FLMEXP RCODE FLMAPI FlmDbGetTransId( HFDB hDb, FLMUINT * puiTrNumRV) { RCODE rc = FERR_OK; FDB * pDb = (FDB *)hDb; FLMBOOL bIgnore; if (IsInCSMode( hDb)) { fdbInitCS( pDb); CS_CONTEXT * pCSContext = pDb->pCSContext; FCL_WIRE Wire( pCSContext, pDb); // Send a request to get the transaction ID. if (RC_BAD( rc = Wire.sendOp( FCS_OPCLASS_DATABASE, FCS_OP_GET_TRANS_ID))) { goto Exit; } if (RC_BAD( rc = Wire.sendTerminate())) { goto Transmission_Error; } // Read the response if (RC_BAD( rc = Wire.read())) { goto Transmission_Error; } *puiTrNumRV = Wire.getTransId(); rc = Wire.getRCode(); goto Exit; Transmission_Error: pCSContext->bConnectionGood = FALSE; goto Exit; } if (RC_BAD( rc = fdbInit( pDb, FLM_UPDATE_TRANS, FDB_TRANS_GOING_OK, 0, &bIgnore))) { goto Exit; } *puiTrNumRV = pDb->LogHdr.uiCurrTransID; Exit: flmExit( FLM_DB_GET_TRANS_ID, pDb, rc); return( rc); }
/**************************************************************************** Desc: Searches for an available DRN in the dictionary container. Differs from FlmReserveNextDrn in that it will attempt to reuse dictionary DRNS. ****************************************************************************/ FLMEXP RCODE FLMAPI FlmFindUnusedDictDrn( HFDB hDb, FLMUINT uiStartDrn, FLMUINT uiEndDrn, FLMUINT * puiDrnRV) { RCODE rc; FDB * pDb = (FDB *)hDb; FLMBOOL bIgnore = FALSE; FDICT * pDict; FLMUINT uiCurrDrn; FLMUINT uiStopSearch; if( RC_BAD( rc = fdbInit( pDb, FLM_UPDATE_TRANS, FDB_TRANS_GOING_OK, 0, &bIgnore))) { *puiDrnRV = (FLMUINT)-1; goto Exit; } // Search through the ITT table looking for the first occurance // of ITT_EMPTY_SLOT pDict = pDb->pDict; uiCurrDrn = f_max( uiStartDrn, 1); uiStopSearch = f_min( uiEndDrn, pDict->uiIttCnt - 1); while (uiCurrDrn <= uiStopSearch) { if (pDict->pIttTbl[ uiCurrDrn].uiType == ITT_EMPTY_SLOT) { break; } else { uiCurrDrn++; } } if (uiCurrDrn > uiEndDrn) { rc = RC_SET( FERR_NO_MORE_DRNS); goto Exit; } *puiDrnRV = uiCurrDrn; Exit: fdbExit( pDb); return( rc); }
/**************************************************************************** Desc: Aborts an active transaction. ****************************************************************************/ FLMEXP RCODE FLMAPI FlmDbTransAbort( HFDB hDb) { RCODE rc; FDB * pDb = (FDB *)hDb; FLMBOOL bIgnore; if (IsInCSMode( hDb)) { fdbInitCS( pDb); FCL_WIRE Wire( pDb->pCSContext, pDb); if (!pDb->pCSContext->bConnectionGood) { rc = RC_SET( FERR_BAD_SERVER_CONNECTION); } else { rc = Wire.doTransOp( FCS_OP_TRANSACTION_ABORT, 0, 0, 0); } goto Exit; } if (RC_BAD( rc = fdbInit( pDb, FLM_NO_TRANS, FDB_TRANS_GOING_OK | FDB_CLOSING_OK, 0, &bIgnore))) { goto Exit; } // If there is an invisible transaction going, it should not be // abortable by an application. if ((pDb->uiTransType == FLM_NO_TRANS) || (pDb->uiFlags & FDB_INVISIBLE_TRANS)) { rc = RC_SET( FERR_NO_TRANS_ACTIVE); goto Exit; } rc = flmAbortDbTrans( pDb); Exit: if( RC_OK( rc)) { rc = flmCheckDatabaseState( pDb); } flmExit( FLM_DB_TRANS_ABORT, pDb, rc); return( rc); }
/**************************************************************************** Desc : If the index was suspended, restart the background process that will get the index up to date so that it will eventually be online. Returns FERR_OK with no change if the index is already online. Notes: An update transaction will be started if necessary. ****************************************************************************/ FLMEXP RCODE FLMAPI FlmIndexResume( HFDB hDb, FLMUINT uiIndexNum) { RCODE rc = FERR_OK; FDB * pDb = (FDB *)hDb; IXD * pIxd; FLMUINT uiLastContainerIndexed; FLMUINT uiLastDrnIndexed; FLMUINT uiOnlineTransId; FLMBOOL bStartedTrans = FALSE; if( IsInCSMode( hDb)) { fdbInitCS( pDb); CS_CONTEXT * pCSContext = pDb->pCSContext; FCL_WIRE Wire( pCSContext, pDb); if( !pCSContext->bConnectionGood) { rc = RC_SET( FERR_BAD_SERVER_CONNECTION); goto Transmission_Error; } if( RC_BAD( rc = Wire.sendOp( FCS_OPCLASS_INDEX, FCS_OP_INDEX_RESUME))) { goto Exit; } if (RC_BAD( rc = Wire.sendNumber( WIRE_VALUE_INDEX_ID, uiIndexNum))) { goto Transmission_Error; } // Send the "auto-online" flag (only needed for // backwards compatibility) if (RC_BAD( rc = Wire.sendNumber( WIRE_VALUE_BOOLEAN, 1))) { goto Transmission_Error; } // Send a priority of high (only needed for // backwards compatibility) if (RC_BAD( rc = Wire.sendNumber( WIRE_VALUE_NUMBER1, 1))) { goto Transmission_Error; } if( RC_BAD( rc = Wire.sendTerminate())) { goto Transmission_Error; } // Read the response. if (RC_BAD( rc = Wire.read())) { goto Transmission_Error; } if( RC_BAD( rc = Wire.getRCode())) { goto Exit; } goto Exit; Transmission_Error: pCSContext->bConnectionGood = FALSE; goto Exit; } if (RC_BAD( rc = fdbInit( pDb, FLM_UPDATE_TRANS, FDB_TRANS_GOING_OK, FLM_AUTO_TRANS | FLM_NO_TIMEOUT, &bStartedTrans))) { goto Exit; } // See if the index is valid if( RC_BAD( rc = fdictGetIndex( pDb->pDict, pDb->pFile->bInLimitedMode, uiIndexNum, NULL, &pIxd, TRUE))) { goto Exit; } if( pIxd->uiFlags & IXD_UNIQUE) { // Can't suspend or resume unique indexes flmAssert( !(pIxd->uiFlags & (IXD_SUSPENDED | IXD_OFFLINE))); rc = RC_SET( FERR_ILLEGAL_OP); goto Exit; } if( !(pIxd->uiFlags & (IXD_SUSPENDED | IXD_OFFLINE))) { // Index is already on-line goto Exit; } // If we're in limited mode and this is an encrypted index, // it can't be resumed if (pDb->pFile->bInLimitedMode && pIxd->uiEncId) { rc = RC_SET( FERR_ENCRYPTION_UNAVAILABLE); goto Exit; } if( !(pIxd->uiFlags & IXD_SUSPENDED)) { // Index is not suspended. It is offline (see test // above), but a thread should already be building the // index, or it better be in the start list. #ifdef FLM_DEBUG if (flmBackgroundIndexGet( pDb->pFile, uiIndexNum, FALSE) == NULL) { F_BKGND_IX * pBackgroundIx; for( pBackgroundIx = pDb->pIxStartList; pBackgroundIx; pBackgroundIx = pBackgroundIx->pNext) { if( pBackgroundIx->indexStatus.uiIndexNum == uiIndexNum) { break; } } flmAssert( pBackgroundIx); } #endif goto Exit; } // Better not have a background thread running, or it better be // in the stop list - because its state shows suspended. #ifdef FLM_DEBUG if (flmBackgroundIndexGet( pDb->pFile, uiIndexNum, FALSE) != NULL) { F_BKGND_IX * pBackgroundIx; for( pBackgroundIx = pDb->pIxStopList; pBackgroundIx; pBackgroundIx = pBackgroundIx->pNext) { if( pBackgroundIx->indexStatus.uiIndexNum == uiIndexNum) { break; } } flmAssert( pBackgroundIx); } #endif // Get the tracker info if( RC_BAD( rc = flmGetIxTrackerInfo( pDb, uiIndexNum, &uiLastContainerIndexed, &uiLastDrnIndexed, &uiOnlineTransId, NULL))) { goto Exit; } // Update the tracker info so that the index state will // be changed to "unsuspended." if( RC_BAD( rc = flmSetIxTrackerInfo( pDb, uiIndexNum, uiLastContainerIndexed, uiLastDrnIndexed, uiOnlineTransId, FALSE))) { goto Exit; } // Add an entry to the start list so that an indexing thread // will be started when this transaction commits. if( !(pDb->uiFlags & FDB_REPLAYING_RFL)) { if( RC_BAD( rc = flmAddToStartList( pDb, uiIndexNum))) { goto Exit; } } // Create a new dictionary. if( !(pDb->uiFlags & FDB_UPDATED_DICTIONARY)) { if( RC_BAD( rc = fdictCloneDict( pDb))) { goto Exit; } // Get a pointer to the new IXD if( RC_BAD( rc = fdictGetIndex( pDb->pDict, pDb->pFile->bInLimitedMode, uiIndexNum, NULL, &pIxd, TRUE))) { goto Exit; } } // Update the IXDs flags so that the current update // transaction will see the correct state of the index. // Old read transactions will continue to use a prior // version of the dictionary. pIxd->uiFlags &= ~IXD_SUSPENDED; pIxd->uiFlags |= IXD_OFFLINE; // Log the resume packet to the RFL if( RC_BAD( rc = pDb->pFile->pRfl->logIndexSuspendOrResume( uiIndexNum, RFL_INDEX_RESUME_PACKET))) { goto Exit; } Exit: if( bStartedTrans) { rc = flmEndAutoTrans( pDb, rc); } flmExit( FLM_INDEX_RESUME, pDb, rc); return( rc); }
/**************************************************************************** Desc : Return the status of the index. ****************************************************************************/ FLMEXP RCODE FLMAPI FlmIndexStatus( HFDB hDb, FLMUINT uiIndexNum, FINDEX_STATUS * pIndexStatus) { RCODE rc = FERR_OK; FLMBOOL bStartedAutoTrans = FALSE; FLMUINT uiLastDrnIndexed; FDB * pDb = (FDB *)hDb; F_BKGND_IX * pBackgroundIx; FLMBOOL bSuspended; FLMBOOL bMutexLocked = FALSE; flmAssert( pIndexStatus != NULL); if( IsInCSMode( hDb)) { fdbInitCS( pDb); rc = flmIndexStatusCS( pDb, uiIndexNum, pIndexStatus); goto Exit; } if( RC_BAD( rc = fdbInit( (FDB *)hDb, FLM_READ_TRANS, FDB_TRANS_GOING_OK, 0, &bStartedAutoTrans))) { goto Exit; } f_mutexLock( gv_FlmSysData.hShareMutex); bMutexLocked = TRUE; pBackgroundIx = flmBackgroundIndexGet( pDb->pFile, uiIndexNum, TRUE); if( pBackgroundIx) { f_memcpy( pIndexStatus, &pBackgroundIx->indexStatus, sizeof( FINDEX_STATUS)); f_mutexUnlock( gv_FlmSysData.hShareMutex); bMutexLocked = FALSE; flmAssert( pIndexStatus->uiIndexNum == uiIndexNum); } else { IXD * pIxd; FLMBOOL bTrackerIxSuspended; if( RC_BAD( rc = fdictGetIndex( pDb->pDict, pDb->pFile->bInLimitedMode, uiIndexNum,NULL, &pIxd, TRUE))) { goto Exit; } bSuspended = (pIxd->uiFlags & IXD_SUSPENDED) ? TRUE : FALSE; f_mutexUnlock( gv_FlmSysData.hShareMutex); bMutexLocked = FALSE; // Get the index state from the tracker if( RC_BAD( rc = flmGetIxTrackerInfo( pDb, uiIndexNum, NULL, &uiLastDrnIndexed, NULL, &bTrackerIxSuspended))) { if( rc == FERR_NOT_FOUND) { rc = RC_SET( FERR_BAD_IX); } goto Exit; } // Sanity check #ifdef FLM_DEBUG if( pDb->pFile->FileHdr.uiVersionNum >= FLM_FILE_FORMAT_VER_4_51 && bSuspended != bTrackerIxSuspended) { flmAssert( 0); } #endif // Populate the index status structure. f_memset( pIndexStatus, 0, sizeof( FINDEX_STATUS)); pIndexStatus->uiIndexNum = uiIndexNum; pIndexStatus->uiLastRecordIdIndexed = uiLastDrnIndexed; pIndexStatus->bSuspended = bSuspended; } Exit: if( bMutexLocked) { f_mutexUnlock( gv_FlmSysData.hShareMutex); } if( bStartedAutoTrans) { rc = flmEndAutoTrans( pDb, rc); } flmExit( FLM_INDEX_STATUS, pDb, rc); return( rc); }
/**************************************************************************** Desc : Suspend the selected index from doing any key updates on records that are equal or higher than the next record ID value in the container that the index references. If the index is offline then the background process will be suspended. If the index is online then it will be suspended. If the index is already suspended FERR_OK will be returned. A suspended index is not persistant if the database goes down. Notes: An update transaction will be started if necessary. ****************************************************************************/ FLMEXP RCODE FLMAPI FlmIndexSuspend( HFDB hDb, FLMUINT uiIndexNum) { RCODE rc = FERR_OK; FDB * pDb = (FDB *)hDb; IXD * pIxd; FLMUINT uiHighestRecId; FLMUINT uiContainerNum; FLMBOOL bSuspended; FLMBOOL bStartedTrans = FALSE; LFILE * pLFile; if( IsInCSMode( hDb)) { fdbInitCS( pDb); CS_CONTEXT * pCSContext = pDb->pCSContext; FCL_WIRE Wire( pCSContext, pDb); if( !pCSContext->bConnectionGood) { rc = RC_SET( FERR_BAD_SERVER_CONNECTION); goto Transmission_Error; } if( RC_BAD( rc = Wire.sendOp( FCS_OPCLASS_INDEX, FCS_OP_INDEX_SUSPEND))) { goto Exit; } if (RC_BAD( rc = Wire.sendNumber( WIRE_VALUE_INDEX_ID, uiIndexNum))) { goto Transmission_Error; } if( RC_BAD( rc = Wire.sendTerminate())) { goto Transmission_Error; } // Read the response if (RC_BAD( rc = Wire.read())) { goto Transmission_Error; } if( RC_BAD( rc = Wire.getRCode())) { goto Exit; } goto Exit; Transmission_Error: pCSContext->bConnectionGood = FALSE; goto Exit; } if (RC_BAD( rc = fdbInit( pDb, FLM_UPDATE_TRANS, FDB_TRANS_GOING_OK, FLM_AUTO_TRANS | FLM_NO_TIMEOUT, &bStartedTrans))) { goto Exit; } // See if the index is valid if( RC_BAD( rc = fdictGetIndex( pDb->pDict, pDb->pFile->bInLimitedMode, uiIndexNum, NULL, &pIxd, TRUE))) { goto Exit; } if( pIxd->uiFlags & IXD_UNIQUE) { // Can't suspend unique indexes rc = RC_SET( FERR_ILLEGAL_OP); goto Exit; } if( pIxd->uiFlags & IXD_SUSPENDED) { // Index is already suspended. goto Exit; } // Get the current index info from the tracker if( RC_BAD( rc = flmGetIxTrackerInfo( pDb, uiIndexNum, &uiContainerNum, &uiHighestRecId, NULL, &bSuspended))) { goto Exit; } flmAssert( !bSuspended); // Get information about the container(s) being indexed if( !(pIxd->uiFlags & IXD_OFFLINE)) { if ((uiContainerNum = pIxd->uiContainerNum) == 0) { // The index was on-line and up-to-date. For an index that // crosses all containers, we will suspend on the highest DRN of // the FLM_DATA_CONTAINER. uiContainerNum = FLM_DATA_CONTAINER; } if( RC_BAD( rc = fdictGetContainer( pDb->pDict, uiContainerNum, &pLFile))) { goto Exit; } uiHighestRecId = 0; if( RC_BAD( rc = FSGetNextDrn( pDb, pLFile, FALSE, &uiHighestRecId))) { goto Exit; } // Decrement uiHighestRecId by 1 to correctly reflect the // last record that was indexed. flmAssert( uiHighestRecId != 0); uiHighestRecId--; } // There may be a background thread still assigned to the // index even though the index may be "on-line." This is because // the background thread may have just commited a transaction that // transitioned the index from off-line to on-line, but the thread // has not yet exited (even though it will not do any more work // to update the index). We want to wait for the thread to terminate // before our transaction is allowed to commit. This is so that if // we immediately call resume, it won't find the yet-to-terminate // thread still running in the background. if( !(pDb->uiFlags & FDB_REPLAYING_RFL)) { if( RC_BAD( rc = flmAddToStopList( pDb, uiIndexNum))) { goto Exit; } } flmAssert( uiContainerNum != 0xFFFFFFFF); if( RC_BAD( rc = flmSetIxTrackerInfo( pDb, uiIndexNum, uiContainerNum, uiHighestRecId, TRANS_ID_OFFLINE, TRUE))) { goto Exit; } // Create a new dictionary if( !(pDb->uiFlags & FDB_UPDATED_DICTIONARY)) { if( RC_BAD( rc = fdictCloneDict( pDb))) { goto Exit; } // Get a pointer to the new IXD if( RC_BAD( rc = fdictGetIndex( pDb->pDict, pDb->pFile->bInLimitedMode, uiIndexNum, NULL, &pIxd, TRUE))) { goto Exit; } } // Update the IXDs flags so that the current update // transaction will see the correct state of the index. // Old read transactions will continue to use a prior // version of the dictionary. pIxd->uiFlags |= (IXD_SUSPENDED | IXD_OFFLINE); // Log the suspend packet to the RFL if( RC_BAD( rc = pDb->pFile->pRfl->logIndexSuspendOrResume( uiIndexNum, RFL_INDEX_SUSPEND_PACKET))) { goto Exit; } Exit: if( bStartedTrans) { rc = flmEndAutoTrans( pDb, rc); } flmExit( FLM_INDEX_SUSPEND, pDb, rc); return( rc); }
/**************************************************************************** Desc : Return the number of the next index. Pass in zero to get the first index. ****************************************************************************/ FLMEXP RCODE FLMAPI FlmIndexGetNext( HFDB hDb, FLMUINT * puiIndexNum) { RCODE rc = FERR_OK; FDB * pDb = (FDB *)hDb; FLMBOOL bStartedAutoTrans = FALSE; IXD * pIxd; flmAssert( puiIndexNum != NULL); if( IsInCSMode( hDb)) { fdbInitCS( pDb); CS_CONTEXT * pCSContext = pDb->pCSContext; FCL_WIRE Wire( pCSContext, pDb); if( !pCSContext->bConnectionGood) { rc = RC_SET( FERR_BAD_SERVER_CONNECTION); goto Transmission_Error; } if( RC_BAD( rc = Wire.sendOp( FCS_OPCLASS_INDEX, FCS_OP_INDEX_GET_NEXT))) { goto Exit; } if (RC_BAD( rc = Wire.sendNumber( WIRE_VALUE_INDEX_ID, *puiIndexNum))) { goto Transmission_Error; } if( RC_BAD( rc = Wire.sendTerminate())) { goto Transmission_Error; } // Read the response if (RC_BAD( rc = Wire.read())) { goto Transmission_Error; } if( RC_BAD( rc = Wire.getRCode())) { goto Exit; } *puiIndexNum = Wire.getIndexId(); goto Exit; Transmission_Error: pCSContext->bConnectionGood = FALSE; goto Exit; } if( RC_BAD( rc = fdbInit( (FDB *)hDb, FLM_READ_TRANS, FDB_TRANS_GOING_OK, 0, &bStartedAutoTrans))) { goto Exit; } (void) fdictGetNextIXD( pDb->pDict, *puiIndexNum, &pIxd); if( pIxd && pIxd->uiIndexNum < FLM_RESERVED_TAG_NUMS) { *puiIndexNum = pIxd->uiIndexNum; } else { rc = RC_SET( FERR_EOF_HIT); } Exit: if( bStartedAutoTrans) { rc = flmEndAutoTrans( pDb, rc); } flmExit( FLM_INDEX_GET_NEXT, pDb, rc); return( rc); }
/**************************************************************************** Desc: Thread that will build an index in the background. Caller will create a pDb to use. This pDb must be freed at the conclusion of the routine. ****************************************************************************/ FSTATIC RCODE FLMAPI flmBackgroundIndexBuildThrd( IF_Thread * pThread) { RCODE rc = FERR_OK; IXD * pIxd; F_BKGND_IX * pBackgroundIx = (F_BKGND_IX *)pThread->getParm1(); FLMBOOL bStartedTrans; FLMBOOL bDbInitialized; FLMUINT uiContainerNum; FLMUINT uiFirstDrn; FLMUINT uiIndexNum; FDB * pDb = NULL; FLMBOOL bForcedShutdown = FALSE; FLMBOOL bHitEnd; FINDEX_STATUS savedIxStatus; FlmRecord * pReusableRec = NULL; char szMsg[ 128]; FLMINT iErrorLine = 0; FLMBOOL bLimitedMode = FALSE; pThread->setThreadStatus( FLM_THREAD_STATUS_INITIALIZING); if( (pReusableRec = f_new FlmRecord) != NULL) { if( RC_BAD( pReusableRec->preallocSpace( 512, 1024 * 64))) { pReusableRec->Release(); pReusableRec = NULL; } } Loop_Again: rc = FERR_OK; uiIndexNum = pBackgroundIx->indexStatus.uiIndexNum; flmAssert( pThread->getThreadAppId() == uiIndexNum); bDbInitialized = FALSE; bStartedTrans = FALSE; pDb = NULL; // We could loop forever on flmOpenFile errors, check if we should exit. if( pThread->getShutdownFlag()) { bForcedShutdown = TRUE; goto Exit; } if( RC_BAD( rc = flmOpenFile( pBackgroundIx->pFile, NULL, NULL, NULL, 0, TRUE, NULL, NULL, pBackgroundIx->pFile->pszDbPassword, &pDb))) { // If the file is being closed, this is not an error. if( pBackgroundIx->pFile->uiFlags & DBF_BEING_CLOSED) { bForcedShutdown = TRUE; rc = FERR_OK; } else { iErrorLine = (FLMINT)__LINE__; } goto Exit; } flmAssert( pDb->pSFileHdl); bDbInitialized = TRUE; if (RC_BAD( rc = fdbInit( pDb, FLM_NO_TRANS, 0, 0, &bStartedTrans))) { iErrorLine = (FLMINT)__LINE__; goto Exit; } flmAssert( !bStartedTrans); pDb->uiFlags |= FDB_BACKGROUND_INDEXING; for(;;) { // Set the thread's status pThread->setThreadStatus( FLM_THREAD_STATUS_RUNNING); // See if we should shut down. if( pThread->getShutdownFlag()) { bForcedShutdown = TRUE; break; } // Obtain the file lock flmAssert( !(pDb->uiFlags & FDB_HAS_FILE_LOCK)); if( RC_BAD( rc = pDb->pFile->pFileLockObj->lock( pDb->hWaitSem, TRUE, FLM_NO_TIMEOUT, FLM_BACKGROUND_LOCK_PRIORITY, pDb->pDbStats ? &pDb->pDbStats->LockStats : NULL))) { if( rc == FERR_IO_FILE_LOCK_ERR) { // This would only happen if we were signaled to shut down. // So, it's ok to exit flmAssert( pThread->getShutdownFlag()); bForcedShutdown = TRUE; rc = FERR_OK; } else { iErrorLine = (FLMINT)__LINE__; } goto Exit; } // The lock needs to be marked as implicit so that flmCommitDbTrans // will unlock the file and allow the next update transaction to // begin before all writes are complete. pDb->uiFlags |= (FDB_HAS_FILE_LOCK | FDB_FILE_LOCK_IMPLICIT); // If there are higher priority waiters in the lock queue, // or we are being told to shut down, we want to relinquish. if( pThread->getShutdownFlag() || pDb->pFile->pFileLockObj->haveHigherPriorityWaiter( FLM_BACKGROUND_LOCK_PRIORITY)) { if (RC_BAD( rc = pDb->pFile->pFileLockObj->unlock())) { iErrorLine = (FLMINT)__LINE__; goto Exit; } pDb->uiFlags &= ~(FDB_HAS_FILE_LOCK | FDB_FILE_LOCK_IMPLICIT); continue; } // Start an update transaction if( RC_BAD( rc = flmBeginDbTrans( pDb, FLM_UPDATE_TRANS, FLM_NO_TIMEOUT, FLM_DONT_POISON_CACHE))) { if( rc == FERR_IO_FILE_LOCK_ERR) { // This would only happen if we were signaled to shut down. // So, it's ok to exit flmAssert( pThread->getShutdownFlag()); bForcedShutdown = TRUE; rc = FERR_OK; } else { iErrorLine = (FLMINT)__LINE__; } goto Exit; } bStartedTrans = TRUE; if( RC_BAD( rc = fdictGetIndex( pDb->pDict, pDb->pFile->bInLimitedMode, uiIndexNum, NULL, &pIxd, TRUE))) { // Index may have been deleted by another transaction, or // there may have been some other error. iErrorLine = (FLMINT)__LINE__; goto Exit; } // If we're running in limited mode, then we can't mess with encrypted // indexes. On the other hand, since the index is marked as offline, // but not suspended, this thread has to exist, or else it will cause // all kinds of problems elsewhere. So, in such a case, we will simply // sleep in an inifinite loop until the shutdown flag is set. // (We consider this acceptable becase running in limited mode is not // the norm, and Flaim probably won't be up for very long in this mode.) if( pDb->pFile->bInLimitedMode && pIxd->uiEncId) { bLimitedMode = TRUE; goto Exit; } // Look up the tracker info to determine where we need to being indexing if (RC_BAD( rc = flmGetIxTrackerInfo( pDb, pBackgroundIx->indexStatus.uiIndexNum, &uiContainerNum, &uiFirstDrn, NULL, &pBackgroundIx->indexStatus.bSuspended))) { iErrorLine = (FLMINT)__LINE__; goto Exit; } // If the index is suspended, this thread should have been // shut down. The suspending thread will keep the database // locked until we exit. So, if we have the database locked, // the index better not be suspended. flmAssert( !pBackgroundIx->indexStatus.bSuspended && !(pIxd->uiFlags & IXD_SUSPENDED)); if (pIxd->uiContainerNum) { uiContainerNum = pIxd->uiContainerNum; if( uiFirstDrn == DRN_LAST_MARKER) { goto Exit; } } else { if( uiFirstDrn == DRN_LAST_MARKER && uiContainerNum == 0xFFFFFFFF) { goto Exit; } else { // The container number from the tracker record // may not be a real container. // Determine what the next actual container number is. if (uiContainerNum != FLM_DATA_CONTAINER) { while( uiContainerNum < pDb->pDict->uiIttCnt) { ITT * pItt = &pDb->pDict->pIttTbl [uiContainerNum]; if (ITT_IS_CONTAINER( pItt)) { break; } else { uiContainerNum++; } } if (uiContainerNum >= pDb->pDict->uiIttCnt) { uiContainerNum = FLM_DATA_CONTAINER; } } } } // Setup the DRN range we want to index. uiFirstDrn++; flmAssert( pIxd->uiLastDrnIndexed == uiFirstDrn - 1); // Set the thread's status pThread->setThreadStatus( "Indexing %u:%u", (unsigned)uiContainerNum, (unsigned)uiFirstDrn); // Read and index up to the highest drn (or record higher than uiEndDrn) // or until time runs out. The 500 is millisecs to take for the transaction. f_memcpy( &savedIxStatus, &pBackgroundIx->indexStatus, sizeof( FINDEX_STATUS)); if( RC_BAD( rc = flmIndexSetOfRecords( pDb, uiIndexNum, uiContainerNum, uiFirstDrn, DRN_LAST_MARKER, NULL, NULL, NULL, NULL, &pBackgroundIx->indexStatus, &bHitEnd, pThread, pReusableRec))) { // Lock the mutex while copying the saved index status back to // the main index status so that someone requesting the index status // won't see the status while the memcpy is in progress. f_mutexLock( gv_FlmSysData.hShareMutex); f_memcpy( &pBackgroundIx->indexStatus, &savedIxStatus, sizeof( FINDEX_STATUS)); f_mutexUnlock( gv_FlmSysData.hShareMutex); iErrorLine = (FLMINT)__LINE__; goto Exit; } if( pBackgroundIx->indexStatus.uiRecordsProcessed - savedIxStatus.uiRecordsProcessed) { if( RC_BAD( rc = pDb->pFile->pRfl->logIndexSet( uiIndexNum, uiContainerNum, uiFirstDrn, pBackgroundIx->indexStatus.uiLastRecordIdIndexed))) { iErrorLine = (FLMINT)__LINE__; goto Exit; } } // Commit the transaction (even if we didn't do any indexing work). if( RC_BAD( rc = flmCommitDbTrans( pDb, 0, FALSE))) { iErrorLine = (FLMINT)__LINE__; goto Exit; } bStartedTrans = FALSE; pBackgroundIx->indexStatus.uiTransactions++; if( bHitEnd) { // flmIndexSetOfRecords brought the index on-line if( gv_FlmSysData.UpdateEvents.pEventCBList) { flmDoEventCallback( F_EVENT_UPDATES, F_EVENT_INDEXING_COMPLETE, (void *)uiIndexNum, (void *)0); } // Log a message flmLogIndexingProgress( uiIndexNum, 0); break; } } Exit: pThread->setThreadStatus( FLM_THREAD_STATUS_TERMINATING); if( bStartedTrans) { (void)flmAbortDbTrans( pDb); bStartedTrans = FALSE; } if( pDb && pDb->uiFlags & FDB_HAS_FILE_LOCK) { (void)pDb->pFile->pFileLockObj->unlock(); pDb->uiFlags &= ~(FDB_HAS_FILE_LOCK | FDB_FILE_LOCK_IMPLICIT); } if( bDbInitialized) { fdbExit( pDb); bDbInitialized = FALSE; } if( pDb) { (void)FlmDbClose( (HFDB *) &pDb); } if( RC_BAD(rc) && !bForcedShutdown) { if (rc == FERR_MEM || rc == FERR_IO_DISK_FULL || rc == FERR_MUST_WAIT_CHECKPOINT) { // Log the error f_sprintf( (char *)szMsg, "Background indexing thread %u (index %u)", (unsigned)pThread->getThreadId(), (unsigned)uiIndexNum); flmLogError( rc, (char *)szMsg, __FILE__, iErrorLine); // Sleep a half second and try again. pThread->sleep( 500); goto Loop_Again; } else { f_sprintf( (char *)szMsg, "Background indexing thread %u (index %u) -- unrecoverable error.", (unsigned)pThread->getThreadId(), (unsigned)uiIndexNum); flmLogError( rc, (char *)szMsg, __FILE__, iErrorLine); } } if( pReusableRec) { flmAssert( pReusableRec->getRefCount() == 1); pReusableRec->Release(); } if( bLimitedMode) { flmAssert( RC_OK( rc)); for (;;) { if( pThread->getShutdownFlag()) { break; } pThread->sleep( 1000); } } // Set the thread's app ID to 0, so that it will not // be found now that the thread is terminating (we don't // want flmBackgroundIndexGet to find the thread). pThread->setThreadAppId( 0); // Free the background index structure f_mutexLock( gv_FlmSysData.hShareMutex); f_free( &pBackgroundIx); pThread->setParm1( NULL); f_mutexUnlock( gv_FlmSysData.hShareMutex); return( rc); }
/**************************************************************************** Desc : Deletes a record from a container. Notes: If an index definition record or a container definition record is deleted from the dictionary container, the index B-TREE container or container B-TREE will be deleted automatically when the transaction commits. Field definition records can only be deleted from the dictionary using this routine if the field is not in use. For more information on deletion of field definitions, see the Dictionary Syntax document. ****************************************************************************/ FLMEXP RCODE FLMAPI FlmRecordDelete( HFDB hDb, FLMUINT uiContainer, FLMUINT uiDrn, FLMUINT uiAutoTrans ) { RCODE rc = FERR_OK; LFILE * pLFile; FDB * pDb = (FDB *)hDb; FLMBOOL bStartedAutoTrans; FlmRecord * pOldRecord; FlmRecord ** ppOldRecord; DB_STATS * pDbStats = NULL; F_TMSTAMP StartTime; if( uiContainer == FLM_TRACKER_CONTAINER) { rc = RC_SET( FERR_ILLEGAL_OP); goto Exit; } if (IsInCSMode( hDb)) { fdbInitCS( pDb); rc = flmDoUpdateCS( pDb, FCS_OP_RECORD_DELETE, uiContainer, &uiDrn, NULL, uiAutoTrans); goto ExitCS; } bStartedAutoTrans = FALSE; pOldRecord = NULL; ppOldRecord = NULL; if( RC_BAD( rc = fdbInit( pDb, FLM_UPDATE_TRANS, FDB_TRANS_GOING_OK, uiAutoTrans, &bStartedAutoTrans))) { goto Exit; } if( (pDbStats = pDb->pDbStats) != NULL) { f_timeGetTimeStamp( &StartTime); } if( !uiDrn || uiDrn == (FLMUINT) DRN_LAST_MARKER) { rc = RC_SET( FERR_BAD_DRN); goto Exit; } if( RC_BAD(rc = fdictGetContainer( pDb->pDict, uiContainer, &pLFile))) { goto Exit; } if( gv_FlmSysData.UpdateEvents.pEventCBList) { // Do not have flmDeleteRecord fetch the old version of the record // unless an event callback is registered. ppOldRecord = &pOldRecord; } // NOTE: pLFile should NOT be used after this call, because flmDeleteRecord // could actually change its position in memory due to field changes. if (RC_BAD( rc = flmDeleteRecord( pDb, pLFile, uiDrn, ppOldRecord, FALSE))) { goto Exit; } Exit: if( RC_OK( rc)) { rc = pDb->pFile->pRfl->logUpdate( uiContainer, uiDrn, uiAutoTrans, NULL, NULL); } if( pDbStats) { flmAddElapTime( &StartTime, &pDbStats->RecordDeletes.ui64ElapMilli); pDbStats->RecordDeletes.ui64Count++; pDbStats->bHaveStats = TRUE; } if( gv_FlmSysData.UpdateEvents.pEventCBList) { flmUpdEventCallback( pDb, F_EVENT_DELETE_RECORD, hDb, rc, uiDrn, uiContainer, NULL, pOldRecord); } #ifdef FLM_DBG_LOG flmDbgLogUpdate( pDb->pFile->uiFFileId, pDb->LogHdr.uiCurrTransID, uiContainer, uiDrn, rc, "RDel"); #endif // If started an automatic transaction, end it. if( bStartedAutoTrans) { rc = flmEndAutoTrans( pDb, rc); } if( pOldRecord) { pOldRecord->Release(); pOldRecord = NULL; } ExitCS: flmExit( FLM_RECORD_DELETE, pDb, rc); return( rc); }
/**************************************************************************** Desc : Modifies a record within a container. Notes: If an index definition record is modified in the dictionary container, the index B-TREE will be deleted and rebuilt automatically when the transaction commits. If an index definition record is changed into a field definition record or a container definition record, the index will be automatically deleted when the transaction commits. When a non-unique index is changed to a unique index, or the fields in a unique index are changed, FLAIM needs to verify that each key in the proposed index is indeed a unique key. However, this verification does NOT occur until the dictionary transaction commits and the index is actually rebuilt. If FLAIM discovers that the keys in the proposed index are not unique, the transaction commit will fail and return FERR_NOT_UNIQUE. If a container definition record is changed into a field definition record, the container will be deleted automatically when the transaction commits. If a container definition record is changed into an index definition record, the container will be automatically deleted and the index will be automatically built when the transaction commits. Only the name and state of field definition records can be modified. Changing a field type or changing a field definition record into an index definition record is not allowed. For information on changing the state of a field, see the Dictionary Syntax document. ****************************************************************************/ FLMEXP RCODE FLMAPI FlmRecordModify( HFDB hDb, FLMUINT uiContainer, FLMUINT uiDrn, FlmRecord * pRecord, FLMUINT uiAutoTrans ) { RCODE rc = FERR_OK; RCODE rc1; FDB * pDb = (FDB *)hDb; FlmRecord * pOldRecord = NULL; LFILE * pLFile; FLMBOOL bStartedAutoTrans = FALSE; FLMBOOL bProcessedKeys = FALSE; FLMBOOL bLogCompleteIndexSet = FALSE; FLMBOOL bHadUniqueKeys; DB_STATS * pDbStats = NULL; F_TMSTAMP StartTime; if( uiContainer == FLM_TRACKER_CONTAINER) { rc = RC_SET( FERR_ILLEGAL_OP); goto Exit; } if (IsInCSMode( hDb)) { fdbInitCS( pDb); rc = flmDoUpdateCS( pDb, FCS_OP_RECORD_MODIFY, uiContainer, &uiDrn, pRecord, uiAutoTrans); goto ExitCS; } if( RC_BAD( rc = fdbInit( pDb, FLM_UPDATE_TRANS, FDB_TRANS_GOING_OK, uiAutoTrans, &bStartedAutoTrans))) { goto Exit; } if( pDb->uiFlags & FDB_COMMITTING_TRANS) { flmAssert( 0); rc = RC_SET( FERR_ILLEGAL_TRANS_OP); goto Exit; } if ((pDbStats = pDb->pDbStats) != NULL) { f_timeGetTimeStamp( &StartTime); } // Make sure we have a valid record if( !pRecord) { rc = RC_SET( FERR_INVALID_PARM); goto Exit; } // We cannot modify a record that is marked read-only. if( pRecord->isReadOnly()) { flmAssert( 0); rc = RC_SET( FERR_ILLEGAL_OP); goto Exit; } if( !uiDrn || (uiDrn == (FLMUINT) DRN_LAST_MARKER)) { rc = RC_SET( FERR_BAD_DRN); goto Exit; } if( RC_BAD( rc = fdictGetContainer( pDb->pDict, uiContainer, &pLFile))) { goto Exit; } if( RC_BAD( rc = KrefCntrlCheck( pDb))) { goto Exit; } // DICTIONARY RECORD MODIFY if( uiContainer == FLM_LOCAL_DICT_CONTAINER) { if( RC_BAD( rc = flmRcaRetrieveRec( pDb, NULL, uiContainer, uiDrn, FALSE, NULL, NULL, &pOldRecord))) { // NOTE: Deliberately not reading in to cache if not found. if (rc != FERR_NOT_FOUND) { goto Exit; } if( RC_BAD( rc = FSReadRecord( pDb, pLFile, uiDrn, &pOldRecord, NULL, NULL))) { goto Exit; } } // Sanity check -- make sure that the new and old records point at // different objects. flmAssert( pRecord != pOldRecord); if( RC_BAD( rc = flmLFileDictUpdate( pDb, &pLFile, &uiDrn, pRecord, pOldRecord, (uiAutoTrans & FLM_DO_IN_BACKGROUND) ? TRUE : FALSE, (uiAutoTrans & FLM_SUSPENDED) ? TRUE : FALSE, &bLogCompleteIndexSet))) { goto Exit; } pRecord->setID( uiDrn); pRecord->setContainerID( uiContainer); if( RC_BAD( rc = flmRcaInsertRec( pDb, pLFile, uiDrn, pRecord))) { goto Exit; } goto Exit; } // First read the old record, and delete it's keys. // To do this we need to be able to read any purged fields and delete any // keys they build. if( RC_BAD( rc = flmRcaRetrieveRec( pDb, NULL, uiContainer, uiDrn, FALSE, NULL, NULL, &pOldRecord))) { // NOTE: Deliberately not reading in to cache if not found. if (rc != FERR_NOT_FOUND) { goto Exit; } if( RC_BAD( rc = FSReadRecord( pDb, pLFile, uiDrn, &pOldRecord, NULL, NULL))) { goto Exit; } } // Sanity check -- make sure that the new and old records point at // different objects. flmAssert( pRecord != pOldRecord); bProcessedKeys = TRUE; bHadUniqueKeys = FALSE; if( RC_BAD( rc = flmProcessRecFlds( pDb, NULL, uiContainer, uiDrn, pOldRecord, KREF_DEL_KEYS | KREF_IN_MODIFY, TRUE, // Purged Field OK &bHadUniqueKeys))) { goto Exit; } if( RC_BAD( rc = flmProcessRecFlds( pDb, NULL, uiContainer, uiDrn, pRecord, KREF_ADD_KEYS | KREF_IN_MODIFY, FALSE, &bHadUniqueKeys))) { goto Exit; } // NOTE: The LFile table may have changed locations if the dictionary // was update because of a change in a field state if( RC_BAD( rc = fdictGetContainer( pDb->pDict, uiContainer, &pLFile))) { goto Exit; } if( RC_BAD( rc = FSRecUpdate( pDb, pLFile, pRecord, uiDrn, REC_UPD_MODIFY))) { goto Exit; } // Finish up the keys adding the unique keys to the indexes. if( RC_BAD( rc = KYProcessDupKeys( pDb, bHadUniqueKeys))) { // Undo the record that was modified - replace with original record. if( RC_BAD( rc1 = FSRecUpdate( pDb, pLFile, pOldRecord, uiDrn, REC_UPD_MODIFY))) { rc = (rc == FERR_NOT_UNIQUE) ? rc1 : rc; } goto Exit; } // Insert record into cache pRecord->setID( uiDrn); pRecord->setContainerID( uiContainer); if( RC_BAD( rc = flmRcaInsertRec( pDb, pLFile, uiDrn, pRecord))) { if ( rc != FERR_MEM) { flmAssert( 0); } // Undo the record that was modified - replace with original record. FSRecUpdate( pDb, pLFile, pOldRecord, uiDrn, REC_UPD_MODIFY); goto Exit; } // Don't make this call until we are sure of success - because we want to // be able to back things out of KREF table. KYFinishCurrentRecord( pDb); Exit: if( RC_BAD( rc) && bProcessedKeys) { KYAbortCurrentRecord( pDb); } // Add the BLOB entries to the blob list. rc = FB_OperationEnd( pDb, rc); if( RC_OK( rc)) { if( RC_OK( rc = pDb->pFile->pRfl->logUpdate( uiContainer, uiDrn, uiAutoTrans, pOldRecord, pRecord)) && bLogCompleteIndexSet && pDb->pFile->FileHdr.uiVersionNum <= FLM_FILE_FORMAT_VER_4_51) { // Log the fact that we indexed everything so the redo will also // index all data records in the container. rc = pDb->pFile->pRfl->logIndexSet( uiDrn, 0, 1, 0xFFFFFFFF); } } if( pDbStats) { flmAddElapTime( &StartTime, &pDbStats->RecordModifies.ui64ElapMilli); pDbStats->RecordModifies.ui64Count++; pDbStats->bHaveStats = TRUE; } if( gv_FlmSysData.UpdateEvents.pEventCBList) { flmUpdEventCallback( pDb, F_EVENT_MODIFY_RECORD, hDb, rc, uiDrn, uiContainer, pRecord, pOldRecord); } #ifdef FLM_DBG_LOG flmDbgLogUpdate( pDb->pFile->uiFFileId, pDb->LogHdr.uiCurrTransID, uiContainer, uiDrn, rc, "RMod"); #endif // If started an automatic transaction, end it. if( bStartedAutoTrans) { rc = flmEndAutoTrans( pDb, rc); } if( pOldRecord) { pOldRecord->Release(); pOldRecord = NULL; } ExitCS: flmExit( FLM_RECORD_MODIFY, pDb, rc); return( rc); }
/**************************************************************************** Desc : Adds a record to a container. Notes: If an index definition record is added to the dictionary container, the index will be built automatically when the transaction commits. When a unique index is added to the database, FLAIM needs to verify that each key in the proposed index is indeed a unique key. However, this verification does NOT occur until the dictionary transaction commits and the index is actually built. If FLAIM discovers that the keys in an index are not unique, the transaction commit will fail and return an error. ****************************************************************************/ FLMEXP RCODE FLMAPI FlmRecordAdd( HFDB hDb, FLMUINT uiContainer, FLMUINT * puiDrn, FlmRecord * pRecord, FLMUINT uiAutoTrans) { RCODE rc = FERR_OK; FLMUINT uiDrn = 0; FDB * pDb = (FDB *)hDb; LFILE * pLFile; FLMBOOL bStartedAutoTrans = FALSE; FLMBOOL bLogCompleteIndexSet = FALSE; DB_STATS * pDbStats = NULL; F_TMSTAMP StartTime; if( puiDrn) { uiDrn = *puiDrn; } if( uiContainer == FLM_TRACKER_CONTAINER) { rc = RC_SET( FERR_ILLEGAL_OP); goto Exit; } if (IsInCSMode( hDb)) { fdbInitCS( pDb); rc = flmDoUpdateCS( pDb, FCS_OP_RECORD_ADD, uiContainer, &uiDrn, pRecord, uiAutoTrans); goto ExitCS; } if( RC_BAD( rc = fdbInit( (FDB *)hDb, FLM_UPDATE_TRANS, FDB_TRANS_GOING_OK, uiAutoTrans, &bStartedAutoTrans))) { goto Exit; } if( (pDbStats = pDb->pDbStats) != NULL) { f_timeGetTimeStamp( &StartTime); } // Make sure we have a valid record if( !pRecord) { rc = RC_SET( FERR_INVALID_PARM); goto Exit; } // We cannot add a record that is marked read-only, because it // is probably already in the cache under a different record ID. if( pRecord->isReadOnly()) { flmAssert( 0); rc = RC_SET( FERR_ILLEGAL_OP); goto Exit; } if( RC_BAD( rc = fdictGetContainer( pDb->pDict, uiContainer, &pLFile))) { goto Exit; } rc = flmAddRecord( pDb, pLFile, &uiDrn, pRecord, FALSE, (uiAutoTrans & FLM_DO_IN_BACKGROUND) ? TRUE : FALSE, (uiAutoTrans & FLM_SUSPENDED) ? TRUE : FALSE, (FLMBOOL)((uiAutoTrans & FLM_DONT_INSERT_IN_CACHE) ? FALSE : TRUE), &bLogCompleteIndexSet); Exit: rc = FB_OperationEnd( pDb, rc); if( RC_OK( rc)) { if( RC_OK( rc = pDb->pFile->pRfl->logUpdate( uiContainer, uiDrn, uiAutoTrans, NULL, pRecord)) && bLogCompleteIndexSet && pDb->pFile->FileHdr.uiVersionNum <= FLM_FILE_FORMAT_VER_4_51) { // Log the fact that we indexed everything so the redo will also // index all data records in the container. rc = pDb->pFile->pRfl->logIndexSet( uiDrn, 0, 1, 0xFFFFFFFF); } } if( pDbStats) { flmAddElapTime( &StartTime, &pDbStats->RecordAdds.ui64ElapMilli); pDbStats->RecordAdds.ui64Count++; pDbStats->bHaveStats = TRUE; } if( gv_FlmSysData.UpdateEvents.pEventCBList) { flmUpdEventCallback( pDb, F_EVENT_ADD_RECORD, hDb, rc, uiDrn, uiContainer, pRecord, NULL); } // If started an automatic transaction end it. if( bStartedAutoTrans) { rc = flmEndAutoTrans( pDb, rc); } ExitCS: if( puiDrn) { *puiDrn = uiDrn; } flmExit( FLM_RECORD_ADD, pDb, rc); return( rc); }
/**************************************************************************** Desc: Returns the next DRN that record ADD would return. The database must be in an existing update transaction. ****************************************************************************/ FLMEXP RCODE FLMAPI FlmReserveNextDrn( HFDB hDb, FLMUINT uiContainer, FLMUINT * puiDrnRV) { RCODE rc; FDB * pDb = (FDB *)hDb; LFILE * pLFile; FLMBOOL bIgnore; FLMUINT uiDrn = 0; if (IsInCSMode( hDb)) { fdbInitCS( pDb); CS_CONTEXT * pCSContext = pDb->pCSContext; FCL_WIRE Wire( pCSContext, pDb); // Send the request if( RC_BAD( rc = Wire.sendOp( FCS_OPCLASS_RECORD, FCS_OP_RESERVE_NEXT_DRN))) { goto ExitCS; } if( uiContainer) { if (RC_BAD( rc = Wire.sendNumber( WIRE_VALUE_CONTAINER_ID, uiContainer))) { goto Transmission_Error; } } if( RC_BAD( rc = Wire.sendTerminate())) { goto Transmission_Error; } // Read the response if( RC_BAD( rc = Wire.read())) { goto Transmission_Error; } if( RC_BAD( rc = Wire.getRCode())) { goto ExitCS; } *puiDrnRV = Wire.getDrn(); goto ExitCS; Transmission_Error: pCSContext->bConnectionGood = FALSE; goto ExitCS; } bIgnore = FALSE; // Set to shut up compiler. if( RC_BAD( rc = fdbInit( pDb, FLM_UPDATE_TRANS, FDB_TRANS_GOING_OK, // byFlags 0, // wAutoTrans &bIgnore))) // bStartedAutoTrans { goto Exit; } if( pDb->uiFlags & FDB_COMMITTING_TRANS) { flmAssert( 0); rc = RC_SET( FERR_ILLEGAL_TRANS_OP); goto Exit; } if( RC_BAD( fdictGetContainer( pDb->pDict, uiContainer, &pLFile))) { #ifdef FLM_DBG_LOG uiDrn = 0; #endif goto Exit; } uiDrn = (FLMUINT) 0; // Must initialize before call. if( RC_BAD( rc = FSGetNextDrn( pDb, pLFile, TRUE, &uiDrn))) { #ifdef FLM_DBG_LOG uiDrn = 0; #endif goto Exit; } *puiDrnRV = uiDrn; // Set return value. Exit: if (RC_OK( rc)) { rc = pDb->pFile->pRfl->logUpdatePacket( RFL_RESERVE_DRN_PACKET, uiContainer, *puiDrnRV, 0); } if( gv_FlmSysData.UpdateEvents.pEventCBList) { flmUpdEventCallback( pDb, F_EVENT_RESERVE_DRN, hDb, rc, *puiDrnRV, uiContainer, NULL, NULL); } #ifdef FLM_DBG_LOG flmDbgLogUpdate( pDb->pFile->uiFFileId, pDb->LogHdr.uiCurrTransID, uiContainer, uiDrn, rc, "RDrn"); #endif ExitCS: flmExit( FLM_RESERVE_NEXT_DRN, pDb, rc); return( rc); }
/**************************************************************************** Desc: Forces a checkpoint on the database. ****************************************************************************/ FLMEXP RCODE FLMAPI FlmDbCheckpoint( HFDB hDb, FLMUINT uiTimeout) { RCODE rc = FERR_OK; FDB * pDb = (FDB *)hDb; FLMBOOL bStartedTrans; bStartedTrans = FALSE; if (IsInCSMode( hDb)) { fdbInitCS( pDb); CS_CONTEXT * pCSContext = pDb->pCSContext; FCL_WIRE Wire( pCSContext, pDb); if( !pCSContext->bConnectionGood) { rc = RC_SET( FERR_BAD_SERVER_CONNECTION); goto Transmission_Error; } if( RC_BAD( rc = Wire.sendOp( FCS_OPCLASS_DATABASE, FCS_OP_DATABASE_CHECKPOINT))) { goto Exit; } if (RC_BAD( rc = Wire.sendNumber( WIRE_VALUE_FLAGS, uiTimeout))) { goto Transmission_Error; } if( RC_BAD( rc = Wire.sendTerminate())) { goto Transmission_Error; } // Read the response if (RC_BAD( rc = Wire.read())) { goto Transmission_Error; } if( RC_BAD( rc = Wire.getRCode())) { goto Exit; } goto Exit; Transmission_Error: pCSContext->bConnectionGood = FALSE; goto Exit; } // Start an update transaction. Must not already be one going. if (RC_BAD( rc = fdbInit( pDb, FLM_UPDATE_TRANS, 0, uiTimeout | FLM_AUTO_TRANS, &bStartedTrans))) { goto Exit; } // Commit the transaction, forcing it to be checkpointed. bStartedTrans = FALSE; pDb->bHadUpdOper = FALSE; if (RC_BAD( rc = flmCommitDbTrans( pDb, 0, TRUE))) { goto Exit; } Exit: if (bStartedTrans) { (void)flmAbortDbTrans( pDb); } flmExit( FLM_DB_CHECKPOINT, pDb, rc); return( rc); }
/**************************************************************************** Desc: Retrieves the last commit sequence number of a database. Notes: Whenever a transaction is committed, FLAIM increments the commit sequence number to indicate that the database has been modified. An application may use this routine to determine if the database has been modified. ****************************************************************************/ FLMEXP RCODE FLMAPI FlmDbGetCommitCnt( HFDB hDb, FLMUINT * puiCommitCount) { RCODE rc = FERR_OK; FDB * pDb = (FDB *)hDb; FLMBOOL bIgnore; if (IsInCSMode( hDb)) { fdbInitCS( pDb); CS_CONTEXT * pCSContext = pDb->pCSContext; FCL_WIRE Wire( pCSContext, pDb); // Send a request to get the commit count if (RC_BAD( rc = Wire.sendOp( FCS_OPCLASS_DATABASE, FCS_OP_GET_COMMIT_CNT))) { goto Exit; } if (RC_BAD( rc = Wire.sendTerminate())) { goto Transmission_Error; } // Read the response. if (RC_BAD( rc = Wire.read())) { goto Transmission_Error; } *puiCommitCount = (FLMUINT)Wire.getCount(); rc = Wire.getRCode(); goto ExitCS; Transmission_Error: pCSContext->bConnectionGood = FALSE; goto ExitCS; } if (RC_BAD( rc = fdbInit( pDb, FLM_NO_TRANS, FDB_TRANS_GOING_OK, 0, &bIgnore))) { goto Exit; } // See if we have a transaction going which should be aborted. if (pDb->uiTransType != FLM_NO_TRANS) { if (flmCheckBadTrans( pDb)) { rc = RC_SET( FERR_ABORT_TRANS); goto Exit; } } f_mutexLock( gv_FlmSysData.hShareMutex); *puiCommitCount = (FLMUINT)FB2UD( &pDb->pFile->ucLastCommittedLogHdr [LOG_COMMIT_COUNT]); f_mutexUnlock( gv_FlmSysData.hShareMutex); Exit: ExitCS: flmExit( FLM_DB_GET_COMMIT_CNT, pDb, rc); return( rc); }
/**************************************************************************** Desc: Starts a transaction. ****************************************************************************/ FLMEXP RCODE FLMAPI FlmDbTransBegin( HFDB hDb, FLMUINT uiTransType, FLMUINT uiMaxLockWait, FLMBYTE * pucHeader) { RCODE rc = FERR_OK; FLMBOOL bIgnore; FLMUINT uiFlags = FLM_GET_TRANS_FLAGS( uiTransType); FDB * pDb = (FDB *)hDb; uiTransType = FLM_GET_TRANS_TYPE( uiTransType); if (IsInCSMode( hDb)) { fdbInitCS( pDb); FCL_WIRE Wire( pDb->pCSContext, pDb); if (!pDb->pCSContext->bConnectionGood) { rc = RC_SET( FERR_BAD_SERVER_CONNECTION); } else { if( RC_BAD( rc = Wire.doTransOp( FCS_OP_TRANSACTION_BEGIN, uiTransType, uiFlags, uiMaxLockWait, pucHeader))) { goto Exit; } } goto Exit; } if (RC_BAD( rc = fdbInit( pDb, FLM_NO_TRANS, FDB_TRANS_GOING_OK, 0, &bIgnore))) { goto Exit; } // Verify the transaction type. if (( uiTransType != FLM_UPDATE_TRANS) && ( uiTransType != FLM_READ_TRANS)) { rc = RC_SET( FERR_ILLEGAL_TRANS); goto Exit; } // Verify the transaction flags if( (uiFlags & FLM_DONT_KILL_TRANS) && uiTransType != FLM_READ_TRANS) { rc = RC_SET( FERR_ILLEGAL_TRANS); goto Exit; } // Can't start an update transaction on a database that // is locked in shared mode. if ((uiTransType == FLM_UPDATE_TRANS) && (pDb->uiFlags & FDB_FILE_LOCK_SHARED)) { rc = RC_SET( FERR_PERMISSION); goto Exit; } // If the database has an invisible transaction going, abort it // before going any further - we don't want application transactions // to be nested under invisible transactions. Application transactions // take precedence over invisible transactions. if ((pDb->uiTransType != FLM_NO_TRANS) && (pDb->uiFlags & FDB_INVISIBLE_TRANS)) { if (RC_BAD( rc = flmAbortDbTrans( pDb))) { goto Exit; } } // If the database is not running a transaction, start one. // Otherwise, start a nested transaction - first verifying that // the transation type matches. if (pDb->uiTransType == FLM_NO_TRANS) { FLMUINT uiBytesRead; if( pucHeader) { if( RC_BAD( rc = pDb->pSFileHdl->readBlock( 0, 2048, pucHeader, &uiBytesRead))) { goto Exit; } } if (RC_BAD( rc = flmBeginDbTrans( pDb, uiTransType, uiMaxLockWait, uiFlags, pucHeader ? &pucHeader [16] : NULL))) { goto Exit; } pDb->bHadUpdOper = FALSE; } else { // Cannot nest transactions. rc = RC_SET( FERR_TRANS_ACTIVE); goto Exit; } Exit: flmExit( FLM_DB_TRANS_BEGIN, pDb, rc); return( rc); }