static void readMany(Bytes bytes, int size) { for (int i = 0; i < size; i += 32) { s32 = bytes.readInt();// 4 bytes f32 = bytes.readFloat();// 4 bytes s64 = bytes.readLong();// 8 bytes f64 = bytes.readDouble();// 8 bytes s = bytes.readUtf8(); // 8 bytes assertEquals("Hello!!", s); } }
private static void readMessage(final ChronicleQueue queue, final boolean manuallyReleaseResources, final Consumer<ExcerptTailer> refHolder) { final Bytes<ByteBuffer> bytes = Bytes.elasticByteBuffer(); try { final ExcerptTailer tailer = queue.createTailer(); while (bytes.isEmpty()) { tailer.toStart().readBytes(bytes); } refHolder.accept(tailer); assertThat(Math.signum(bytes.readInt()) >= 0, is(true)); if (manuallyReleaseResources) { try { ((SingleChronicleQueueExcerpts.StoreTailer) tailer).releaseResources(); } catch (RuntimeException e) { // ignore } } } finally { bytes.release(); } }
@Test public void testReadingLessBytesThanWritten() { try (final ChronicleQueue queue = builder(getTmpDir(), wireType) .build()) { final ExcerptAppender appender = queue.acquireAppender(); final Bytes<byte[]> expected = Bytes.wrapForRead("some long message".getBytes(ISO_8859_1)); for (int i = 0; i < 10; i++) { appender.writeBytes(expected); } final ExcerptTailer tailer = queue.createTailer(); // Sequential read for (int i = 0; i < 10; i++) { Bytes b = Bytes.allocateDirect(8); tailer.readBytes(b); Assert.assertEquals(expected.readInt(0), b.readInt(0)); b.release(); } } }
@NotNull @Override public Integer read(Bytes in, @Nullable Integer using) { return in.readInt(); }
@NotNull @Override public Integer read(@NotNull Bytes in, long size, @Nullable Integer using) { return in.readInt(); }
@NotNull @Override public List<T> read(Bytes in, @Nullable List<T> using) { int size = in.readInt(); if (using == null) { using = new ArrayList<>(size); for (int i = 0; i < size; i++) { using.add(null); } } else if (using.size() < size) { while (using.size() < size) { using.add(null); } } else if (using.size() > size) { using.subList(size, using.size()).clear(); } for (int i = 0; i < size; i++) { using.set(i, elementReader.read(in, using.get(i))); } return using; }
@NotNull @Override public Set<T> read(Bytes in, @Nullable Set<T> using) { int size = in.readInt(); if (using == null) { using = new HashSet<>((int) (size / 0.75)); for (int i = 0; i < size; i++) { using.add(elementReader.read(in, null)); } } else { orderedElements.addAll(using); using.clear(); for (int i = 0; i < size; i++) { using.add(elementReader.read(in, orderedElements.pollFirst())); } orderedElements.clear(); // for GC, avoid zombie object links } return using; }
@NotNull @Override public Map<K, V> read(Bytes in, @Nullable Map<K, V> using) { int size = in.readInt(); if (using == null) { using = new HashMap<>(((int) (size / 0.75))); for (int i = 0; i < size; i++) { using.put(keyReader.read(in, null), valueReader.read(in, null)); } } else { using.forEach((k, v) -> { orderedKeys.add(k); orderedValues.add(v); }); using.clear(); for (int i = 0; i < size; i++) { using.put(keyReader.read(in, orderedKeys.pollFirst()), valueReader.read(in, orderedValues.pollFirst())); } orderedKeys.clear(); // for GC, avoid zombie object links orderedValues.clear(); } return using; }
@NotNull @Override public HistogramKey read(Bytes in, @Nullable HistogramKey using) { if (using == null) { using = new HistogramKey(); } using.granularityOrdinal = in.readByte(); using.binId = in.readInt(); using.metric = readString(in); using.source = readString(in); int numTags = in.readShort(); if (numTags > 0) { using.tags = new String[numTags]; for (int i = 0; i < numTags; ++i) { using.tags[i] = readString(in); } } return using; }
@NotNull @Override public HistogramKey read(Bytes in, @Nullable HistogramKey using) { if (using == null) { using = new HistogramKey(); } using.granularityOrdinal = in.readByte(); using.binId = in.readInt(); using.metric = readString(in); using.source = readString(in); int numTags = in.readShort(); if (numTags > 0) { using.tags = new String[numTags]; for (int i = 0; i < numTags; ++i) { using.tags[i] = readString(in); } } return using; }