net.openhft.chronicle.core.values.LongValue Java Examples

The following examples show how to use net.openhft.chronicle.core.values.LongValue. 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: OffHeapVarBitMetricStore.java    From yuvi with Apache License 2.0 6 votes vote down vote up
public OffHeapVarBitMetricStore(long size, int valueSize, String chunkInfo, String dir) {
  this.chunkInfo = chunkInfo;

  ChronicleMapBuilder<LongValue, ByteBuffer> mapBuilder = ChronicleMap
      .of(LongValue.class, ByteBuffer.class)
      .entries(size)
      .averageValueSize(valueSize);

  if (chunkInfo != null && !chunkInfo.isEmpty() && !dir.isEmpty()) {
    File offHeapFile = new File(dir + "/" + offHeapNamePrefix + "_" + chunkInfo);
    try {
      timeSeries = mapBuilder.name(offHeapNamePrefix + "_" + chunkInfo)
          .createPersistedTo(offHeapFile);
    } catch (IOException e) {
      LOG.error("Failed to create an offheap store {} with error {}", offHeapFile, e.getMessage());
      throw new IllegalArgumentException("Failed to create an off heap store.", e);
    }
  } else {
    timeSeries = mapBuilder.name(offHeapNamePrefix).create();
  }
  LOG.info("Created an off heap metric store of size={} valueSize={} chunkInfo={} in dir={}",
      size, valueSize, chunkInfo, dir);
}
 
Example #2
Source File: DataKeyValueTest.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
@Test
public void dataKeyValueTest() {
    ChronicleMap<IntValue, LongValue> map =
            ChronicleMapBuilder.of(IntValue.class, LongValue.class)
                    .entries(1000).create();
    IntValue heapKey = Values.newHeapInstance(IntValue.class);
    LongValue heapValue = Values.newHeapInstance(LongValue.class);
    LongValue directValue = Values.newNativeReference(LongValue.class);

    heapKey.setValue(1);
    heapValue.setValue(1);
    map.put(heapKey, heapValue);
    assertEquals(1, map.get(heapKey).getValue());
    assertEquals(1, map.getUsing(heapKey, heapValue).getValue());

    heapKey.setValue(1);
    map.getUsing(heapKey, directValue).addValue(1);
    assertEquals(2, map.getUsing(heapKey, heapValue).getValue());

    heapKey.setValue(2);
    heapValue.setValue(3);
    map.put(heapKey, heapValue);
    assertEquals(3, map.get(heapKey).getValue());
}
 
Example #3
Source File: ChronicleMapTest.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
@Test
public void testPutLongValue() throws IOException {
    final ChronicleMapBuilder<CharSequence, LongValue> builder = ChronicleMapBuilder
            .of(CharSequence.class, LongValue.class)
            .entries(1000)
            .averageKeySize("x".length());

    try (final ChronicleMap<CharSequence, LongValue> map = builder.create()) {

        LongValue value = nativeLongValue();
        try {
            map.put("x", value);
        } catch (IllegalStateException | NullPointerException e) {
            // ok
            return;
        }
        throw new AssertionError("Should throw either IllegalStateException or " +
                "NullPointerException, but succeed");
    }
}
 
Example #4
Source File: ValueInterfaceWithEnumTest.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
/**
 * This test will throw an {@link ArrayIndexOutOfBoundsException}. This seems to occur only with Enums having even number of
 * values
 */
@Test
public void testValueInterface() {
    LongValue longValue = Values.newHeapInstance(LongValue.class);
    SimpleValueInterface simpleValueInterface = Values.newHeapInstance(SimpleValueInterface.class);

    ChronicleMap<LongValue, SimpleValueInterface> map = ChronicleMapBuilder.of(LongValue.class, SimpleValueInterface.class).entries(50).create();

    IntStream.range(1, 20).forEach(value -> {
        longValue.setValue(value);
        simpleValueInterface.setId(value);
        simpleValueInterface.setTruth(false);
        simpleValueInterface.setSVIEnum(SimpleValueInterface.SVIEnum.SIX);

        map.put(longValue, simpleValueInterface);
    });

    IntStream.range(1, 10).forEach(value -> {
        longValue.setValue(value);
        SimpleValueInterface simpleValueInterface1 = map.get(longValue);
        System.out.println(simpleValueInterface1.getId());
    });
}
 
