net.openhft.chronicle.values.Values Java Examples
The following examples show how to use
net.openhft.chronicle.values.Values.
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: MemoryLeaksTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
private ChronicleMap<IntValue, String> getMap() throws IOException { VanillaChronicleMap<IntValue, String, ?> map; if (persisted) { map = (VanillaChronicleMap<IntValue, String, ?>) builder.createPersistedTo(folder.newFile()); } else { map = (VanillaChronicleMap<IntValue, String, ?>) builder.create(); } IntValue key = Values.newHeapInstance(IntValue.class); int i = 0; while (!map.hasExtraTierBulks()) { key.setValue(i++); map.put(key, "string" + i); } return map; }
Example #2
Source File: ValueInterfaceWithEnumTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
/** * 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 #3
Source File: StringArrayExample.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@Test public void examplePutAndGet() { ChronicleMap<Integer, CharSequenceArray> map = ChronicleMapBuilder .of(Integer.class, CharSequenceArray.class) .entries(100) .create(); { CharSequenceArray charSequenceArray = Values.newHeapInstance(CharSequenceArray.class); map.put(1, charSequenceArray); } { // compute - change the value in the array map.compute(1, this::setToHello); } { // get - read the value CharSequence charSequence = map.getUsing(1, charSequenceArray).getCharSequenceWrapperAt(1).getCharSequence(); System.out.println(charSequence); } { // to string all the values System.out.println(map.getUsing(1, charSequenceArray).toString()); } }
Example #4
Source File: SimpleMapOperationsListeningTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@Test public void simpleLoggingTest() { ChronicleMap<Integer, IntValue> map = ChronicleMapBuilder .of(Integer.class, IntValue.class) .entries(100) .entryOperations(simpleLoggingMapEntryOperations()) .defaultValueProvider(simpleLoggingDefaultValueProvider()) .create(); IntValue value = Values.newHeapInstance(IntValue.class); value.setValue(2); map.put(1, value); map.remove(1); map.acquireUsing(3, Values.newNativeReference(IntValue.class)).addAtomicValue(1); IntValue value2 = Values.newHeapInstance(IntValue.class); value2.setValue(5); map.forEachEntry(e -> e.context().replaceValue(e, e.context().wrapValueAsData(value2))); map.forEachEntry(e -> e.context().remove(e)); }
Example #5
Source File: DataKeyValueTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@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 #6
Source File: TrickyContextCasesTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@Test(expected = IllegalStateException.class) public void nestedContextsSameKeyTest() { ChronicleMap<Integer, IntValue> map = ChronicleMapBuilder .of(Integer.class, IntValue.class) .entries(1).create(); IntValue v = Values.newHeapInstance(IntValue.class); v.setValue(2); map.put(1, v); try (ExternalMapQueryContext<Integer, IntValue, ?> q = map.queryContext(1)) { q.writeLock().lock(); // assume the value is 2 IntValue v2 = q.entry().value().get(); // this call should throw ISE, as accessing the key 1 in a nested context, but if not... map.remove(1); v.setValue(3); map.put(2, v); // prints 3 System.out.println(v2.getValue()); } }
Example #7
Source File: AbstractChronicleMapConverter.java From Chronicle-Map with Apache License 2.0 | 6 votes |
private static Class forName(String clazz) { try { return Class.forName(clazz); } catch (ClassNotFoundException e) { boolean isNative = clazz.endsWith($$NATIVE); boolean isHeap = clazz.endsWith($$HEAP); if (!isNative && !isHeap) throw new ConversionException("class=" + clazz, e); final String nativeInterface = isNative ? clazz.substring(0, clazz.length() - $$NATIVE.length()) : clazz.substring(0, clazz.length() - $$HEAP.length()); try { Values.newNativeReference(Class.forName(clazz)); return Class.forName(nativeInterface); } catch (Exception e1) { throw new ConversionException("class=" + clazz, e1); } } }
Example #8
Source File: IntValueMapTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@Test public void test() throws IOException { try (final ChronicleMap<IntValue, CharSequence> map = ChronicleMapBuilder .of(IntValue.class, CharSequence.class) .averageValue("test") .entries(20000).create()) { IntValue value = Values.newNativeReference(IntValue.class); ((Byteable) value).bytesStore(NativeBytesStore.nativeStoreWithFixedCapacity(4), 0, 4); value.setValue(1); final String expected = "test"; map.put(value, expected); final CharSequence actual = map.get(value); assertEquals(expected, actual.toString()); // this will fail map.toString(); } }
Example #9
Source File: CHMUseCasesTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@Test public void bondExample() throws IOException { ChronicleMapBuilder builder = ChronicleMapBuilder.of(String.class, BondVOInterface.class) .entries(1) .averageKeySize(10); try (ChronicleMap<String, BondVOInterface> chm = newInstance(builder)) { BondVOInterface bondVO = Values.newNativeReference(BondVOInterface.class); try (net.openhft.chronicle.core.io.Closeable c = chm.acquireContext("369604103", bondVO)) { bondVO.setIssueDate(parseYYYYMMDD("20130915")); bondVO.setMaturityDate(parseYYYYMMDD("20140915")); bondVO.setCoupon(5.0 / 100); // 5.0% BondVOInterface.MarketPx mpx930 = bondVO.getMarketPxIntraDayHistoryAt(0); mpx930.setAskPx(109.2); mpx930.setBidPx(106.9); BondVOInterface.MarketPx mpx1030 = bondVO.getMarketPxIntraDayHistoryAt(1); mpx1030.setAskPx(109.7); mpx1030.setBidPx(107.6); } } }
Example #10
Source File: CHMUseCasesTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@Test(expected = IllegalArgumentException.class) public void testAcquireUsingImmutableUsing() throws IOException { ChronicleMapBuilder<IntValue, CharSequence> builder = ChronicleMapBuilder .of(IntValue.class, CharSequence.class) .entries(1); try (ChronicleMap<IntValue, CharSequence> map = newInstance(builder)) { IntValue using = Values.newHeapInstance(IntValue.class); using.setValue(1); try (Closeable c = map.acquireContext(using, "")) { assertTrue(using instanceof IntValue); using.setValue(1); } assertEquals(null, map.get("1")); mapChecks(); } }
Example #11
Source File: CHMUseCasesTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@Test public void testAcquireUsingWithIntValueKeyStringBuilderValue() throws IOException { ChronicleMapBuilder<IntValue, StringBuilder> builder = ChronicleMapBuilder .of(IntValue.class, StringBuilder.class) .entries(1); try (ChronicleMap<IntValue, StringBuilder> map = newInstance(builder)) { IntValue key = Values.newHeapInstance(IntValue.class); key.setValue(1); StringBuilder using = new StringBuilder(); try (net.openhft.chronicle.core.io.Closeable c = map.acquireContext(key, using)) { using.append("Hello"); } assertEquals("Hello", map.get(key).toString()); mapChecks(); } }
Example #12
Source File: CHMUseCasesTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@Test public void testGeneratedDataValue() throws IOException { ChronicleMapBuilder<String, IBean> builder = ChronicleMapBuilder .of(String.class, IBean.class).averageKeySize(5).entries(1000); try (ChronicleMap<String, IBean> map = newInstance(builder)) { IBean iBean = Values.newNativeReference(IBean.class); try (net.openhft.chronicle.core.io.Closeable c = map.acquireContext("1", iBean)) { iBean.setDouble(1.2); iBean.setLong(2); iBean.setInt(4); IBean.Inner innerAt = iBean.getInnerAt(1); innerAt.setMessage("Hello world"); } assertEquals(2, map.get("1").getLong()); assertEquals("Hello world", map.get("1").getInnerAt(1).getMessage()); mapChecks(); } }
Example #13
Source File: CHMUseCasesTest.java From Chronicle-Map with Apache License 2.0 | 6 votes |
@Test public void testBytesMarshallable() throws IOException { ChronicleMapBuilder<IData, IData> builder = ChronicleMapBuilder .of(IData.class, IData.class) .entries(1000); try (ChronicleMap<IData, IData> map = newInstance(builder)) { for (int i = 0; i < 100; i++) { IData key = Values.newHeapInstance(IData.class); IData value = Values.newHeapInstance(IData.class); key.setText("key-" + i); key.setNumber(i); value.setNumber(i); value.setText("value-" + i); map.put(key, value); // check the map is still valid. map.entrySet().toString(); } } }
Example #14
Source File: MemoryLeaksTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
public MemoryLeaksTest(String testType, boolean replicated, boolean persisted, boolean closeWithinContext) { this.persisted = persisted; this.closeWithinContext = closeWithinContext; builder = ChronicleMap .of(IntValue.class, String.class).constantKeySizeBySample(Values.newHeapInstance(IntValue.class)) .valueReaderAndDataAccess(new CountedStringReader(this), new StringUtf8DataAccess()); if (replicated) builder.replication((byte) 1); builder.entries(1).averageValueSize(10); }
Example #15
Source File: ChronicleMapTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
@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 #16
Source File: ProcessInstanceLimiter.java From Chronicle-Map with Apache License 2.0 | 5 votes |
/** * Set the maximum number of processes of type processType that can run * concurrently on the same machine * * @param processType - any string, specifies the type of process that is limited to * maxNumberOfProcessesAllowed processes * @param maxNumberOfProcessesAllowed - any positive number, specifies the maximum number of * processes of this type that can run concurrently on the same * machine */ public void setMaxNumberOfProcessesOfType(String processType, int maxNumberOfProcessesAllowed) { if (maxNumberOfProcessesAllowed <= 0) { throw new IllegalArgumentException("maxNumberOfProcessesAllowed must be a positive number, not " + maxNumberOfProcessesAllowed); } Data data = Values.newNativeReference(Data.class); this.timedata.put(processType, data); this.theSharedMap.acquireUsing(processType, data); if (data.getMaxNumberOfProcessesAllowed() != maxNumberOfProcessesAllowed) { //it's either a new object, set to 0, or //another process set it to an invalid value if (data.compareAndSwapMaxNumberOfProcessesAllowed(0, maxNumberOfProcessesAllowed)) { //What we expected, everything's good } else { //something else set a value, if it's not 2 we've got a conflict if (data.getMaxNumberOfProcessesAllowed() != maxNumberOfProcessesAllowed) { throw new IllegalArgumentException("The existing shared map already specifies that the maximum number of processes allowed is " + data.getMaxNumberOfProcessesAllowed() + " and changing that to " + maxNumberOfProcessesAllowed + " is not supported"); } } } String name = processType + '#'; data = Values.newNativeReference(Data.class); this.starttimedata.put(processType, data); this.processTypeToStartTimeType.put(processType, name); this.theSharedMap.acquireUsing(name, data); //this time just set it, we've done the guarding with the other value if (data.getMaxNumberOfProcessesAllowed() == 0) { data.setMaxNumberOfProcessesAllowed(maxNumberOfProcessesAllowed); } }
Example #17
Source File: LataTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
public static void main(String args[]) throws Exception { long startTime = 0; long endTime = 0; ChronicleMapBuilder<StringValue, IData> builder = ChronicleMapBuilder .of(StringValue.class, IData.class) .entries(max + 1000000); ChronicleMap<StringValue, IData> map = builder.create(); StringValue[] keys = new StringValue[300]; for (int i = 0; i < keys.length; i++) { keys[i] = Values.newHeapInstance(StringValue.class); keys[i].setValue("" + i); } IData value = Values.newHeapInstance(IData.class); IData dataValue = Values.newHeapInstance(IData.class); for (int index = 0; index < run; index++) { currentRun++; startTime = System.nanoTime(); for (int i = 0; i < max; i++) { StringValue key = keys[i % keys.length]; if (!(map.containsKey(key))) { value.setData(i); map.put(key, value); } else { value = map.acquireUsing(key, dataValue); value.addAtomicData(10); } } endTime = System.nanoTime(); map.clear(); System.out.println("Run" + currentRun + "Time taken" + (endTime - startTime)); } map.close(); }
Example #18
Source File: CHMUseCasesTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
@Test public void testAcquireUsingWithIntValueKey() throws IOException { ChronicleMapBuilder<IntValue, CharSequence> builder = ChronicleMapBuilder .of(IntValue.class, CharSequence.class) .entries(3); try (ChronicleMap<IntValue, CharSequence> map = newInstance(builder)) { IntValue key = Values.newHeapInstance(IntValue.class); key.setValue(1); CharSequence using = new StringBuilder(); try (net.openhft.chronicle.core.io.Closeable c = map.acquireContext(key, using)) { key.setValue(3); ((StringBuilder) using).append("Hello"); } key.setValue(2); try (net.openhft.chronicle.core.io.Closeable c = map.acquireContext(key, using)) { ((StringBuilder) using).append("World"); } key.setValue(1); assertEquals("Hello", map.get(key).toString()); mapChecks(); } }
Example #19
Source File: OffHeapVarBitMetricStore.java From yuvi with Apache License 2.0 | 5 votes |
@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 #20
Source File: ChronicleMapTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
@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 #21
Source File: MemoryLeaksTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
private void tryCloseFromContext(ChronicleMap<IntValue, String> map) { // Test that the map could still be successfully closed and no leaks are introduced // by an attempt to close the map from within context. if (closeWithinContext) { IntValue key = Values.newHeapInstance(IntValue.class); try (ExternalMapQueryContext<IntValue, String, ?> c = map.queryContext(key)) { c.updateLock().lock(); try { map.close(); } catch (IllegalStateException expected) { // expected } } } }
Example #22
Source File: PortfolioValueTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
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 #23
Source File: SingleChroniclePerfMain.java From Chronicle-Queue with Apache License 2.0 | 5 votes |
@Test public void testFacarde() { IFacade f = Values.newNativeReference(IFacade.class); Byteable byteable = (Byteable) f; long capacity = byteable.maxSize(); byteable.bytesStore(NativeBytesStore.nativeStore(capacity), 0, capacity); System.out.println(f); }
Example #24
Source File: ChronicleMapUnitTest.java From tutorials with MIT License | 5 votes |
@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 #25
Source File: ChronicleMapUnitTest.java From tutorials with MIT License | 5 votes |
@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 #26
Source File: ValueDataAccess.java From Chronicle-Map with Apache License 2.0 | 5 votes |
private void initTransients() { nativeInstance = (Byteable) Values.newNativeReference(valueType); nativeInstanceAsCopyable = (Copyable) nativeInstance; nativeClass = (Class<? extends T>) nativeInstance.getClass(); heapClass = Values.heapClassFor(valueType); nativeInstance.bytesStore(allocateBytesStoreForInstance(), 0, nativeInstance.maxSize()); }
Example #27
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 #28
Source File: OffHeapByteArrayExampleTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
@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 #29
Source File: PortfolioValueTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
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 #30
Source File: ChronicleMapTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
@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))); } }