From fdd5988d6ffcdbc5e415fae4634ee612b41f969d Mon Sep 17 00:00:00 2001 From: leventov Date: Tue, 7 Oct 2014 01:25:09 +0400 Subject: [PATCH] Made keys immutability configurable --- .../chronicle/map/ChronicleMapBuilder.java | 16 ++++++++++++++++ .../chronicle/map/SerializationBuilder.java | 17 ++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java b/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java index aa33102f8..b0d7da343 100644 --- a/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java +++ b/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java @@ -622,6 +622,22 @@ public class ChronicleMapBuilder implements Cloneable { return this; } + /** + * Specifies that key objects, queried with the maps created by this builder, are inherently + * immutable. Keys in {@code ChronicleMap} are not required to be immutable, as in ordinary + * {@link Map} implementations, because they are serialized off-heap. However, + * {@code ChronicleMap} implementations can benefit from the knowledge that keys are not mutated + * between queries. By default, {@code ChronicleMapBuilder} detects immutability automatically + * only for very few standard JDK types (for example, for {@link String}), it is not recommended + * to rely on {@code ChronicleMapBuilder} to be smart enough about this. + * + * @return this builder back + */ + public ChronicleMapBuilder immutableKeys() { + keyBuilder.instancesAreMutable(false); + return this; + } + public ChronicleMapBuilder valueMarshallerAndFactory( @NotNull BytesMarshaller valueMarshaller, @NotNull ObjectFactory valueFactory) { valueBuilder.marshaller(valueMarshaller, valueFactory); diff --git a/src/main/java/net/openhft/chronicle/map/SerializationBuilder.java b/src/main/java/net/openhft/chronicle/map/SerializationBuilder.java index afdefe5f6..5e57e8f12 100644 --- a/src/main/java/net/openhft/chronicle/map/SerializationBuilder.java +++ b/src/main/java/net/openhft/chronicle/map/SerializationBuilder.java @@ -45,11 +45,16 @@ final class SerializationBuilder implements Cloneable { Externalizable.class.isAssignableFrom(c); } + private static boolean instancesAreMutable(Class c) { + return c != String.class; + } + enum Role {KEY, VALUE} private enum CopyingInterop {FROM_MARSHALLER, FROM_WRITER} private final Serializable bufferIdentity; final Class eClass; + private boolean instancesAreMutable; private SizeMarshaller sizeMarshaller = stopBit(); private BytesReader reader; private Object interop; @@ -62,6 +67,7 @@ final class SerializationBuilder implements Cloneable { SerializationBuilder(Class eClass, Role role) { this.bufferIdentity = role; this.eClass = eClass; + instancesAreMutable = instancesAreMutable(eClass); ObjectFactory factory = null; if (role == Role.VALUE) { @@ -144,13 +150,13 @@ final class SerializationBuilder implements Cloneable { } public SerializationBuilder maxSize(long maxSize) { - boolean mutable = eClass != String.class; // for example if (copyingInterop == CopyingInterop.FROM_MARSHALLER) { metaInteropProvider(CopyingMetaBytesInterop - .>providerForBytesMarshaller(mutable, maxSize)); + .>providerForBytesMarshaller( + instancesAreMutable, maxSize)); } else if (copyingInterop == CopyingInterop.FROM_WRITER) { metaInteropProvider(CopyingMetaBytesInterop - .>providerForBytesWriter(mutable, maxSize)); + .>providerForBytesWriter(instancesAreMutable, maxSize)); } return this; } @@ -165,6 +171,11 @@ final class SerializationBuilder implements Cloneable { return this; } + public SerializationBuilder instancesAreMutable(boolean mutable) { + this.instancesAreMutable = mutable; + return this; + } + public SizeMarshaller sizeMarshaller() { return sizeMarshaller; }