net.openhft.chronicle.core.Maths Java Examples

The following examples show how to use net.openhft.chronicle.core.Maths. 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: SingleChronicleQueueStore.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
/**
 * @param rollCycle    the current rollCycle
 * @param wireType     the wire type that is being used
 * @param mappedBytes  used to mapped the data store file
 * @param indexCount   the number of entries in each index.
 * @param indexSpacing the spacing between indexed entries.
 */
public SingleChronicleQueueStore(@NotNull RollCycle rollCycle,
                                 @NotNull final WireType wireType,
                                 @NotNull MappedBytes mappedBytes,
                                 int indexCount,
                                 int indexSpacing) {
    this.mappedBytes = mappedBytes;
    this.mappedFile = mappedBytes.mappedFile();
    mappedFile.reserve(this);
    indexCount = Maths.nextPower2(indexCount, 8);
    indexSpacing = Maths.nextPower2(indexSpacing, 1);

    this.indexing = new SCQIndexing(wireType, indexCount, indexSpacing);
    this.indexing.writePosition = this.writePosition = wireType.newTwoLongReference().get();
    this.indexing.sequence = this.sequence = new RollCycleEncodeSequence(writePosition,
            rollCycle.defaultIndexCount(),
            rollCycle.defaultIndexSpacing());
    this.dataVersion = 1;
}
 
Example #2
Source File: ValueDataAccess.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
private BytesStore allocateBytesStoreForInstance() {
    long instanceSize = nativeInstance.maxSize();
    if (instanceSize > Bytes.MAX_BYTE_BUFFER_CAPACITY) {
        return NativeBytesStore.nativeStoreWithFixedCapacity(instanceSize);
    } else {
        return BytesStore.wrap(ByteBuffer.allocate(Maths.toUInt31(instanceSize)));
    }
}
 
Example #3
Source File: CompactOffHeapLinearHashTable.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
public static long capacityFor(long entriesPerSegment) {
    if (entriesPerSegment < 0L)
        throw new AssertionError("entriesPerSegment should be positive: " + entriesPerSegment);
    long capacity = Maths.nextPower2(entriesPerSegment, 64L);
    if (entriesPerSegment > MAX_UPPER_BOUND_LOAD_FACTOR * capacity)
        capacity *= 2;
    return capacity;
}
 
Example #4
Source File: VanillaChronicleHash.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
private long computeNumberOfTiersInBulk() {
    // TODO review heuristics
    int tiersInBulk = actualSegments / 8;
    tiersInBulk = Maths.nextPower2(tiersInBulk, 1);
    while (computeTierBulkBytesSize(tiersInBulk) < OS.pageSize()) {
        tiersInBulk *= 2;
    }
    return tiersInBulk;
}
 
Example #5
Source File: MovingAverageArray.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@Override
public void readMarshallable(BytesIn bytes) throws IORuntimeException {
    int len = Maths.toUInt31(bytes.readStopBit());
    values.clear();
    for (int i = 0; i < len; i++) {
        if (buffer.size() <= values.size()) {
            buffer.add(ObjectUtils.newInstance(MovingAverageCompact.class));
        }
        MovingAverageCompact next = buffer.get(i);
        next.readMarshallable(bytes);
        values.add(next);
    }
}
 
Example #6
Source File: SCQIndexing.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
private SCQIndexing(int indexCount, int indexSpacing, LongValue index2Index, LongValue nextEntryToBeIndexed, Supplier<LongArrayValues> longArraySupplier) {
    this.indexCount = indexCount;
    this.indexCountBits = Maths.intLog2(indexCount);
    this.indexSpacing = indexSpacing;
    this.indexSpacingBits = Maths.intLog2(indexSpacing);
    this.index2Index = index2Index;
    this.nextEntryToBeIndexed = nextEntryToBeIndexed;
    this.longArraySupplier = longArraySupplier;
    this.index2indexArray = new ThreadLocal<>();
    this.indexArray = new ThreadLocal<>();
    this.index2IndexTemplate = w -> w.writeEventName(() -> "index2index").int64array(indexCount);
    this.indexTemplate = w -> w.writeEventName(() -> "index").int64array(indexCount);
}
 
