Java Code Examples for net.openhft.chronicle.bytes.Bytes#clear()

The following examples show how to use net.openhft.chronicle.bytes.Bytes#clear() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TcpChannelHub.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
/**
 * process system messages which originate from the server
 *
 * @param header      a value representing the type of message
 * @param messageSize the size of the message
 * @throws IOException if a buffer cannot be read
 */
private void processServerSystemMessage(final int header, final int messageSize) throws IOException {

    serverHeartBeatHandler.clear();
    final Bytes bytes = serverHeartBeatHandler;

    bytes.clear();
    @NotNull final ByteBuffer byteBuffer = (ByteBuffer) bytes.underlyingObject();
    byteBuffer.clear();
    // we have to first write the header back to the bytes so that is can be
    // viewed as a document
    bytes.writeInt(0, header);
    byteBuffer.position(SIZE_OF_SIZE);
    byteBuffer.limit(SIZE_OF_SIZE + messageSize);
    readBuffer(byteBuffer);

    bytes.readLimit(byteBuffer.position());

    final StringBuilder eventName = Wires.acquireStringBuilder();
    final Wire inWire = TcpChannelHub.this.wireType.apply(bytes);
    if (YamlLogging.showHeartBeats())
        logToStandardOutMessageReceived(inWire);
    inWire.readDocument(null, d -> {
                @NotNull final ValueIn valueIn = d.readEventName(eventName);
                if (EventId.heartbeat.contentEquals(eventName))
                    reflectServerHeartbeatMessage(valueIn);
                else if (EventId.onClosingReply.contentEquals(eventName))
                    receivedClosedAcknowledgement.countDown();

            }
    );
}
 
Example 2
Source File: DirectChronicleQueueStringTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
private void writeSome(DirectChronicleQueue chronicle) {
    NativeBytesStore allocate = NativeBytesStore.nativeStoreWithFixedCapacity(EXPECTED_BYTES.length);
    final Bytes toWrite = allocate.bytes();
    for (int i = 0; i < RUNS; i++) {
        toWrite.clear();
        toWrite.write(EXPECTED_BYTES);
        toWrite.flip();
        chronicle.appendDocument(toWrite);
    }
}
 
Example 3
Source File: DirectChronicleQueueStringTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
private void readSome(DirectChronicleQueue chronicle) {
    NativeBytesStore allocate = NativeBytesStore.nativeStoreWithFixedCapacity(EXPECTED_BYTES.length);
    final Bytes toRead = allocate.bytes();
    AtomicLong offset = new AtomicLong(chronicle.firstBytes());
    for (int i = 0; i < RUNS; i++) {
        toRead.clear();
        chronicle.readDocument(offset, toRead);
    }
}
 
Example 4
Source File: ThreadedQueueTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void testTailerReadingEmptyQueue() {
    final File path = DirectoryUtils.tempDir("testTailerReadingEmptyQueue");

    try (final ChronicleQueue rqueue = SingleChronicleQueueBuilder.fieldlessBinary(path)
            .testBlockSize()
            .rollCycle(TEST_DAILY)
            .build()) {

        final ExcerptTailer tailer = rqueue.createTailer();

        try (final ChronicleQueue wqueue = SingleChronicleQueueBuilder.fieldlessBinary(path)
                .testBlockSize()
                .rollCycle(TEST_DAILY)
                .build()) {

            Bytes bytes = Bytes.elasticByteBuffer();
            assertFalse(tailer.readBytes(bytes));

            final ExcerptAppender appender = wqueue.acquireAppender();
            appender.writeBytes(Bytes.wrapForRead("Hello World".getBytes(ISO_8859_1)));

            bytes.clear();
            assertTrue(tailer.readBytes(bytes));
            assertEquals("Hello World", bytes.toString());

            bytes.releaseLast();
        }
    }
}
 
