Java Code Examples for net.openhft.chronicle.values.Values#newNativeReference()

The following examples show how to use net.openhft.chronicle.values.Values#newNativeReference() . 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: IntValueMapTest.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
@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 2
Source File: AbstractChronicleMapConverter.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
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 3
Source File: CHMUseCasesTest.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
@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 4
Source File: CHMUseCasesTest.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
@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 5
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 6
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 7
Source File: SingleChroniclePerfMain.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@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 8
Source File: WordCountTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@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 9
Source File: ProcessInstanceLimiter.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
/**
 * 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 10
Source File: ChronicleMapImportExportTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@Test
public void testBondVOInterface() throws IOException, InterruptedException {

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

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

        final BondVOInterface value = Values.newNativeReference(BondVOInterface.class);

        // this will add the entry
        try (Closeable c = expected.acquireContext("one", value)) {
            value.setCoupon(8.98);
            BondVOInterface.MarketPx marketPxIntraDayHistoryAt =
                    value.getMarketPxIntraDayHistoryAt(1);

            marketPxIntraDayHistoryAt.setAskPx(12.0);
        }

        expected.getAll(file);

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

            actual.putAll(file);

            Assert.assertEquals(expected.get("one").getCoupon(),
                    actual.get("one").getCoupon(), 0);
        }
    } finally {
        file.delete();
    }
}
 
Example 11
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 12
Source File: ValueDataAccess.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
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 13
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 14
Source File: ReplicatedChronicleMap.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@Override
protected VanillaGlobalMutableState createGlobalMutableState() {
    return Values.newNativeReference(ReplicatedGlobalMutableState.class);
}
 
Example 15
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 16
Source File: ChronicleMapTest.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
public static IntValue nativeIntValue() {
    return Values.newNativeReference(IntValue.class);
}
 
Example 17
Source File: ChronicleMapTest.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
public static LongValue nativeLongValue() {
    return Values.newNativeReference(LongValue.class);
}
 
Example 18
Source File: GlobalMutableStateTest.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@Test
public void globalMutableStateTest() {
    Values.newNativeReference(ReplicatedGlobalMutableState.class);
    Values.newNativeReference(VanillaGlobalMutableState.class);
}
 
Example 19
Source File: VanillaChronicleHash.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
protected VanillaGlobalMutableState createGlobalMutableState() {
    return Values.newNativeReference(VanillaGlobalMutableState.class);
}
 
Example 20
Source File: ValueReader.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
private void initTransients() {
    nativeClass = Values.nativeClassFor(valueType);
    heapClass = Values.heapClassFor(valueType);
    nativeReference = (Byteable) Values.newNativeReference(valueType);
}