private void serializeCacheValue(long value, byte[] target, int targetOffset) { BitConverter.writeLong(target, targetOffset, value); }
/** * Writes the given 64-bit Unsigned Long to the given byte array at the given offset. This value can then be * deserialized using {@link #readUnsignedLong}. This method is not interoperable with {@link #readLong}. * * The advantage of serializing as Unsigned Long (vs. a normal Signed Long) is that the serialization will have the * same natural order as the input value type (i.e., if compared using a lexicographic bitwise comparator such as * ByteArrayComparator, it will have the same ordering as the typical Long type). * * @param target The byte array to write to. * @param offset The offset within the byte array to write at. * @param value The (signed) value to write. The value will be converted into the range [0, 2^64-1] before * serialization by flipping the high order bit (so positive values will begin with 1 and negative values * will begin with 0). * @return The number of bytes written. */ public static int writeUnsignedLong(byte[] target, int offset, long value) { return writeLong(target, offset, value ^ Long.MIN_VALUE); }
public byte[] toBytes() { byte[] b = new byte[Integer.BYTES + Long.BYTES]; BitConverter.writeInt(b, 0, msb); BitConverter.writeLong(b, Integer.BYTES, lsb); return b; }
@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 ByteArraySegment serializeValue(Long value) { if (value == null || value == Attributes.NULL_ATTRIBUTE_VALUE) { // Deletion. return null; } byte[] result = new byte[VALUE_LENGTH]; BitConverter.writeLong(result, 0, value); return new ByteArraySegment(result); }
@Override CompletableFuture<Void> storeCreationTimeIfAbsent(final long creationTime) { byte[] b = new byte[Long.BYTES]; BitConverter.writeLong(b, 0, creationTime); return Futures.toVoid(store.createZNodeIfNotExist(creationPath, b)); }
@Override CompletableFuture<Void> createMarkerData(long segmentId, long timestamp) { byte[] b = new byte[Long.BYTES]; BitConverter.writeLong(b, 0, timestamp); synchronized (markersLock) { markers.putIfAbsent(segmentId, new Data(b, new Version.IntVersion(0))); } return CompletableFuture.completedFuture(null); }
@Override public CompletableFuture<Void> createMarkerData(long segmentId, long timestamp) { final String path = ZKPaths.makePath(markerPath, String.format("%d", segmentId)); byte[] b = new byte[Long.BYTES]; BitConverter.writeLong(b, 0, timestamp); return store.createZNodeIfNotExist(path, b) .thenAccept(x -> cache.invalidateCache(markerPath)); }
private ByteArraySegment getFooter(long rootPageOffset, int rootPageLength) { byte[] result = new byte[FOOTER_LENGTH]; BitConverter.writeLong(result, 0, rootPageOffset); BitConverter.writeInt(result, Long.BYTES, rootPageLength); return new ByteArraySegment(result); }
/** * 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); }
private ByteArraySegment serializeLong(long value) { byte[] r = new byte[Long.BYTES]; BitConverter.writeLong(r, 0, value); return new ByteArraySegment(r); }
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; }
@Override public CompletableFuture<Void> setColdMarker(long segmentId, long timestamp) { return getMarkerData(segmentId).thenCompose(x -> { if (x != null) { byte[] b = new byte[Long.BYTES]; BitConverter.writeLong(b, 0, timestamp); final Data data = new Data(b, x.getVersion()); return Futures.toVoid(updateMarkerData(segmentId, data)); } else { return createMarkerData(segmentId, timestamp); } }); }
/** * 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; }