diff --git a/README.md b/README.md index 768c406d7..76ee1a7aa 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,10 @@ Click here to get the [Latest Version Number](http://search.maven.org/#search%7C * [Identifier](https://github.com/OpenHFT/Chronicle-Map#identifier) * [Port](https://github.com/OpenHFT/Chronicle-Map#port) * [Heart Beat Interval](https://github.com/OpenHFT/Chronicle-Map#heart-beat-interval) - * [Clustering](https://github.com/OpenHFT/Chronicle-Map#cluster) +* [Clustering](https://github.com/OpenHFT/Chronicle-Map#cluster) + +#### Miscellaneous + * [Known Issues](https://github.com/OpenHFT/Chronicle-Map#known-issues) * [Stackoverflow](http://stackoverflow.com/tags/chronicle/info) * [Development Tasks - JIRA] (https://higherfrequencytrading.atlassian.net/browse/HCOLL) @@ -54,8 +57,10 @@ Click here to get the [Latest Version Number](http://search.maven.org/#search%7C #### Examples -* [Replicating data between process on different servers with TCP/IP Replication](https://github.com/OpenHFT/Chronicle-Map#example--replicating-data-between-process-on-different-servers) -* [Replicating data between process on different servers with UDP] (https://github.com/OpenHFT/Chronicle-Map/blob/master/README.md#example--replicating-data-between-process-on-different-servers-using-udp) + * [Hello World - A map which stores data off heap](https://github.com/OpenHFT/Chronicle-Map/blob/master/README.md#example--simple-hello-world) + * [Sharing the map between two ( or more ) processes on the same computer](https://github.com/OpenHFT/Chronicle-Map/blob/master/README.md#example--sharing-the-map-on-two--or-more--processes-on-the-same-machine) + * [Replicating data between process on different servers with TCP/IP Replication](https://github.com/OpenHFT/Chronicle-Map/blob/master/README.md#example--replicating-data-between-process-on-different-servers-via-tcp) + * [Replicating data between process on different servers with UDP] (https://github.com/OpenHFT/Chronicle-Map/blob/master/README.md#example--replicating-data-between-process-on-different-servers-using-udp) #### Performance Topics @@ -593,6 +598,45 @@ will run out of space between 1 and 2 million entries. You should set the .entries(..) and .entrySize(..) to those you require. +# Example : Simple Hello World + +This simple chronicle map, works just like ConcurrentHashMap but stores its data off-heap. If you want to use Chronicle Map to share data between java process you should look at the next exampl + +``` java +Map map = ChronicleMapBuilder.of(Integer.class, + CharSequence.class).create(); + +map.put(1, "hello world"); +System.out.println(map.get(1)); + +``` + +# Example : Sharing the map on two ( or more ) processes on the same machine + +Lets assume that we had two server, lets call them server1 and server2, if we wished to share a map +between them, this is how we could set it up + +``` java + +// --- RUN ON ONE JAVA PROCESS ( BUT ON THE SAME SERVER ) +{ + File file = new File("a-new-file-on-your-sever"); + Map map1 = ChronicleMapBuilder.of(Integer.class, + CharSequence.class).create(file); // this has to be the same file as used by map 2 + map1.put(1, "hello world"); +} + +// --- RUN ON THE OTHER JAVA PROCESS ( BUT ON THE SAME SERVER ) +{ + File file = new File("a-new-file-on-your-sever"); // this has to be the same file as used by map 1 + Map map1 = ChronicleMapBuilder.of(Integer.class, + CharSequence.class).create(file); + + System.out.println(map1.get(1)); +} +``` + + # Example : Replicating data between process on different servers via TCP Lets assume that we had two server, lets call them server1 and server2, if we wished to share a map @@ -617,7 +661,7 @@ Map map2; map1 = map1Builder.create(); } -// ---------- SERVER2 2 on the same server as ---------- +// ---------- SERVER2 on the same server as ---------- { TcpReplicationConfig tcpConfig = TcpReplicationConfig.of(8077) diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java b/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java index b7aa0831b..cf20ec795 100644 --- a/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java +++ b/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java @@ -823,7 +823,8 @@ public class ChronicleMapBuilder implements Cloneable { public ChronicleMap create() throws IOException { VanillaChronicleMap map = newMap(); - BytesStore bytesStore = new DirectStore(objectSerializer(), map.sizeInBytes(), true); + BytesStore bytesStore = new DirectStore(JDKObjectSerializer.INSTANCE, + map.sizeInBytes(), true); map.createMappedStoreAndSegments(bytesStore); return establishReplication(map); } diff --git a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java index 13f445976..8718a0d38 100644 --- a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java @@ -200,7 +200,7 @@ class VanillaChronicleMap, long createMappedStoreAndSegments(File file) throws IOException { return createMappedStoreAndSegments(new MappedStore(file, FileChannel.MapMode.READ_WRITE, - sizeInBytes(), objectSerializer)); + sizeInBytes(), JDKObjectSerializer.INSTANCE)); } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { diff --git a/src/main/java/net/openhft/chronicle/map/serialization/DirectBytesBuffer.java b/src/main/java/net/openhft/chronicle/map/serialization/DirectBytesBuffer.java deleted file mode 100644 index 1f454a62b..000000000 --- a/src/main/java/net/openhft/chronicle/map/serialization/DirectBytesBuffer.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.openhft.chronicle.map.serialization; - -import net.openhft.lang.io.Bytes; -import net.openhft.lang.io.DirectBytes; -import net.openhft.lang.io.DirectStore; -import net.openhft.lang.io.serialization.JDKObjectSerializer; -import net.openhft.chronicle.map.threadlocal.StatefulCopyable; - -final class DirectBytesBuffer implements StatefulCopyable { - - private final Object identity; - private DirectBytes buffer; - - DirectBytesBuffer(Object identity) { - this.identity = identity; - } - - private Bytes buffer(long maxSize) { - DirectBytes buf; - if ((buf = buffer) != null) { - if (maxSize <= buf.capacity()) { - return buf.clear(); - } else { - DirectStore store = (DirectStore) buf.store(); - store.resize(maxSize, false); - return buffer = store.bytes(); - } - } else { - return buffer = new DirectStore(JDKObjectSerializer.INSTANCE, maxSize, false).bytes(); - } - } - - @Override - public Object stateIdentity() { - return identity; - } - - @Override - public DirectBytesBuffer copy() { - return new DirectBytesBuffer(identity); - } -} diff --git a/src/test/java/net/openhft/chronicle/map/TestBooleanValues.java b/src/test/java/net/openhft/chronicle/map/TestBooleanValues.java index b6d33e2ed..7306e153f 100644 --- a/src/test/java/net/openhft/chronicle/map/TestBooleanValues.java +++ b/src/test/java/net/openhft/chronicle/map/TestBooleanValues.java @@ -11,13 +11,13 @@ import java.io.IOException; */ public class TestBooleanValues { - @Test - @Ignore /** * see issue http://stackoverflow.com/questions/26219313/strange-npe-from-chronicle-map-toy-code */ + @Test public void testTestBooleanValues() throws IOException, InterruptedException { - ChronicleMap map = ChronicleMapBuilder.of(Integer.class, Boolean.class).create(); + ChronicleMap map = + ChronicleMapBuilder.of(Integer.class, Boolean.class).create(); map.put(7, true); Boolean currentValue = map.get(7); // IllegalStateException here }