private void ensureCapacity() throws IOException { if (offset >= BLOCK_SIZE) { // get the next block if (blockCount >= usedBlocks.length) { int[] blocks = new int[blockCount * 2]; System.arraycopy(usedBlocks, 0, blocks, 0, usedBlocks.length); Arrays.fill(blocks, usedBlocks.length, blocks.length, -1); usedBlocks = blocks; } int nextBlockId = usedBlocks[blockCount]; if (nextBlockId == -1) { nextBlockId = file.allocBlock(); usedBlocks[blockCount] = nextBlockId; } // flush the current block into the disk if (blockId != -1) { BTreeUtils.integerToBytes(nextBlockId, bytes); file.writeBlock(blockId, bytes); } blockId = nextBlockId; blockCount++; Arrays.fill(bytes, (byte) 0); offset = 4; } }
public void close() throws IOException { if (blockId != -1) { BTreeUtils.integerToBytes(-1, bytes); file.writeBlock(blockId, bytes); } for (int i = blockCount; i < usedBlocks.length; i++) { int freeBlock = usedBlocks[i]; if (freeBlock != -1) { file.freeBlock(freeBlock); } } }
public static BTreeTestContext create( IBufferCache bufferCache, IFileMapProvider fileMapProvider, FileReference file, ISerializerDeserializer[] fieldSerdes, int numKeyFields, BTreeLeafFrameType leafType) throws Exception { ITypeTraits[] typeTraits = SerdeUtils.serdesToTypeTraits(fieldSerdes); IBinaryComparatorFactory[] cmpFactories = SerdeUtils.serdesToComparatorFactories(fieldSerdes, numKeyFields); BTree btree = BTreeUtils.createBTree( bufferCache, fileMapProvider, typeTraits, cmpFactories, leafType, file); BTreeTestContext testCtx = new BTreeTestContext(fieldSerdes, btree); return testCtx; }