@Test public void testWrite() throws Exception { IndexWriter writer = newIndexWriter(1); writer.addEntry(7, 8, 9, 10, 11, 12, true); writer.addEntry(1, 2, 3, 4, 5, 6, false); ByteBuffer buffer = ByteBuffer.allocate(2 * IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE); buffer.duplicate() .putLong(1).putLong(2).putInt(3).putInt(4).putInt(5).putInt(6).put((byte) 0) .putLong(7).putLong(8).putInt(9).putInt(10).putInt(11).putInt(12).put((byte) 1) .putInt(0xE2138EB4) .putInt(2) .putInt(2 * IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE) .putInt(IndexLoaderV2.MAGIC); assertArrayEquals(buffer.array(), writer.write()); }
public byte[] write() { int dataSize = entries.size() * IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE; int totalSize = ((dataSize + blockSize - 1) / blockSize) * blockSize; Buffer buffer = Buffer.allocate(totalSize); buffer.position(totalSize - dataSize); entries.sort((a, b) -> { if (a.msb < b.msb) { return -1; } if (a.msb > b.msb) { return 1; } if (a.lsb < b.lsb) { return -1; } if (a.lsb > b.lsb) { return 1; } return 0; }); for (Entry entry : entries) { buffer.putLong(entry.msb); buffer.putLong(entry.lsb); buffer.putInt(entry.offset); buffer.putInt(entry.size); buffer.putInt(entry.generation); buffer.putInt(entry.fullGeneration); buffer.put((byte) (entry.isCompacted ? 1 : 0)); } CRC32 checksum = new CRC32(); checksum.update(buffer.array(), totalSize - dataSize, dataSize - IndexV2.FOOTER_SIZE); buffer.putInt((int) checksum.getValue()); buffer.putInt(entries.size()); buffer.putInt(totalSize); buffer.putInt(IndexLoaderV2.MAGIC); return buffer.array(); }
IndexWriter { public byte[] write() { int dataSize = entries.size() * IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE; int totalSize = ((dataSize + blockSize - 1) / blockSize) * blockSize; Buffer buffer = Buffer.allocate(totalSize); buffer.position(totalSize - dataSize); entries.sort((a, b) -> { if (a.msb < b.msb) { return -1; } if (a.msb > b.msb) { return 1; } if (a.lsb < b.lsb) { return -1; } if (a.lsb > b.lsb) { return 1; } return 0; }); for (Entry entry : entries) { buffer.putLong(entry.msb); buffer.putLong(entry.lsb); buffer.putInt(entry.offset); buffer.putInt(entry.size); buffer.putInt(entry.generation); buffer.putInt(entry.fullGeneration); buffer.put((byte) (entry.isCompacted ? 1 : 0)); } CRC32 checksum = new CRC32(); checksum.update(buffer.array(), totalSize - dataSize, dataSize - IndexV2.FOOTER_SIZE); buffer.putInt((int) checksum.getValue()); buffer.putInt(entries.size()); buffer.putInt(totalSize); buffer.putInt(IndexLoaderV2.MAGIC); return buffer.array(); } }
IndexWriter { public byte[] write() { int dataSize = entries.size() * IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE; int totalSize = ((dataSize + blockSize - 1) / blockSize) * blockSize; Buffer buffer = Buffer.allocate(totalSize); buffer.position(totalSize - dataSize); entries.sort((a, b) -> { if (a.msb < b.msb) { return -1; } if (a.msb > b.msb) { return 1; } if (a.lsb < b.lsb) { return -1; } if (a.lsb > b.lsb) { return 1; } return 0; }); for (Entry entry : entries) { buffer.putLong(entry.msb); buffer.putLong(entry.lsb); buffer.putInt(entry.offset); buffer.putInt(entry.size); buffer.putInt(entry.generation); buffer.putInt(entry.fullGeneration); buffer.put((byte) (entry.isCompacted ? 1 : 0)); } CRC32 checksum = new CRC32(); checksum.update(buffer.array(), totalSize - dataSize, dataSize - IndexV2.FOOTER_SIZE); buffer.putInt((int) checksum.getValue()); buffer.putInt(entries.size()); buffer.putInt(totalSize); buffer.putInt(IndexLoaderV2.MAGIC); return buffer.array(); } private  IndexWriter(int blockSize); }
IndexWriter { public byte[] write() { int dataSize = entries.size() * IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE; int totalSize = ((dataSize + blockSize - 1) / blockSize) * blockSize; Buffer buffer = Buffer.allocate(totalSize); buffer.position(totalSize - dataSize); entries.sort((a, b) -> { if (a.msb < b.msb) { return -1; } if (a.msb > b.msb) { return 1; } if (a.lsb < b.lsb) { return -1; } if (a.lsb > b.lsb) { return 1; } return 0; }); for (Entry entry : entries) { buffer.putLong(entry.msb); buffer.putLong(entry.lsb); buffer.putInt(entry.offset); buffer.putInt(entry.size); buffer.putInt(entry.generation); buffer.putInt(entry.fullGeneration); buffer.put((byte) (entry.isCompacted ? 1 : 0)); } CRC32 checksum = new CRC32(); checksum.update(buffer.array(), totalSize - dataSize, dataSize - IndexV2.FOOTER_SIZE); buffer.putInt((int) checksum.getValue()); buffer.putInt(entries.size()); buffer.putInt(totalSize); buffer.putInt(IndexLoaderV2.MAGIC); return buffer.array(); } private  IndexWriter(int blockSize); static IndexWriter newIndexWriter(int blockSize); void addEntry(long msb, long lsb, int offset, int size, int generation, int fullGeneration, boolean isCompacted); byte[] write(); }
IndexWriter { public byte[] write() { int dataSize = entries.size() * IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE; int totalSize = ((dataSize + blockSize - 1) / blockSize) * blockSize; Buffer buffer = Buffer.allocate(totalSize); buffer.position(totalSize - dataSize); entries.sort((a, b) -> { if (a.msb < b.msb) { return -1; } if (a.msb > b.msb) { return 1; } if (a.lsb < b.lsb) { return -1; } if (a.lsb > b.lsb) { return 1; } return 0; }); for (Entry entry : entries) { buffer.putLong(entry.msb); buffer.putLong(entry.lsb); buffer.putInt(entry.offset); buffer.putInt(entry.size); buffer.putInt(entry.generation); buffer.putInt(entry.fullGeneration); buffer.put((byte) (entry.isCompacted ? 1 : 0)); } CRC32 checksum = new CRC32(); checksum.update(buffer.array(), totalSize - dataSize, dataSize - IndexV2.FOOTER_SIZE); buffer.putInt((int) checksum.getValue()); buffer.putInt(entries.size()); buffer.putInt(totalSize); buffer.putInt(IndexLoaderV2.MAGIC); return buffer.array(); } private  IndexWriter(int blockSize); static IndexWriter newIndexWriter(int blockSize); void addEntry(long msb, long lsb, int offset, int size, int generation, int fullGeneration, boolean isCompacted); byte[] write();  }