SPI implementation

This commit is contained in:
leventov
2015-05-27 00:03:20 +01:00
parent 8e4027da08
commit b4c8554b77
111 changed files with 13637 additions and 3991 deletions
@@ -22,7 +22,7 @@ import net.openhft.chronicle.hash.ChronicleHashBuilder;
import net.openhft.chronicle.hash.serialization.internal.SerializationBuilder;
//TODO remove this temporary interface
public interface ChronicleHashBuilderImpl<K, H extends ChronicleHash<K, ?>,
public interface ChronicleHashBuilderImpl<K, H extends ChronicleHash<K, ?, ?>,
B extends ChronicleHashBuilder<K, H, B>> extends ChronicleHashBuilder<K, H, B> {
SerializationBuilder<K> keyBuilder();
@@ -1,26 +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.hash.impl;
public interface ContextFactory<T extends HashContext> {
T createContext(HashContext root, int indexInContextCache);
T createRootContext();
Class<T> contextClass();
}
@@ -0,0 +1,77 @@
/*
* 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.impl;
import net.openhft.chronicle.bytes.ReadAccess;
import net.openhft.chronicle.hash.AbstractValue;
import net.openhft.chronicle.hash.Value;
import net.openhft.lang.io.DirectBytes;
import net.openhft.lang.io.DirectStore;
import net.openhft.lang.io.serialization.JDKObjectSerializer;
import static net.openhft.chronicle.hash.impl.JavaLangBytesAccessors.uncheckedBytesAccessor;
public abstract class CopyingInstanceValue<I, T> extends AbstractValue<I, T> {
public static DirectBytes getBuffer(DirectBytes b, long capacity) {
if (b != null) {
if (b.capacity() >= capacity) {
return (DirectBytes) b.clear();
} else {
DirectStore store = (DirectStore) b.store();
store.resize(capacity, false);
return store.bytes();
}
} else {
return new DirectStore(JDKObjectSerializer.INSTANCE, Math.max(1, capacity), true)
.bytes();
}
}
public abstract I instance();
public abstract DirectBytes buffer();
@Override
public ReadAccess<T> access() {
//noinspection unchecked
return (ReadAccess<T>) uncheckedBytesAccessor(buffer()).access(buffer());
}
@Override
public T handle() {
//noinspection unchecked
return (T) uncheckedBytesAccessor(buffer()).handle(buffer());
}
@Override
public long offset() {
return uncheckedBytesAccessor(buffer()).offset(buffer(), 0);
}
@Override
public long size() {
return buffer().limit();
}
@Override
public I get() {
return instance();
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,93 @@
/*
* 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.impl;
import net.openhft.chronicle.bytes.Access;
import net.openhft.lang.io.Bytes;
import java.nio.ByteOrder;
enum JavaLangBytesAccess implements Access<Bytes> {
INSTANCE;
@Override
public byte readByte(Bytes handle, long offset) {
return handle.readByte(offset);
}
@Override
public short readShort(Bytes handle, long offset) {
return handle.readShort(offset);
}
@Override
public char readChar(Bytes handle, long offset) {
return handle.readChar(offset);
}
@Override
public int readInt(Bytes handle, long offset) {
return handle.readInt(offset);
}
@Override
public long readLong(Bytes handle, long offset) {
return handle.readLong(offset);
}
@Override
public void writeByte(Bytes handle, long offset, byte i8) {
handle.writeByte(offset, i8);
}
@Override
public void writeShort(Bytes handle, long offset, short i) {
handle.writeShort(offset, i);
}
@Override
public void writeChar(Bytes handle, long offset, char c) {
handle.writeChar(offset, c);
}
@Override
public void writeInt(Bytes handle, long offset, int i) {
handle.writeInt(offset, i);
}
@Override
public void writeLong(Bytes handle, long offset, long i) {
handle.writeLong(offset, i);
}
@Override
public void writeFloat(Bytes handle, long offset, float d) {
handle.writeFloat(offset, d);
}
@Override
public void writeDouble(Bytes handle, long offset, double d) {
handle.writeDouble(offset, d);
}
@Override
public ByteOrder byteOrder(Bytes handle) {
return handle.byteOrder();
}
}
@@ -0,0 +1,74 @@
/*
* 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.impl;
import net.openhft.chronicle.bytes.Access;
import net.openhft.chronicle.bytes.Accessor;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.io.NativeBytes;
import static net.openhft.chronicle.bytes.Access.nativeAccess;
public final class JavaLangBytesAccessors {
public static Accessor.Full<Bytes, ?> uncheckedBytesAccessor(Bytes bytes) {
return bytes instanceof NativeBytes ? NativeBytesAccessor.INSTANCE :
GenericBytesAccessor.INSTANCE;
}
enum NativeBytesAccessor implements Accessor.Full<Bytes, Void> {
INSTANCE;
@Override
public Access<Void> access(Bytes source) {
return nativeAccess();
}
@Override
public Void handle(Bytes source) {
return null;
}
@Override
public long offset(Bytes source, long index) {
return source.address() + index;
}
}
enum GenericBytesAccessor implements Accessor.Full<Bytes, Bytes> {
INSTANCE;
@Override
public Access<Bytes> access(Bytes source) {
return JavaLangBytesAccess.INSTANCE;
}
@Override
public Bytes handle(Bytes source) {
return source;
}
@Override
public long offset(Bytes source, long index) {
return index;
}
}
private JavaLangBytesAccessors() {}
}
@@ -0,0 +1,36 @@
/*
* 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.impl;
public enum LocalLockState {
UNLOCKED(false, false, false),
READ_LOCKED(true, false, false),
UPDATE_LOCKED(true, true, false),
WRITE_LOCKED(true, true, true);
public final boolean read;
public final boolean update;
public final boolean write;
LocalLockState(boolean read, boolean update, boolean write) {
this.read = read;
this.update = update;
this.write = write;
}
}
@@ -0,0 +1,33 @@
/*
* 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.impl;
import net.openhft.lang.io.MultiStoreBytes;
/**
* This awful class is made for "conversion" from new ch.bytes.Bytes to old lang.io.Bytes
*
* TODO remove when Bytes system stabilize
*/
public class PublicMultiStoreBytes extends MultiStoreBytes {
public void startAddr(long startAddr) { this.startAddr = startAddr; }
public void positionAddr(long positionAddr) { this.positionAddr = positionAddr; }
public void limitAddr(long limitAddr) { this.limitAddr = limitAddr; }
public void capacityAddr(long capacityAddr) { this.capacityAddr = capacityAddr; }
}
@@ -17,6 +17,7 @@
package net.openhft.chronicle.hash.impl;
import net.openhft.chronicle.hash.ChronicleHash;
import net.openhft.chronicle.hash.ExternalHashQueryContext;
import net.openhft.chronicle.hash.KeyContext;
import net.openhft.chronicle.hash.impl.hashlookup.HashLookup;
import net.openhft.chronicle.hash.impl.util.BuildVersion;
@@ -44,7 +45,8 @@ import static java.lang.Math.max;
import static net.openhft.lang.MemoryUnit.*;
public abstract class VanillaChronicleHash<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
C extends KeyContext<K>> implements ChronicleHash<K, C>, Serializable {
C extends KeyContext<K>, ECQ extends ExternalHashQueryContext<K>>
implements ChronicleHash<K, C, ECQ>, Serializable {
private static final long serialVersionUID = 0L;
@@ -62,12 +64,12 @@ public abstract class VanillaChronicleHash<K, KI, MKI extends MetaBytesInterop<K
public final MetaProvider<K, KI, MKI> metaKeyInteropProvider;
public transient Provider<BytesReader<K>> keyReaderProvider;
transient Provider<KI> keyInteropProvider;
public transient Provider<KI> keyInteropProvider;
/////////////////////////////////////////////////
// Concurrency (number of segments), memory management and dependent fields
public final int actualSegments;
final HashSplitting hashSplitting;
public final HashSplitting hashSplitting;
public final long entriesPerSegment;
@@ -79,18 +81,18 @@ public abstract class VanillaChronicleHash<K, KI, MKI extends MetaBytesInterop<K
// Precomputed offsets and sizes for fast Context init
final int segmentHeaderSize;
final int segmentHashLookupValueBits;
final int segmentHashLookupKeyBits;
final int segmentHashLookupEntrySize;
final long segmentHashLookupCapacity;
public final int segmentHashLookupValueBits;
public final int segmentHashLookupKeyBits;
public final int segmentHashLookupEntrySize;
public final long segmentHashLookupCapacity;
final long segmentHashLookupInnerSize;
final long segmentHashLookupOuterSize;
public final long segmentHashLookupOuterSize;
final long segmentFreeListInnerSize;
final long segmentFreeListOuterSize;
public final long segmentFreeListInnerSize;
public final long segmentFreeListOuterSize;
final long segmentEntrySpaceInnerSize;
final int segmentEntrySpaceInnerOffset;
public final int segmentEntrySpaceInnerOffset;
final long segmentEntrySpaceOuterSize;
final long segmentSize;
@@ -0,0 +1,8 @@
package net.openhft.chronicle.hash.impl.value.instance;
import net.openhft.chronicle.hash.Value;
public interface KeyInitableValue<K, T> extends Value<K, T> {
void initKey(K key);
}