net.openhft.chronicle.map.ChronicleMap Java Examples
The following examples show how to use
net.openhft.chronicle.map.ChronicleMap.
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: ExternalizableTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@Test public void externalizable() throws IOException { String path = OS.TARGET + "/test-" + System.nanoTime() + ".map"; new File(path).deleteOnExit(); try (ChronicleMap<Long, SomeClass> storage = ChronicleMapBuilder .of(Long.class, SomeClass.class) .averageValueSize(128) .entries(128) .createPersistedTo(new File(path))) { SomeClass value = new SomeClass(); value.hits.add("one"); value.hits.add("two"); storage.put(1L, value); SomeClass value2 = storage.get(1L); assertEquals(value.hits, value2.hits); } }
Example #2
Source File: PointSerializationTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@Test public void pointSerializationTest() { try (ChronicleMap<String, List<Point>> objects = ChronicleMap .of(String.class, (Class<List<Point>>) (Class) List.class) .averageKey("range") .valueMarshaller(PointListSizedMarshaller.INSTANCE) .averageValue(asList(of(0, 0), of(1, 1))) .entries(10) .create()) { objects.put("range", asList(of(0, 0), of(1, 1))); objects.put("square", asList(of(0, 0), of(0, 100), of(100, 100), of(100, 0))); Assert.assertEquals(2, objects.get("range").size()); Assert.assertEquals(4, objects.get("square").size()); } }
Example #3
Source File: CustomCharSequenceEncodingTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@Test public void customCharSequenceEncodingTest() { Charset charset = StandardCharsets.UTF_8; int charBufferSize = 4; int bytesBufferSize = 8; CharSequenceCustomEncodingBytesWriter writer = new CharSequenceCustomEncodingBytesWriter(charset, charBufferSize); CharSequenceCustomEncodingBytesReader reader = new CharSequenceCustomEncodingBytesReader(charset, bytesBufferSize); try (ChronicleMap<String, CharSequence> map = ChronicleMap .of(String.class, CharSequence.class) .valueMarshallers(reader, writer) .averageKey("Russian") .averageValue("Всем нравится субботний вечерок") .entries(10) .create()) { map.put("Russian", "Всем нравится субботний вечерок"); map.put("", "Quick brown fox jumps over the lazy dog"); } }
Example #4
Source File: MarshallingTest.java From log4j2-elasticsearch with Apache License 2.0 | 6 votes |
@Test public void canMarshallKeySequenceConfigList() throws IOException { // given ChronicleMap<CharSequence, ItemSource> map = createDefaultTestChronicleMap(); List<CharSequence> keys = new ArrayList<>(); String key1 = UUID.randomUUID().toString(); String key2 = UUID.randomUUID().toString(); keys.add(key1); keys.add(key2); KeySequenceConfigKeys expected = new KeySequenceConfigKeys(keys); String expectedKey = UUID.randomUUID().toString(); // when map.put(expectedKey, expected); KeySequenceConfigKeys result = (KeySequenceConfigKeys) map.get(expectedKey); // then Collection<CharSequence> source = result.getSource().getKeys(); assertTrue(source.contains(key1)); assertTrue(source.contains(key2)); }
Example #5
Source File: KeySequenceConfigRepositoryTest.java From log4j2-elasticsearch with Apache License 2.0 | 6 votes |
@Test public void keySequenceConfigListIsCreatedIfItIsAbsentInGivenMap() { // given ChronicleMap<CharSequence, ItemSource> failedItems = mock(ChronicleMap.class); when(failedItems.containsKey(eq(KeySequenceConfigRepository.INDEX_KEY_NAME))) .thenReturn(false); // when new KeySequenceConfigRepository(failedItems, DEFAULT_TEST_KEY_SEQUENCE_EXPIRY); // then ArgumentCaptor<KeySequenceConfigKeys> configRegistryCaptor = ArgumentCaptor.forClass(KeySequenceConfigKeys.class); verify(failedItems).put(eq(KeySequenceConfigRepository.INDEX_KEY_NAME), configRegistryCaptor.capture()); assertNotNull(configRegistryCaptor.getValue()); }
Example #6
Source File: ChronicleMapRetryFailoverPolicyTest.java From log4j2-elasticsearch with Apache License 2.0 | 6 votes |
@Test public void lifecycleStopWithTimeoutClosesGivenKeySequenceSelector() throws IOException { // given String fileName = createTempFile().getAbsolutePath(); ChronicleMap<CharSequence, ItemSource> failedItems = createDefaultTestChronicleMap(); KeySequenceSelector keySequenceSelector = spy(createDummyKeySequenceSelector()); ChronicleMapRetryFailoverPolicy.Builder builder = createDefaultTestFailoverPolicyBuilder(fileName, failedItems) .withKeySequenceSelector(keySequenceSelector); ChronicleMapRetryFailoverPolicy failoverPolicy = builder.build(); failoverPolicy.addListener((RetryListener)failedItemSource -> true); failoverPolicy.start(); assertTrue(failoverPolicy.isStarted()); // when failoverPolicy.stop(100, false); // then verify(keySequenceSelector).close(); }
Example #7
Source File: ChronicleMapRetryFailoverPolicyTest.java From log4j2-elasticsearch with Apache License 2.0 | 6 votes |
@NotNull public ChronicleMap<CharSequence, ItemSource> createDefaultTestChronicleMap() { ChronicleMap<CharSequence, ItemSource> failedItems = mock(ChronicleMap.class); KeySequence sequence = createDefaultTestKeySequence(); KeySequenceConfig config = spy(sequence.getConfig(true)); CharSequence sequenceConfigKey = config.getKey(); ArrayList<CharSequence> keySequenceConfigKeys = new ArrayList<>(); keySequenceConfigKeys.add(sequenceConfigKey); when(failedItems.get(eq(KeySequenceConfigRepository.INDEX_KEY_NAME))) .thenReturn(new KeySequenceConfigKeys(keySequenceConfigKeys)); when(failedItems.get(eq(sequenceConfigKey))).thenReturn(config); return failedItems; }
Example #8
Source File: DoubleArrayTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@Test @Ignore("TODO What is HACK???") public void addToAMap2() { DoubleArray.HACK = false; DoubleArray a = new DoubleArray(10); a.setData(new double[]{1, 2, 3, 4, 5}); DoubleArray b = new DoubleArray(10); b.setData(new double[]{5, 6, 7, 8, 9}); ChronicleMap<Integer, DoubleArray> proxyMap = ChronicleMapBuilder .of(Integer.class, DoubleArray.class) .averageValueSize(6 * 8) .create(); proxyMap.put(1, a); proxyMap.put(2, b); System.out.println(proxyMap.get(1)); System.out.println(proxyMap.get(2)); proxyMap.close(); DoubleArray.HACK = true; }
Example #9
Source File: ChronicleMapProxyTest.java From log4j2-elasticsearch with Apache License 2.0 | 6 votes |
@SuppressWarnings("ResultOfMethodCallIgnored") @Test public void containsKeyDelegates() { // given ChronicleMap<CharSequence, ItemSource> chronicleMap = createDefaultTestChronicleMap(); CharSequence key = mock(CharSequence.class); ChronicleMapProxy proxy = createDefaultTestProxy(chronicleMap); // when proxy.containsKey(key); // then Mockito.verify(chronicleMap).containsKey(eq(key)); }
Example #10
Source File: DoubleArrayTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@Test public void addToAMap() { DoubleArray a = new DoubleArray(10); a.setData(new double[]{1, 2, 3, 4, 5}); DoubleArray b = new DoubleArray(10); b.setData(new double[]{5, 6, 7, 8, 9}); ChronicleMap<Integer, DoubleArray> proxyMap = ChronicleMap .of(Integer.class, DoubleArray.class) .constantValueSizeBySample(a) .entries(2) .create(); proxyMap.put(1, a); proxyMap.put(2, b); System.out.println(proxyMap.get(1)); System.out.println(proxyMap.get(2)); proxyMap.close(); }
Example #11
Source File: VanillaChronicleMapConverter.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@Override public void marshal(Object o, final HierarchicalStreamWriter writer, final MarshallingContext marshallingContext) { ((ChronicleMap<K, V>) o).forEachEntry(e -> { writer.startNode("entry"); { final Object key = e.key().get(); writer.startNode(key.getClass().getName()); marshallingContext.convertAnother(key); writer.endNode(); Object value = e.value().get(); writer.startNode(value.getClass().getName()); marshallingContext.convertAnother(value); writer.endNode(); } writer.endNode(); }); }
Example #12
Source File: ChronicleMapTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
/** * entrySet contains all pairs */ @Test(timeout = 5000) public void testEntrySet() throws IOException { try (ChronicleMap<Integer, CharSequence> map = map5()) { Set<Entry<Integer, CharSequence>> s = map.entrySet(); assertEquals(5, s.size()); Iterator<Entry<Integer, CharSequence>> it = s.iterator(); while (it.hasNext()) { Entry<Integer, CharSequence> e = it.next(); assertTrue( (e.getKey().equals(one) && "A".contentEquals(e.getValue())) || (e.getKey().equals(two) && "B".contentEquals(e.getValue())) || (e.getKey().equals(three) && "C".contentEquals(e.getValue())) || (e.getKey().equals(four) && "D".contentEquals(e.getValue())) || (e.getKey().equals(five) && "E".contentEquals(e.getValue())) ); } } }
Example #13
Source File: ChronicleMapTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
/** * putAll adds all key-value pairs from the given map */ @Test(timeout = 5000) public void testPutAll() throws IOException { try (ChronicleMap empty = newShmIntString(8076)) { try (ChronicleMap map = map5()) { empty.putAll(map); assertEquals(5, empty.size()); assertTrue(empty.containsKey(one)); assertTrue(empty.containsKey(two)); assertTrue(empty.containsKey(three)); assertTrue(empty.containsKey(four)); assertTrue(empty.containsKey(five)); } } }
Example #14
Source File: OffHeapVarBitMetricStore.java From yuvi with Apache License 2.0 | 6 votes |
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 #15
Source File: ChronicleMapTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
/** * remove removes the correct key-value pair from the map */ @Test(timeout = 5000) public void testRemove() throws IOException { try (ChronicleMap map = map5()) { map.remove(five); assertEquals(4, map.size()); assertFalse(map.containsKey(five)); } }
Example #16
Source File: ChronicleMapTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
/** * putIfAbsent works when the given key is not present */ @Test(timeout = 5000) public void testPutIfAbsent() throws IOException { try (ChronicleMap map = map5()) { map.putIfAbsent(six, "Z"); assertTrue(map.containsKey(six)); } }
Example #17
Source File: ChronicleMapTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
/** * keySet returns a Set containing all the keys */ @Test(timeout = 5000) public void testKeySet() throws IOException { try (ChronicleMap map = map5()) { Set s = map.keySet(); assertEquals(5, s.size()); assertTrue(s.contains(one)); assertTrue(s.contains(two)); assertTrue(s.contains(three)); assertTrue(s.contains(four)); assertTrue(s.contains(five)); } }
Example #18
Source File: ChronicleMapTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
/** * Values.toArray contains all values */ @Test(timeout = 5000) public void testValuesToArray() throws IOException { try (ChronicleMap<Integer, CharSequence> map = map5()) { Collection<CharSequence> vs = map.values(); ArrayList<CharSequence> s = new ArrayList<>(vs); assertEquals(5, s.size()); assertTrue(s.stream().anyMatch("A"::contentEquals)); assertTrue(s.stream().anyMatch("B"::contentEquals)); assertTrue(s.stream().anyMatch("C"::contentEquals)); assertTrue(s.stream().anyMatch("D"::contentEquals)); assertTrue(s.stream().anyMatch("E"::contentEquals)); } }
Example #19
Source File: ChronicleMapTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
/** * isEmpty is true of empty map and false for non-empty */ @Test(timeout = 5000) public void testIsEmpty() throws IOException { try (ChronicleMap empty = newShmIntString(8078)) { try (ChronicleMap map = map5()) { if (!empty.isEmpty()) { System.out.print("not empty " + empty); } assertTrue(empty.isEmpty()); assertFalse(map.isEmpty()); } } }
Example #20
Source File: ChronicleMapTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
/** * entrySet.toArray contains all entries */ @Test(timeout = 5000) public void testEntrySetToArray() throws IOException { try (ChronicleMap map = map5()) { Set s = map.entrySet(); Object[] ar = s.toArray(); assertEquals(5, ar.length); for (int i = 0; i < 5; ++i) { assertTrue(map.containsKey(((Entry) (ar[i])).getKey())); assertTrue(map.containsValue(((Entry) (ar[i])).getValue())); } } }
Example #21
Source File: WordCountTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
@Test public void wordCountTest() throws IOException { try (ChronicleMap<CharSequence, IntValue> map = ChronicleMap .of(CharSequence.class, IntValue.class) .averageKeySize(7) // average word is 7 ascii bytes long (text in english) .entries(expectedSize) .create()) { IntValue v = Values.newNativeReference(IntValue.class); for (String word : words) { try (Closeable ignored = map.acquireContext(word, v)) { v.addValue(1); } } } }
Example #22
Source File: ChronicleMapTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
/** * replace fails when the given key is not present */ @Test(timeout = 5000) public void testReplace() throws IOException { try (ChronicleMap map = map5()) { assertNull(map.replace(six, "Z")); assertFalse(map.containsKey(six)); } }
Example #23
Source File: PingPongCASLeft.java From Chronicle-Map with Apache License 2.0 | 5 votes |
static ChronicleMap<String, BondVOInterface> acquireCHM() throws IOException { // ensure thread ids are globally unique. AffinitySupport.setThreadId(); return ChronicleMapBuilder.of(String.class, BondVOInterface.class) .entries(16) .averageKeySize("369604101".length()).create(); }
Example #24
Source File: PingPongCASLeft.java From Chronicle-Map with Apache License 2.0 | 5 votes |
static void playPingPong(ChronicleMap<String, BondVOInterface> chm, double _coupon, double _coupon2, boolean setFirst, final String desc) { BondVOInterface bond1 = newNativeReference(BondVOInterface.class); BondVOInterface bond2 = newNativeReference(BondVOInterface.class); BondVOInterface bond3 = newNativeReference(BondVOInterface.class); BondVOInterface bond4 = newNativeReference(BondVOInterface.class); chm.acquireUsing("369604101", bond1); chm.acquireUsing("369604102", bond2); chm.acquireUsing("369604103", bond3); chm.acquireUsing("369604104", bond4); System.out.printf("\n\n" + desc + ": Timing 1 x off-heap operations on /dev/chm/RDR_DIM_Mock\n"); if (setFirst) { bond1.setCoupon(_coupon); bond2.setCoupon(_coupon); bond3.setCoupon(_coupon); bond4.setCoupon(_coupon); } int timeToCallNanoTime = 30; int runs = 1000000; long[] timings = new long[runs]; for (int j = 0; j < 10; j++) { for (int i = 0; i < runs; i++) { long _start = System.nanoTime(); // while (!bond1.compareAndSwapCoupon(_coupon, _coupon2)) ; while (!bond2.compareAndSwapCoupon(_coupon, _coupon2)) ; while (!bond3.compareAndSwapCoupon(_coupon, _coupon2)) ; while (!bond4.compareAndSwapCoupon(_coupon, _coupon2)) ; timings[i] = (System.nanoTime() - _start - timeToCallNanoTime) / 4; } Arrays.sort(timings); System.out.printf("#%d: compareAndSwapCoupon() 50/90/99%%tile was %,d / %,d / %,d%n", j, timings[runs / 2], timings[runs * 9 / 10], timings[runs * 99 / 100]); } }
Example #25
Source File: MapCheck.java From Chronicle-Map with Apache License 2.0 | 5 votes |
static void closeMap(Map map) { if (map instanceof ChronicleMap) { ChronicleMap chm = (ChronicleMap) map; try { chm.close(); } catch (Exception e) { throw new RuntimeException("Can't close CHM : " + e); } } }
Example #26
Source File: ChronicleMapTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
/** * replace(null, x, y) throws NPE */ @Test(timeout = 5000) public void testReplaceValue_NullPointerException () throws IOException { try (ChronicleMap c = newShmIntString(8076)) { c.replace(null, "A", "whatever"); shouldThrow(); } catch (NullPointerException success) { } }
Example #27
Source File: PingPongLockLeft.java From Chronicle-Map with Apache License 2.0 | 5 votes |
static void playPingPong(ChronicleMap<String, BondVOInterface> chm, double _coupon, double _coupon2, boolean setFirst, final String desc) throws InterruptedException { BondVOInterface bond1 = newNativeReference(BondVOInterface.class); BondVOInterface bond2 = newNativeReference(BondVOInterface.class); BondVOInterface bond3 = newNativeReference(BondVOInterface.class); BondVOInterface bond4 = newNativeReference(BondVOInterface.class); chm.acquireUsing("369604101", bond1); chm.acquireUsing("369604102", bond2); chm.acquireUsing("369604103", bond3); chm.acquireUsing("369604104", bond4); System.out.printf("\n\n" + desc + ": Timing 1 x off-heap operations on " + chm.file() + "\n"); if (setFirst) { bond1.setCoupon(_coupon); bond2.setCoupon(_coupon); bond3.setCoupon(_coupon); bond4.setCoupon(_coupon); } int timeToCallNanoTime = 30; int runs = 1000000; long[] timings = new long[runs]; for (int j = 0; j < 10; j++) { for (int i = 0; i < runs; i++) { long _start = System.nanoTime(); toggleCoupon(bond1, _coupon, _coupon2); toggleCoupon(bond2, _coupon, _coupon2); toggleCoupon(bond3, _coupon, _coupon2); toggleCoupon(bond4, _coupon, _coupon2); timings[i] = (System.nanoTime() - _start - timeToCallNanoTime) / 4; } Arrays.sort(timings); System.out.printf("#%d: lock,compare,set,unlock 50/90/99%%tile was %,d / %,d / %,d%n", j, timings[runs / 2], timings[runs * 9 / 10], timings[runs * 99 / 100]); } }
Example #28
Source File: PointListSerializationTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
@Test public void testComplexSerialization() { try (ChronicleMap<String, A> map = ChronicleMapBuilder .of(String.class, A.class) .valueMarshaller(AMarshaller.INSTANCE) .entries(5) .averageKeySize(4) .averageValueSize(1000) .create()) { A obj_A = new A(); obj_A.str_ = "a"; obj_A.list_ = new ArrayList<>(); B b = new B(); b.str_ = "b"; obj_A.list_.add(b); map.put("KEY1", obj_A); map.get("KEY1"); } }
Example #29
Source File: PortfolioValueTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
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 #30
Source File: PortfolioValueTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
@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); } } }