From 340ab0940945ceac7e3b52e1f8fabd1306d26940 Mon Sep 17 00:00:00 2001 From: leventov Date: Fri, 24 Jul 2015 21:36:55 +0300 Subject: [PATCH] Moved DefaultValueProvider to the new Contexts API; removed defaultValue() SPI from MapEntryOperations (essentially replaced by DefaultValueProvider); Removed the old contexts, KeyContext and MapKeyContext completely --- pom.xml | 1 + .../openhft/chronicle/hash/KeyContext.java | 119 -------------- .../chronicle/map/ChronicleMapBuilder.java | 32 ++-- .../chronicle/map/ConstantValueProvider.java | 10 +- .../net/openhft/chronicle/map/DefaultSpi.java | 7 +- .../chronicle/map/DefaultValueProvider.java | 23 +-- .../openhft/chronicle/map/MapAbsentEntry.java | 8 +- .../net/openhft/chronicle/map/MapContext.java | 3 +- .../chronicle/map/MapEntryOperations.java | 11 -- .../openhft/chronicle/map/MapKeyContext.java | 40 ----- .../chronicle/map/VanillaChronicleMap.java | 29 ++-- .../chronicle/map/impl/MapQueryContext.java | 1 - .../map/impl/QueryContextInterface.java | 5 +- .../map/impl/ReplicatedMapQueryContext.java | 6 +- .../map/MapEntryOperationsDelegation.java | 2 +- .../map/impl/stage/query/AcquireHandle.java | 6 - .../query/DeprecatedMapKeyContextOnQuery.java | 148 ------------------ .../map/impl/stage/query/MapAbsent.java | 9 +- .../map/impl/stage/query/MapQuery.java | 1 - .../openhft/chronicle/set/ChronicleSet.java | 1 - .../net/openhft/chronicle/set/SetFromMap.java | 1 - .../SimpleMapOperationsListeningTest.java | 14 ++ .../jsr166/map/StatelessChronicleMapTest.java | 1 - 23 files changed, 89 insertions(+), 389 deletions(-) delete mode 100644 src/main/java/net/openhft/chronicle/hash/KeyContext.java delete mode 100644 src/main/java/net/openhft/chronicle/map/MapKeyContext.java delete mode 100644 src/main/java/net/openhft/chronicle/map/impl/stage/query/DeprecatedMapKeyContextOnQuery.java diff --git a/pom.xml b/pom.xml index 827016c56..2e5cb2c78 100755 --- a/pom.xml +++ b/pom.xml @@ -230,6 +230,7 @@ net/openhft/chronicle/map/MapKeyContext.java net/openhft/chronicle/hash/locks/LockingStrategy.java net/openhft/chronicle/map/TcpReplicator.java + net/openhft/chronicle/map/ConstantValueProvider.java false -Xdoclint:none diff --git a/src/main/java/net/openhft/chronicle/hash/KeyContext.java b/src/main/java/net/openhft/chronicle/hash/KeyContext.java deleted file mode 100644 index 2ebf848a7..000000000 --- a/src/main/java/net/openhft/chronicle/hash/KeyContext.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright 2015 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.hash; - -import net.openhft.chronicle.hash.locks.InterProcessReadWriteUpdateLock; -import net.openhft.lang.io.Bytes; -import org.jetbrains.annotations.NotNull; - -import java.util.ConcurrentModificationException; - -/** - * Common considerations: - * - * - * @param key type - * @deprecated incoherent abstraction. Replaced by {@link HashQueryContext} and {@link HashEntry} - */ -@Deprecated -public interface KeyContext extends AutoCloseable, InterProcessReadWriteUpdateLock { - - /** - * Returns the key object of this context. The instance might be reused, so you shouldn't - * save the returned object and use it after the context is closed or the callback (to which - * this context is provided) return. Might acquire {@link #readLock} before reading the key, - * if the context is not locked yet. - * - * @return the key object of this context - */ - @NotNull - K key(); - - /** - * Returns the entry bytes, if the key is present in the hash, otherwise throws - * {@code IllegalStateException}. Might acquire {@link #readLock} before searching for - * the entry, if the context is not locked yet. - * - * @return the entry bytes - * @throws IllegalStateException if the hash doesn't {@link #containsKey} - */ - @NotNull - Bytes entry(); - - /** - * Returns the offset of the key bytes within the {@link #entry}, if the key is present - * in the hash, otherwise throws {@code IllegalStateException}. Might acquire {@link #readLock} - * before searching for the entry, if the context is not locked yet. - * - * @return the offset of the key bytes within the {@link #entry} - * @throws IllegalStateException if the hash doesn't {@link #containsKey} - */ - long keyOffset(); - - /** - * Returns the size of the key bytes representation. Bytes from {@link #keyOffset} to - * {@code keyOffset + keySize} are the key bytes in the {@link #entry}. - * - * @return the size of the key bytes representation - */ - long keySize(); - - /** - * Checks if the key is present in the hash. Might acquire {@link #readLock} before searching - * for the key, if the context is not locked yet. - * - * @return {@code true} if the key is present in the hash, {@code false} otherwise - * @throws IllegalStateException if at least {@link #readLock} is not held - */ - boolean containsKey(); - - /** - * Removes the key from the hash. Might acquire {@link #updateLock} or {@link #writeLock}, - * if the context is not locked yet. Might upgrade from update to write lock, if update lock - * is already held. - * - * @return {@code true} if the key was actually removed (hash was updated), - * {@code false} if the key is already absent in the hash - * @throws IllegalStateException if the context is locked for read, but not for update, and - * update or write lock is actually required for {@code remove()} in this {@code KeyContext} - * implementation - */ - boolean remove(); - - /** - * Closes the context. Among other things, it releases all locks, acquired within the context, - * that means you shouldn't bother with unlocking and try-finally pattern, if the whole - * context operation if performed within try-with-resources block:
{@code
-     * try (KeyContext c = map.context(key)) {
-     *     c.writeLock().lock();
-     *     if (random.nextBoolean())
-     *         c.remove();
-     *     // Note, there is no c.writeLock().unlock(), and it's OK,
-     *     // because all locks are released in close()
-     * }}
- */ - @Override - void close(); -} diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java b/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java index 998886298..5766911be 100755 --- a/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java +++ b/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java @@ -18,7 +18,6 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.core.OS; import net.openhft.chronicle.hash.ChronicleHashBuilder; import net.openhft.chronicle.hash.ChronicleHashInstanceBuilder; import net.openhft.chronicle.hash.impl.ChronicleHashBuilderImpl; @@ -162,7 +161,8 @@ public final class ChronicleMapBuilder implements private BytesMarshallerFactory bytesMarshallerFactory; private ObjectSerializer objectSerializer; private V defaultValue = null; - private DefaultValueProvider defaultValueProvider = null; + private ConstantValueProvider constantValueProvider = null; + DefaultValueProvider defaultValueProvider = DefaultSpi.defaultValueProvider(); private SingleChronicleHashReplication singleHashReplication = null; private InetSocketAddress[] pushToAddresses; @@ -943,7 +943,6 @@ public final class ChronicleMapBuilder implements ", keyBuilder=" + keyBuilder + ", valueBuilder=" + valueBuilder + ", defaultValue=" + defaultValue + - ", defaultValueProvider=" + pretty(defaultValueProvider) + '}'; } @@ -1144,7 +1143,7 @@ public final class ChronicleMapBuilder implements if (defaultValue == null) throw new IllegalArgumentException("default ChronicleMap value couldn't be null"); this.defaultValue = defaultValue; - this.defaultValueProvider = null; + this.constantValueProvider = null; return this; } @@ -1158,22 +1157,20 @@ public final class ChronicleMapBuilder implements * @param defaultValueProvider the strategy to obtain a default value by the absent key * @return this builder object back * @see #defaultValue(Object) - * @deprecated specialize {@link MapEntryOperations#defaultValue(MapAbsentEntry)} instead */ - @Deprecated public ChronicleMapBuilder defaultValueProvider( @NotNull DefaultValueProvider defaultValueProvider) { + Objects.requireNonNull(defaultValueProvider); this.defaultValueProvider = defaultValueProvider; - this.defaultValue = null; return this; } /** * Non-public because should be called only after {@link #preMapConstruction(boolean)} */ - DefaultValueProvider defaultValueProvider() { - if (defaultValueProvider != null) - return defaultValueProvider; + ConstantValueProvider constantValueProvider() { + if (constantValueProvider != null) + return constantValueProvider; V defaultValue = this.defaultValue; if (defaultValue == null) defaultValue = zeroValue(); @@ -1204,6 +1201,21 @@ public final class ChronicleMapBuilder implements try { return DataValueClasses.newDirectInstance(valueBuilder.eClass); } catch (Exception e) { + if (valueBuilder.eClass == Long.class) { + return (V) (Long) 0L; + } else if (valueBuilder.eClass == Integer.class) { + return (V) (Integer) 0; + } else if (valueBuilder.eClass == Short.class) { + return (V) (Short) (short) 0; + } else if (valueBuilder.eClass == Character.class) { + return (V) (Character) (char) 0; + } else if (valueBuilder.eClass == Byte.class) { + return (V) (Byte) (byte) 0; + } else if (valueBuilder.eClass == Float.class) { + return (V) (Float) 0.0f; + } else if (valueBuilder.eClass == Double.class) { + return (V) (Double) 0.0; + } return null; } } diff --git a/src/main/java/net/openhft/chronicle/map/ConstantValueProvider.java b/src/main/java/net/openhft/chronicle/map/ConstantValueProvider.java index 478bc085b..1469d663c 100644 --- a/src/main/java/net/openhft/chronicle/map/ConstantValueProvider.java +++ b/src/main/java/net/openhft/chronicle/map/ConstantValueProvider.java @@ -18,14 +18,17 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.KeyContext; +import net.openhft.chronicle.hash.Data; import net.openhft.chronicle.hash.serialization.BytesReader; +import net.openhft.chronicle.hash.serialization.internal.MetaBytesInterop; import net.openhft.chronicle.hash.serialization.internal.MetaBytesWriter; +import net.openhft.chronicle.map.impl.stage.data.instance.WrappedValueInstanceData; import net.openhft.lang.io.ByteBufferBytes; +import java.io.Serializable; import java.nio.ByteBuffer; -final class ConstantValueProvider implements DefaultValueProvider { +public final class ConstantValueProvider implements Serializable { private static final long serialVersionUID = 0L; private transient V value; @@ -48,8 +51,7 @@ final class ConstantValueProvider implements DefaultValueProvider { value = reader.read(new ByteBufferBytes(ByteBuffer.wrap(serializedValueBytes)), size); } - @Override - public V get(KeyContext keyContext) { + public V defaultValue() { return value; } } diff --git a/src/main/java/net/openhft/chronicle/map/DefaultSpi.java b/src/main/java/net/openhft/chronicle/map/DefaultSpi.java index da4710857..962afe908 100644 --- a/src/main/java/net/openhft/chronicle/map/DefaultSpi.java +++ b/src/main/java/net/openhft/chronicle/map/DefaultSpi.java @@ -2,7 +2,8 @@ package net.openhft.chronicle.map; import net.openhft.chronicle.map.replication.MapRemoteOperations; -final class DefaultSpi implements MapMethods, MapEntryOperations, MapRemoteOperations { +final class DefaultSpi implements MapMethods, MapEntryOperations, MapRemoteOperations, + DefaultValueProvider { static final DefaultSpi DEFAULT_SPI = new DefaultSpi(); static MapMethods mapMethods() { return DEFAULT_SPI; @@ -15,4 +16,8 @@ final class DefaultSpi implements MapMethods, MapEntryOperations, MapRemoteOpera static MapRemoteOperations mapRemoteOperations() { return DEFAULT_SPI; } + + static DefaultValueProvider defaultValueProvider() { + return DEFAULT_SPI; + } } diff --git a/src/main/java/net/openhft/chronicle/map/DefaultValueProvider.java b/src/main/java/net/openhft/chronicle/map/DefaultValueProvider.java index 26458e069..f74ee203a 100644 --- a/src/main/java/net/openhft/chronicle/map/DefaultValueProvider.java +++ b/src/main/java/net/openhft/chronicle/map/DefaultValueProvider.java @@ -18,9 +18,8 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.KeyContext; - -import java.io.Serializable; +import net.openhft.chronicle.hash.Data; +import org.jetbrains.annotations.NotNull; /** * Default value computation strategy, used @@ -28,16 +27,18 @@ import java.io.Serializable; * * @param map key class * @param map value class - * @deprecated specialize {@link MapEntryOperations#defaultValue(MapAbsentEntry)} instead + * @see ChronicleMapBuilder#defaultValue(Object) */ -@Deprecated -public interface DefaultValueProvider extends Serializable { +public interface DefaultValueProvider { + /** - * Returns a value to be put during {@link ChronicleMap#acquireUsing(Object, Object)} call - * for the specified key, if it is absent in the map. + * Returns the "nil" value, which should be inserted into the map, in the given + * {@code absentEntry} context. This is primarily used in {@link ChronicleMap#acquireUsing} + * operation implementation, i. e. {@link MapMethods#acquireUsing}. * - * @param keyContext key absent in the map - * @return value to be put for the specified key in the map + * @implNote simply delegates to {@link MapAbsentEntry#defaultValue()}. */ - V get(KeyContext keyContext); + default Data defaultValue(@NotNull MapAbsentEntry absentEntry) { + return absentEntry.defaultValue(); + } } diff --git a/src/main/java/net/openhft/chronicle/map/MapAbsentEntry.java b/src/main/java/net/openhft/chronicle/map/MapAbsentEntry.java index 4274878db..1be104944 100644 --- a/src/main/java/net/openhft/chronicle/map/MapAbsentEntry.java +++ b/src/main/java/net/openhft/chronicle/map/MapAbsentEntry.java @@ -57,10 +57,10 @@ public interface MapAbsentEntry extends HashAbsentEntry { * implementation, i. e. {@link MapMethods#acquireUsing}. * *

