/** * Formats the Header and Footer of this BTreePage with the given information. * * @param itemCount The number of items in this BTreePage. * @param id The Id of this BTreePage. */ private void formatHeaderAndFooter(int itemCount, int id) { // Header. this.header.set(VERSION_OFFSET, CURRENT_VERSION); this.header.set(FLAGS_OFFSET, getFlags(this.config.isIndexPage ? FLAG_INDEX_PAGE : FLAG_NONE)); setHeaderId(id); setCount(itemCount); // Matching footer. setFooterId(id); }
void commit() { Preconditions.checkState(this.buffer != null && !this.buffer.isReadOnly(), "Cannot commit a read-only FrameHeader"); assert this.buffer.getLength() == SERIALIZATION_LENGTH; // We already checked the size of the target buffer (in the constructor); no need to do it here again. int bufferOffset = 0; this.buffer.set(bufferOffset, getVersion()); bufferOffset += Byte.BYTES; bufferOffset += BitConverter.writeInt(this.buffer, bufferOffset, getContentLength()); this.buffer.set(bufferOffset, encodeFlags()); } }
/** * Appends a byte to the DataFrame. * * @param b The byte to append. * @return The number of bytes written. If zero, then the frame is full and we cannot write anything anymore. * @throws IllegalStateException If the frame is sealed or no entry has been started. */ int append(byte b) { ensureAppendConditions(); if (getAvailableLength() >= 1) { this.contents.set(writePosition, b); writePosition++; return 1; } else { // Current DataFrame is full. we can't write anymore. return 0; } }
void serialize() { Preconditions.checkState(this.data != null && !this.data.isReadOnly(), "Cannot serialize a read-only EntryHeader."); // Write length. BitConverter.writeInt(this.data, 0, getEntryLength()); // Write flags. byte flags = isFirstRecordEntry() ? FIRST_ENTRY_MASK : 0; flags |= isLastRecordEntry() ? LAST_ENTRY_MASK : 0; this.data.set(FLAGS_OFFSET, flags); } }
inexistentKey.set(inexistentKey.getLength() - 1, (byte) (inexistentKey.get(inexistentKey.getLength() - 1) | 1));
/** * 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]); } }
/** * Tests the behavior of the ByteArraySegment in read-only mode. */ @Test public void testReadOnly() throws Exception { final byte[] buffer = createFormattedBuffer(); ByteArraySegment segment = new ByteArraySegment(buffer, 0, buffer.length, true); // Check the isReadonly flag Assert.assertTrue("Unexpected value for isReadOnly() for read-only segment.", segment.isReadOnly()); Assert.assertFalse("Unexpected value for isReadOnly() for non-read-only segment.", new ByteArraySegment(buffer).isReadOnly()); // Verify that "mutator" methods do not work. checkReadOnlyException("copyFrom", () -> segment.copyFrom(new ByteArraySegment(new byte[10], 0, 10), 0, 10)); checkReadOnlyException("getWriter", segment::getWriter); checkReadOnlyException("set", () -> segment.set(0, (byte) 0)); // Check to see that, even though we did get an exception, the buffer was not modified. for (int i = 0; i < buffer.length; i++) { Assert.assertEquals("One of the 'mutator' methods modified the buffer at index " + i, i, buffer[i]); } // Check that a subsegment is also read-only. Assert.assertTrue("Unexpected value for isReadOnly() for read-only sub-segment.", segment.subSegment(0, 1).isReadOnly()); Assert.assertTrue("Unexpected value for isReadOnly() for read-only sub-segment from non-read-only segment (when attempting to create a non-read-only segment).", segment.subSegment(0, 1, false).isReadOnly()); Assert.assertTrue("Unexpected value for isReadOnly() for read-only sub-segment from non-read-only segment.", new ByteArraySegment(buffer).subSegment(0, 1, true).isReadOnly()); }