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

The following examples show how to use net.openhft.chronicle.bytes.Bytes#readPosition() . 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: KeySearch.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
public void initKeySearch() {
    for (long pos; (pos = hashLookupSearch.nextPos()) >= 0L; ) {
        // otherwise we are inside iteration relocation.
        // During iteration, key search occurs when doReplaceValue() exhausts space in
        // the current segment, and insertion into the tiered segment requires to locate
        // an empty slot in the hashLookup.
        if (inputKeyInit()) {
            long keySizeOffset = s.entrySpaceOffset + pos * mh.m().chunkSize;
            Bytes segmentBytes = s.segmentBytesForRead();
            segmentBytes.readPosition(keySizeOffset);
            long keySize = mh.h().keySizeMarshaller.readSize(segmentBytes);
            long keyOffset = segmentBytes.readPosition();
            if (!keyEquals(keySize, keyOffset))
                continue;
            hashLookupSearch.found();
            entry.readFoundEntry(pos, keySizeOffset, keySize, keyOffset);
            searchState = PRESENT;
            return;
        }
    }
    searchState = SearchState.ABSENT;
}
 
Example 2
Source File: ReplicatedInput.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
public void processReplicatedEvent(byte remoteNodeIdentifier, Bytes replicatedInputBytes) {
    long timestamp = replicatedInputBytes.readStopBit();
    byte identifier = replicatedInputBytes.readByte();
    ru.initReplicationUpdate(identifier, timestamp, remoteNodeIdentifier);

    boolean isDeleted = replicatedInputBytes.readBoolean();
    long keySize = mh.m().keySizeMarshaller.readSize(replicatedInputBytes);
    long keyOffset = replicatedInputBytes.readPosition();

    q.initInputKey(q.getInputKeyBytesAsData(replicatedInputBytes, keyOffset, keySize));
    replicatedInputBytes.readSkip(keySize);
    if (isDeleted) {
        s.innerUpdateLock.lock();
        mh.m().remoteOperations.remove(this);
    } else {
        long valueSize = mh.m().valueSizeMarshaller.readSize(replicatedInputBytes);
        long valueOffset = replicatedInputBytes.readPosition();
        Data<V> value = q.wrapValueBytesAsData(replicatedInputBytes, valueOffset, valueSize);
        replicatedInputBytes.readSkip(valueSize);
        s.innerWriteLock.lock();
        mh.m().remoteOperations.put(this, value);
    }
}
 
Example 3
Source File: FixSaxParserTest.java    From SAXophone with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void timeParseSingleOrder() {
    String s = "8=FIX.4.2|9=130|35=D|34=659|49=BROKER04|56=REUTERS|52=20070123-19:09:43|38=1000|59=1|100=N|40=1|11=ORD10001|60=20070123-19:01:17|55=HPQ|54=1|21=2|10=004|";
    Bytes nb = Bytes.from(s.replace('|', '\u0001'));

    final AtomicInteger count = new AtomicInteger();
    FixSaxParser parser = new FixSaxParser(new MyFixHandler(count));
    int runs = 200000;
    for (int t = 0; t < 5; t++) {
        count.set(0);
        long start = System.nanoTime();
        for (int i = 0; i < runs; i++) {
            nb.readPosition(0);
            parser.parse(nb);
        }
        long time = System.nanoTime() - start;
        System.out.printf("Average parse time was %.2f us, fields per message %.2f%n",
                time / runs / 1e3, (double) count.get() / runs);
    }
}
 
Example 4
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 5
Source File: FixSaxParser.java    From SAXophone with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void parse(Bytes bytes) {
    long limit = bytes.readLimit(), limit2 = limit;
    while (limit2 > bytes.readPosition() && bytes.readByte(limit2 - 1) != FIELD_TERMINATOR)
        limit2--;
    bytes.readLimit(limit2);

    while (bytes.readRemaining() > 0) {
        long fieldNum = bytes.parseLong();
        long pos = bytes.readPosition();

        searchForTheEndOfField(bytes);

        long end = bytes.readPosition() - 1;
        bytes.readLimit(end);
        bytes.readPosition(pos);
        handler.completeMessage(bytes);
        handler.onField(fieldNum, bytes);

        bytes.readLimit(limit);
        bytes.readPosition(end + 1);
    }

    bytes.readLimit(limit);
    bytes.readPosition(limit2);
}
 
Example 6
Source File: JsonParser.java    From SAXophone with GNU Lesser General Public License v3.0 6 votes vote down vote up
boolean on() throws IOException {
    Bytes buf = lexer.outBuf;
    long bufPos = lexer.outPos;
    long bufLen = lexer.outLen;
    boolean borrowBuf = buf.readPosition() != bufPos ||
            buf.readRemaining() != bufLen;
    long pos = 0, lim = 0;
    if (borrowBuf) {
        pos = buf.readPosition();
        lim = buf.readLimit();
        //buf.clear();
        buf.readLimit(bufPos + bufLen);
        buf.readPosition(bufPos);
    }
    boolean go = apply(value(buf));
    if (borrowBuf) {
        //buf.clear();
        buf.readLimit(lim);
        buf.readPosition(pos);
    }
    return go;
}
 
