Entry(ArrayView inputData) { this.data = new byte[inputData.getLength()]; System.arraycopy(inputData.array(), inputData.arrayOffset(), this.data, 0, this.data.length); }
/** * Reads a 16-bit Short from the given ArrayView starting at the given position. * * @param source The ArrayView to read from. * @param position The position in the ArrayView to start reading at. * @return The read number. */ public static short readShort(ArrayView source, int position) { return (short) ((source.get(position) & 0xFF) << 8 | (source.get(position + 1) & 0xFF)); }
/** * Deserializes data from the given ArrayView into the given object. * * @param data The ArrayView to deserialize from. * @param target The target object to apply the deserialization to. * @throws IOException If an IO Exception occurred. */ public void deserialize(ArrayView data, ReaderType target) throws IOException { deserialize(data.getReader(), target); }
/** * Writes the given 16-bit Short to the given ArrayView at the given offset. * * @param target The ArrayView to write to. * @param offset The offset within the ArrayView to write at. * @param value The value to write. * @return The number of bytes written. */ public static int writeShort(ArrayView target, int offset, short value) { return writeShort(target.array(), target.arrayOffset() + offset, value); }
/** * Creates a new instance of the TableValueTooLongException class. * * @param entry The TableEntry. * @param maximumLength The maximum allowed value length. */ public TableValueTooLongException(TableEntry entry, int maximumLength) { super(String.format("Table Value too long. Maximum length: %s, given: %s.", entry.getValue().getLength(), maximumLength)); } }
sourceOffset += LENGTH_LENGTH; Preconditions.checkArgument(this.contentLength >= 0, "Payload length must be a positive integer."); Preconditions.checkElementIndex(sourceOffset + contentLength, source.getLength() + 1, "Insufficient data in given source."); source.getReader(sourceOffset - HEADER_LENGTH, getTotalLength()), getTotalLength())); } else { this.serialization = source;
Assert.assertEquals("Unexpected buffer index after flush #" + (i + 1), expectedStartIndex, av.arrayOffset()); expectedStartIndex += av.getLength(); if (maxFrameSize - expectedStartIndex < av.getLength()) { expectedStartIndex = 0;
private WireCommands.TableEntries getTableEntries(List<TableEntry> updateData) { List<Map.Entry<WireCommands.TableKey, WireCommands.TableValue>> entries = updateData.stream().map(te -> { if (te == null) { return new AbstractMap.SimpleImmutableEntry<>(WireCommands.TableKey.EMPTY, WireCommands.TableValue.EMPTY); } else { val tableKey = new WireCommands.TableKey(ByteBuffer.wrap(te.getKey().getKey().array()), te.getKey().getVersion()); val tableValue = new WireCommands.TableValue(ByteBuffer.wrap(te.getValue().array())); return new AbstractMap.SimpleImmutableEntry<>(tableKey, tableValue); } }).collect(toList()); return new WireCommands.TableEntries(entries); }
/** * Creates a new instance of the TableKeyTooLongException class. * * @param key The Key. * @param maximumLength The maximum allowed key length. */ public TableKeyTooLongException(TableKey key, int maximumLength) { super(String.format("Table Key too long. Maximum length: %s, given: %s.", key.getKey().getLength(), maximumLength)); } }
/** * Writes the given 32-bit Integer to the given ArrayView at the given offset. * * @param target The ArrayView to write to. * @param offset The offset within the ArrayView to write at. * @param value The value to write. * @return The number of bytes written. */ public static int writeInt(ArrayView target, int offset, int value) { return writeInt(target.array(), target.arrayOffset() + offset, value); }
private ArrayList<LogItem> toDataFrames(ArrayList<TestItem> items) throws Exception { val result = new ArrayList<LogItem>(RECORD_COUNT); Consumer<DataFrame> addCallback = df -> result.add(new LogItem(df.getData().getReader(), df.getData().getLength(), new TestLogAddress(result.size()))); try (val dos = new DataFrameOutputStream(FRAME_SIZE, addCallback)) { for (TestItem item : items) { dos.startNewRecord(); long beginSequence = result.size(); dos.write(item.data); dos.endRecord(); item.address = new TestLogAddress(result.size()); for (long s = beginSequence; s <= item.address.getSequence(); s++) { item.dataFrames.add(s); } } dos.flush(); } return result; }
WireCommands.TableKey key = new WireCommands.TableKey(ByteBuffer.wrap(e1.getKey().getKey().array()), 0L); processor.removeTableKeys(new WireCommands.RemoveTableKeys(3, streamSegmentName, "", singletonList(key))); order.verify(connection).send(new WireCommands.TableKeysRemoved(3, streamSegmentName)); key = new WireCommands.TableKey(ByteBuffer.wrap(e1.getKey().getKey().array()), 0L); processor.removeTableKeys(new WireCommands.RemoveTableKeys(4, streamSegmentName, "", singletonList(key))); order.verify(connection).send(new WireCommands.TableKeyBadVersion(4, streamSegmentName, "" ));
/** * Creates a new instance of the HashedArray class. * * @param array An {@link ArrayView} to wrap. */ public HashedArray(ArrayView array) { super(array.array(), array.arrayOffset(), array.getLength()); this.hashCode = hashCode(array); }
/** * Calculates the number of bytes required to serialize the given {@link TableKey} removal. * * @param key The {@link TableKey} to serialize for removal. * @return The number of bytes required to serialize. */ int getRemovalLength(@NonNull TableKey key) { return HEADER_LENGTH + key.getKey().getLength(); }
/** * Writes the given 64-bit Long to the given ArrayView at the given offset. * * @param target The ArrayView to write to. * @param offset The offset within the ArrayView to write at. * @param value The value to write. * @return The number of bytes written. */ public static int writeLong(ArrayView target, int offset, long value) { return writeLong(target.array(), target.arrayOffset() + offset, value); }
/** * Tests the ability to append a set of records to a DataFrame, serialize it, deserialize it, and then read those * records back. */ @Test public void testSerialization() throws Exception { int maxFrameSize = 2 * 1024 * 1024; int maxRecordCount = 4500; int minRecordSize = 0; int maxRecordSize = 1024; List<ByteArraySegment> allRecords = DataFrameTestHelpers.generateRecords(maxRecordCount, minRecordSize, maxRecordSize, ByteArraySegment::new); // Append some records. DataFrame writeFrame = DataFrame.ofSize(maxFrameSize); int recordsAppended = appendRecords(allRecords, writeFrame); AssertExtensions.assertGreaterThan("Did not append enough records. Test may not be valid.", allRecords.size() / 2, recordsAppended); writeFrame.seal(); val frameData = writeFrame.getData(); Assert.assertEquals("Unexpected length from getData().", writeFrame.getLength(), frameData.getLength()); // Read them back, by deserializing the frame. val contents = DataFrame.read(frameData.getReader(), frameData.getLength(), writeFrame.getAddress()); DataFrameTestHelpers.checkReadRecords(contents, allRecords, b -> b); }
WireCommands.TableKey key = new WireCommands.TableKey(ByteBuffer.wrap(entry.getKey().getKey().array()), TableKey.NO_VERSION); processor.readTable(new WireCommands.ReadTable(2, streamSegmentName, "", singletonList(key))); key = new WireCommands.TableKey(ByteBuffer.wrap(entry.getKey().getKey().array()), 0L); TableEntry expectedEntry = TableEntry.versioned(entry.getKey().getKey(), entry.getValue(), 0L); processor.readTable(new WireCommands.ReadTable(4, streamSegmentName, "", singletonList(key)));
/** * Reads a 32-bit integer from the given ArrayView starting at the given position. * * @param source The ArrayView to read from. * @param position The position in the ArrayView to start reading at. * @return The read number. */ public static int readInt(ArrayView source, int position) { return (source.get(position) & 0xFF) << 24 | (source.get(position + 1) & 0xFF) << 16 | (source.get(position + 2) & 0xFF) << 8 | (source.get(position + 3) & 0xFF); }
/** * Deserializes data from the given ArrayView and creates a new object with the result. * * @param data The ArrayView to deserialize from. * @return A new instance of TargetType with the deserialized data. * @throws IOException If an IO Exception occurred. */ public TargetType deserialize(ArrayView data) throws IOException { return deserialize(data.getReader()); }