mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
Made keys immutability configurable
This commit is contained in:
@@ -622,6 +622,22 @@ public class ChronicleMapBuilder<K, V> 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<K, V> immutableKeys() {
|
||||
keyBuilder.instancesAreMutable(false);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ChronicleMapBuilder<K, V> valueMarshallerAndFactory(
|
||||
@NotNull BytesMarshaller<V> valueMarshaller, @NotNull ObjectFactory<V> valueFactory) {
|
||||
valueBuilder.marshaller(valueMarshaller, valueFactory);
|
||||
|
||||
@@ -45,11 +45,16 @@ final class SerializationBuilder<E> 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<E> eClass;
|
||||
private boolean instancesAreMutable;
|
||||
private SizeMarshaller sizeMarshaller = stopBit();
|
||||
private BytesReader<E> reader;
|
||||
private Object interop;
|
||||
@@ -62,6 +67,7 @@ final class SerializationBuilder<E> implements Cloneable {
|
||||
SerializationBuilder(Class<E> eClass, Role role) {
|
||||
this.bufferIdentity = role;
|
||||
this.eClass = eClass;
|
||||
instancesAreMutable = instancesAreMutable(eClass);
|
||||
|
||||
ObjectFactory<E> factory = null;
|
||||
if (role == Role.VALUE) {
|
||||
@@ -144,13 +150,13 @@ final class SerializationBuilder<E> implements Cloneable {
|
||||
}
|
||||
|
||||
public SerializationBuilder<E> maxSize(long maxSize) {
|
||||
boolean mutable = eClass != String.class; // for example
|
||||
if (copyingInterop == CopyingInterop.FROM_MARSHALLER) {
|
||||
metaInteropProvider(CopyingMetaBytesInterop
|
||||
.<E, BytesMarshaller<E>>providerForBytesMarshaller(mutable, maxSize));
|
||||
.<E, BytesMarshaller<E>>providerForBytesMarshaller(
|
||||
instancesAreMutable, maxSize));
|
||||
} else if (copyingInterop == CopyingInterop.FROM_WRITER) {
|
||||
metaInteropProvider(CopyingMetaBytesInterop
|
||||
.<E, BytesWriter<E>>providerForBytesWriter(mutable, maxSize));
|
||||
.<E, BytesWriter<E>>providerForBytesWriter(instancesAreMutable, maxSize));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
@@ -165,6 +171,11 @@ final class SerializationBuilder<E> implements Cloneable {
|
||||
return this;
|
||||
}
|
||||
|
||||
public SerializationBuilder<E> instancesAreMutable(boolean mutable) {
|
||||
this.instancesAreMutable = mutable;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SizeMarshaller sizeMarshaller() {
|
||||
return sizeMarshaller;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user