This method if the default implementation for {@link - * MapEntryOperations#defaultValue}, which might be customized over the default. - * - * @return the default value to be inserted into the map - * @see MapEntryOperations#defaultValue(MapAbsentEntry) + * DefaultValueProvider#defaultValue(MapAbsentEntry)}, + * which might be customized over the default. + * + * @see DefaultValueProvider#defaultValue(MapAbsentEntry) */ @NotNull Data defaultValue(); diff --git a/src/main/java/net/openhft/chronicle/map/MapContext.java b/src/main/java/net/openhft/chronicle/map/MapContext.java index e31ae7ccb..87a8d93c2 100644 --- a/src/main/java/net/openhft/chronicle/map/MapContext.java +++ b/src/main/java/net/openhft/chronicle/map/MapContext.java @@ -31,7 +31,8 @@ import net.openhft.chronicle.hash.Data; * @param the map value type * @param the return type of {@link MapEntryOperations} specified for the queried map */ -public interface MapContext extends HashContext, MapEntryOperations { +public interface MapContext + extends HashContext, MapEntryOperations, DefaultValueProvider { /** * Returns the accessed {@code ChronicleMap}. Synonym to {@link #map()}. */ diff --git a/src/main/java/net/openhft/chronicle/map/MapEntryOperations.java b/src/main/java/net/openhft/chronicle/map/MapEntryOperations.java index ef7b3e3ee..7350bf178 100644 --- a/src/main/java/net/openhft/chronicle/map/MapEntryOperations.java +++ b/src/main/java/net/openhft/chronicle/map/MapEntryOperations.java @@ -150,15 +150,4 @@ public interface MapEntryOperations { absentEntry.doInsert(value); return null; } - - /** - * Returns the "nil" value, which should be inserted into the map, in the given - * {@code absentEntry} context. This is primarily used in {@link ChronicleMap#acquireUsing} - * operation implementation, i. e. {@link MapMethods#acquireUsing}. - * - * @implNote simply delegates to {@link MapAbsentEntry#defaultValue()}. - */ - default Data defaultValue(@NotNull MapAbsentEntry absentEntry) { - return absentEntry.defaultValue(); - } } diff --git a/src/main/java/net/openhft/chronicle/map/MapKeyContext.java b/src/main/java/net/openhft/chronicle/map/MapKeyContext.java deleted file mode 100644 index f7d124ec8..000000000 --- a/src/main/java/net/openhft/chronicle/map/MapKeyContext.java +++ /dev/null @@ -1,40 +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; - -import net.openhft.chronicle.hash.KeyContext; - -/** - * @param the map key type - * @param the map value type - * @deprecated incoherent abstraction. Replaced by {@link MapQueryContext} and {@link MapEntry} - */ -@Deprecated -public interface MapKeyContext extends KeyContext { - - long valueOffset(); - - long valueSize(); - - boolean valueEqualTo(V value); - - V get(); - - V getUsing(V usingValue); - - boolean put(V newValue); -} diff --git a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java index 535c86406..e7638e92a 100755 --- a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java @@ -20,7 +20,6 @@ package net.openhft.chronicle.map; import net.openhft.chronicle.core.io.Closeable; import net.openhft.chronicle.hash.Data; -import net.openhft.chronicle.hash.KeyContext; import net.openhft.chronicle.hash.impl.VanillaChronicleHash; import net.openhft.chronicle.hash.serialization.BytesReader; import net.openhft.chronicle.hash.serialization.SizeMarshaller; @@ -67,7 +66,7 @@ public class VanillaChronicleMap metaValueInteropProvider; - final DefaultValueProvider defaultValueProvider; + public final ConstantValueProvider constantValueProvider; public final boolean constantlySizedEntry; @@ -91,9 +90,9 @@ public class VanillaChronicleMap> entrySet; - // TODO initialize public transient MapEntryOperations entryOperations; public transient MapMethods methods; + public transient DefaultValueProvider defaultValueProvider; transient ThreadLocal queryCxt; transient ThreadLocal iterCxt; @@ -123,7 +122,7 @@ public class VanillaChronicleMap builder) { this.entryOperations = (MapEntryOperations) builder.entryOperations; this.methods = (MapMethods) builder.methods; + this.defaultValueProvider = builder.defaultValueProvider; } @Override @@ -160,15 +160,12 @@ public class VanillaChronicleMap constantValueProvider = - (ConstantValueProvider) defaultValueProvider; - if (constantValueProvider.wasDeserialized()) { - ThreadLocalCopies copies = valueReaderProvider.getCopies(null); - BytesReader valueReader = valueReaderProvider.get(copies, originalValueReader); - constantValueProvider.initTransients(valueReader); - } + if (constantValueProvider != null && constantValueProvider.wasDeserialized()) { + ThreadLocalCopies copies = valueReaderProvider.getCopies(null); + BytesReader valueReader = valueReaderProvider.get(copies, originalValueReader); + constantValueProvider.initTransients(valueReader); } + initQueryContext(); initIterationContext(); } @@ -209,14 +206,6 @@ public class VanillaChronicleMap extends ExternalMapQueryContext { @@ -21,8 +20,6 @@ public interface QueryContextInterface extends ExternalMapQueryContext< ValueInitializableData inputValueInstanceValue(); - MapKeyContext deprecatedMapKeyContext(); - Closeable acquireHandle(); void initTheSegmentIndex(int segmentIndex); diff --git a/src/main/java/net/openhft/chronicle/map/impl/ReplicatedMapQueryContext.java b/src/main/java/net/openhft/chronicle/map/impl/ReplicatedMapQueryContext.java index 4a2c3561a..f3a66b6c0 100644 --- a/src/main/java/net/openhft/chronicle/map/impl/ReplicatedMapQueryContext.java +++ b/src/main/java/net/openhft/chronicle/map/impl/ReplicatedMapQueryContext.java @@ -41,7 +41,10 @@ import net.openhft.chronicle.map.impl.stage.data.instance.WrappedValueInstanceDa import net.openhft.chronicle.map.impl.stage.entry.ReplicatedMapEntryStages; import net.openhft.chronicle.map.impl.stage.input.MapInputBytesValues; import net.openhft.chronicle.map.impl.stage.input.ReplicatedInput; -import net.openhft.chronicle.map.impl.stage.map.*; +import net.openhft.chronicle.map.impl.stage.map.MapEntryOperationsDelegation; +import net.openhft.chronicle.map.impl.stage.map.ReplicatedChronicleMapHolderImpl; +import net.openhft.chronicle.map.impl.stage.map.ValueBytesInterop; +import net.openhft.chronicle.map.impl.stage.map.WrappedValueInstanceValueHolder; import net.openhft.chronicle.map.impl.stage.query.*; import net.openhft.chronicle.map.impl.stage.replication.ReplicationUpdate; import net.openhft.chronicle.map.impl.stage.ret.BytesReturnValue; @@ -100,7 +103,6 @@ nested = { InputSecondValueBytesData.class, WrappedValueInstanceData.class, - DeprecatedMapKeyContextOnQuery.class, AcquireHandle.class, DefaultReturnValue.class, UsingReturnValue.class, diff --git a/src/main/java/net/openhft/chronicle/map/impl/stage/map/MapEntryOperationsDelegation.java b/src/main/java/net/openhft/chronicle/map/impl/stage/map/MapEntryOperationsDelegation.java index aa046186a..4c655a05f 100644 --- a/src/main/java/net/openhft/chronicle/map/impl/stage/map/MapEntryOperationsDelegation.java +++ b/src/main/java/net/openhft/chronicle/map/impl/stage/map/MapEntryOperationsDelegation.java @@ -32,7 +32,7 @@ public abstract class MapEntryOperationsDelegation implements MapContex @Override public Data defaultValue(@NotNull MapAbsentEntry absentEntry) { checkOnEachPublicOperation.checkOnEachPublicOperation(); - return mh.m().entryOperations.defaultValue(absentEntry); + return mh.m().defaultValueProvider.defaultValue(absentEntry); } @Override diff --git a/src/main/java/net/openhft/chronicle/map/impl/stage/query/AcquireHandle.java b/src/main/java/net/openhft/chronicle/map/impl/stage/query/AcquireHandle.java index ca468a5fa..75c92def4 100644 --- a/src/main/java/net/openhft/chronicle/map/impl/stage/query/AcquireHandle.java +++ b/src/main/java/net/openhft/chronicle/map/impl/stage/query/AcquireHandle.java @@ -19,17 +19,11 @@ package net.openhft.chronicle.map.impl.stage.query; import net.openhft.chronicle.core.io.Closeable; -import net.openhft.chronicle.hash.Data; import net.openhft.chronicle.hash.impl.stage.hash.CheckOnEachPublicOperation; -import net.openhft.chronicle.hash.locks.InterProcessLock; -import net.openhft.chronicle.map.MapEntry; -import net.openhft.chronicle.map.MapKeyContext; import net.openhft.chronicle.map.impl.stage.entry.MapEntryStages; import net.openhft.chronicle.map.impl.stage.ret.UsingReturnValue; -import net.openhft.lang.io.Bytes; import net.openhft.sg.StageRef; import net.openhft.sg.Staged; -import org.jetbrains.annotations.NotNull; @Staged public class AcquireHandle implements Closeable { diff --git a/src/main/java/net/openhft/chronicle/map/impl/stage/query/DeprecatedMapKeyContextOnQuery.java b/src/main/java/net/openhft/chronicle/map/impl/stage/query/DeprecatedMapKeyContextOnQuery.java deleted file mode 100644 index 3d282e6d7..000000000 --- a/src/main/java/net/openhft/chronicle/map/impl/stage/query/DeprecatedMapKeyContextOnQuery.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Copyright 2015 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.impl.stage.query; - -import net.openhft.chronicle.hash.Data; -import net.openhft.chronicle.hash.impl.stage.hash.CheckOnEachPublicOperation; -import net.openhft.chronicle.hash.locks.InterProcessLock; -import net.openhft.chronicle.map.MapEntry; -import net.openhft.chronicle.map.MapKeyContext; -import net.openhft.chronicle.map.impl.stage.entry.MapEntryStages; -import net.openhft.lang.io.Bytes; -import net.openhft.sg.StageRef; -import net.openhft.sg.Staged; -import org.jetbrains.annotations.NotNull; - -@Staged -public class DeprecatedMapKeyContextOnQuery implements MapKeyContext { - - @StageRef CheckOnEachPublicOperation checkOnEachPublicOperation; - @StageRef MapEntryStages e; - @StageRef MapQuery q; - - @Override - public long valueOffset() { - checkOnEachPublicOperation.checkOnEachPublicOperation(); - return e.valueOffset; - } - - @Override - public long valueSize() { - checkOnEachPublicOperation.checkOnEachPublicOperation(); - return e.valueSize; - } - - @Override - public boolean valueEqualTo(V value) { - checkOnEachPublicOperation.checkOnEachPublicOperation(); - return Data.bytesEquivalent(e.entryValue, q.wrapValueAsData(value)); - } - - @Override - public V get() { - return containsKey() ? q.value().get() : null; - } - - @Override - public V getUsing(V usingValue) { - return containsKey() ? q.value().getUsing(usingValue) : null; - } - - @Override - public boolean put(V newValue) { - checkOnEachPublicOperation.checkOnEachPublicOperation(); - updateLock().lock(); - MapEntry entry = q.entry(); - if (entry != null) { - q.replaceValue(entry, q.wrapValueAsData(newValue)); - } else { - q.insert(q.absentEntry(), q.wrapValueAsData(newValue)); - } - return true; - } - - @NotNull - @Override - public K key() { - checkOnEachPublicOperation.checkOnEachPublicOperation(); - return q.queriedKey().get(); - } - - @NotNull - @Override - public Bytes entry() { - checkOnEachPublicOperation.checkOnEachPublicOperation(); - return e.entryBytes; - } - - @Override - public long keyOffset() { - checkOnEachPublicOperation.checkOnEachPublicOperation(); - return e.keyOffset; - } - - @Override - public long keySize() { - checkOnEachPublicOperation.checkOnEachPublicOperation(); - return e.keySize; - } - - @Override - public boolean containsKey() { - checkOnEachPublicOperation.checkOnEachPublicOperation(); - return q.entry() != null; - } - - @Override - public boolean remove() { - checkOnEachPublicOperation.checkOnEachPublicOperation(); - updateLock().lock(); - MapEntry entry = q.entry(); - if (entry != null) { - q.remove(entry); - return true; - } else { - return false; - } - } - - @Override - public void close() { - checkOnEachPublicOperation.checkOnEachPublicOperation(); - q.close(); - } - - @NotNull - @Override - public InterProcessLock readLock() { - return q.readLock(); - } - - @NotNull - @Override - public InterProcessLock updateLock() { - return q.updateLock(); - } - - @NotNull - @Override - public InterProcessLock writeLock() { - return q.writeLock(); - } -} diff --git a/src/main/java/net/openhft/chronicle/map/impl/stage/query/MapAbsent.java b/src/main/java/net/openhft/chronicle/map/impl/stage/query/MapAbsent.java index 5f305d481..50e9127a9 100644 --- a/src/main/java/net/openhft/chronicle/map/impl/stage/query/MapAbsent.java +++ b/src/main/java/net/openhft/chronicle/map/impl/stage/query/MapAbsent.java @@ -23,7 +23,6 @@ public class MapAbsent implements MapAbsentEntry { @StageRef public CheckOnEachPublicOperation checkOnEachPublicOperation; @StageRef public SegmentStages s; @StageRef VanillaChronicleMapHolder mh; - @StageRef public DeprecatedMapKeyContextOnQuery deprecatedMapKeyContext; void putEntry(Data value) { assert q.searchStateAbsent(); @@ -69,6 +68,12 @@ public class MapAbsent implements MapAbsentEntry { @Override public Data defaultValue() { checkOnEachPublicOperation.checkOnEachPublicOperation(); - return q.wrapValueAsData(mh.m().defaultValue(deprecatedMapKeyContext)); + if (mh.m().constantValueProvider == null) { + throw new IllegalStateException("to call acquireUsing(), " + + "or defaultValue() on AbsentEntry, you should configure " + + "ChronicleMapBuilder.defaultValue(), or use one of the 'known' value types: " + + "boxed primitives, or so-called data-value-generated interface as a value"); + } + return context().wrapValueAsData(mh.m().constantValueProvider.defaultValue()); } } diff --git a/src/main/java/net/openhft/chronicle/map/impl/stage/query/MapQuery.java b/src/main/java/net/openhft/chronicle/map/impl/stage/query/MapQuery.java index 06a362796..e0e2d8603 100644 --- a/src/main/java/net/openhft/chronicle/map/impl/stage/query/MapQuery.java +++ b/src/main/java/net/openhft/chronicle/map/impl/stage/query/MapQuery.java @@ -49,7 +49,6 @@ public abstract class MapQuery extends HashQuery @StageRef VanillaChronicleMapHolder mh; @StageRef MapEntryStages e; @StageRef SearchAllocatedChunks allocatedChunks; - @StageRef public DeprecatedMapKeyContextOnQuery deprecatedMapKeyContext; @StageRef public AcquireHandle acquireHandle; @StageRef public InputValueInstanceData inputValueInstanceValue; diff --git a/src/main/java/net/openhft/chronicle/set/ChronicleSet.java b/src/main/java/net/openhft/chronicle/set/ChronicleSet.java index 370a204fb..3a2db27fd 100644 --- a/src/main/java/net/openhft/chronicle/set/ChronicleSet.java +++ b/src/main/java/net/openhft/chronicle/set/ChronicleSet.java @@ -19,7 +19,6 @@ package net.openhft.chronicle.set; import net.openhft.chronicle.hash.ChronicleHash; -import net.openhft.chronicle.hash.KeyContext; import java.util.Set; diff --git a/src/main/java/net/openhft/chronicle/set/SetFromMap.java b/src/main/java/net/openhft/chronicle/set/SetFromMap.java index e388f1e38..84a9b9e1c 100644 --- a/src/main/java/net/openhft/chronicle/set/SetFromMap.java +++ b/src/main/java/net/openhft/chronicle/set/SetFromMap.java @@ -19,7 +19,6 @@ package net.openhft.chronicle.set; import net.openhft.chronicle.hash.Data; -import net.openhft.chronicle.hash.KeyContext; import net.openhft.chronicle.hash.serialization.internal.DummyValue; import net.openhft.chronicle.map.ChronicleMap; import org.jetbrains.annotations.NotNull; diff --git a/src/test/java/net/openhft/chronicle/map/example/SimpleMapOperationsListeningTest.java b/src/test/java/net/openhft/chronicle/map/example/SimpleMapOperationsListeningTest.java index 8bfd8303a..c1a9e76e1 100644 --- a/src/test/java/net/openhft/chronicle/map/example/SimpleMapOperationsListeningTest.java +++ b/src/test/java/net/openhft/chronicle/map/example/SimpleMapOperationsListeningTest.java @@ -8,6 +8,7 @@ import org.jetbrains.annotations.NotNull; import org.junit.Ignore; import org.junit.Test; +import static net.openhft.chronicle.map.example.SimpleMapOperationsListeningTest.SimpleLoggingDefaultValueProvider.simpleLoggingDefaultValueProvider; import static net.openhft.chronicle.map.example.SimpleMapOperationsListeningTest.SimpleLoggingMapEntryOperations.simpleLoggingMapEntryOperations; public class SimpleMapOperationsListeningTest { @@ -43,6 +44,18 @@ public class SimpleMapOperationsListeningTest { absentEntry.doInsert(value); return null; } + } + + static class SimpleLoggingDefaultValueProvider implements DefaultValueProvider { + + private static final SimpleLoggingDefaultValueProvider INSTANCE = + new SimpleLoggingDefaultValueProvider(); + + public static DefaultValueProvider simpleLoggingDefaultValueProvider() { + return INSTANCE; + } + + private SimpleLoggingDefaultValueProvider() {} @Override public Data defaultValue(@NotNull MapAbsentEntry absentEntry) { @@ -59,6 +72,7 @@ public class SimpleMapOperationsListeningTest { .of(Integer.class, IntValue.class) .entries(100) .entryOperations(simpleLoggingMapEntryOperations()) + .defaultValueProvider(simpleLoggingDefaultValueProvider()) .create(); IntValue value = DataValueClasses.newDirectInstance(IntValue.class); diff --git a/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java index 5ebc0db3a..c4f2ea8f1 100755 --- a/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java @@ -22,7 +22,6 @@ import net.openhft.chronicle.hash.Data; import net.openhft.chronicle.hash.function.SerializableFunction; import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig; import net.openhft.chronicle.map.*; -import net.openhft.chronicle.map.MapKeyContext; import net.openhft.chronicle.map.jsr166.JSR166TestCase; import org.jetbrains.annotations.NotNull; import org.junit.After;