/* ** Write a new value into the database. */ int lsm_write(lsm_db *pDb, void *pKey, int nKey, void *pVal, int nVal){ int rc = LSM_OK; /* Return code */ int bCommit = 0; /* True to commit before returning */ if( pDb->nTransOpen==0 ){ bCommit = 1; rc = lsm_begin(pDb, 1); } if( rc==LSM_OK ){ assert( pDb->pTV && lsmTreeIsWriteVersion(pDb->pTV) ); rc = lsmLogWrite(pDb, pKey, nKey, pVal, nVal); } lsmSortedSaveTreeCursors(pDb); if( rc==LSM_OK ){ int pgsz = lsmFsPageSize(pDb->pFS); int nQuant = 32 * pgsz; int nBefore; int nAfter; int nDiff; if( nQuant>pDb->nTreeLimit ){ nQuant = pDb->nTreeLimit; } nBefore = lsmTreeSize(pDb->pTV); rc = lsmTreeInsert(pDb, pKey, nKey, pVal, nVal); nAfter = lsmTreeSize(pDb->pTV); nDiff = (nAfter/nQuant) - (nBefore/nQuant); if( rc==LSM_OK && pDb->bAutowork && nDiff!=0 ){ rc = dbAutoWork(pDb, nDiff*nQuant / pgsz); } } /* If a transaction was opened at the start of this function, commit it. ** Or, if an error has occurred, roll it back. */ if( bCommit ){ if( rc==LSM_OK ){ rc = lsm_commit(pDb, 0); }else{ lsm_rollback(pDb, 0); } } return rc; }
static int doWriteOp( lsm_db *pDb, int bDeleteRange, const void *pKey, int nKey, /* Key to write or delete */ const void *pVal, int nVal /* Value to write. Or nVal==-1 for a delete */ ){ int rc = LSM_OK; /* Return code */ int bCommit = 0; /* True to commit before returning */ if( pDb->nTransOpen==0 ){ bCommit = 1; rc = lsm_begin(pDb, 1); } if( rc==LSM_OK ){ if( bDeleteRange==0 ){ rc = lsmLogWrite(pDb, (void *)pKey, nKey, (void *)pVal, nVal); }else{ /* TODO */ } } lsmSortedSaveTreeCursors(pDb); if( rc==LSM_OK ){ int pgsz = lsmFsPageSize(pDb->pFS); int nQuant = LSM_AUTOWORK_QUANT * pgsz; int nBefore; int nAfter; int nDiff; if( nQuant>pDb->nTreeLimit ){ nQuant = pDb->nTreeLimit; } nBefore = lsmTreeSize(pDb); if( bDeleteRange ){ rc = lsmTreeDelete(pDb, (void *)pKey, nKey, (void *)pVal, nVal); }else{ rc = lsmTreeInsert(pDb, (void *)pKey, nKey, (void *)pVal, nVal); } nAfter = lsmTreeSize(pDb); nDiff = (nAfter/nQuant) - (nBefore/nQuant); if( rc==LSM_OK && pDb->bAutowork && nDiff!=0 ){ rc = lsmSortedAutoWork(pDb, nDiff * LSM_AUTOWORK_QUANT); } } /* If a transaction was opened at the start of this function, commit it. ** Or, if an error has occurred, roll it back. */ if( bCommit ){ if( rc==LSM_OK ){ rc = lsm_commit(pDb, 0); }else{ lsm_rollback(pDb, 0); } } return rc; }
int lsm_config(lsm_db *pDb, int eParam, ...){ int rc = LSM_OK; va_list ap; va_start(ap, eParam); switch( eParam ){ case LSM_CONFIG_AUTOFLUSH: { /* This parameter is read and written in KB. But all internal ** processing is done in bytes. */ int *piVal = va_arg(ap, int *); int iVal = *piVal; if( iVal>=0 && iVal<=(1024*1024) ){ pDb->nTreeLimit = iVal*1024; } *piVal = (pDb->nTreeLimit / 1024); break; } case LSM_CONFIG_AUTOWORK: { int *piVal = va_arg(ap, int *); if( *piVal>=0 ){ pDb->bAutowork = *piVal; } *piVal = pDb->bAutowork; break; } case LSM_CONFIG_AUTOCHECKPOINT: { /* This parameter is read and written in KB. But all internal processing ** (including the lsm_db.nAutockpt variable) is done in bytes. */ int *piVal = va_arg(ap, int *); if( *piVal>=0 ){ int iVal = *piVal; pDb->nAutockpt = (i64)iVal * 1024; } *piVal = (int)(pDb->nAutockpt / 1024); break; } case LSM_CONFIG_PAGE_SIZE: { int *piVal = va_arg(ap, int *); if( pDb->pDatabase ){ /* If lsm_open() has been called, this is a read-only parameter. ** Set the output variable to the page-size according to the ** FileSystem object. */ *piVal = lsmFsPageSize(pDb->pFS); }else{ if( *piVal>=256 && *piVal<=65536 && ((*piVal-1) & *piVal)==0 ){ pDb->nDfltPgsz = *piVal; }else{ *piVal = pDb->nDfltPgsz; } } break; } case LSM_CONFIG_BLOCK_SIZE: { /* This parameter is read and written in KB. But all internal ** processing is done in bytes. */ int *piVal = va_arg(ap, int *); if( pDb->pDatabase ){ /* If lsm_open() has been called, this is a read-only parameter. ** Set the output variable to the block-size in KB according to the ** FileSystem object. */ *piVal = lsmFsBlockSize(pDb->pFS) / 1024; }else{ int iVal = *piVal; if( iVal>=64 && iVal<=65536 && ((iVal-1) & iVal)==0 ){ pDb->nDfltBlksz = iVal * 1024; }else{ *piVal = pDb->nDfltBlksz / 1024; } } break; } case LSM_CONFIG_SAFETY: { int *piVal = va_arg(ap, int *); if( *piVal>=0 && *piVal<=2 ){ pDb->eSafety = *piVal; } *piVal = pDb->eSafety; break; } case LSM_CONFIG_MMAP: { int *piVal = va_arg(ap, int *); if( pDb->iReader<0 && *piVal>=0 ){ pDb->iMmap = *piVal; rc = lsmFsConfigure(pDb); } *piVal = pDb->iMmap; break; } case LSM_CONFIG_USE_LOG: { int *piVal = va_arg(ap, int *); if( pDb->nTransOpen==0 && (*piVal==0 || *piVal==1) ){ pDb->bUseLog = *piVal; } *piVal = pDb->bUseLog; break; } case LSM_CONFIG_AUTOMERGE: { int *piVal = va_arg(ap, int *); if( *piVal>1 ) pDb->nMerge = *piVal; *piVal = pDb->nMerge; break; } case LSM_CONFIG_MAX_FREELIST: { int *piVal = va_arg(ap, int *); if( *piVal>=2 && *piVal<=LSM_MAX_FREELIST_ENTRIES ){ pDb->nMaxFreelist = *piVal; } *piVal = pDb->nMaxFreelist; break; } case LSM_CONFIG_MULTIPLE_PROCESSES: { int *piVal = va_arg(ap, int *); if( pDb->pDatabase ){ /* If lsm_open() has been called, this is a read-only parameter. ** Set the output variable to true if this connection is currently ** in multi-process mode. */ *piVal = lsmDbMultiProc(pDb); }else{ pDb->bMultiProc = *piVal = (*piVal!=0); } break; } case LSM_CONFIG_READONLY: { int *piVal = va_arg(ap, int *); /* If lsm_open() has been called, this is a read-only parameter. */ if( pDb->pDatabase==0 && *piVal>=0 ){ pDb->bReadonly = *piVal = (*piVal!=0); } *piVal = pDb->bReadonly; break; } case LSM_CONFIG_SET_COMPRESSION: { lsm_compress *p = va_arg(ap, lsm_compress *); if( pDb->iReader>=0 && pDb->bInFactory==0 ){ /* May not change compression schemes with an open transaction */ rc = LSM_MISUSE_BKPT; }else{ if( pDb->compress.xFree ){ /* Invoke any destructor belonging to the current compression. */ pDb->compress.xFree(pDb->compress.pCtx); } if( p->xBound==0 ){ memset(&pDb->compress, 0, sizeof(lsm_compress)); pDb->compress.iId = LSM_COMPRESSION_NONE; }else{ memcpy(&pDb->compress, p, sizeof(lsm_compress)); } rc = lsmFsConfigure(pDb); } break; } case LSM_CONFIG_SET_COMPRESSION_FACTORY: { lsm_compress_factory *p = va_arg(ap, lsm_compress_factory *); if( pDb->factory.xFree ){ /* Invoke any destructor belonging to the current factory. */ pDb->factory.xFree(pDb->factory.pCtx); } memcpy(&pDb->factory, p, sizeof(lsm_compress_factory)); break; } case LSM_CONFIG_GET_COMPRESSION: { lsm_compress *p = va_arg(ap, lsm_compress *); memcpy(p, &pDb->compress, sizeof(lsm_compress)); break; } default: rc = LSM_MISUSE; break; } va_end(ap); return rc; }
int lsm_config(lsm_db *pDb, int eParam, ...){ int rc = LSM_OK; va_list ap; va_start(ap, eParam); switch( eParam ){ case LSM_CONFIG_WRITE_BUFFER: { int *piVal = va_arg(ap, int *); if( *piVal>0 ){ pDb->nTreeLimit = *piVal; } *piVal = pDb->nTreeLimit; break; } case LSM_CONFIG_AUTOWORK: { int *piVal = va_arg(ap, int *); if( *piVal>=0 ){ pDb->bAutowork = *piVal; } *piVal = pDb->bAutowork; break; } case LSM_CONFIG_LOG_SIZE: { int *piVal = va_arg(ap, int *); if( *piVal>0 ){ pDb->nLogSz = *piVal; } *piVal = pDb->nLogSz; break; } case LSM_CONFIG_PAGE_SIZE: { int *piVal = va_arg(ap, int *); if( pDb->pDatabase ){ /* If lsm_open() has been called, this is a read-only parameter. ** Set the output variable to the page-size according to the ** FileSystem object. */ *piVal = lsmFsPageSize(pDb->pFS); }else{ if( *piVal>=256 && *piVal<=65536 && ((*piVal-1) & *piVal)==0 ){ pDb->nDfltPgsz = *piVal; }else{ *piVal = pDb->nDfltPgsz; } } break; } case LSM_CONFIG_BLOCK_SIZE: { int *piVal = va_arg(ap, int *); if( pDb->pDatabase ){ /* If lsm_open() has been called, this is a read-only parameter. ** Set the output variable to the page-size according to the ** FileSystem object. */ *piVal = lsmFsBlockSize(pDb->pFS); }else{ if( *piVal>=65536 && ((*piVal-1) & *piVal)==0 ){ pDb->nDfltBlksz = *piVal; }else{ *piVal = pDb->nDfltBlksz; } } break; } case LSM_CONFIG_SAFETY: { int *piVal = va_arg(ap, int *); if( *piVal>=0 && *piVal<=2 ){ pDb->eSafety = *piVal; } *piVal = pDb->eSafety; break; } case LSM_CONFIG_MMAP: { int *piVal = va_arg(ap, int *); rc = lsmConfigMmap(pDb, piVal); break; } case LSM_CONFIG_USE_LOG: { int *piVal = va_arg(ap, int *); if( pDb->nTransOpen==0 && (*piVal==0 || *piVal==1) ){ pDb->bUseLog = *piVal; } *piVal = pDb->bUseLog; break; } case LSM_CONFIG_NMERGE: { int *piVal = va_arg(ap, int *); if( *piVal>1 ) pDb->nMerge = *piVal; *piVal = pDb->nMerge; break; } default: rc = LSM_MISUSE; break; } va_end(ap); return rc; }