diff --git a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java index af2ce9369..f1c78c1d6 100755 --- a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java @@ -59,8 +59,9 @@ class VanillaChronicleMap, implements ChronicleMap, Serializable, ReadValue, InstanceOrBytesToInstance, GetValueInterops { + static final boolean checkSegmentMultiMapsAndBitSetsConsistencyOnBootstrap = Boolean.getBoolean( + "chronicleMap.checkSegmentMultiMapsAndBitSetsConsistencyOnBootstrap"); private static final Logger LOG = LoggerFactory.getLogger(VanillaChronicleMap.class); - private static final long serialVersionUID = 2L; final Class kClass; final String dataFileVersion; @@ -93,14 +94,13 @@ class VanillaChronicleMap, // rather than as returning the Object can be expensive for something you probably don't use. final boolean putReturnsNull; final boolean removeReturnsNull; + final Class nativeValueClass; + final MultiMapFactory multiMapFactory; + final int maxChunksPerEntry; private final long lockTimeOutNS; private final ChronicleHashErrorListener errorListener; private final int segmentHeaderSize; private final HashSplitting hashSplitting; - final Class nativeValueClass; - final MultiMapFactory multiMapFactory; - final int maxChunksPerEntry; - transient Provider> keyReaderProvider; transient Provider keyInteropProvider; transient Provider> valueReaderProvider; @@ -110,6 +110,30 @@ class VanillaChronicleMap, transient BytesStore ms; transient long headerSize; transient Set> entrySet; + transient InstanceOrBytesToInstance keyBytesToInstance = + new InstanceOrBytesToInstance() { + @Override + public K toInstance(@NotNull ThreadLocalCopies copies, Bytes keyBytes, long size) { + assertNotNull(copies); + BytesReader keyReader = keyReaderProvider.get(copies, originalKeyReader); + return keyReader.read(keyBytes, size); + } + }; + transient InstanceOrBytesToInstance valueBytesToInstance = + new InstanceOrBytesToInstance() { + @Override + public V toInstance(@NotNull ThreadLocalCopies copies, Bytes valueBytes, long size) { + return readValue(copies, valueBytes, null, size); + } + }; + transient InstanceOrBytesToInstance outputValueBytesToInstance = + new InstanceOrBytesToInstance() { + @Override + public V toInstance(@NotNull ThreadLocalCopies copies, Bytes outputBytes, long size) { + outputBytes.position(outputBytes.position() - size); + return readValue(copies, outputBytes, null, size); + } + }; public VanillaChronicleMap(ChronicleMapBuilder builder) throws IOException { @@ -178,6 +202,48 @@ class VanillaChronicleMap, initTransients(); } + static T newInstance(Class aClass, boolean isKey) { + try { + return isKey ? DataValueClasses.newInstance(aClass) : + DataValueClasses.newDirectInstance(aClass); + + } catch (Exception e) { + if (e.getCause() instanceof IllegalStateException) + throw e; + + if (aClass.isInterface()) + throw new IllegalStateException("It not possible to create a instance from " + + "interface=" + aClass.getSimpleName() + " we recommend you create an " + + "instance in the usual way.", e); + + try { + return aClass.newInstance(); + } catch (Exception e1) { + throw new IllegalStateException("It has not been possible to create a instance " + + "of class=" + aClass.getSimpleName() + + ", Note : its more efficient if your chronicle map is configured with " + + "interface key " + + "and value types rather than classes, as this method is able to use " + + "interfaces to generate off heap proxies that point straight at your data. " + + "In this case you have used a class and chronicle is unable to create an " + + "instance of this class has it does not have a default constructor. " + + "If your class is mutable, we " + + "recommend you create and instance of your class=" + aClass.getSimpleName() + + " in the usual way, rather than using this method.", e); + } + } + } + + static void segmentStateNotNullImpliesCopiesNotNull( + @Nullable ThreadLocalCopies copies, @Nullable SegmentState segmentState) { + assert copies != null || segmentState == null; // segmentState != null -> copies != null + } + + static void expectedValueNotNullImpliesBooleanResult( + @Nullable Object expectedValue, boolean booleanResult) { + assert booleanResult || expectedValue == null; + } + /** * @return the version of Chronicle Map that was used to create the current data file */ @@ -192,7 +258,6 @@ class VanillaChronicleMap, return BuildVersion.version(); } - final long segmentHash(long hash) { return hashSplitting.segmentHash(hash); } @@ -250,37 +315,6 @@ class VanillaChronicleMap, return offset; } - void warnOnWindows() { - if (!Jvm.isWindows()) - return; - long offHeapMapSize = sizeInBytes(); - long oneGb = GIGABYTES.toBytes(1L); - double offHeapMapSizeInGb = offHeapMapSize * 1.0 / oneGb; - if (offHeapMapSize > GIGABYTES.toBytes(4L)) { - System.out.printf( - "WARNING: On Windows, you probably cannot create a ChronicleMap\n" + - "of more than 4 GB. The configured map requires %.2f GB of off-heap memory.\n", - offHeapMapSizeInGb); - } - if (Boolean.getBoolean("win.check")) - try { - long freePhysicalMemory = Jvm.freePhysicalMemoryOnWindowsInBytes(); - if (offHeapMapSize > freePhysicalMemory * 0.9) { - double freePhysicalMemoryInGb = freePhysicalMemory * 1.0 / oneGb; - System.out.printf( - "WARNING: On Windows, you probably cannot create a ChronicleMap\n" + - "of more than 90%% of available free memory in the system.\n" + - "The configured map requires %.2f GB of off-heap memory.\n" + - "There is only %.2f GB of free physical memory in the system.\n", - offHeapMapSizeInGb, freePhysicalMemoryInGb); - - } - } catch (IOException e) { - // ignore -- anyway we just warn the user - } - } - - void warnOnWindows() { if (!Jvm.isWindows()) return; @@ -673,38 +707,6 @@ class VanillaChronicleMap, return newInstance(kClass, true); } - static T newInstance(Class aClass, boolean isKey) { - try { - return isKey ? DataValueClasses.newInstance(aClass) : - DataValueClasses.newDirectInstance(aClass); - - } catch (Exception e) { - if (e.getCause() instanceof IllegalStateException) - throw e; - - if (aClass.isInterface()) - throw new IllegalStateException("It not possible to create a instance from " + - "interface=" + aClass.getSimpleName() + " we recommend you create an " + - "instance in the usual way.", e); - - try { - return aClass.newInstance(); - } catch (Exception e1) { - throw new IllegalStateException("It has not been possible to create a instance " + - "of class=" + aClass.getSimpleName() + - ", Note : its more efficient if your chronicle map is configured with " + - "interface key " + - "and value types rather than classes, as this method is able to use " + - "interfaces to generate off heap proxies that point straight at your data. " + - "In this case you have used a class and chronicle is unable to create an " + - "instance of this class has it does not have a default constructor. " + - "If your class is mutable, we " + - "recommend you create and instance of your class=" + aClass.getSimpleName() + - " in the usual way, rather than using this method.", e); - } - } - } - @Override public Class keyClass() { return kClass; @@ -791,29 +793,11 @@ class VanillaChronicleMap, return this; } - transient InstanceOrBytesToInstance keyBytesToInstance = - new InstanceOrBytesToInstance() { - @Override - public K toInstance(@NotNull ThreadLocalCopies copies, Bytes keyBytes, long size) { - assertNotNull(copies); - BytesReader keyReader = keyReaderProvider.get(copies, originalKeyReader); - return keyReader.read(keyBytes, size); - } - }; - @SuppressWarnings("unchecked") final InstanceOrBytesToInstance valueIdentity() { return this; } - transient InstanceOrBytesToInstance valueBytesToInstance = - new InstanceOrBytesToInstance() { - @Override - public V toInstance(@NotNull ThreadLocalCopies copies, Bytes valueBytes, long size) { - return readValue(copies, valueBytes, null, size); - } - }; - @Override public final boolean containsKey(final Object k) { checkKey(k); @@ -832,15 +816,6 @@ class VanillaChronicleMap, } } - transient InstanceOrBytesToInstance outputValueBytesToInstance = - new InstanceOrBytesToInstance() { - @Override - public V toInstance(@NotNull ThreadLocalCopies copies, Bytes outputBytes, long size) { - outputBytes.position(outputBytes.position() - size); - return readValue(copies, outputBytes, null, size); - } - }; - final boolean containsBytesKey(Bytes key) throws InterruptedException { return containsKey(null, DelegatingMetaBytesInterop.>instance(), @@ -1286,11 +1261,6 @@ class VanillaChronicleMap, } } - static void segmentStateNotNullImpliesCopiesNotNull( - @Nullable ThreadLocalCopies copies, @Nullable SegmentState segmentState) { - assert copies != null || segmentState == null; // segmentState != null -> copies != null - } - private static class ReadValueToLazyBytes extends ReadValueToBytes { DirectBytes lazyBytes; @@ -1315,11 +1285,6 @@ class VanillaChronicleMap, } } - static void expectedValueNotNullImpliesBooleanResult( - @Nullable Object expectedValue, boolean booleanResult) { - assert booleanResult || expectedValue == null; - } - static class GetRemoteBytesValueInterops implements GetValueInterops, MetaBytesInterop { @@ -1384,9 +1349,6 @@ class VanillaChronicleMap, } } - static final boolean checkSegmentMultiMapsAndBitSetsConsistencyOnBootstrap = Boolean.getBoolean( - "chronicleMap.checkSegmentMultiMapsAndBitSetsConsistencyOnBootstrap"); - public static final class SegmentState implements StatefulCopyable, AutoCloseable { private static final Provider segmentStateProvider =