Example 5
Source File: VanillaChronicleMap.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
private V searchValue(CompiledMapQueryContext<K, V, R> q,
                      long searchKey, long searchStartPos, long tierBaseAddr,
                      long inputKeySize, Data<K> inputKey, V using) {
    CompactOffHeapLinearHashTable hl = this.hashLookup;

    PointerBytesStore segmentBytesStore = q.segmentBS;
    segmentBytesStore.set(tierBaseAddr, tierSize);
    Bytes bs = q.segmentBytes;
    bs.clear();
    long freeListOffset = tierHashLookupOuterSize + TIER_COUNTERS_AREA_SIZE;
    long entrySpaceOffset = freeListOffset + tierFreeListOuterSize + tierEntrySpaceInnerOffset;

    long hlPos = searchStartPos;
    searchLoop:
    while (true) {
        long entryPos;
        nextPos:
        {
            while (true) {
                long entry = hl.readEntryVolatile(tierBaseAddr, hlPos);
                if (hl.empty(entry)) {
                    break searchLoop;
                }
                hlPos = hl.step(hlPos);
                if (hlPos == searchStartPos) {
                    throw new IllegalStateException(
                            toIdentityString() + ": HashLookup overflow should never occur");
                }

                if ((hl.key(entry)) == searchKey) {
                    entryPos = hl.value(entry);
                    break nextPos;
                }
            }
        }

        long keySizeOffset = entrySpaceOffset + (entryPos * chunkSize);
        bs.readLimitToCapacity();
        bs.readPosition(keySizeOffset);
        long keySize = keySizeMarshaller.readSize(bs);
        long keyOffset = bs.readPosition();
        if (!((inputKeySize == keySize) &&
                (inputKey.equivalent(segmentBytesStore, keyOffset)))) {
            continue;
        }
        long valueSizeOffset = keyOffset + keySize;
        bs.readPosition(valueSizeOffset);
        long valueSize = readValueSize(bs);
        return q.valueReader.read(bs, valueSize, using);
    }
    return null;
}
 
Example 6
Source File: ThroughputPerfMain2.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
public static void main(String[] args) {
    String base = path + "/delete-" + System.nanoTime() + ".me";
    long start = System.nanoTime();
    long count = 0;
    nbs = NativeBytesStore.nativeStoreWithFixedCapacity(size);

    long blockSize = OS.is64Bit() ? 4L << 30 : 256L << 20;
    try (ChronicleQueue q = SingleChronicleQueueBuilder.binary(base)
            .rollCycle(RollCycles.LARGE_HOURLY_XSPARSE)
            .blockSize(blockSize)
            .build()) {

        ExcerptAppender appender = q.acquireAppender();
        count += appender.batchAppend(time * 1000, ThroughputPerfMain2::writeMessages);
    }

    nbs.releaseLast();
    long mid = System.nanoTime();
    long time1 = mid - start;

    Bytes bytes = Bytes.allocateElasticDirect(64);
    try (ChronicleQueue q = SingleChronicleQueueBuilder.binary(base)
            .rollCycle(RollCycles.LARGE_HOURLY_XSPARSE)
            .blockSize(blockSize)
            .build()) {
        ExcerptTailer tailer = q.createTailer();
        for (long i = 0; i < count; i++) {
            try (DocumentContext dc = tailer.readingDocument()) {
                bytes.clear();
                bytes.write(dc.wire().bytes());
            }
        }
    }
    bytes.releaseLast();
    long end = System.nanoTime();
    long time2 = end - mid;

    System.out.printf("Writing %,d messages took %.3f seconds, at a rate of %,d per second%n",
            count, time1 / 1e9, (long) (1e9 * count / time1));
    System.out.printf("Reading %,d messages took %.3f seconds, at a rate of %,d per second%n",
            count, time2 / 1e9, (long) (1e9 * count / time2));

    System.gc(); // make sure its cleaned up for windows to delete.
    IOTools.deleteDirWithFiles(base, 2);
}