/** * Determines whether the given ByteArraySegment represents an Index Page * * @param pageContents The ByteArraySegment to check. * @return True if Index Page, False if Leaf page. * @throws IllegalDataFormatException If the given contents is not a valid BTreePage format. */ static boolean isIndexPage(@NonNull ByteArraySegment pageContents) { // Check ID match. int headerId = BitConverter.readInt(pageContents, ID_OFFSET); int footerId = BitConverter.readInt(pageContents, pageContents.getLength() - FOOTER_LENGTH); if (headerId != footerId) { throw new IllegalDataFormatException("Invalid Page Format (id mismatch). HeaderId=%s, FooterId=%s.", headerId, footerId); } int flags = pageContents.get(FLAGS_OFFSET); return (flags & FLAG_INDEX_PAGE) == FLAG_INDEX_PAGE; }
/** * Tests the functionality of getWriter (the ability to return an OutputStream that can be used to write to the main buffer). */ @Test public void testGetWriter() throws IOException { final byte[] buffer = new byte[Byte.MAX_VALUE]; ByteArraySegment segment = new ByteArraySegment(buffer); try (OutputStream writer = segment.getWriter()) { for (int i = 0; i < buffer.length; i++) { writer.write(i); } } for (int i = 0; i < buffer.length; i++) { Assert.assertEquals("Unexpected value in segment at index " + i, i, segment.get(i)); Assert.assertEquals("Unexpected value in source buffer at index " + i, i, buffer[i]); } }
/** * Tests the functionality of getReader (the ability to return an InputStream from a sub-segment of the main buffer). */ @Test public void testGetReader() throws IOException { final byte[] buffer = createFormattedBuffer(); ByteArraySegment segment = new ByteArraySegment(buffer); for (int offset = 0; offset < buffer.length / 2; offset++) { int length = buffer.length - offset * 2; byte[] readBuffer = new byte[length]; try (InputStream stream = segment.getReader(offset, length)) { int readBytes = StreamHelpers.readAll(stream, readBuffer, 0, readBuffer.length); Assert.assertEquals("Unexpected number of bytes read from the InputStream at offset " + offset, length, readBytes); } for (int i = 0; i < length; i++) { Assert.assertEquals("Unexpected value at index " + i + " after reading from offset " + offset, segment.get(i + offset), readBuffer[i]); } } }
inexistentKey.set(inexistentKey.getLength() - 1, (byte) (inexistentKey.get(inexistentKey.getLength() - 1) | 1));
/** * Tests the functionality of copyFrom. */ @Test public void testCopyFrom() { final byte[] sourceBuffer = createFormattedBuffer(); final int targetOffset = 11; final byte[] targetBuffer = new byte[sourceBuffer.length + targetOffset]; final int copyLength = sourceBuffer.length - 7; ByteArraySegment source = new ByteArraySegment(sourceBuffer); ByteArraySegment target = new ByteArraySegment(targetBuffer); // Copy second part. target.copyFrom(source, targetOffset, copyLength); for (int i = 0; i < targetBuffer.length; i++) { int expectedValue = i < targetOffset || i >= targetOffset + copyLength ? 0 : i - targetOffset; Assert.assertEquals("Unexpected value after copyFrom (1) in segment at offset " + i, expectedValue, target.get(i)); Assert.assertEquals("Unexpected value after copyFrom (1) in base buffer at offset " + i, expectedValue, targetBuffer[i]); } // Test copyFrom with source offset. Arrays.fill(targetBuffer, (byte) 0); final int sourceOffset = 3; target.copyFrom(source, sourceOffset, targetOffset, copyLength); for (int i = 0; i < targetBuffer.length; i++) { int expectedValue = i < targetOffset || i >= targetOffset + copyLength ? 0 : (i - targetOffset + sourceOffset); Assert.assertEquals("Unexpected value after copyFrom (2) in segment at offset " + i, expectedValue, target.get(i)); Assert.assertEquals("Unexpected value after copyFrom (2) in base buffer at offset " + i, expectedValue, targetBuffer[i]); } }
private void assertSame(String message, CheckpointOperationBase expected, CheckpointOperationBase actual) { Assert.assertEquals(message + " Lengths mismatch.", expected.getContents().getLength(), actual.getContents().getLength()); for (int j = 0; j < expected.getContents().getLength(); j++) { if (expected.getContents().get(j) != actual.getContents().get(j)) { Assert.fail(String.format("%s Contents differ at index %d.", message, j)); } } } }
/** * Tests the ability for the ByteArraySegment to create sub-segments. */ @Test public void testSubSegment() { final byte[] buffer = createFormattedBuffer(); ByteArraySegment segment = new ByteArraySegment(buffer); // As long as the size of the segment > 1, choose a segment half the length of the current one. // If current iteration is odd, choose the upper segment. If it is even, choose the lower one. int iteration = 0; int startOffset = 0; while (segment.getLength() > 1) { iteration++; // Check correctness. for (int i = 0; i < segment.getLength(); i++) { Assert.assertEquals(String.format("Unexpected value at offset %d for subsegment (O=%d, L=%d), iteration %d.", i, startOffset, segment.getLength(), iteration), buffer[i + startOffset], segment.get(i)); } // Pick a new size and create a new subsegment. if (iteration % 2 == 0) { // Upper half for even iterations. startOffset = startOffset + segment.getLength() / 2; segment = segment.subSegment(segment.getLength() / 2, segment.getLength() - segment.getLength() / 2); } else { // Lower half for odd iterations. segment = segment.subSegment(0, segment.getLength() / 2); } } }
/** * Tests the functionality of writeTo and readFrom. */ @Test public void testWriteToReadFrom() throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int count = 10; ArrayList<ByteArraySegment> sourceSegments = new ArrayList<>(); for (int i = 0; i < count; i++) { ByteArraySegment s = new ByteArraySegment(createFormattedBuffer()); sourceSegments.add(s); s.writeTo(outputStream); } InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); for (int i = 0; i < count; i++) { ByteArraySegment s = sourceSegments.get(i); ByteArraySegment t = new ByteArraySegment(new byte[s.getLength()]); t.readFrom(inputStream); Assert.assertEquals("Source and target lengths differ.", s.getLength(), t.getLength()); for (int j = 0; j < s.getLength(); j++) { if (t.get(j) != s.get(j)) { Assert.fail(String.format("Source at target differ at index %d.", j)); } } } }
/** * Tests the basic functionality of get() and set() methods. */ @Test public void testGetSet() { final int incrementValue = 7; final int decrementValue = 11; final byte[] buffer = createFormattedBuffer(); final int halfOffset = buffer.length / 2; ByteArraySegment s1 = new ByteArraySegment(buffer, 0, halfOffset); ByteArraySegment s2 = new ByteArraySegment(buffer, halfOffset, buffer.length - halfOffset); //s1 - increment by 7 for (int i = 0; i < s1.getLength(); i++) { Assert.assertEquals("Unexpected value for initial get in first half of buffer at index " + i, i, s1.get(i)); byte newValue = (byte) (s1.get(i) + incrementValue); s1.set(i, newValue); Assert.assertEquals("Unexpected value for updated get in first half of buffer at index " + i, newValue, s1.get(i)); Assert.assertEquals("Unexpected value for the base buffer (first half) at index " + i, newValue, buffer[i]); } //s2 - decrement by 11 for (int i = 0; i < s2.getLength(); i++) { Assert.assertEquals("Unexpected value for initial get in second half of buffer at index " + i, i + halfOffset, s2.get(i)); byte newValue = (byte) (s2.get(i) - decrementValue); s2.set(i, newValue); Assert.assertEquals("Unexpected value for updated get in second half of buffer at index " + i, newValue, s2.get(i)); Assert.assertEquals("Unexpected value for the base buffer (second half) at index " + (i - halfOffset), newValue, buffer[i + halfOffset]); } }
if (this.soughtKey.get(i) != keyData.get(i)) {
for (byte[] data : state.getCurrentRecordEntries()) { for (int i = 0; i < data.length; i++) { if (currentRecord.get(recordOffset) != data[i]) { Assert.fail(String.format("Unexpected entry contents. FrameIndex = %d, RecordIndex = %d, EntryNumberInRecord = %d.", state.getFrameIndex(), state.getNextGoodRecordIndex(), i));