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

The following examples show how to use net.openhft.chronicle.bytes.Bytes#releaseLast() . 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: AppenderFileHandleLeakTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
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);
        assertTrue(Math.signum(bytes.readInt()) >= 0);

        if (manuallyReleaseResources) {
            try {
                ((StoreTailer) tailer).releaseResources();
            } catch (RuntimeException e) {
                // ignore
            }
        }
    } finally {
        bytes.releaseLast();
    }
}
 
Example 2
Source File: Ed25519Test.java    From Chronicle-Salt with Apache License 2.0 6 votes vote down vote up
@Test
public void sign3() {
    final String SIGN_PRIVATE = "B18E1D0045995EC3D010C387CCFEB984D783AF8FBB0F40FA7DB126D889F6DADD";
    Bytes privateKey = fromHex(SIGN_PRIVATE);
    Bytes publicKey = bytesWithZeros(32);
    Bytes secretKey = bytesWithZeros(64);
    Ed25519.privateToPublicAndSecret(publicKey, secretKey, privateKey);
    checkZeros(secretKey);
    checkZeros(privateKey);

    Bytes message = privateKey;
    Bytes signAndMsg = Bytes.allocateDirect(Ed25519.SIGNATURE_LENGTH + message.readRemaining());
    Ed25519.sign(signAndMsg, message, secretKey);

    String SIGN_EXPECTED = "86b4707fadb1ef4613efadd12143cd9dffb2eac329c38923c03f9e315c3dd33bde1ef101137fbc403eb3f3d7ff283155053c667eb65908fe6fcd653eab550e0f";
    Bytes signExpected = fromHex(SIGN_EXPECTED + SIGN_PRIVATE);
    assertEquals(signExpected.toHexString(), signAndMsg.toHexString());

    signAndMsg.releaseLast();
}
 
Example 3
Source File: Ed25519Test.java    From Chronicle-Salt with Apache License 2.0 6 votes vote down vote up
@Test
public void sign2() {
    final String SIGN_PRIVATE = "B18E1D0045995EC3D010C387CCFEB984D783AF8FBB0F40FA7DB126D889F6DADD";
    Bytes privateKey = fromHex(SIGN_PRIVATE);
    Bytes publicKey = bytesWithZeros(32);
    Bytes secretKey = bytesWithZeros(64);
    Ed25519.privateToPublicAndSecret(publicKey, secretKey, privateKey);
    checkZeros(secretKey);
    checkZeros(privateKey);

    Bytes message = privateKey;
    Bytes signAndMsg = Bytes.allocateDirect(Ed25519.SIGNATURE_LENGTH + message.readRemaining());
    signAndMsg.writeSkip(Ed25519.SIGNATURE_LENGTH);
    signAndMsg.write(privateKey);
    Ed25519.sign(signAndMsg, secretKey);

    String SIGN_EXPECTED = "86b4707fadb1ef4613efadd12143cd9dffb2eac329c38923c03f9e315c3dd33bde1ef101137fbc403eb3f3d7ff283155053c667eb65908fe6fcd653eab550e0f";
    Bytes signExpected = fromHex(SIGN_EXPECTED + SIGN_PRIVATE);
    assertEquals(signExpected.toHexString(), signAndMsg.toHexString());

    signAndMsg.releaseLast();
}
 
Example 4
Source File: BatchSha256Rc4Test.java    From Chronicle-Salt with Apache License 2.0 6 votes vote down vote up
@Test
public void testHash() {
    if ((testCounter % 250) == 0) {
        long newTime = System.currentTimeMillis();
        System.out.println("Executing test number " + testCounter + " for data size " + size + " time since last log "
                + String.format("%.2f", ((newTime - timePassed) / 1000.0)) + " sec(s)");
        timePassed = newTime;
    }
    testCounter++;
    Bytes<?> bytesMessage = testDataBytes;
    bytesMessage.readPositionRemaining(0, size);
    Bytes<?> sha256Actual = hash256Bytes.get();
    sha256Actual.writePosition(0);
    SHA2.appendSha256(sha256Actual, bytesMessage);
    sha256Actual.readPosition(0);

    Bytes<?> sha256Expected = bft.fromHex(sha256);
    sha256Expected.readPosition(0);

    assertEquals(sha256Expected.toHexString(SHA2.HASH_SHA256_BYTES), sha256Actual.toHexString(SHA2.HASH_SHA256_BYTES));
    sha256Expected.releaseLast();
}
 
