diff --git a/src/main/java/net/openhft/chronicle/hash/HashContext.java b/src/main/java/net/openhft/chronicle/hash/HashContext.java new file mode 100644 index 000000000..ed90de0ad --- /dev/null +++ b/src/main/java/net/openhft/chronicle/hash/HashContext.java @@ -0,0 +1,31 @@ +/* + * 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; + +/** + * Root interface for contexts, in which {@link HashEntry HashEntries} could be accessed. + * + * @param the key type of accessed {@link ChronicleHash} + */ +public interface HashContext { + /** + * Returns the accessed {@code ChronicleHash}. + */ + ChronicleHash hash(); +} diff --git a/src/main/java/net/openhft/chronicle/hash/HashEntry.java b/src/main/java/net/openhft/chronicle/hash/HashEntry.java index bb62ee277..0b0fbdda3 100644 --- a/src/main/java/net/openhft/chronicle/hash/HashEntry.java +++ b/src/main/java/net/openhft/chronicle/hash/HashEntry.java @@ -29,6 +29,11 @@ import org.jetbrains.annotations.NotNull; * @see MapEntryOperations */ public interface HashEntry { + /** + * Returns the context, in which the entry is accessed. + */ + HashContext context(); + /** * Returns the entry key. */ diff --git a/src/main/java/net/openhft/chronicle/hash/ReplicationContext.java b/src/main/java/net/openhft/chronicle/hash/ReplicationContext.java new file mode 100644 index 000000000..dcb2adb45 --- /dev/null +++ b/src/main/java/net/openhft/chronicle/hash/ReplicationContext.java @@ -0,0 +1,27 @@ +/* + * 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; + +/** + * Context of internal replication operation. + * + * @param the key type of accessed {@link ChronicleHash} + */ +public interface ReplicationContext extends HashContext { +} diff --git a/src/main/java/net/openhft/chronicle/hash/Value.java b/src/main/java/net/openhft/chronicle/hash/Value.java index 2653bb6b3..17e341065 100644 --- a/src/main/java/net/openhft/chronicle/hash/Value.java +++ b/src/main/java/net/openhft/chronicle/hash/Value.java @@ -25,7 +25,7 @@ import org.jetbrains.annotations.Nullable; /** * Dual bytes/object access to keys/values/elements. * - *

Bytes access: {@link #access()} + {@link #handle()} + {@link #size()}. + *

Bytes access: {@link #access()} + {@link #handle()} + {@link #offset()} + {@link #size()}. * *

Object access: {@link #get()}. * diff --git a/src/main/java/net/openhft/chronicle/map/MapAbsentEntry.java b/src/main/java/net/openhft/chronicle/map/MapAbsentEntry.java index fc9bcbe5a..62e9049da 100644 --- a/src/main/java/net/openhft/chronicle/map/MapAbsentEntry.java +++ b/src/main/java/net/openhft/chronicle/map/MapAbsentEntry.java @@ -19,12 +19,50 @@ package net.openhft.chronicle.map; import net.openhft.chronicle.hash.Value; +import org.jetbrains.annotations.NotNull; +/** + * Low-level operational context for the situations, when the new entry is going to be inserted + * into the {@link ChronicleMap}. + * + * @param type of the key in {@code ChronicleMap} + * @param type of the value in {@code ChronicleMap} + * + * @see MapAbsentEntryOperations + * @see MapQueryContext#absentEntry() + */ public interface MapAbsentEntry { - - Value absentKey(); - + + /** + * Returns the context, in which the entry is going to be inserted into the map. + */ + @NotNull MapContext context(); + + /** + * Returns the key is going to be inserted into the {@code ChronicleMap}. + */ + @NotNull Value absentKey(); + + /** + * Inserts the new entry into the map, of {@link #absentKey() the key} and the given value. + * Returns {@code true} if the insertion was successful, {@code false} if it failed for + * any reason. + * + *

This method if the default implementation for {@link MapAbsentEntryOperations#insert}, + * which might be customized over the default. + * + * @throws IllegalStateException if some locking/state conditions required to perform insertion + * operation are not met + */ boolean defaultInsert(Value value); - - Value defaultValue(); + + /** + * Returns the "nil" value, which should be inserted into the map in this context. This + * is primarily used in {@link ChronicleMap#acquireUsing} operation implementation, i. e. {@link + * MapMethods#acquireUsing}. + * + *

This method if the default implementation for {@link + * MapAbsentEntryOperations#defaultValue}, which might be customized over the default. + */ + @NotNull Value defaultValue(); } diff --git a/src/main/java/net/openhft/chronicle/map/MapAbsentEntryOperations.java b/src/main/java/net/openhft/chronicle/map/MapAbsentEntryOperations.java index 824262b4e..dfbf63b12 100644 --- a/src/main/java/net/openhft/chronicle/map/MapAbsentEntryOperations.java +++ b/src/main/java/net/openhft/chronicle/map/MapAbsentEntryOperations.java @@ -21,12 +21,36 @@ package net.openhft.chronicle.map; import net.openhft.chronicle.hash.Value; import org.jetbrains.annotations.NotNull; +/** + * SPI interface for customizing "low-level" insertion of a new entry into {@link ChronicleMap}. + * + * @param type of the key in {@code ChronicleMap} + * @param type of the value in {@code ChronicleMap} + */ public interface MapAbsentEntryOperations { - + + /** + * Inserts the new entry into the map, of {@link MapAbsentEntry#absentKey() the key} from + * the given insertion context (absentEntry) and the given {@code value}. + * Returns {@code true} if the insertion was successful, {@code false} if it failed + * for any reason. + * + * @implNote simply delegates to {@link MapAbsentEntry#defaultInsert(Value)}. + * + * @throws IllegalStateException if some locking/state conditions required to perform insertion + * operation are not met + */ default boolean insert(@NotNull MapAbsentEntry absentEntry, Value value) { return absentEntry.defaultInsert(value); } + /** + * 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 Value defaultValue(@NotNull MapAbsentEntry absentEntry) { return absentEntry.defaultValue(); } diff --git a/src/main/java/net/openhft/chronicle/map/MapContext.java b/src/main/java/net/openhft/chronicle/map/MapContext.java new file mode 100644 index 000000000..6346ab0e5 --- /dev/null +++ b/src/main/java/net/openhft/chronicle/map/MapContext.java @@ -0,0 +1,40 @@ +/* + * 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; + +import net.openhft.chronicle.hash.HashContext; + +/** + * Context, in which {@link MapEntry MapEntries} are accessed. + * + * @param the key type of accessed {@link ChronicleMap} + * @param the value type of accessed {@code ChronicleMap} + */ +public interface MapContext extends HashContext { + /** + * Returns the accessed {@code ChronicleMap}. + */ + @Override + ChronicleMap hash(); + + /** + * Synonym to {@link #hash()}. + */ + ChronicleMap map(); +} diff --git a/src/main/java/net/openhft/chronicle/map/MapEntry.java b/src/main/java/net/openhft/chronicle/map/MapEntry.java index 7a064b273..4dfa6ddd3 100644 --- a/src/main/java/net/openhft/chronicle/map/MapEntry.java +++ b/src/main/java/net/openhft/chronicle/map/MapEntry.java @@ -22,6 +22,7 @@ package net.openhft.chronicle.map; import net.openhft.chronicle.hash.HashEntry; import net.openhft.chronicle.hash.Value; +import org.jetbrains.annotations.NotNull; /** * Access to the present {@link ChronicleMap} entry. @@ -32,10 +33,13 @@ import net.openhft.chronicle.hash.Value; * @see MapEntryOperations */ public interface MapEntry extends HashEntry { + @Override + @NotNull MapContext context(); + /** * Returns the entry value. */ - Value value(); + @NotNull Value value(); /** * Replaces the entry's value with the new one. Returns {@code true} if the replace operation diff --git a/src/main/java/net/openhft/chronicle/map/MapEntryOperations.java b/src/main/java/net/openhft/chronicle/map/MapEntryOperations.java index dd61abdd6..dd7cd3695 100644 --- a/src/main/java/net/openhft/chronicle/map/MapEntryOperations.java +++ b/src/main/java/net/openhft/chronicle/map/MapEntryOperations.java @@ -49,7 +49,7 @@ public interface MapEntryOperations { * operation was successful, {@code false} if it failed for any reason. * * @implNote simply delegates to {@link MapEntry#defaultReplaceValue(Value) - * entry.defaultReplace(newValue)} + * entry.defaultReplace(newValue)}. * * @param entry the entry to replace the value in * @throws IllegalStateException if some locking/state conditions required to perform replace diff --git a/src/main/java/net/openhft/chronicle/map/MapQueryContext.java b/src/main/java/net/openhft/chronicle/map/MapQueryContext.java index c969e256b..7a1a2c423 100644 --- a/src/main/java/net/openhft/chronicle/map/MapQueryContext.java +++ b/src/main/java/net/openhft/chronicle/map/MapQueryContext.java @@ -33,7 +33,7 @@ public interface MapQueryContext extends InterProcessReadWriteUpdateLock, MapEntryOperations, MapAbsentEntryOperations { /** - * Returns the queried key as a value. + * Returns the queried key as a {@code Value}. */ Value queriedKey();