Example #5
Source File: SingleChronicleQueue.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
/**
 * This method does not update the index, as indexes are not used for meta data
 *
 * @param buffer
 * @return the addressForRead of the appended data
 */
private long appendMetaDataReturnAddress(@NotNull Bytes buffer) {
    long length = checkRemainingForAppend(buffer);

    LongValue writeByte = header.writeByte();
    long lastByte = writeByte.getVolatileValue();

    for (; ; ) {
        if (bytes.compareAndSwapInt(lastByte, 0, NOT_COMPLETE | (int) length)) {
            long lastByte2 = lastByte + 4 + buffer.remaining();
            bytes.write(lastByte + 4, buffer);
            writeByte.setOrderedValue(lastByte2);
            bytes.writeOrderedInt(lastByte, (int) (META_DATA | length));
            return lastByte;
        }
        int length2 = length30(bytes.readVolatileInt());
        bytes.skip(length2);
        try {
            Jvm.checkInterrupted();
        } catch (InterruptedException e) {
            throw new InterruptedRuntimeException(e);
        }
    }
}
 
Example #6
Source File: SingleChronicleQueue.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@Override
public long appendDocument(@NotNull Bytes buffer) {
    long length = checkRemainingForAppend(buffer);

    LongValue writeByte = header.writeByte();

    for (; ; ) {
        long lastByte = writeByte.getVolatileValue();

        if (bytes.compareAndSwapInt(lastByte, 0, NOT_COMPLETE | (int) length)) {
            long lastByte2 = lastByte + 4 + buffer.remaining();
            bytes.write(lastByte + 4, buffer);
            long lastIndex = header.lastIndex().addAtomicValue(1);
            writeByte.setOrderedValue(lastByte2);
            bytes.writeOrderedInt(lastByte, (int) length);
            return lastIndex;
        }
        int length2 = length30(bytes.readVolatileInt());
        bytes.skip(length2);
        try {
            Jvm.checkInterrupted();
        } catch (InterruptedException e) {
            throw new InterruptedRuntimeException(e);
        }
    }
}
 
Example #7
Source File: StoreTailer.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
public StoreTailer(@NotNull final SingleChronicleQueue queue, final LongValue indexValue) {
    this.queue = queue;
    this.indexValue = indexValue;
    this.setCycle(Integer.MIN_VALUE);
    this.index = 0;
    queue.addCloseListener(this);

    if (indexValue == null) {
        toStart();
    } else {
        moveToIndex(indexValue.getVolatileValue());
    }
    finalizer = Jvm.isResourceTracing() ? new Finalizer() : null;
}
 
Example #8
Source File: ChronicleMapUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenGetUsingQuery_whenCalled_shouldReturnResult() {
    LongValue key = Values.newHeapInstance(LongValue.class);
    StringBuilder country = new StringBuilder();
    key.setValue(1);
    persistedCountryMap.getUsing(key, country);
    assertThat(country.toString(), is(equalTo("Romania")));
    key.setValue(2);
    persistedCountryMap.getUsing(key, country);
    assertThat(country.toString(), is(equalTo("India")));
}
 
Example #9
Source File: ChronicleMapUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenGetQuery_whenCalled_shouldReturnResult() {
    LongValue key = Values.newHeapInstance(LongValue.class);
    key.setValue(1);
    CharSequence country = inMemoryCountryMap.get(key);
    assertThat(country.toString(), is(equalTo("Qatar")));
}
 
Example #10
Source File: OffHeapByteArrayExampleTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {

    // this objects will be reused
    ByteValue byteValue = Values.newHeapInstance(ByteValue.class);
    ByteArray value = Values.newHeapInstance(ByteArray.class);
    LongValue key = Values.newHeapInstance(LongValue.class);

    key.setValue(1);

    // this is kind of like byteValue[1] = 'b'
    byteValue.setValue((byte) EXPECTED);
    value.setByteValueAt(1, byteValue);

    chm.put(key, value);

    // clear the value to prove it works
    byteValue.setValue((byte) 0);
    value.setByteValueAt(1, byteValue);

    chm.getUsing(key, value);

    Assert.assertEquals(0, value.getByteValueAt(2).getValue());
    Assert.assertEquals(0, value.getByteValueAt(3).getValue());

    byte actual = value.getByteValueAt(1).getValue();
    Assert.assertEquals(EXPECTED, actual);

}
 