Example #7
Source File: RollingResourcesCache.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
/**
 * Cache some resources for a rollCycle number.
 *
 * @param cycle the rollCycle number to format
 * @return the Resource
 */
@NotNull
public Resource resourceFor(long cycle) {
    long millisSinceBeginningOfEpoch = (cycle * length);
    int hash = Maths.hash32(millisSinceBeginningOfEpoch) & (CACHE_SIZE - 1);
    Resource dv = values[hash];
    if (dv == null || dv.millis != millisSinceBeginningOfEpoch) {
        final Instant instant = Instant.ofEpochMilli(millisSinceBeginningOfEpoch + epoch);
        @NotNull String text = formatter.format(instant);
        values[hash] = dv = new Resource(millisSinceBeginningOfEpoch, text, fileFactory.apply(text));
    }
    return dv;
}
 
Example #8
Source File: RollingResourcesCache.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
private int parseCount0(@NotNull String name) {
    try {
        TemporalAccessor parse = formatter.parse(name);

        long epochDay = parse.getLong(ChronoField.EPOCH_DAY) * 86400;
        if (parse.isSupported(ChronoField.SECOND_OF_DAY))
            epochDay += parse.getLong(ChronoField.SECOND_OF_DAY);

        return Maths.toInt32((epochDay - ((epoch) / 1000)) / (length / 1000));
    } catch (DateTimeParseException e) {
        throw new RuntimeException(String.format(
                "Unable to parse %s using format %s", name, format), e);
    }
}
 
Example #9
Source File: WeeklyRollCycle.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
WeeklyRollCycle(String format, int length, int indexCount, int indexSpacing) {
    this.format = format;
    this.length = length;
    this.indexCount = Maths.nextPower2(indexCount, 8);
    this.indexSpacing = Maths.nextPower2(indexSpacing, 1);
    cycleShift = Math.max(32, Maths.intLog2(indexCount) * 2 + Maths.intLog2(indexSpacing));
    sequenceMask = (1L << cycleShift) - 1;
}
 
Example #10
Source File: VanillaChronicleHash.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
public VanillaChronicleHash(ChronicleMapBuilder<K, ?> builder) {
    // Version
    dataFileVersion = BuildVersion.version();

    createdOrInMemory = true;

    @SuppressWarnings({"deprecation", "unchecked"})
    ChronicleHashBuilderPrivateAPI<K, ?> privateAPI =
            (ChronicleHashBuilderPrivateAPI<K, ?>) builder.privateAPI();

    // Data model
    SerializationBuilder<K> keyBuilder = privateAPI.keyBuilder();
    keyClass = keyBuilder.tClass;
    keySizeMarshaller = keyBuilder.sizeMarshaller();
    keyReader = keyBuilder.reader();
    keyDataAccess = keyBuilder.dataAccess();

    actualSegments = privateAPI.actualSegments();
    hashSplitting = HashSplitting.forSegments(actualSegments);

    chunkSize = privateAPI.chunkSize();
    maxChunksPerEntry = privateAPI.maxChunksPerEntry();
    actualChunksPerSegmentTier = privateAPI.actualChunksPerSegmentTier();

    // Precomputed offsets and sizes for fast Context init
    segmentHeaderSize = privateAPI.segmentHeaderSize();

    tierHashLookupValueBits = valueBits(actualChunksPerSegmentTier);
    tierHashLookupKeyBits = keyBits(privateAPI.entriesPerSegment(), tierHashLookupValueBits);
    tierHashLookupSlotSize =
            entrySize(tierHashLookupKeyBits, tierHashLookupValueBits);
    if (!privateAPI.aligned64BitMemoryOperationsAtomic() && tierHashLookupSlotSize > 4) {
        throw new IllegalStateException("aligned64BitMemoryOperationsAtomic() == false, " +
                "but hash lookup slot is " + tierHashLookupSlotSize);
    }
    tierHashLookupCapacity = privateAPI.tierHashLookupCapacity();
    maxEntriesPerHashLookup = (long) (tierHashLookupCapacity * MAX_LOAD_FACTOR);
    tierHashLookupInnerSize = tierHashLookupCapacity * tierHashLookupSlotSize;
    tierHashLookupOuterSize = CACHE_LINES.align(tierHashLookupInnerSize, BYTES);

    tierFreeListInnerSize = LONGS.align(
            BYTES.alignAndConvert(actualChunksPerSegmentTier, BITS), BYTES);
    tierFreeListOuterSize = CACHE_LINES.align(tierFreeListInnerSize, BYTES);

    tierEntrySpaceInnerSize = chunkSize * actualChunksPerSegmentTier;
    tierEntrySpaceInnerOffset = privateAPI.segmentEntrySpaceInnerOffset();
    tierEntrySpaceOuterSize = CACHE_LINES.align(
            tierEntrySpaceInnerOffset + tierEntrySpaceInnerSize, BYTES);

    tierSize = tierSize();

    maxExtraTiers = privateAPI.maxExtraTiers();
    tiersInBulk = computeNumberOfTiersInBulk();
    log2TiersInBulk = Maths.intLog2(tiersInBulk);
    tierBulkInnerOffsetToTiers = computeTierBulkInnerOffsetToTiers(tiersInBulk);
    tierBulkSizeInBytes = computeTierBulkBytesSize(tiersInBulk);

    checksumEntries = privateAPI.checksumEntries();

    preShutdownAction = privateAPI.getPreShutdownAction();
    skipCloseOnExitHook = privateAPI.skipCloseOnExitHook();
}
 