Example 7
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 8
Source File: BenchmarkMain.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
private static long readMessage(Bytes<?> bytes) {
    Jvm.safepoint();
    long start = bytes.readLong();
    long rp = bytes.readPosition();
    long rl = bytes.readLimit();
    long addr = bytes.addressForRead(rp);
    long addrEnd = bytes.addressForRead(rl);
    Memory memory = OS.memory();
    for (addr += 8; addr + 7 < addrEnd; addr += 8)
        memory.readLong(addr);
    Jvm.safepoint();
    return start;
}
 
Example 9
Source File: HashEntryStages.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
public void readExistingEntry(long pos) {
    initPos(pos);
    Bytes segmentBytes = s.segmentBytesForRead();
    segmentBytes.readPosition(keySizeOffset);
    initKeySize(hh.h().keySizeMarshaller.readSize(segmentBytes));
    initKeyOffset(segmentBytes.readPosition());
}
 
Example 10
Source File: MapEntryStages.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
void initValueSize() {
    Bytes segmentBytes = s.segmentBytesForRead();
    segmentBytes.readPosition(valueSizeOffset);
    valueSize = mh.m().readValueSize(segmentBytes);
    valueOffset = segmentBytes.readPosition();
}
 
Example 11
Source File: Lexer.java    From SAXophone with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * scan a string for interesting characters that might need further
 * review.  return the number of chars that are uninteresting and can
 * be skipped.
 * (lth) hi world, any thoughts on how to make this routine faster?
 */
private void stringScan(Bytes bytes) {
    int mask = IJC | NFP | (validateUTF8 ? NUC : 0);
    long pos = bytes.readPosition();
    long limit = bytes.readLimit();
    while (pos < limit && ((CHAR_LOOKUP_TABLE[bytes.readUnsignedByte(pos)] & mask) == 0)) {
        pos++;
    }
    bytes.readPosition(pos);
}
 
Example 12
Source File: JsonParser.java    From SAXophone with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void tryRestoreErrorEffect(Bytes jsonText, long startOffset) {
    long pos = jsonText.readPosition();
    if (pos - startOffset >= lexer.outLen) {
        jsonText.readPosition(pos - lexer.outLen);

    } else {
        jsonText.readPosition(startOffset);
    }
}
 
Example 13
Source File: WireTcpHandler.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
/**
 * write and exceptions and rolls back if no data was written
 */
protected void writeData(final boolean isNotComplete,
                         @NotNull final Bytes<?> inBytes,
                         @NotNull final WriteMarshallable c) {

    @NotNull final WriteMarshallable marshallable = out -> {
        final long readPosition = inBytes.readPosition();
        final long position = outWire.bytes().writePosition();
        try {
            c.writeMarshallable(outWire);
        } catch (Throwable t) {
            inBytes.readPosition(readPosition);
            if (LOG.isInfoEnabled())
                LOG.info("While reading " + inBytes.toDebugString(),
                        " processing wire " + c, t);
            outWire.bytes().writePosition(position);
            outWire.writeEventName(() -> "exception").throwable(t);
        }

        // write 'reply : {} ' if no data was sent
        if (position == outWire.bytes().writePosition()) {
            outWire.writeEventName(reply).marshallable(EMPTY);
        }
    };

    if (isNotComplete)
        outWire.writeNotCompleteDocument(false, marshallable);
    else
        outWire.writeDocument(false, marshallable);

    logYaml(outWire);
}
 
Example 14
Source File: WireTcpHandler.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
private void ensureCapacity() {
    @NotNull final Bytes<?> bytes = inWire.bytes();
    if (bytes.readRemaining() >= 4) {
        final long pos = bytes.readPosition();
        int length = bytes.readInt(pos);
        final long size = pos + Wires.SPB_HEADER_SIZE * 2 + Wires.lengthOf(length);
        if (size > bytes.realCapacity()) {
            resizeInWire(size);
        }
    }
}
 