Example 5
Source File: SingleCQFormat2Test.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
public void appendMessage(@NotNull ChronicleQueue queue, long expectedIndex, String msg) {
    @NotNull ExcerptAppender appender = queue.acquireAppender();
    switch (appendMode) {
        case 1:
            appender.writeDocument(w -> w.write(() -> "msg").text(msg));
            break;

        case 2:
            Bytes bytes = Bytes.elasticByteBuffer();
            new BinaryWire(bytes).write(() -> "msg").text(msg);
            appender.writeBytes(bytes);
            bytes.releaseLast();

            break;

        default:
            try (DocumentContext dc = appender.writingDocument()) {
                Wire wire = dc.wire();
                wire.write(() -> "msg").text(msg);
            }
            break;
    }

    long index = appender.lastIndexAppended();
    assertHexEquals(expectedIndex, index);
}
 
Example 6
Source File: BytesBufferHandler.java    From Chronicle-Network with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void handleDecryptedData(final ByteBuffer input, final ByteBuffer output) {
    final Bytes<ByteBuffer> applicationInput;
    if (input.position() != 0) {
        input.flip();
        applicationInput = Bytes.wrapForRead(input);
        AbstractReferenceCounted.unmonitor(applicationInput); // temporary wrapper
    } else {
        applicationInput = EMPTY_APPLICATION_INPUT;
    }

    final Bytes<ByteBuffer> applicationOutput = Bytes.wrapForWrite(output);
    try {
        delegateHandler.process(applicationInput, applicationOutput, networkContext);
        output.position((int) applicationOutput.writePosition());
    } finally {
        applicationOutput.releaseLast();
    }

    input.position((int) applicationInput.readPosition());
    if (applicationInput.readPosition() != 0) {
        input.compact();
    }
}
 
Example 7
Source File: Ed25519.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
public KeyPair(char ch) {
    Bytes<Void> privateKey = Bytes.allocateDirect(PRIVATE_KEY_LENGTH);
    while (privateKey.writeRemaining() > 0)
        privateKey.append(ch);
    privateToPublicAndSecret(publicKey, secretKey, privateKey);
    privateKey.releaseLast();
}
 
Example 8
Source File: BatchAppenderNativeTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void testNative() {
    if (!OS.isMacOSX())
        return;

    Bytes<ByteBuffer> bytes = Bytes.elasticByteBuffer();
    try {

        BatchAppenderNative batchAppenderNative = new BatchAppenderNative();

        // this will append a message in wire of hello world
        long result = batchAppenderNative.writeMessages(bytes.addressForWrite(0), bytes.realCapacity(), 1);

        int len = (int) result;
        int count = (int) (result >> 32);
        bytes.readLimit(len);

        Assert.assertEquals(16, len);
        Assert.assertEquals(1, count);

        Wire w = WireType.BINARY.apply(bytes);

        for (int i = 0; i < count; i++) {
            try (DocumentContext dc = w.readingDocument()) {
                Assert.assertEquals("hello world", dc.wire().getValueIn().text());
            }
        }
    } finally {
        bytes.releaseLast();
    }
}
 
Example 9
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 10
Source File: ChronicleAppenderCycleTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void testAppenderCycle() throws IOException {
    String id = "testAppenderCycle";
    Bytes msg = Bytes.allocateDirect(64);
    int n = 20;
    for (int i = 0; i < n; ++i)
        runTest(id + '-' + i, msg);
    msg.releaseLast();
}
 
Example 11
Source File: DumpQueueMain.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
private static void dumpFile(@NotNull File file, @NotNull PrintStream out, long upperLimit) {
    Bytes<ByteBuffer> buffer = Bytes.elasticByteBuffer();
    try (MappedBytes bytes = MappedBytes.mappedBytes(file, 4 << 20, OS.pageSize(), !OS.isWindows())) {
        bytes.readLimit(bytes.realCapacity());
        StringBuilder sb = new StringBuilder();
        WireDumper dumper = WireDumper.of(bytes, !UNALIGNED);
        while (bytes.readRemaining() >= 4) {
            sb.setLength(0);
            boolean last = dumper.dumpOne(sb, buffer);
            if (sb.indexOf("\nindex2index:") != -1 || sb.indexOf("\nindex:") != -1) {
                // truncate trailing zeros
                if (sb.indexOf(", 0\n]\n") == sb.length() - 6) {
                    int i = indexOfLastZero(sb);
                    if (i < sb.length())
                        sb.setLength(i - 5);
                    sb.append(" # truncated trailing zeros\n]");
                }
            }

            out.println(sb);

            if (last)
                break;
            if (bytes.readPosition() > upperLimit) {
                out.println("# limit reached.");
                return;
            }
        }
    } catch (IOException ioe) {
        err.println("Failed to read " + file + " " + ioe);
    } finally {
        buffer.releaseLast();
    }
}
 
Example 12
Source File: Sizer.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
public static int size(final BytesMarshallable message) {
    final Bytes<ByteBuffer> buffer = Bytes.elasticByteBuffer();
    try {
        message.writeMarshallable(buffer);
        return (int) buffer.writePosition();
    } finally {
        buffer.releaseLast();
    }
}
 
