private void serializeNewChunk(RollingSegmentHandle handle, SegmentChunk newSegmentChunk) throws StreamSegmentException { updateHandle(handle, HandleSerializer.serializeChunk(newSegmentChunk)); }
/** * Serializes an entry that indicates a number of SegmentChunks are concatenated at a specified offset. * * @param chunkCount The number of SegmentChunks to concat. * @param concatOffset The concat offset. * @return A byte array containing the serialization. */ static byte[] serializeConcat(int chunkCount, long concatOffset) { return combine(KEY_CONCAT, chunkCount + CONCAT_SEPARATOR + concatOffset); }
os.write(serialize(source)); for (int i = 0; i < concatCount; i++) { os.write(HandleSerializer.serializeConcat(chunkCount, source.length())); if (i % failConcatEvery != 0) { chunk.setLength(i + 1); source.addChunks(Collections.singletonList(chunk)); os.write(HandleSerializer.serializeChunk(chunk)); val newHandle = HandleSerializer.deserialize(serialization, source.getHeaderHandle()); assertHandleEquals(source, newHandle, source.getHeaderHandle()); Assert.assertEquals("getHeaderLength", serialization.length, newHandle.getHeaderLength());
ArrayList<SegmentChunk> segmentChunks = new ArrayList<>(); while (st.hasMoreTokens()) { val entry = parse(st.nextToken()); if (entry.getKey().equalsIgnoreCase(KEY_POLICY_MAX_SIZE)) { Preconditions.checkArgument(isValidLong(entry.getValue()), "Invalid entry value for '%s'.", entry); policy = new SegmentRollingPolicy(Long.parseLong(entry.getValue())); val concatInfo = parseConcat(entry.getValue()); om.set(concatInfo.getKey(), concatInfo.getValue()); } else { Preconditions.checkArgument(isValidLong(entry.getKey()), "Invalid key value for '%s'.", entry); long offset = om.adjustOffset(Long.parseLong(entry.getKey())); SegmentChunk s = new SegmentChunk(entry.getValue(), offset);
/** * Serializes an entire RollingSegmentHandle into a new ByteArraySegment. * * @param handle The RollingSegmentHandle to serialize. * @return A ByteArraySegment with the serialization. */ @SneakyThrows(IOException.class) static ByteArraySegment serialize(RollingSegmentHandle handle) { try (EnhancedByteArrayOutputStream os = new EnhancedByteArrayOutputStream()) { //1. Policy Max Size. os.write(combine(KEY_POLICY_MAX_SIZE, Long.toString(handle.getRollingPolicy().getMaxLength()))); //2. Chunks. handle.chunks().forEach(chunk -> os.write(serializeChunk(chunk))); return os.getData(); } }
private RollingSegmentHandle readHeader(SegmentProperties headerInfo, SegmentHandle headerHandle) throws StreamSegmentException { byte[] readBuffer = new byte[(int) headerInfo.getLength()]; this.baseStorage.read(headerHandle, 0, readBuffer, 0, readBuffer.length); RollingSegmentHandle handle = HandleSerializer.deserialize(readBuffer, headerHandle); if (headerInfo.isSealed()) { handle.markSealed(); } return handle; }
@SneakyThrows(IOException.class) private byte[] serialize(RollingSegmentHandle handle) { val s = HandleSerializer.serialize(handle); return StreamHelpers.readAll(s.getReader(), s.getLength()); }
private void serializeBeginConcat(RollingSegmentHandle targetHandle, RollingSegmentHandle sourceHandle) throws StreamSegmentException { byte[] updateData = HandleSerializer.serializeConcat(sourceHandle.chunks().size(), targetHandle.length()); updateHandle(targetHandle, updateData); }
/** * Tests the basic Serialization-Deserialization for a Handle with no concat executed on it. */ @Test public void testNormalSerialization() { final int chunkCount = 1000; val source = newHandle(chunkCount); val serialization = serialize(source); val newHandle = HandleSerializer.deserialize(serialization, source.getHeaderHandle()); assertHandleEquals(source, newHandle, source.getHeaderHandle()); Assert.assertEquals("getHeaderLength", serialization.length, newHandle.getHeaderLength()); }
private void serializeHandle(RollingSegmentHandle handle) throws StreamSegmentException { ByteArraySegment handleData = HandleSerializer.serialize(handle); try { this.baseStorage.write(handle.getHeaderHandle(), 0, handleData.getReader(), handleData.getLength()); handle.setHeaderLength(handleData.getLength()); log.debug("Header for '{}' fully serialized to '{}'.", handle.getSegmentName(), handle.getHeaderHandle().getSegmentName()); } catch (BadOffsetException ex) { // If we get BadOffsetException when writing the Handle, it means it was modified externally. throw new StorageNotPrimaryException(handle.getSegmentName(), ex); } }
/** * Serializes a single SegmentChunk. * * @param segmentChunk The SegmentChunk to serialize. * @return A byte array containing the serialization. */ static byte[] serializeChunk(SegmentChunk segmentChunk) { return combine(Long.toString(segmentChunk.getStartOffset()), segmentChunk.getName()); }