Example 15
Source File: BatchSignAndVerifyEd25519Test.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void signAndVerify() {
    Bytes privateKeyBuffer = null;
    Bytes secretKeyBuffer = null;
    Bytes privateOrSecret = bft.fromHex(privateOrSecretKey);
    if (privateOrSecret.readRemaining() == Ed25519.SECRET_KEY_LENGTH) {
        secretKeyBuffer = privateOrSecret;
    } else {
        privateKeyBuffer = privateOrSecret;
    }

    Bytes publicKeyBuffer = bft.fromHex(publicKey);
    if (secretKeyBuffer == null) {
        secretKeyBuffer = bft.bytesWithZeros(Ed25519.SECRET_KEY_LENGTH);
        Bytes tmpPublicKeyBuffer = bft.bytesWithZeros(Ed25519.PUBLIC_KEY_LENGTH);
        Ed25519.privateToPublicAndSecret(tmpPublicKeyBuffer, secretKeyBuffer, privateKeyBuffer);
        assertEquals(publicKeyBuffer.toHexString(), tmpPublicKeyBuffer.toHexString());
    }
    Bytes messageBuffer = bft.fromHex(message);
    Bytes signExpectedBuffer;
    if (signExpected.length() == 128) {
        signExpectedBuffer = Bytes.wrapForRead(DatatypeConverter.parseHexBinary(signExpected + message));
    } else {
        signExpectedBuffer = Bytes.wrapForRead(DatatypeConverter.parseHexBinary(signExpected));
    }
    Bytes signedMsgBuffer = bft.fromHex(Ed25519.SIGNATURE_LENGTH, message);
    signedMsgBuffer.writePosition(0);
    Ed25519.sign(signedMsgBuffer, messageBuffer, secretKeyBuffer);
    assertEquals(signExpectedBuffer.toHexString(), signedMsgBuffer.toHexString());
    signedMsgBuffer.readPosition(0);
    publicKeyBuffer.readPositionRemaining(0, Ed25519.PUBLIC_KEY_LENGTH);
    assertTrue(Ed25519.verify(signedMsgBuffer, publicKeyBuffer));
}
 
Example 16
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 17
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 18
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 19
Source File: ChronicleMapBuilder.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
private VanillaChronicleMap<K, V, ?> openWithExistingFile(
        File file, RandomAccessFile raf, ChronicleHashResources resources,
        boolean recover, boolean overrideBuilderConfig,
        ChronicleHashCorruption.Listener corruptionListener)
        throws IOException {
    ChronicleHashCorruptionImpl corruption = recover ? new ChronicleHashCorruptionImpl() : null;
    try {
        int headerSize = waitUntilReady(raf, file, recover);
        FileChannel fileChannel = raf.getChannel();
        ByteBuffer headerBuffer = readSelfBootstrappingHeader(
                file, raf, headerSize, recover, corruptionListener, corruption);
        if (headerSize != headerBuffer.remaining())
            throw new AssertionError();
        boolean headerCorrect = checkSumSelfBootstrappingHeader(headerBuffer, headerSize);
        boolean headerWritten = false;
        if (!headerCorrect) {
            if (overrideBuilderConfig) {
                VanillaChronicleMap<K, V, ?> mapObjectForHeaderOverwrite = newMap();
                headerBuffer = writeHeader(fileChannel, mapObjectForHeaderOverwrite);
                headerSize = headerBuffer.remaining();
                headerWritten = true;
            } else {
                throw throwRecoveryOrReturnIOException(file,
                        "Self Bootstrapping Header checksum doesn't match the stored checksum",
                        recover);
            }
        }
        Bytes<ByteBuffer> headerBytes = Bytes.wrapForRead(headerBuffer);
        headerBytes.readPosition(headerBuffer.position());
        headerBytes.readLimit(headerBuffer.limit());
        Wire wire = new TextWire(headerBytes);
        VanillaChronicleMap<K, V, ?> map = wire.getValueIn().typedMarshallable();
        map.initBeforeMapping(file, raf, headerBuffer.limit(), recover);
        long dataStoreSize = map.globalMutableState().getDataStoreSize();
        if (!recover && dataStoreSize > file.length()) {
            throw new IOException("The file " + file + " the map is serialized from " +
                    "has unexpected length " + file.length() + ", probably corrupted. " +
                    "Data store size is " + dataStoreSize);
        }
        map.initTransientsFromBuilder(this);
        if (!recover) {
            map.createMappedStoreAndSegments(resources);
        } else {
            if (!headerWritten)
                writeNotComplete(fileChannel, headerBuffer, headerSize);
            map.recover(resources, corruptionListener, corruption);
            commitChronicleMapReady(map, raf, headerBuffer, headerSize);
        }
        return map;
    } catch (Throwable t) {
        if (recover && !(t instanceof IOException) &&
                !(t instanceof ChronicleHashRecoveryFailedException)) {
            throw new ChronicleHashRecoveryFailedException(t);
        }
        throw Throwables.propagateNotWrapping(t, IOException.class);
    }
}
 
Example 20
Source File: EntryKeyBytesData.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
private K innerGetUsing(K usingKey) {
    Bytes bytes = s.segmentBytesForRead();
    bytes.readPosition(entry.keyOffset);
    return ki.keyReader.read(bytes, size(), usingKey);
}