/** * Updates the Header of this BTreePage to reflect that it contains the given number of items. This does not perform * any resizing. * * @param itemCount The count to set. */ private void setCount(int itemCount) { BitConverter.writeInt(this.header, COUNT_OFFSET, itemCount); this.count = itemCount; }
/** * Gets this BTreePage's Id from its footer. */ private int getFooterId() { return BitConverter.readInt(this.footer, 0); }
/** * Reads a 64-bit Unsigned Long from the given byte array starting at the given position. This value must have been * serialized using {@link #writeUnsignedLong} for proper results. This method is not interoperable with {@link #writeLong}. * * @param source The byte array to read from. * @param position The position in the byte array to start reading at. * @return The read number. */ public static long readUnsignedLong(ArrayView source, int position) { return readLong(source, position) ^ Long.MIN_VALUE; } }
@Override public byte[] serialize() { byte[] result = new byte[SERIALIZATION_LENGTH]; BitConverter.writeLong(result, 0, SegmentKeyCache.this.segmentId); BitConverter.writeShort(result, Long.BYTES, this.keyHashGroup); return result; }
private PagePointer deserializePointer(ByteArraySegment serialization, ByteArraySegment pageKey) { long pageOffset = BitConverter.readLong(serialization, 0); int pageLength = BitConverter.readShort(serialization, Long.BYTES); long minOffset = deserializePointerMinOffset(serialization); return new PagePointer(pageKey, pageOffset, pageLength, minOffset); }
private ByteArraySegment serializeKey(UUID key) { // Keys are serialized using Unsigned Longs. This ensures that they will be stored in the Attribute Index in their // natural order (i.e., the same as the one done by UUID.compare()). byte[] result = new byte[KEY_LENGTH]; BitConverter.writeUnsignedLong(result, 0, key.getMostSignificantBits()); BitConverter.writeUnsignedLong(result, Long.BYTES, key.getLeastSignificantBits()); return new ByteArraySegment(result); }
/** * 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); }
@Override CompletableFuture<Void> recordLastStreamSegment(final String scope, final String stream, final int lastActiveSegment, OperationContext context, final Executor executor) { final String deletePath = String.format(DELETED_STREAMS_PATH, getScopedStreamName(scope, stream)); byte[] maxSegmentNumberBytes = new byte[Integer.BYTES]; BitConverter.writeInt(maxSegmentNumberBytes, 0, lastActiveSegment); return storeHelper.getData(deletePath) .exceptionally(e -> { if (e instanceof StoreException.DataNotFoundException) { return null; } else { throw new CompletionException(e); } }) .thenCompose(data -> { log.debug("Recording last segment {} for stream {}/{} on deletion.", lastActiveSegment, scope, stream); if (data == null) { return Futures.toVoid(storeHelper.createZNodeIfNotExist(deletePath, maxSegmentNumberBytes)); } else { final int oldLastActiveSegment = BitConverter.readInt(data.getData(), 0); Preconditions.checkArgument(lastActiveSegment >= oldLastActiveSegment, "Old last active segment ({}) for {}/{} is higher than current one {}.", oldLastActiveSegment, scope, stream, lastActiveSegment); return Futures.toVoid(storeHelper.setData(deletePath, new Data(maxSegmentNumberBytes, data.getVersion()))); } }); }
public Event(ArrayView source, int sourceOffset) { int prefix = BitConverter.readInt(source, sourceOffset); sourceOffset += PREFIX_LENGTH; Preconditions.checkArgument(prefix == PREFIX, "Prefix mismatch."); this.ownerId = BitConverter.readInt(source, sourceOffset); sourceOffset += OWNER_ID_LENGTH; this.routingKey = BitConverter.readInt(source, sourceOffset); sourceOffset += ROUTING_KEY_LENGTH; this.sequence = BitConverter.readInt(source, sourceOffset); sourceOffset += SEQUENCE_LENGTH; this.startTime = BitConverter.readLong(source, sourceOffset); sourceOffset += START_TIME_LENGTH; this.contentLength = BitConverter.readInt(source, sourceOffset); sourceOffset += LENGTH_LENGTH; Preconditions.checkArgument(this.contentLength >= 0, "Payload length must be a positive integer.");
private ByteArraySegment serializePointer(PagePointer pointer) { assert pointer.getLength() <= Short.MAX_VALUE : "PagePointer.length too large"; ByteArraySegment result = new ByteArraySegment(new byte[this.indexPageConfig.getValueLength()]); BitConverter.writeLong(result, 0, pointer.getOffset()); BitConverter.writeShort(result, Long.BYTES, (short) pointer.getLength()); BitConverter.writeLong(result, Long.BYTES + Short.BYTES, pointer.getMinOffset()); return result; }
private ArrayList<byte[]> generateSortedData() { val sortedData = new ArrayList<byte[]>(); int maxValue = COUNT / 2; for (int i = -maxValue; i < maxValue; i++) { byte[] data = new byte[Long.BYTES]; BitConverter.writeUnsignedLong(data, 0, i); sortedData.add(data); } return sortedData; }
/** * Updates the Header to contain the given id. */ private void setHeaderId(int id) { BitConverter.writeInt(this.header, ID_OFFSET, id); }
/** * Format: [Header][Key][Length][Contents] * * [Header]: is a sequence of bytes identifying the start of an append * * [OwnerId]: the owning Stream/Segment id * * [RoutingKey]: the event's routing key * * [Sequence]: the event's sequence number * * [StartTime]: the event's start time. * * [Length]: length of [Contents] * * [Contents]: a deterministic result of [Key] & [Length]. */ private byte[] serialize(int length) { Preconditions.checkArgument(length >= HEADER_LENGTH, "length is insufficient to accommodate header."); byte[] payload = new byte[length]; // Header: PREFIX + ownerId + routingKey + sequence + start time + Key + Length int offset = 0; offset += BitConverter.writeInt(payload, offset, PREFIX); offset += BitConverter.writeInt(payload, offset, this.ownerId); offset += BitConverter.writeInt(payload, offset, this.routingKey); offset += BitConverter.writeInt(payload, offset, this.sequence); offset += BitConverter.writeLong(payload, offset, this.startTime); int contentLength = length - HEADER_LENGTH; offset += BitConverter.writeInt(payload, offset, contentLength); assert offset == HEADER_LENGTH : "Event header has a different length than expected"; // Content writeContent(payload, offset); return payload; }
/** * Gets this BTreePage's Id from its header. */ int getHeaderId() { return BitConverter.readInt(this.header, ID_OFFSET); }