Example 13
Source File: BatchSha256Sha512RandomTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testHash() {
    Bytes<?> bytesMessage = bft.fromHex(data);
    bytesMessage.readPosition(0);
    bytesMessage.readPositionRemaining(0, size);

    Bytes<?> actualSha256 = hash256Bytes.get();
    actualSha256.writePosition(0);
    SHA2.appendSha256(actualSha256, bytesMessage);
    actualSha256.readPosition(0);
    Bytes<?> expectedSha256 = bft.fromHex(sha256);
    actualSha256.readPosition(0);
    assertEquals(expectedSha256.toHexString(), actualSha256.toHexString());
    expectedSha256.releaseLast();

    bytesMessage.readPositionRemaining(0, size);
    Bytes<?> actualSha512 = hash512Bytes.get();
    actualSha512.writePosition(0);
    SHA2.appendSha512(actualSha512, bytesMessage);
    actualSha512.readPosition(0);
    Bytes<?> expectedSha512 = bft.fromHex(sha512);
    actualSha512.readPosition(0);
    assertEquals(expectedSha512.toHexString(), actualSha512.toHexString());
    expectedSha512.releaseLast();

    bytesMessage.releaseLast();
}
 
Example 14
Source File: SHA2Test.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
private static void doTest512(String inputStr, String expectedHex) {
    Bytes<?> input = Bytes.allocateElasticDirect();
    input.append(inputStr);
    Bytes<?> hash512 = Bytes.allocateElasticDirect();
    SHA2.appendSha512(hash512, input);
    hash512.readPosition(0);
    Bytes<?> expected = Bytes.allocateElasticDirect();
    expected.write(DatatypeConverter.parseHexBinary(expectedHex));
    assertEquals(expected.toHexString(), hash512.toHexString());
    expected.releaseLast();
    input.releaseLast();
    hash512.releaseLast();
}
 
Example 15
Source File: SHA2Test.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
private static void doTest256(byte[] inputStr, String expectedHex) {
    Bytes<?> input = Bytes.allocateElasticDirect();
    input.write(inputStr);
    Bytes<?> hash256 = Bytes.allocateElasticDirect();
    SHA2.appendSha256(hash256, input);
    Bytes<?> expected = Bytes.allocateElasticDirect();
    expected.write(DatatypeConverter.parseHexBinary(expectedHex));
    assertEquals(expected.toHexString(), hash256.toHexString());
    expected.releaseLast();
    input.releaseLast();
    hash256.releaseLast();
}
 
Example 16
Source File: Ed25519.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
public KeyPair(long id) {
    Bytes<Void> privateKey = Bytes.allocateDirect(PRIVATE_KEY_LENGTH);
    privateKey.zeroOut(0, PRIVATE_KEY_LENGTH);
    privateKey.writeLong(PRIVATE_KEY_LENGTH - Long.BYTES, id);
    privateKey.writeSkip(PRIVATE_KEY_LENGTH);
    privateToPublicAndSecret(publicKey, secretKey, privateKey);
    privateKey.releaseLast();
}
 
Example 17
Source File: MappingReferenceCountTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
/**
     * tests that blocks are acquired and released as needed
     *
     * @
     */
    @Test
    public void testMappingReferenceCount() {

        File tempFile = File.createTempFile("chronicle", "q");

        try {
            int BLOCK_SIZE = 4096;
            final MappedFile mappedFile = MappedFile.mappedFile(tempFile.getName(), BLOCK_SIZE, 8);
            final Bytes bytes = mappedFile.bytes();

            // write into block 1
            bytes.writeLong(4096 + 8, Long.MAX_VALUE);
//            Assert.assertEquals(1, mappedFile.getRefCount(1));
            assertEquals("refCount: 2, 0, 2", mappedFile.referenceCounts());

            // we move from block 1 to block 2
            bytes.writeLong((4096 * 2) + 8, Long.MAX_VALUE);
//            assertEquals(0, mappedFile.getRefCount(1));
//            assertEquals(1, mappedFile.getRefCount(2));
            assertEquals("refCount: 3, 0, 1, 2", mappedFile.referenceCounts());

            // we move from block 2 back to block 1
            bytes.writeLong((4096 * 1) + 8, Long.MAX_VALUE);
//            assertEquals(1, mappedFile.getRefCount(1));
//            assertEquals(0, mappedFile.getRefCount(2));
            assertEquals("refCount: 3, 0, 2, 1", mappedFile.referenceCounts());

            // we move from block 2 back to block 1
            bytes.writeLong((4096 * 3) + 8, Long.MAX_VALUE);
//            assertEquals(1, mappedFile.getRefCount(3));
            assertEquals("refCount: 4, 0, 1, 1, 2", mappedFile.referenceCounts());

            bytes.releaseLast();
            mappedFile.close();

            assertEquals("refCount: 0, 0, 0, 0, 0", mappedFile.referenceCounts());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            tempFile.delete();
        }
    }
 
Example 18
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);
}