@Override public Block getWrite(long id) { // NB: If we are in a stack of BlockMgrs, after a transaction has committed, // we would be called via getRead and the upper Blockgr does the promotion. checkActive() ; checkIfClosed() ; Block block = localBlock(id) ; if ( block != null ) return block ; // Get-as-read. block = blockMgr.getRead(id) ; // If most blocks get modified, then a copy is needed // anyway so now is as good a time as any. block = _promote(block) ; return block ; }
/** Set, or reset, this BlockMgr. */ private void reset(Transaction txn, FileRef fileRef, BlockMgr underlyingBlockMgr) { this.fileRef = fileRef ; this.blockMgr = underlyingBlockMgr ; this.active = true ; clear(txn) ; }
private Block _promote(Block block) { checkActive() ; block = replicate(block) ; writeBlocks.put(block.getId(), block) ; return block ; }
@Override public void commitPrepare(Transaction txn) { checkActive() ; for ( Block blk : writeBlocks.values() ) writeJournalEntry(blk) ; this.active = false ; }
@Override public void sync() { checkIfClosed() ; }
@Override public Block getReadIterator(long id) { //logState() ; checkIfClosed() ; Block block = localBlock(id) ; if ( block == null ) block = blockMgr.getReadIterator(id) ; if ( block == null ) throw new BlockException("No such block: "+getLabel()+" "+id) ; if ( active ) iteratorBlocks.add(block.getId()) ; return block ; }
@Override public Block promote(Block block) { checkIfClosed() ; if ( writeBlocks.containsKey(block.getId()) ) return block ; return _promote(block) ; }
@Override public Block getRead(long id) { checkIfClosed() ; Block block = localBlock(id) ; if ( block != null ) return block ; block = blockMgr.getRead(id) ; if ( active ) readBlocks.add(block.getId()) ; return block ; }
@Override public Block allocate(int blockSize) { checkIfClosed() ; // Might as well allocate now. // This allocates the id. Block block = blockMgr.allocate(blockSize) ; // But we "copy" it by allocating ByteBuffer space. if ( active ) { block = replicate(block) ; writeBlocks.put(block.getId(), block) ; } return block ; }
@Override public void begin(Transaction txn) { reset(txn, fileRef, blockMgr) ; }
@Override public void abort(Transaction txn) { checkActive() ; this.active = false ; // Do clearup of in-memory structures in clearup(). }
@Override public BlockMgr buildBlockMgr(FileSet fileSet, String ext, IndexParams params) { // Find from file ref. FileRef ref = FileRef.create(fileSet, ext) ; BlockMgr baseMgr = blockMgrs.get(ref) ; if ( baseMgr == null ) { //System.out.flush(); System.out.println("No BlockMgr for " + ref+" : "+blockMgrs.keySet()); //throw new TDBException("No BlockMgr for " + ref) ; } BlockMgrJournal blkMgr = new BlockMgrJournal(txn, ref, baseMgr) ; txn.addComponent(blkMgr) ; return blkMgr ; } }
private void logState() { Log.info(this, "state: "+getLabel()) ; Log.info(this, " readBlocks: "+readBlocks) ; Log.info(this, " writeBlocks: "+writeBlocks) ; Log.info(this, " iteratorBlocks: "+iteratorBlocks) ; Log.info(this, " freedBlocks: "+freedBlocks) ; }
private Block localBlock(long id) { checkIfClosed() ; return writeBlocks.get(id) ; }
public BlockMgrJournal(Transaction txn, FileRef fileRef, BlockMgr underlyingBlockMgr) { Context context = txn.getBaseDataset().getContext() ; String mode = (null != context) ? (String) context.get(TDB.transactionJournalWriteBlockMode, "") : "" ; if ("direct".equalsIgnoreCase(mode)) writeBlockBufferAllocator = new BufferAllocatorDirect() ; else if ("mapped".equalsIgnoreCase(mode)) writeBlockBufferAllocator = new BufferAllocatorMapped(SystemTDB.BlockSize) ; else writeBlockBufferAllocator = new BufferAllocatorMem() ; reset(txn, fileRef, underlyingBlockMgr) ; if ( txn.getTxnMode() == ReadWrite.READ && underlyingBlockMgr instanceof BlockMgrJournal ) System.err.println("Two level BlockMgrJournal") ; }
@Override public boolean isEmpty() { checkIfClosed() ; return writeBlocks.isEmpty() && blockMgr.isEmpty() ; }
@Override public void clearupCommitted(Transaction txn) { // Persistent state is in the system journal. clear(txn) ; }
@Override public boolean valid(int id) { checkIfClosed() ; if ( writeBlocks.containsKey(Long.valueOf(id))) return true ; return blockMgr.valid(id) ; }
@Override public void endUpdate() { checkIfClosed() ; blockMgr.endRead() ; }
@Override public void beginIterator(Iterator<?> iterator) { checkIfClosed() ; transaction.addIterator(iterator) ; // Don't pass down the beginIterator call - we track and manage here, not lower down. //blockMgr.beginIterator(iterator) ; }