@Override public void beginIterator(Iterator<?> iter) { synchronized (this) { if (activeIterators.contains(iter)) error(BeginIter, "Iterator already active: " + iter); activeIterators.add(iter); } blockMgr.beginIterator(iter); }
@Override public synchronized void beginRead() { synchronized (this) { if (inUpdate) error(BeginRead, "beginRead when already in update"); inRead++; inUpdate = false; } blockMgr.beginRead(); }
@Override public void beginUpdate() { synchronized (this) { if (inRead > 0) error(BeginUpdate, "beginUpdate when already in read"); if (inUpdate) error(BeginUpdate, "beginUpdate when already in update"); inUpdate = true; } blockMgr.beginUpdate(); }
@Override public void endIterator(Iterator<?> iter) { synchronized (this) { if (!activeIterators.contains(iter)) error(EndIter, "Iterator not active: " + iter); activeIterators.remove(iter); if (activeIterators.size() == 0) checkEmpty("Outstanding iterator read blocks", activeIterBlocks); } blockMgr.endIterator(iter); }
@Override public Block getWrite(long id) { synchronized (this) { checkUpdate(GetWrite); Long x = id; add(GetWrite, x); activeWriteBlocks.add(x); } return blockMgr.getWrite(id); }
@Override public Block getReadIterator(long id) { synchronized (this) { checkReadOrIter(IterRead); Long x = id; add(IterRead, x); activeIterBlocks.add(x); } return blockMgr.getReadIterator(id); }
@Override public void free(Block block) { synchronized (this) { checkUpdate(Free); Long id = block.getId(); add(Free, id); if (activeReadBlocks.contains(id)) error(Free, id + " is a read block"); else if (!activeWriteBlocks.contains(id)) error(Free, id + " is not a write block"); activeWriteBlocks.remove(id); } blockMgr.free(block); }
@Override public Block allocate(int blockSize) { Block block; synchronized (this) { checkUpdate(Alloc); block = blockMgr.allocate(blockSize); Long id = block.getId(); activeWriteBlocks.add(id); add(Alloc, id); } return block; }
@Override public Block getRead(long id) { // What if this is a write block already? synchronized (this) { checkRead(GetRead); Long x = id; add(GetRead, x); if (activeWriteBlocks.contains(x)) activeWriteBlocks.add(x); else activeReadBlocks.add(x); } return blockMgr.getRead(id); }
@Override public void endUpdate() { synchronized (this) { if (!inUpdate) error(EndUpdate, "endUpdate but not in update"); if (inRead > 0) error(EndUpdate, "endUpdate when in read"); checkEmpty("Outstanding read blocks at end of update operations", activeReadBlocks); checkEmpty("Outstanding write blocks at end of update operations", activeWriteBlocks); inUpdate = false; inRead = 0; clearInternalRW(); } blockMgr.endUpdate(); }
@Override public void release(Block block) { synchronized (this) { checkReadOrIter(Release); Long id = block.getId(); add(Release, id); if (!activeReadBlocks.contains(id) && !activeIterBlocks.contains(id) && !activeWriteBlocks.contains(id)) error(Release, id + " is not an active block"); // May have been promoted. if (activeWriteBlocks.contains(id)) activeWriteBlocks.remove(id); else activeReadBlocks.remove(block.getId()); activeIterBlocks.remove(block.getId()); } blockMgr.release(block); }
@Override public synchronized void endRead() { synchronized (this) { if (inRead == 0) error(EndRead, "endRead but not in read"); if (inUpdate) error(EndRead, "endRead when in update"); checkEmpty("Outstanding write blocks at end of read operations!", activeWriteBlocks); if (inRead == 0) { // Check at end of multiple reads or a write checkEmpty("Outstanding read blocks at end of read operations", activeReadBlocks); clearInternalRW(); } inUpdate = false; inRead--; } blockMgr.endRead(); }
@Override public Block promote(Block block) { synchronized (this) { checkUpdate(Promote); Long id = block.getId(); add(Promote, id); if (!activeWriteBlocks.contains(id) && !activeReadBlocks.contains(id)) error(Promote, id + " is not an active block"); if (activeReadBlocks.contains(id)) // Remove one read count // Really, do this if obtained via "getRead" and not "getWrite" activeReadBlocks.remove(id); // Double promotion results in only one entry. if (!activeWriteBlocks.contains(id)) activeWriteBlocks.add(id); } return blockMgr.promote(block); }
@Override public void syncForce() { blockMgr.syncForce(); }
@Override public synchronized void overwrite(Block block) { writeTracker(block); blockMgr.overwrite(block); }
@Override public void write(Block block) { writeTracker(block); blockMgr.write(block); }
@Override public boolean isClosed() { return blockMgr.isClosed(); }
@Override public boolean valid(int id) { return blockMgr.valid(id); }
@Override public boolean isEmpty() { return blockMgr.isEmpty(); }
@Override public void sync() { blockMgr.sync(); }
public static BlockMgr track(BlockMgr blkMgr) { return track(blkMgr.getLabel(), blkMgr); }
public BlockMgrTracker(Logger logger, String label, BlockMgr blockMgr) { this.blockMgr = blockMgr; this.log = logger; this.label = blockMgr.getLabel(); }
@Override public void close() { blockMgr.close(); }