diff --git a/src/main/java/net/openhft/chronicle/ChronicleHashBuilder.java b/src/main/java/net/openhft/chronicle/ChronicleHashBuilder.java index 53345e6b9..20cd595b5 100644 --- a/src/main/java/net/openhft/chronicle/ChronicleHashBuilder.java +++ b/src/main/java/net/openhft/chronicle/ChronicleHashBuilder.java @@ -189,9 +189,7 @@ public interface ChronicleHashBuilder{@code * ChronicleMap map = ChronicleMapBuilder * .of(Key.class, Value.class) @@ -66,25 +65,22 @@ import static net.openhft.chronicle.map.Objects.builderEquals; * *

Use static {@link #of(Class, Class)} method to obtain a {@code ChronicleMapBuilder} instance. * - *

{@code ChronicleMapBuilder} is mutable. Configuration methods mutate the builder and - * return the builder itself back to support chaining pattern, rather than the builder copies - * with the corresponding configuration changed. To make an independent configuration, - * {@linkplain #clone} the builder. + *

{@code ChronicleMapBuilder} is mutable. Configuration methods mutate the builder and return the + * builder itself back to support chaining pattern, rather than the builder copies with the corresponding + * configuration changed. To make an independent configuration, {@linkplain #clone} the builder. * - *

Later in this documentation, "ChronicleMap" means - * "ChronicleMaps, created by {@code ChronicleMapBuilder}", unless specified different, because - * theoretically someone might provide {@code ChronicleMap} implementations with completely - * different properties. + *

Later in this documentation, "ChronicleMap" means "ChronicleMaps, created by {@code + * ChronicleMapBuilder}", unless specified different, because theoretically someone might provide {@code + * ChronicleMap} implementations with completely different properties. * - *

{@code ChronicleMap} ("ChronicleMaps, created by {@code ChronicleMapBuilder}") currently - * doesn't support resizing. That is why you should always configure {@linkplain - * #entries(long) number of entries} you are going to insert into the created map at most. - * See {@link #entries(long)} method documentation for more information on this. + *

{@code ChronicleMap} ("ChronicleMaps, created by {@code ChronicleMapBuilder}") currently doesn't support + * resizing. That is why you should always configure {@linkplain #entries(long) number of entries} you + * are going to insert into the created map at most. See {@link #entries(long)} method documentation + * for more information on this. * - *

{@code ChronicleMap} allocates memory by equally sized chunks. This size is called - * {@linkplain #entrySize(int) entry size}, you are strongly recommended to configure it to achieve - * least memory consumption and best speed. See {@link #entrySize(int)} method documentation for - * more information on this. + *

{@code ChronicleMap} allocates memory by equally sized chunks. This size is called {@linkplain + * #entrySize(int) entry size}, you are strongly recommended to configure it to achieve least memory + * consumption and best speed. See {@link #entrySize(int)} method documentation for more information on this. * * @param key type of the maps, produced by this builder * @param value type of the maps, produced by this builder @@ -97,10 +93,11 @@ public class ChronicleMapBuilder implements Cloneable, public static final short UDP_REPLICATION_MODIFICATION_ITERATOR_ID = 128; private static final Logger LOG = LoggerFactory.getLogger(ChronicleMapBuilder.class.getName()); + SerializationBuilder keyBuilder; SerializationBuilder valueBuilder; - Map, Replicator> replicators = new HashMap, Replicator>(); + private Map, Replicator> replicators = new HashMap, Replicator>(); // used when configuring the number of segments. private int minSegments = -1; @@ -190,9 +187,9 @@ public class ChronicleMapBuilder implements Cloneable, * .keySize(10) * // shouldn't specify valueSize(), because it is statically known * .create();} - * (Note that 10 is chosen as key size in bytes despite strings in Java are UTF-16 encoded - * (and each character takes 2 bytes on-heap), because default off-heap {@link String} encoding - * is UTF-8 in {@code ChronicleMap}.) + * (Note that 10 is chosen as key size in bytes despite strings in Java are UTF-16 encoded (and each + * character takes 2 bytes on-heap), because default off-heap {@link String} encoding is UTF-8 in {@code + * ChronicleMap}.) * * @see #constantKeySizeBySample(Object) * @see #valueSize(int) @@ -229,17 +226,16 @@ public class ChronicleMapBuilder implements Cloneable, } /** - * Configures the optimal number of bytes, taken by serialized form of values, put into maps, - * created by this builder. If value size is always the same, call - * {@link #constantValueSizeBySample(Object)} method instead of this one. + * Configures the optimal number of bytes, taken by serialized form of values, put into maps, created by + * this builder. If value size is always the same, call {@link #constantValueSizeBySample(Object)} method + * instead of this one. * - *

If value is a boxed primitive type or {@link Byteable} subclass, i. e. if value size - * is known statically, it is automatically accounted and shouldn't be specified by user. + *

If value is a boxed primitive type or {@link Byteable} subclass, i. e. if value size is known + * statically, it is automatically accounted and shouldn't be specified by user. * - *

If value size varies moderately, specify the size higher than average, but lower than - * the maximum possible, to minimize average memory overuse. If value size varies in a wide - * range, it's better to use {@linkplain #entrySize(int) entry size} in "chunk" mode and - * configure it directly. + *

If value size varies moderately, specify the size higher than average, but lower than the maximum + * possible, to minimize average memory overuse. If value size varies in a wide range, it's better to use + * {@linkplain #entrySize(int) entry size} in "chunk" mode and configure it directly. * * @param valueSize number of bytes, taken by serialized form of values * @return this {@code ChronicleMapBuilder} back @@ -254,15 +250,15 @@ public class ChronicleMapBuilder implements Cloneable, } /** - * Configures the constant number of bytes, taken by serialized form of values, put into maps, - * created by this builder. This is done by providing the {@code sampleValue}, all values should - * take the same number of bytes in serialized form, as this sample object. + * Configures the constant number of bytes, taken by serialized form of values, put into maps, created by + * this builder. This is done by providing the {@code sampleValue}, all values should take the same number + * of bytes in serialized form, as this sample object. * - *

If values are of boxed primitive type or {@link Byteable} subclass, i. e. if value size - * is known statically, it is automatically accounted and this method shouldn't be called. + *

If values are of boxed primitive type or {@link Byteable} subclass, i. e. if value size is known + * statically, it is automatically accounted and this method shouldn't be called. * - *

If value size varies, method {@link #valueSize(int)} or {@link #entrySize(int)} should be - * called instead of this one. + *

If value size varies, method {@link #valueSize(int)} or {@link #entrySize(int)} should be called + * instead of this one. * * @param sampleValue the sample value * @return this builder back @@ -291,18 +287,18 @@ public class ChronicleMapBuilder implements Cloneable, * {@inheritDoc} * *

In fully default case you can expect entry size to be about 256 bytes. But it is strongly - * recommended always to configure {@linkplain #keySize(int) key size} and - * {@linkplain #valueSize(int) value size}, if they couldn't be derived statically. + * recommended always to configure {@linkplain #keySize(int) key size} and {@linkplain #valueSize(int) + * value size}, if they couldn't be derived statically. * - *

If entry size is not configured explicitly by calling this method, it is computed - * based on {@linkplain #metaDataBytes(int) meta data bytes}, plus {@linkplain #keySize(int) key - * size}, plus {@linkplain #valueSize(int) value size}, plus a few bytes required by - * implementations, with respect to {@linkplain #entryAndValueAlignment(Alignment) alignment}. + *

If entry size is not configured explicitly by calling this method, it is computed based on + * {@linkplain #metaDataBytes(int) meta data bytes}, plus {@linkplain #keySize(int) key size}, plus + * {@linkplain #valueSize(int) value size}, plus a few bytes required by implementations, with respect to + * {@linkplain #entryAndValueAlignment(Alignment) alignment}. * *

Note that the actual entrySize will be aligned to 4 (default {@linkplain - * #entryAndValueAlignment(Alignment) entry alignment}). I. e. if you set entry size to 30, - * the actual entry size will be 32 (30 aligned to 4 bytes). If you don't want entry size - * to be aligned, set {@code entryAndValueAlignment(Alignment.NO_ALIGNMENT)}. + * #entryAndValueAlignment(Alignment) entry alignment}). I. e. if you set entry size to 30, the actual + * entry size will be 32 (30 aligned to 4 bytes). If you don't want entry size to be aligned, set {@code + * entryAndValueAlignment(Alignment.NO_ALIGNMENT)}. * * @see #entryAndValueAlignment(Alignment) * @see #entries(long) @@ -344,16 +340,16 @@ public class ChronicleMapBuilder implements Cloneable, } /** - * Configures alignment strategy of address in memory of entries and independently of address - * in memory of values within entries in ChronicleMaps, created by this builder. + * Configures alignment strategy of address in memory of entries and independently of address in memory of + * values within entries in ChronicleMaps, created by this builder. * - *

Useful when values of the map are updated intensively, particularly fields with - * volatile access, because it doesn't work well if the value crosses cache lines. Also, on some - * (nowadays rare) architectures any misaligned memory access is more expensive than aligned. + *

Useful when values of the map are updated intensively, particularly fields with volatile access, + * because it doesn't work well if the value crosses cache lines. Also, on some (nowadays rare) + * architectures any misaligned memory access is more expensive than aligned. * - *

Note that {@linkplain #entrySize(int) entry size} will be aligned according to this - * alignment. I. e. if you set {@code entrySize(20)} and {@link Alignment#OF_8_BYTES}, actual - * entry size will be 24 (20 aligned to 8 bytes). + *

Note that {@linkplain #entrySize(int) entry size} will be aligned according to this alignment. I. e. + * if you set {@code entrySize(20)} and {@link Alignment#OF_8_BYTES}, actual entry size will be 24 (20 + * aligned to 8 bytes). * *

Default is {@link Alignment#OF_4_BYTES}. * @@ -432,20 +428,20 @@ public class ChronicleMapBuilder implements Cloneable, } /** - * Configures if the maps created by this builder should return {@code null} instead of previous - * mapped values on {@link ChronicleMap#put(Object, Object) ChornicleMap.put(key, value)} calls. + * Configures if the maps created by this builder should return {@code null} instead of previous mapped + * values on {@link ChronicleMap#put(Object, Object) ChornicleMap.put(key, value)} calls. * - *

{@link Map#put(Object, Object) Map.put()} returns the previous value, functionality which - * is rarely used but fairly cheap for {@link HashMap}. In the case, for an off heap collection, - * it has to create a new object and deserialize the data from off-heap memory. It's expensive - * for something you probably don't use. + *

{@link Map#put(Object, Object) Map.put()} returns the previous value, functionality which is rarely + * used but fairly cheap for {@link HashMap}. In the case, for an off heap collection, it has to create a + * new object and deserialize the data from off-heap memory. It's expensive for something you probably + * don't use. * - *

By default, of cause, {@code ChronicleMap} conforms the general {@code Map} contract - * and returns the previous mapped value on {@code put()} calls. + *

By default, of cause, {@code ChronicleMap} conforms the general {@code Map} contract and returns the + * previous mapped value on {@code put()} calls. * * @param putReturnsNull {@code true} if you want {@link ChronicleMap#put(Object, Object) - * ChronicleMap.put()} to not return the value that was replaced - * but instead return {@code null} + * ChronicleMap.put()} to not return the value that was replaced but instead return + * {@code null} * @return an instance of the map builder back * @see #removeReturnsNull(boolean) */ @@ -459,17 +455,17 @@ public class ChronicleMapBuilder implements Cloneable, } /** - * Configures if the maps created by this builder should return {@code null} instead of - * the last mapped value on {@link ChronicleMap#remove(Object) ChronicleMap.remove(key)} calls. + * Configures if the maps created by this builder should return {@code null} instead of the last mapped + * value on {@link ChronicleMap#remove(Object) ChronicleMap.remove(key)} calls. * - *

{@link Map#remove(Object) Map.remove()} returns the previous value, functionality which - * is rarely used but fairly cheap for {@link HashMap}. In the case, for an off heap collection, - * it has to create a new object and deserialize the data from off-heap memory. It's expensive - * for something you probably don't use. + *

{@link Map#remove(Object) Map.remove()} returns the previous value, functionality which is rarely + * used but fairly cheap for {@link HashMap}. In the case, for an off heap collection, it has to create a + * new object and deserialize the data from off-heap memory. It's expensive for something you probably + * don't use. * * @param removeReturnsNull {@code true} if you want {@link ChronicleMap#remove(Object) - * ChronicleMap.remove()} to not return the value of the removed entry - * but instead return {@code null} + * ChronicleMap.remove()} to not return the value of the removed entry but + * instead return {@code null} * @return an instance of the map builder back * @see #putReturnsNull(boolean) */ @@ -500,6 +496,7 @@ public class ChronicleMapBuilder implements Cloneable, return this; } + int metaDataBytes() { return metaDataBytes; } @@ -643,19 +640,18 @@ public class ChronicleMapBuilder implements Cloneable, } /** - * Specifies the value to be put for each key queried in {@link ChronicleMap#get get()} and - * {@link ChronicleMap#getUsing(Object, Object) getUsing()} methods, if the key is absent - * in the map. Then this default value is returned from query method. + * Specifies the value to be put for each key queried in {@link ChronicleMap#get get()} and {@link + * ChronicleMap#getUsing(Object, Object) getUsing()} methods, if the key is absent in the map. Then this + * default value is returned from query method. * - *

Setting default value to {@code null} is interpreted as map shouldn't put any default - * value for absent keys. This is by default. + *

Setting default value to {@code null} is interpreted as map shouldn't put any default value for + * absent keys. This is by default. * - *

This configuration overrides any previous - * {@link #defaultValueProvider(DefaultValueProvider)} configuration to this - * {@code ChronicleMapBuilder}. + *

This configuration overrides any previous {@link #defaultValueProvider(DefaultValueProvider)} + * configuration to this {@code ChronicleMapBuilder}. * - * @param defaultValue the default value to be put to the map for absent keys during - * {@code get()} and {@code getUsing()} calls and returned from these calls + * @param defaultValue the default value to be put to the map for absent keys during {@code get()} and + * {@code getUsing()} calls and returned from these calls * @return this builder object back */ public ChronicleMapBuilder defaultValue(V defaultValue) { @@ -665,13 +661,13 @@ public class ChronicleMapBuilder implements Cloneable, } /** - * Specifies the function to obtain a value for the key during {@link ChronicleMap#get get()} - * and {@link ChronicleMap#getUsing(Object, Object) getUsing()} calls, if the key is absent - * in the map. If the obtained value is non-null, it is put for the key in the map and then - * returned from current {@code get()} or {@code getUsing()} call. + * Specifies the function to obtain a value for the key during {@link ChronicleMap#get get()} and {@link + * ChronicleMap#getUsing(Object, Object) getUsing()} calls, if the key is absent in the map. If the + * obtained value is non-null, it is put for the key in the map and then returned from current {@code + * get()} or {@code getUsing()} call. * - *

This configuration overrides any previous {@link #defaultValue(Object)} configuration - * to this {@code ChronicleMapBuilder}. + *

This configuration overrides any previous {@link #defaultValue(Object)} configuration to this {@code + * ChronicleMapBuilder}. * * @param defaultValueProvider the strategy to obtain a default value by the absent key * @return this builder object back diff --git a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java index 9c9642485..6c768546e 100644 --- a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java @@ -17,6 +17,7 @@ package net.openhft.chronicle.map; +import net.openhft.chronicle.TimeProvider; import net.openhft.chronicle.map.serialization.BytesReader; import net.openhft.chronicle.map.serialization.MetaBytesInterop; import net.openhft.chronicle.map.serialization.MetaBytesWriter; diff --git a/src/main/java/net/openhft/chronicle/set/ChronicleSetBuilder.java b/src/main/java/net/openhft/chronicle/set/ChronicleSetBuilder.java index 251b8c000..ec344ca5b 100644 --- a/src/main/java/net/openhft/chronicle/set/ChronicleSetBuilder.java +++ b/src/main/java/net/openhft/chronicle/set/ChronicleSetBuilder.java @@ -16,10 +16,9 @@ package net.openhft.chronicle.set; -import net.openhft.chronicle.TimeProvider; -import net.openhft.chronicle.map.Alignment; import net.openhft.chronicle.ChronicleHashBuilder; import net.openhft.chronicle.ChronicleHashErrorListener; +import net.openhft.chronicle.TimeProvider; import net.openhft.chronicle.map.*; import net.openhft.lang.io.serialization.BytesMarshaller; import net.openhft.lang.io.serialization.BytesMarshallerFactory; @@ -33,7 +32,6 @@ import java.util.AbstractSet; import java.util.Collection; import java.util.Iterator; import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; @@ -90,9 +88,9 @@ public class ChronicleSetBuilder * .entries(50000) * .keySize(10) * .create();} - * (Note that 10 is chosen as key size in bytes despite strings in Java are UTF-16 encoded - * (and each character takes 2 bytes on-heap), because default off-heap {@link String} encoding - * is UTF-8 in {@code ChronicleSet}.) + * (Note that 10 is chosen as key size in bytes despite strings in Java are UTF-16 encoded (and each + * character takes 2 bytes on-heap), because default off-heap {@link String} encoding is UTF-8 in {@code + * ChronicleSet}.) * * @see #constantKeySizeBySample(Object) * @see #entrySize(int) @@ -122,13 +120,13 @@ public class ChronicleSetBuilder /** * {@inheritDoc} * - *

In fully default case you can expect entry size to be about 120-130 bytes. But it is - * strongly recommended always to configure {@linkplain #keySize(int) key size}, - * if they couldn't be derived statically. + *

In fully default case you can expect entry size to be about 120-130 bytes. But it is strongly + * recommended always to configure {@linkplain #keySize(int) key size}, if they couldn't be derived + * statically. * - *

If entry size is not configured explicitly by calling this method, it is computed - * based on {@linkplain #metaDataBytes(int) meta data bytes}, plus {@linkplain #keySize(int) key - * size}, plus a few bytes required by implementations. + *

If entry size is not configured explicitly by calling this method, it is computed based on + * {@linkplain #metaDataBytes(int) meta data bytes}, plus {@linkplain #keySize(int) key size}, plus a few + * bytes required by implementations. */ @Override public ChronicleSetBuilder entrySize(int entrySize) { @@ -166,6 +164,12 @@ public class ChronicleSetBuilder return this; } + @Override + public ChronicleSetBuilder replicators(byte identifier, Replicator... replicators) { + chronicleMapBuilder.replicators(identifier, replicators); + return this; + } + @Override public String toString() { return " ChronicleSetBuilder{" + diff --git a/src/test/java/net/openhft/chronicle/map/fromdocs/GettingStartedTest.java b/src/test/java/net/openhft/chronicle/map/fromdocs/GettingStartedTest.java deleted file mode 100644 index 8e338784a..000000000 --- a/src/test/java/net/openhft/chronicle/map/fromdocs/GettingStartedTest.java +++ /dev/null @@ -1,65 +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.fromdocs; - -import net.openhft.chronicle.map.ChronicleMapBuilder; -import org.junit.Test; - -import java.io.Closeable; -import java.io.File; -import java.io.IOException; -import java.util.Map; - -import static org.junit.Assert.assertEquals; - -/** - * Created by peter on 28/02/14. - */ -public class GettingStartedTest { - - @Test - public void testTheCodeInGuide() throws IOException { - String tmpdir = System.getProperty("java.io.tmpdir"); - Map map = ChronicleMapBuilder.of(String.class, String.class) - - .create(new File(tmpdir + "/shared.map")); - map.put("some.json", "missing"); - map.get("some.json"); - - map.put("Hello", "World"); - String hi = map.get("Hello"); - map.put("Long String", "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"); - String json = "{\"menu\": {\n" + - " \"id\": \"file\",\n" + - " \"value\": \"File\",\n" + - " \"popup\": {\n" + - " \"menuitem\": [\n" + - " {\"value\": \"New\", \"onclick\": \"CreateNew()\"},\n" + - " {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},\n" + - " {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}\n" + - " ]\n" + - " }\n" + - "}}"; - map.put("some.json", json); - - String s2 = map.get("some.json"); - assertEquals(json, s2); - - ((Closeable) map).close(); - - } -}