/** * Gets this BTreePage's Id from its footer. */ private int getFooterId() { return BitConverter.readInt(this.footer, 0); }
/** * Gets this BTreePage's Id from its header. */ int getHeaderId() { return BitConverter.readInt(this.header, ID_OFFSET); }
private int getRootPageLength(ByteArraySegment footer) { return BitConverter.readInt(footer, Long.BYTES); }
/** * Reads the Entry's Header from the given {@link InputStream}. * * @param input The {@link InputStream} to read from. * @return The Entry Header. * @throws IOException If an invalid header was detected or another IOException occurred. */ Header readHeader(@NonNull InputStream input) throws IOException { byte version = (byte) input.read(); int keyLength = BitConverter.readInt(input); int valueLength = BitConverter.readInt(input); validateHeader(keyLength, valueLength); return new Header(version, keyLength, valueLength); }
private int deserializeInt(ByteArraySegment serialized) { return BitConverter.readInt(serialized, 0); }
/** * Reads the Entry's Header from the given {@link ArrayView}. * * @param input The {@link ArrayView} to read from. * @return The Entry Header. * @throws SerializationException If an invalid header was detected. */ Header readHeader(@NonNull ArrayView input) throws SerializationException { byte version = input.get(0); int keyLength = BitConverter.readInt(input, 1); int valueLength = BitConverter.readInt(input, 1 + Integer.BYTES); validateHeader(keyLength, valueLength); return new Header(version, keyLength, valueLength); }
/** * 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; }
ReadFrameHeader(InputStream source) throws IOException { Preconditions.checkNotNull(source, "source"); byte version = (byte) source.read(); setVersion(version); if (version < 0) { throw new EOFException(); } setContentLength(BitConverter.readInt(source)); byte flags = (byte) source.read(); if (flags < 0) { throw new EOFException(); } decodeFlags(flags, version); } }
int count = BitConverter.readInt(data, 0); int offset = Integer.BYTES; byte[] keyHashArray = new byte[HASH_LENGTH];
public static Int96 fromBytes(byte[] b) { int msb = BitConverter.readInt(b, 0); long lsb = BitConverter.readLong(b, Integer.BYTES); return new Int96(msb, lsb); }
ReadEntryHeader(InputStream inputStream) throws IOException { setEntryLength(BitConverter.readInt(inputStream)); byte flags = (byte) inputStream.read(); if (flags < 0) { throw new EOFException(); } setFirstRecordEntry((flags & FIRST_ENTRY_MASK) == FIRST_ENTRY_MASK); setLastRecordEntry((flags & LAST_ENTRY_MASK) == LAST_ENTRY_MASK); } }
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.contentLength = BitConverter.readInt(source, sourceOffset); sourceOffset += LENGTH_LENGTH; Preconditions.checkArgument(this.contentLength >= 0, "Payload length must be a positive integer.");
/** * Creates a new instance of the RevisionDataInputStream class. Upon a successful call to this method, 4 bytes * will have been read from the InputStream representing the expected length of the serialization. * * @param inputStream The InputStream to wrap. * @throws IOException If an IO Exception occurred. */ static RevisionDataInputStream wrap(InputStream inputStream) throws IOException { int bound = BitConverter.readInt(inputStream); return new RevisionDataInputStream(new BoundedInputStream(inputStream, bound)); }
private CompletableFuture<Integer> getSegmentSealedEpoch(long segmentId) { return getSegmentSealedRecordData(segmentId).handle((x, e) -> { if (e != null) { if (Exceptions.unwrap(e) instanceof DataNotFoundException) { return -1; } else { throw new CompletionException(e); } } return BitConverter.readInt(x.getData(), 0); }); }
@Override public CompletableFuture<Integer> getSafeStartingSegmentNumberFor(final String scopeName, final String streamName) { return storeHelper.getData(String.format(DELETED_STREAMS_PATH, getScopedStreamName(scopeName, streamName))) .handleAsync((data, ex) -> { if (ex == null) { return BitConverter.readInt(data.getData(), 0) + 1; } else if (ex instanceof StoreException.DataNotFoundException) { return 0; } else { log.error("Problem found while getting a safe starting segment number for {}.", getScopedStreamName(scopeName, streamName), ex); throw new CompletionException(ex); } }); }
@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()))); } }); }
/** * Creates a new instance of the BTreePage class wrapping an existing ByteArraySegment. * * @param config Page Configuration. * @param contents The ByteArraySegment to wrap. Changes to this BTreePage may change the values in the array backing * this ByteArraySegment. * @param validate If true, will perform validation. * @throws IllegalDataFormatException If the given contents is not a valid BTreePage format and validate == true. */ private BTreePage(@NonNull Config config, @NonNull ByteArraySegment contents, boolean validate) { Preconditions.checkArgument(!contents.isReadOnly(), "Cannot wrap a read-only ByteArraySegment."); this.config = config; this.contents = contents; this.header = contents.subSegment(0, DATA_OFFSET); this.data = contents.subSegment(DATA_OFFSET, contents.getLength() - DATA_OFFSET - FOOTER_LENGTH); this.footer = contents.subSegment(contents.getLength() - FOOTER_LENGTH, FOOTER_LENGTH); if (validate) { int headerId = getHeaderId(); int footerId = getFooterId(); if (headerId != footerId) { throw new IllegalDataFormatException("Invalid Page Format (id mismatch). HeaderId=%s, FooterId=%s.", headerId, footerId); } } // Cache the count value. It's used a lot. this.count = BitConverter.readInt(this.header, COUNT_OFFSET); }
int count = BitConverter.readInt(entryData, 0); BitConverter.writeInt(entryData, 0, count + 1);