Example #11
Source File: RollCycleEncodeSequence.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
RollCycleEncodeSequence(LongValue writePositionAndSequence, int indexCount, int indexSpacing) {
    this.cycleShift = Math.max(32, Maths.intLog2(indexCount) * 2 + Maths.intLog2(indexSpacing));
    this.sequenceMask = (1L << cycleShift) - 1;
    this.writePositionAndSequence = writePositionAndSequence instanceof TwoLongValue ?
            (TwoLongValue) writePositionAndSequence : null;
}
 
Example #12
Source File: WriteReadTextTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
private void doTest(@NotNull String... problematic) {

        String myPath = OS.TARGET + "/writeReadText-" + System.nanoTime();

        try (ChronicleQueue theQueue = SingleChronicleQueueBuilder
                .single(myPath)
                .blockSize(Maths.nextPower2(EXTREMELY_LARGE.length() * 4, 256 << 10))
                // .testBlockSize() not suitable as large message sizes.
                .build()) {

            ExcerptAppender appender = theQueue.acquireAppender();
            ExcerptTailer tailer = theQueue.createTailer();

            StringBuilder tmpReadback = new StringBuilder();

            // If the tests don't fail, try increasing the number of iterations
            // Setting it very high may give you a JVM crash
            final int tmpNumberOfIterations = 5;

            for (int l = 0; l < tmpNumberOfIterations; l++) {
                for (int p = 0; p < problematic.length; p++) {
                    appender.writeText(problematic[p]);
                }
                for (int p = 0; p < problematic.length; p++) {
                    tailer.readText(tmpReadback);
                    Assert.assertEquals("write/readText", problematic[p], tmpReadback.toString());
                }
            }

            for (int l = 0; l < tmpNumberOfIterations; l++) {
                for (int p = 0; p < problematic.length; p++) {
                    final String tmpText = problematic[p];
                    appender.writeDocument(writer -> writer.getValueOut().text(tmpText));

                    tailer.readDocument(reader -> reader.getValueIn().textTo(tmpReadback));
                    String actual = tmpReadback.toString();
                    Assert.assertEquals(problematic[p].length(), actual.length());
                    for (int i = 0; i < actual.length(); i += 1024)
                        Assert.assertEquals("i: " + i, problematic[p].substring(i, Math.min(actual.length(), i + 1024)), actual.substring(i, Math.min(actual.length(), i + 1024)));
                    Assert.assertEquals(problematic[p], actual);
                }
            }
        }
    }
 
Example #13
Source File: WeeklyRollCycle.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@Override
public int toCycle(long index) {
    return Maths.toUInt31(index >> cycleShift);
}