Example #11
Source File: OffHeapByteArrayExampleTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeClass() {
    chm = ChronicleMapBuilder
            .of(LongValue.class, ByteArray.class)
            .entries(1000)
            .create();
}
 
Example #12
Source File: ChecksumEntryTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@Test
public void testChecksumEntriesWithValueInterface() throws IOException {
    File file = Builder.getPersistenceFile();

    try (ChronicleMap<Integer, LongValue> map = ChronicleMap
            .of(Integer.class, LongValue.class)
            .entries(1)
            // Entry checksums make sense only for persisted Chronicle Maps, and are ON by
            // default for such maps
            .createPersistedTo(file)) {

        LongValue value = Values.newHeapInstance(LongValue.class);
        value.setValue(42);
        map.put(1, value);

        try (ExternalMapQueryContext<Integer, LongValue, ?> c = map.queryContext(1)) {
            // Update lock required for calling ChecksumEntry.checkSum()
            c.updateLock().lock();
            MapEntry<Integer, LongValue> entry = c.entry();
            Assert.assertNotNull(entry);
            ChecksumEntry checksumEntry = (ChecksumEntry) entry;
            Assert.assertTrue(checksumEntry.checkSum());

            // to access off-heap bytes, should call value().getUsing() with Native value
            // provided. Simple get() return Heap value by default
            LongValue nativeValue =
                    entry.value().getUsing(Values.newNativeReference(LongValue.class));
            // This value bytes update bypass Chronicle Map internals, so checksum is not
            // updated automatically
            nativeValue.setValue(43);
            Assert.assertFalse(checksumEntry.checkSum());

            // Restore correct checksum
            checksumEntry.updateChecksum();
            Assert.assertTrue(checksumEntry.checkSum());
        }
    }
}
 
Example #13
Source File: SingleChronicleQueue.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public ExcerptTailer createTailer(String id) {
    throwExceptionIfClosed();

    LongValue index = id == null
            ? null
            : metaStore.doWithExclusiveLock(ts -> ts.acquireValueFor("index." + id, 0));
    final StoreTailer storeTailer = new StoreTailer(this, index);
    directoryListing.refresh();
    storeTailer.clearUsedByThread();
    return storeTailer;
}
 
Example #14
Source File: EntryCountMapTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
private void dumpMapStats(int segments, int minSize,
                          ChronicleMap<CharSequence, LongValue> map) {
    long[] a = new long[map.segments()];
    for (int i = 0; i < map.segments(); i++) {
        try (MapSegmentContext<CharSequence, LongValue, ?> c = map.segmentContext(i)) {
            a[i] = c.size();
        }
    }
    System.out.println("segs: " + segments + " min: " + minSize + " was " + map.size() + " "
            + Arrays.toString(a)
            + " sum: " + sum(a));
}
 
Example #15
Source File: EntryCountMapTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
void testEntriesMaxSize0(int segments, int minSize, int maxSize, int counter, int stride,
                         ChronicleMap<CharSequence, LongValue> map) {
    LongValue longValue = newNativeReference(LongValue.class);
    try {
        for (int j = 0; j < moreThanMaxSize(maxSize); j++) {
            String key = "key:" + counter;
            if (minSize > 10 && (counter & 15) == 7)
                key += "-oversized-key";
            counter += stride;
            // give a biased hashcode distribution.
            if ((Integer.bitCount(key.hashCode()) & 7) != 0) {
                j--;
                continue;
            }
            map.acquireUsing(key, longValue);
            longValue.setValue(1);
        }
        dumpMapStats(segments, minSize, map);
        fail("Expected the map to be full.");
    } catch (IllegalStateException e) {
        // calculate the hyperbolic average.
        score += (double) minSize / map.size();
        scoreCount++;
        boolean condition = minSize <= map.size() && map.size() <= minSize * 2 + 8;
        if (!condition) {
            dumpMapStats(segments, minSize, map);
            assertTrue("stride: " + stride + ", seg: " + segments + ", min: " + minSize +
                    ", size: " + map.size(), condition);
        } else if (map.size() > maxSize)
            System.err.println(" warning, larger than expected, stride: " + stride +
                    ", seg: " + segments + ", min: " + minSize +
                    ", size: " + map.size());
    }
}
 
