Java Code Examples for net.openhft.chronicle.core.values.LongValue#setValue()

The following examples show how to use net.openhft.chronicle.core.values.LongValue#setValue() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 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: 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 10
Source File: ChronicleMapTest.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@Test
public void testOnheapAcquireUsingLocked() throws IOException {
    File tmpFile = File.createTempFile("testAcquireUsingLocked", ".deleteme");
    tmpFile.deleteOnExit();
    try (final ChronicleMap<CharSequence, LongValue> map = ChronicleMapBuilder
            .of(CharSequence.class, LongValue.class)
            .entries(1000)
            .averageKeySize("one".length()).createPersistedTo(tmpFile)) {

        LongValue value = Values.newNativeReference(LongValue.class);

        try (ExternalMapQueryContext<CharSequence, LongValue, ?> c = map.queryContext("one")) {
            assertNotNull(c.absentEntry());
        }

        try (net.openhft.chronicle.core.io.Closeable c =
                     map.acquireContext("one", value)) {
            value.setValue(10);
        }

        // this will add the entry
        try (net.openhft.chronicle.core.io.Closeable c =
                     map.acquireContext("one", value)) {
            value.addValue(1);
        }

        // check that the entry was added
        try (ExternalMapQueryContext<CharSequence, LongValue, ?> c = map.queryContext("one")) {
            MapEntry<CharSequence, LongValue> entry = c.entry();
            assertNotNull(entry);
            assertEquals(11, entry.value().get().getValue());
        }

        // this will remove the entry
        try (ExternalMapQueryContext<CharSequence, LongValue, ?> c = map.queryContext("one")) {
            c.updateLock().lock();
            c.remove(c.entry());
        }

        // check that the entry was removed
        try (ExternalMapQueryContext<CharSequence, LongValue, ?> c = map.queryContext("one")) {
            assertNotNull(c.absentEntry());
        }

        try (net.openhft.chronicle.core.io.Closeable c =
                     map.acquireContext("one", value)) {
            assertEquals(0, value.getValue());
        }

        value.setValue(1);

        try (ExternalMapQueryContext<CharSequence, LongValue, ?> c = map.queryContext("one")) {
            assertEquals(1, c.entry().value().get().getValue());
        }

        try (net.openhft.chronicle.core.io.Closeable c =
                     map.acquireContext("one", value)) {
            value.addValue(1);
        }

        // check that the entry was removed
        try (ExternalMapQueryContext<CharSequence, LongValue, ?> c = map.queryContext("one")) {
            LongValue value1 = c.entry().value().get();
            assertEquals(2, value1.getValue());
        }
    }
    tmpFile.delete();
}
 
Example 11
Source File: CHMLatencyTestMain.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
public static void main(String... ignored) throws IOException {
        AffinityLock lock = AffinityLock.acquireCore();
        File file = File.createTempFile("testCHMLatency", "deleteme");
//        File file = new File("testCHMLatency.deleteme");
        file.delete();
        ChronicleMap<LongValue, LongValue> countersMap =
                ChronicleMapBuilder.of(LongValue.class, LongValue.class)
                        .entries(KEYS)
                        .createPersistedTo(file);

        // add keys
        LongValue key = Values.newHeapInstance(LongValue.class);
        LongValue value = Values.newNativeReference(LongValue.class);
        for (long i = 0; i < KEYS; i++) {
            key.setValue(i);
            countersMap.acquireUsing(key, value);
            value.setValue(0);
        }
        System.out.println("Keys created");
//        Monitor monitor = new Monitor();
        LongValue value2 = Values.newNativeReference(LongValue.class);
        for (int t = 0; t < 5; t++) {
            for (int rate : new int[]{2 * 1000 * 1000, 1000 * 1000, 500 * 1000/*, 250 * 1000, 100 * 1000, 50 * 1000*/}) {
                Histogram times = new Histogram();
                int u = 0;
                long start = System.nanoTime();
                long delay = 1000 * 1000 * 1000L / rate;
                long next = start + delay;
                for (long j = 0; j < RUN_TIME * rate; j += KEYS) {
                    int stride = Math.max(1, KEYS / (RUN_TIME * rate));
                    // the timed part
                    for (int i = 0; i < KEYS && u < RUN_TIME * rate; i += stride) {
                        // busy wait for next time.
                        while (System.nanoTime() < next - 12) ;
//                        monitor.sample = System.nanoTime();
                        long start0 = next;

                        // start the update.
                        key.setValue(i);
                        LongValue using = countersMap.getUsing(key, value2);
                        if (using == null)
                            assertNotNull(using);
                        value2.addAtomicValue(1);

                        // calculate the time using the time it should have started, not when it was able.
                        long elapse = System.nanoTime() - start0;
                        times.sample(elapse);
                        next += delay;
                    }
//                    monitor.sample = Long.MAX_VALUE;
                }
                System.out.printf("run %d %,9d : ", t, rate);
                times.printPercentiles(" micro-seconds.");
            }
            System.out.println();
        }
//        monitor.running = false;
        countersMap.close();
        file.delete();
    }
 
Example 12
Source File: RecoverTest.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@Test
public void testCorruptedEntryRecovery() throws IOException {
    File file = getPersistenceFile();
    try (ChronicleMap<Integer, LongValue> map = ChronicleMap
            .of(Integer.class, LongValue.class)
            .entries(1)
            .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();
            assertNotNull(entry);
            ChecksumEntry checksumEntry = (ChecksumEntry) entry;
            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());
        }
    }

    AtomicInteger corruptionCounter = new AtomicInteger(0);
    ChronicleHashCorruption.Listener corruptionListener =
            corruption -> corruptionCounter.incrementAndGet();
    //noinspection EmptyTryBlock
    try (ChronicleMap<Integer, LongValue> ignore = ChronicleMap
            .of(Integer.class, LongValue.class)
            .entries(1)
            .createOrRecoverPersistedTo(file, true, corruptionListener)) {
    }
    assertTrue(corruptionCounter.get() > 0);
}