Example #16
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 #17
Source File: EntryCountMapTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
private static ChronicleMap<CharSequence, LongValue> getSharedMap(
        long entries, int segments, int keySize) throws IOException {
    ChronicleMapBuilder<CharSequence, LongValue> mapBuilder =
            ChronicleMapBuilder.of(CharSequence.class, LongValue.class)
                    .allowSegmentTiering(false)
                    .entries(entries)
                    .actualSegments(segments)
                    .averageKeySize(keySize);
    return mapBuilder.createPersistedTo(getPersistenceFile());
}
 
Example #18
Source File: OffHeapVarBitMetricStore.java    From yuvi with Apache License 2.0 5 votes vote down vote up
@Override
public List<Point> getSeries(long uuid) {
  final LongValue key = Values.newHeapInstance(LongValue.class);
  key.setValue(uuid);
  if (timeSeries.containsKey(key)) {
    ByteBuffer serializedValues = timeSeries.get(key);
    TimeSeriesIterator iterator = VarBitTimeSeries.deserialize(serializedValues);
    return iterator.getPoints();
  } else {
    return Collections.emptyList();
  }
}
 
Example #19
Source File: ChronicleMapTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    try {
        LongValue value = Values.newNativeReference(LongValue.class);
        barrier.await();
        for (int i = 0; i < iterations; i++) {
            map.acquireUsing(key, value);
            value.addAtomicValue(1);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #20
Source File: PortfolioValueTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
protected static double computeTotalUsingKeys(final ChronicleMap<LongValue, PortfolioAssetInterface> cache, long start, long end) {
    final LongValue key = Values.newHeapInstance(LongValue.class);
    PortfolioAssetInterface asset = Values.newHeapInstance(PortfolioAssetInterface.class);

    double total = 0;
    for (long k = start; k < end; k++) {
        key.setValue(k);
        asset = cache.getUsing(key, asset);
        total += asset.getShares() * asset.getPrice();
    }
    return total;
}
 
Example #21
Source File: PortfolioValueTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
protected static double computeTotalUsingIterator(final ChronicleMap<LongValue, PortfolioAssetInterface> cache, int start, int end) {
    if (end > start) {
        final PortfolioAssetInterface asset = Values.newHeapInstance(PortfolioAssetInterface.class);
        PortfolioValueAccumulator accumulator = new PortfolioValueAccumulator(new MutableDouble(), asset);
        for (int s = start; s < end; s++) {
            try (MapSegmentContext<LongValue, PortfolioAssetInterface, ?> context = cache.segmentContext(s)) {
                context.forEachSegmentEntry(accumulator);
            }
        }
        return accumulator.total.doubleValue();
    }
    return 0;
}
 
Example #22
Source File: PortfolioValueTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws ExecutionException, InterruptedException {
    ChronicleMapBuilder<LongValue, PortfolioAssetInterface> mapBuilder = ChronicleMapBuilder.of(LongValue.class, PortfolioAssetInterface.class).entries(nAssets);

    try (ChronicleMap<LongValue, PortfolioAssetInterface> cache = mapBuilder.create()) {
        createData(cache);

        // Compute multiple times to get an reasonable average compute time
        for (int i = 0; i < nRepetitions; i++) {
            computeValue(cache);
        }
    }
}
 
Example #23
Source File: PortfolioValueTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
private void createData(final ChronicleMap<LongValue, PortfolioAssetInterface> cache) throws ExecutionException, InterruptedException {
    long startTime = System.currentTimeMillis();

    ExecutorService executor = Executors.newFixedThreadPool(nThreads,
            new NamedThreadFactory("test"));
    Future<?>[] futures = new Future[nThreads];
    long batchSize = nAssets / nThreads;

    for (int t = 0; t < nThreads; t++) {
        final long batch = t;
        futures[t] = executor.submit(() -> {
            final LongValue key = Values.newHeapInstance(LongValue.class);
            final PortfolioAssetInterface value = Values.newHeapInstance(PortfolioAssetInterface.class);

            long start = batch * batchSize;
            long end = Math.min(nAssets, (batch + 1) * batchSize);
            long n = (end - start);

            if (end > start) {
                System.out.println("Inserting batch " + (batch + 1) + "/" + nThreads + " of " + n + " records");

                for (long k = start; k < end; k++) {
                    key.setValue(k);
                    value.setAssetId(k);
                    value.setShares(1);
                    value.setPrice(2.0);
                    cache.put(key, value);
                }
            }
        });
    }

    for (Future<?> future : futures) {
        future.get();
    }

    long elapsedTime = (System.currentTimeMillis() - startTime);
    System.out.println("Data inserted in " + elapsedTime + " ms");
}
 
Example #24
Source File: ChronicleMapImportExportTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithLongValue() throws IOException, InterruptedException {

    File file = new File(TMP + "/chronicle-map-" + System.nanoTime() + ".json");
    //file.deleteOnExit();

    System.out.println(file.getAbsolutePath());
    ChronicleMapBuilder<CharSequence, LongValue> builder = ChronicleMapBuilder
            .of(CharSequence.class, LongValue.class)
            .averageKeySize("one".length())
            .entries(1000);
    try (ChronicleMap<CharSequence, LongValue> expected = builder.create()) {
        LongValue value = Values.newNativeReference(LongValue.class);

        // this will add the entry
        try (Closeable c =
                     expected.acquireContext("one", value)) {
            assertEquals(0, value.getValue());
            value.addValue(1);
        }

        expected.getAll(file);

        try (ChronicleMap<CharSequence, LongValue> actual = builder.create()) {

            actual.putAll(file);

            Assert.assertEquals(expected, actual);
        }
    } finally {
        // file.delete();
    }
}
 
Example #25
Source File: ChronicleMapTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@Test
public void testAcquireWithNullContainer() {
    try (ChronicleMap<CharSequence, LongValue> map =
                 ChronicleMapBuilder.of(CharSequence.class, LongValue.class)
                         .averageKey("key")
                         .entries(1000)
                         .entryAndValueOffsetAlignment(4)
                         .create()) {
        map.acquireUsing("key", Values.newNativeReference(LongValue.class));
        assertEquals(0, map.acquireUsing("key", null).getValue());
    }
}
 
Example #26
Source File: ChronicleMapTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetWithNullContainer() {
    try (ChronicleMap<CharSequence, LongValue> map =
                 ChronicleMapBuilder.of(CharSequence.class, LongValue.class)
                         .averageKey("key")
                         .entries(10)
                         .entryAndValueOffsetAlignment(4)
                         .create()) {
        map.acquireUsing("key", Values.newNativeReference(LongValue.class));
        assertEquals(0, map.getUsing("key", null).getValue());

    }
}
 
Example #27
Source File: ChronicleMapTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetWithoutAcquireFirst() {
    try (ChronicleMap<CharSequence, LongValue> map =
                 ChronicleMapBuilder.of(CharSequence.class, LongValue.class)
                         .averageKey("key")
                         .entries(10)
                         .entryAndValueOffsetAlignment(4)
                         .create()) {
        assertNull(map.getUsing("key", Values.newNativeReference(LongValue.class)));

    }
}
 
Example #28
Source File: ChronicleMapTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
private IncrementRunnable(ChronicleMap<CharSequence, LongValue> map, CharSequence key,
                          int iterations, CyclicBarrier barrier) {
    this.map = map;
    this.key = key;
    this.iterations = iterations;
    this.barrier = barrier;
}
 
Example #29
Source File: OffHeapVarBitMetricStore.java    From yuvi with Apache License 2.0 4 votes vote down vote up
public void addPoint(long uuid, ByteBuffer series) {
  LongValue key = Values.newHeapInstance(LongValue.class);
  key.setValue(uuid);
  timeSeries.put(key, series);
}
 
Example #30
Source File: PortfolioValueAccumulator.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@Override
public void accept(MapEntry<LongValue, PortfolioAssetInterface> e) {
    e.value().getUsing(asset);
    total.add(asset.getShares() * asset.getPrice());
}