Moved seriaization and threadlocal packages up: n.o.chronicle.map -> n.o.chronicle

This commit is contained in:
leventov
2014-10-19 21:13:13 +04:00
parent 8a71911e53
commit d11e02d48f
34 changed files with 72 additions and 70 deletions
@@ -17,7 +17,7 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.ChronicleHash;
import net.openhft.chronicle.map.serialization.BytesReader;
import net.openhft.chronicle.serialization.BytesReader;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.io.serialization.BytesMarshaller;
import net.openhft.lang.io.serialization.ObjectFactory;
@@ -20,12 +20,12 @@ import net.openhft.chronicle.ChronicleHashBuilder;
import net.openhft.chronicle.ChronicleHashErrorListener;
import net.openhft.chronicle.ChronicleHashErrorListeners;
import net.openhft.chronicle.TimeProvider;
import net.openhft.chronicle.map.serialization.AgileBytesMarshaller;
import net.openhft.chronicle.map.serialization.MetaBytesInterop;
import net.openhft.chronicle.map.serialization.MetaBytesWriter;
import net.openhft.chronicle.map.serialization.MetaProvider;
import net.openhft.chronicle.map.threadlocal.Provider;
import net.openhft.chronicle.map.threadlocal.ThreadLocalCopies;
import net.openhft.chronicle.serialization.AgileBytesMarshaller;
import net.openhft.chronicle.serialization.MetaBytesInterop;
import net.openhft.chronicle.serialization.MetaBytesWriter;
import net.openhft.chronicle.serialization.MetaProvider;
import net.openhft.chronicle.threadlocal.Provider;
import net.openhft.chronicle.threadlocal.ThreadLocalCopies;
import net.openhft.lang.Maths;
import net.openhft.lang.io.ByteBufferBytes;
import net.openhft.lang.io.Bytes;
@@ -16,8 +16,8 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.map.serialization.BytesReader;
import net.openhft.chronicle.map.serialization.MetaBytesWriter;
import net.openhft.chronicle.serialization.BytesReader;
import net.openhft.chronicle.serialization.MetaBytesWriter;
import net.openhft.lang.io.ByteBufferBytes;
import java.nio.ByteBuffer;
@@ -18,10 +18,10 @@ package net.openhft.chronicle.map;
import net.openhft.chronicle.TimeProvider;
import net.openhft.chronicle.map.serialization.BytesReader;
import net.openhft.chronicle.map.serialization.MetaBytesInterop;
import net.openhft.chronicle.map.serialization.MetaBytesWriter;
import net.openhft.chronicle.map.threadlocal.ThreadLocalCopies;
import net.openhft.chronicle.serialization.BytesReader;
import net.openhft.chronicle.serialization.MetaBytesInterop;
import net.openhft.chronicle.serialization.MetaBytesWriter;
import net.openhft.chronicle.threadlocal.ThreadLocalCopies;
import net.openhft.lang.Maths;
import net.openhft.lang.collection.ATSDirectBitSet;
import net.openhft.lang.io.Bytes;
@@ -38,7 +38,7 @@ import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicReferenceArray;
import static net.openhft.chronicle.map.serialization.Hasher.hash;
import static net.openhft.chronicle.serialization.Hasher.hash;
import static net.openhft.lang.collection.DirectBitSet.NOT_FOUND;
/**
@@ -18,8 +18,10 @@ package net.openhft.chronicle.map;
import net.openhft.chronicle.map.serialization.*;
import net.openhft.chronicle.map.serialization.impl.*;
import net.openhft.chronicle.map.threadlocal.Provider;
import net.openhft.chronicle.map.threadlocal.ThreadLocalCopies;
import net.openhft.chronicle.threadlocal.Provider;
import net.openhft.chronicle.threadlocal.ThreadLocalCopies;
import net.openhft.chronicle.serialization.*;
import net.openhft.chronicle.serialization.impl.*;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.io.serialization.BytesMarshallable;
import net.openhft.lang.io.serialization.BytesMarshaller;
@@ -36,7 +38,7 @@ import java.io.Serializable;
import java.lang.reflect.Modifier;
import static net.openhft.chronicle.map.Objects.hash;
import static net.openhft.chronicle.map.serialization.SizeMarshallers.stopBit;
import static net.openhft.chronicle.serialization.SizeMarshallers.stopBit;
final class SerializationBuilder<E> implements Cloneable {
@@ -1,8 +1,8 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.map.serialization.*;
import net.openhft.chronicle.map.threadlocal.Provider;
import net.openhft.chronicle.map.threadlocal.ThreadLocalCopies;
import net.openhft.chronicle.threadlocal.Provider;
import net.openhft.chronicle.threadlocal.ThreadLocalCopies;
import net.openhft.chronicle.serialization.*;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.model.Byteable;
import net.openhft.lang.model.constraints.NotNull;
@@ -17,9 +17,9 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.ChronicleHashErrorListener;
import net.openhft.chronicle.map.serialization.*;
import net.openhft.chronicle.map.threadlocal.Provider;
import net.openhft.chronicle.map.threadlocal.ThreadLocalCopies;
import net.openhft.chronicle.threadlocal.Provider;
import net.openhft.chronicle.threadlocal.ThreadLocalCopies;
import net.openhft.chronicle.serialization.*;
import net.openhft.lang.Maths;
import net.openhft.lang.collection.DirectBitSet;
import net.openhft.lang.collection.SingleThreadedDirectBitSet;
@@ -1,20 +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.serialization;
public interface AgileBytesMarshaller<E> extends BytesReader<E>, BytesInterop<E>, SizeMarshaller {
}
@@ -1,35 +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.serialization;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.model.Byteable;
/**
* Marshaller for byte sequences, which are copied to off-heap {@link Bytes}
* in a very straightforward manner, e. g. {@link Byteable Byteables}, {@code byte[]} arrays,
* {@code Bytes} themselves. The criterion of this interface applicability --
* {@link #equals} implementation shouldn't require deserialization and any garbage creation.
*
* @param <E> type of marshalled objects
*/
public interface BytesInterop<E> extends BytesWriter<E> {
boolean startsWith(Bytes bytes, E e);
long hash(E e);
}
@@ -1,28 +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.serialization;
import net.openhft.lang.io.Bytes;
import java.io.Serializable;
public interface BytesReader<E> extends Serializable {
E read(Bytes bytes, long size);
E read(Bytes bytes, long size, E e);
}
@@ -1,49 +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.serialization;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.io.serialization.BytesMarshaller;
public final class BytesReaders {
public static <E> BytesReader<E> fromBytesMarshaller(BytesMarshaller<E> marshaller) {
return new SimpleBytesReader<E>(marshaller);
}
private static class SimpleBytesReader<E> implements BytesReader<E> {
private static final long serialVersionUID = 0L;
private final BytesMarshaller<E> marshaller;
public SimpleBytesReader(BytesMarshaller<E> marshaller) {
this.marshaller = marshaller;
}
@Override
public E read(Bytes bytes, long size) {
return marshaller.read(bytes);
}
@Override
public E read(Bytes bytes, long size, E e) {
return marshaller.read(bytes, e);
}
}
private BytesReaders() {}
}
@@ -1,28 +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.serialization;
import net.openhft.lang.io.Bytes;
import java.io.Serializable;
public interface BytesWriter<E> extends Serializable {
long size(E e);
void write(Bytes bytes, E e);
}
@@ -1,247 +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.serialization;
import net.openhft.chronicle.map.threadlocal.Provider;
import net.openhft.chronicle.map.threadlocal.StatefulCopyable;
import net.openhft.chronicle.map.threadlocal.ThreadLocalCopies;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.io.DirectBytes;
import net.openhft.lang.io.DirectStore;
import net.openhft.lang.io.serialization.BytesMarshaller;
import net.openhft.lang.io.serialization.JDKObjectSerializer;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
public abstract class CopyingMetaBytesInterop<E, W> implements MetaBytesInterop<E, W> {
private static final long serialVersionUID = 0L;
final DirectBytesBuffer buffer;
transient long size;
transient long hash;
transient W writer;
transient E cur;
private CopyingMetaBytesInterop(DirectBytesBuffer buffer) {
this.buffer = buffer;
}
void init(W writer, E e, boolean mutable, long maxSize) {
if (mutable || writer != this.writer || e != cur) {
this.writer = writer;
cur = e;
Bytes buffer = this.buffer.obtain(maxSize);
innerWrite(writer, buffer, e);
buffer.flip();
size = buffer.remaining();
hash = 0L;
}
}
abstract void innerWrite(W writer, Bytes bytes, E e);
@Override
public long size(W writer, E e) {
return size;
}
@Override
public boolean startsWith(W writer, Bytes bytes, E e) {
return bytes.startsWith(buffer.buffer);
}
@Override
public long hash(W writer, E e) {
long h;
if ((h = hash) == 0L)
return hash = Hasher.hash(buffer.buffer);
return h;
}
@Override
public void write(W writer, Bytes bytes, E e) {
bytes.write(buffer.buffer);
}
DirectBytesBuffer buffer() {
return buffer;
}
public static <E, M extends BytesMarshaller<E>>
MetaBytesInterop<E, M> forBytesMarshaller(Serializable bufferIdentity) {
return new DirectBytesBuffer(bufferIdentity).forBytesMarshaller;
}
public static <E, W extends BytesWriter<E>>
MetaBytesInterop<E, W> forBytesWriter(Serializable bufferIdentity) {
return new DirectBytesBuffer(bufferIdentity).forBytesWriter;
}
private static final Provider<DirectBytesBuffer> provider =
Provider.of(DirectBytesBuffer.class);
private static abstract class AbstractCopyingMetaBytesInteropProvider<E, I,
MI extends CopyingMetaBytesInterop<E, I>>
implements MetaProvider<E, I, MI> {
private static final long serialVersionUID = 0L;
@Override
public ThreadLocalCopies getCopies(ThreadLocalCopies copies) {
return provider.getCopies(copies);
}
}
public static <E, M extends BytesMarshaller<E>>
MetaProvider<E, M, CopyingMetaBytesInterop<E, M>> providerForBytesMarshaller(boolean mutable,
long maxSize) {
return new BytesMarshallerCopyingMetaBytesInteropProvider<E, M>(mutable, maxSize);
}
private static class BytesMarshallerCopyingMetaBytesInteropProvider<E,
M extends BytesMarshaller<E>>
extends AbstractCopyingMetaBytesInteropProvider<E, M, CopyingMetaBytesInterop<E, M>> {
private static final long serialVersionUID = 0L;
private final boolean mutable;
private final long maxSize;
public BytesMarshallerCopyingMetaBytesInteropProvider(boolean mutable, long maxSize) {
this.mutable = mutable;
this.maxSize = maxSize;
}
@Override
public CopyingMetaBytesInterop<E, M> get(
ThreadLocalCopies copies,
CopyingMetaBytesInterop<E, M> originalMetaWriter, M writer, E e) {
DirectBytesBuffer.ForBytesMarshaller forBytesMarshaller =
provider.get(copies, originalMetaWriter.buffer()).forBytesMarshaller;
forBytesMarshaller.init(writer, e, mutable, maxSize);
return forBytesMarshaller;
}
}
public static <E, W extends BytesWriter<E>>
MetaProvider<E, W, CopyingMetaBytesInterop<E, W>> providerForBytesWriter(boolean mutable,
long maxSize) {
return new BytesWriterCopyingMetaBytesInteropProvider<E, W>(mutable, maxSize);
}
private static class BytesWriterCopyingMetaBytesInteropProvider<E, W extends BytesWriter<E>>
extends AbstractCopyingMetaBytesInteropProvider<E, W, CopyingMetaBytesInterop<E, W>> {
private static final long serialVersionUID = 0L;
private final boolean mutable;
private final long maxSize;
public BytesWriterCopyingMetaBytesInteropProvider(boolean mutable, long maxSize) {
this.mutable = mutable;
this.maxSize = maxSize;
}
@Override
public CopyingMetaBytesInterop<E, W> get(
ThreadLocalCopies copies,
CopyingMetaBytesInterop<E, W> originalMetaWriter, W writer, E e) {
DirectBytesBuffer.ForBytesWriter forBytesWriter =
provider.get(copies, originalMetaWriter.buffer()).forBytesWriter;
forBytesWriter.init(writer, e, mutable, maxSize);
return forBytesWriter;
}
}
private static class DirectBytesBuffer
implements StatefulCopyable<DirectBytesBuffer>, Serializable {
private static final long serialVersionUID = 0L;
private final Serializable identity;
private transient DirectBytes buffer;
private transient ForBytesMarshaller forBytesMarshaller;
private transient ForBytesWriter forBytesWriter;
DirectBytesBuffer(Serializable identity) {
this.identity = identity;
initTransients();
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
initTransients();
}
private void initTransients() {
forBytesMarshaller = new ForBytesMarshaller();
forBytesWriter = new ForBytesWriter();
}
Bytes obtain(long maxSize) {
DirectBytes buf;
if ((buf = buffer) != null) {
if (maxSize <= buf.capacity()) {
return buf.clear();
} else {
DirectStore store = (DirectStore) buf.store();
store.resize(maxSize, false);
return buffer = store.bytes();
}
} else {
buffer = new DirectStore(JDKObjectSerializer.INSTANCE, maxSize, false).bytes();
return buffer;
}
}
@Override
public Object stateIdentity() {
return identity;
}
@Override
public DirectBytesBuffer copy() {
return new DirectBytesBuffer(identity);
}
private class ForBytesMarshaller<E, M extends BytesMarshaller<E>>
extends CopyingMetaBytesInterop<E, M> {
private static final long serialVersionUID = 0L;
private ForBytesMarshaller() {
super(DirectBytesBuffer.this);
}
@Override
void innerWrite(M writer, Bytes bytes, E e) {
writer.write(bytes, e);
}
}
private class ForBytesWriter<E, W extends BytesWriter<E>>
extends CopyingMetaBytesInterop<E, W> {
private static final long serialVersionUID = 0L;
private ForBytesWriter() {
super(DirectBytesBuffer.this);
}
@Override
void innerWrite(W writer, Bytes bytes, E e) {
writer.write(bytes, e);
}
}
}
}
@@ -1,58 +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.serialization;
import net.openhft.lang.io.Bytes;
import java.io.ObjectStreamException;
public final class DelegatingMetaBytesInterop<E, I extends BytesInterop<E>>
implements MetaBytesInterop<E, I> {
private static final long serialVersionUID = 0L;
private static final DelegatingMetaBytesInterop INSTANCE = new DelegatingMetaBytesInterop();
public static <E, I extends BytesInterop<E>>
DelegatingMetaBytesInterop<E, I> instance() {
return INSTANCE;
}
private DelegatingMetaBytesInterop() {}
@Override
public long size(I interop, E e) {
return interop.size(e);
}
@Override
public boolean startsWith(I interop, Bytes bytes, E e) {
return interop.startsWith(bytes, e);
}
@Override
public long hash(I interop, E e) {
return interop.hash(e);
}
@Override
public void write(I interop, Bytes bytes, E e) {
interop.write(bytes, e);
}
private Object readResolve() throws ObjectStreamException {
return INSTANCE;
}
}
@@ -1,50 +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.serialization;
import net.openhft.chronicle.map.threadlocal.ThreadLocalCopies;
import java.io.ObjectStreamException;
public final class DelegatingMetaBytesInteropProvider<E, I extends BytesInterop<E>>
implements MetaProvider<E, I, MetaBytesInterop<E, I>> {
private static final long serialVersionUID = 0L;
private static final DelegatingMetaBytesInteropProvider INSTANCE =
new DelegatingMetaBytesInteropProvider();
public static <E, I extends BytesInterop<E>>
MetaProvider<E, I, MetaBytesInterop<E, I>> instance() {
return INSTANCE;
}
private DelegatingMetaBytesInteropProvider() {}
@Override
public MetaBytesInterop<E, I> get(ThreadLocalCopies copies,
MetaBytesInterop<E, I> originalMetaWriter, I writer, E e) {
return originalMetaWriter;
}
@Override
public ThreadLocalCopies getCopies(ThreadLocalCopies copies) {
return copies;
}
private Object readResolve() throws ObjectStreamException {
return INSTANCE;
}
}
@@ -1,81 +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.serialization;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.io.NativeBytes;
import sun.misc.Unsafe;
import static java.nio.ByteOrder.LITTLE_ENDIAN;
import static java.nio.ByteOrder.nativeOrder;
public final class Hasher {
private static final long LONG_LEVEL_PRIME_MULTIPLE = 1011001110001111L;
private static final int SHORT_LEVEL_PRIME_MULTIPLE = 101111;
private static final long BYTE_LEVEL_PRIME_MULTIPLE = 2111;
private static final int INT_HASH_LOW_SHORT_MULTIPLE =
nativeOrder() == LITTLE_ENDIAN ? SHORT_LEVEL_PRIME_MULTIPLE : 1;
private static final int INT_HASH_HIGH_SHORT_MULTIPLE =
nativeOrder() == LITTLE_ENDIAN ? 1 : SHORT_LEVEL_PRIME_MULTIPLE;
public static long hash(Bytes bytes) {
return hash(bytes, bytes.position(), bytes.limit());
}
public static long hash(Bytes bytes, long offset, long limit) {
long h = 0;
long i = offset;
for (; i < limit - 7; i += 8)
h = LONG_LEVEL_PRIME_MULTIPLE * h + bytes.readLong(i);
for (; i < limit - 1; i += 2)
h = SHORT_LEVEL_PRIME_MULTIPLE * h + bytes.readShort(i);
if (i < limit)
h = BYTE_LEVEL_PRIME_MULTIPLE * h + bytes.readByte(i);
return hash(h);
}
public static long hash(Object array, int length) {
Unsafe unsafe = NativeBytes.UNSAFE;
long base = unsafe.arrayBaseOffset(array.getClass());
long scale = unsafe.arrayIndexScale(array.getClass());
long h = 0;
long i = 0;
long limit = ((long) length) * scale;
for (; i < limit - 7; i += 8)
h = LONG_LEVEL_PRIME_MULTIPLE * h + unsafe.getLong(array, base + i);
for (; i < limit - 1; i += 2)
h = SHORT_LEVEL_PRIME_MULTIPLE * h + unsafe.getShort(array, base + i);
if (i < limit)
h = BYTE_LEVEL_PRIME_MULTIPLE * h + unsafe.getByte(array, base + i);
return hash(h);
}
public static long hash(int value) {
return hash(((long) (((short) (value >>> 16)) * INT_HASH_HIGH_SHORT_MULTIPLE)) +
((short) value) * INT_HASH_LOW_SHORT_MULTIPLE);
}
public static long hash(long value) {
value *= 11018881818881011L;
value ^= (value >>> 41) ^ (value >>> 21);
return value;
}
private Hasher() {}
}
@@ -1,27 +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.serialization;
import net.openhft.lang.io.Bytes;
public interface MetaBytesInterop<E, I> extends MetaBytesWriter<E, I> {
boolean startsWith(I interop, Bytes bytes, E e);
long hash(I interop, E e);
}
@@ -1,27 +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.serialization;
import net.openhft.lang.io.Bytes;
import java.io.Serializable;
public interface MetaBytesWriter<E, W> extends Serializable {
long size(W writer, E e);
void write(W writer, Bytes bytes, E e);
}
@@ -1,28 +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.serialization;
import net.openhft.chronicle.map.threadlocal.ThreadLocalCopies;
import java.io.Serializable;
public interface MetaProvider<E, W, MW extends MetaBytesWriter<E, W>> extends Serializable {
MW get(ThreadLocalCopies copies, MW originalMetaWriter, W writer, E e);
ThreadLocalCopies getCopies(ThreadLocalCopies copies);
}
@@ -1,30 +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.serialization;
import net.openhft.lang.io.Bytes;
import java.io.Serializable;
public interface SizeMarshaller extends Serializable {
int sizeEncodingSize(long size);
void writeSize(Bytes bytes, long size);
long readSize(Bytes bytes);
}
@@ -1,78 +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.serialization;
import net.openhft.lang.io.Bytes;
import static net.openhft.lang.io.IOTools.stopBitLength;
public final class SizeMarshallers {
public static SizeMarshaller stopBit() {
return StopBit.INSTANCE;
}
private enum StopBit implements SizeMarshaller {
INSTANCE;
@Override
public int sizeEncodingSize(long size) {
return stopBitLength(size);
}
@Override
public void writeSize(Bytes bytes, long size) {
bytes.writeStopBit(size);
}
@Override
public long readSize(Bytes bytes) {
return bytes.readStopBit();
}
}
public static SizeMarshaller constant(long size) {
return new ConstantSizeMarshaller(size);
}
private static class ConstantSizeMarshaller implements SizeMarshaller {
private static final long serialVersionUID = 0L;
private final long size;
private ConstantSizeMarshaller(long size) {
this.size = size;
}
@Override
public int sizeEncodingSize(long size) {
return 0;
}
@Override
public void writeSize(Bytes bytes, long size) {
// do nothing
}
@Override
public long readSize(Bytes bytes) {
return size;
}
}
private SizeMarshallers() {}
}
@@ -1,66 +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.serialization.impl;
import net.openhft.chronicle.map.serialization.BytesInterop;
import net.openhft.chronicle.map.serialization.BytesReader;
import net.openhft.chronicle.map.serialization.Hasher;
import net.openhft.lang.io.Bytes;
public enum ByteArrayMarshaller implements BytesInterop<byte[]>, BytesReader<byte[]> {
INSTANCE;
@Override
public long size(byte[] ba) {
return ba.length;
}
@Override
public boolean startsWith(Bytes bytes, byte[] ba) {
long pos = bytes.position();
for (int i = 0; i < ba.length; i++) {
if (bytes.readByte(pos + i) != ba[i])
return false;
}
return true;
}
@Override
public long hash(byte[] ba) {
return Hasher.hash(ba, ba.length);
}
@Override
public void write(Bytes bytes, byte[] ba) {
bytes.write(ba);
}
@Override
public byte[] read(Bytes bytes, long size) {
byte[] ba = new byte[(int) size];
bytes.read(ba);
return ba;
}
@Override
public byte[] read(Bytes bytes, long size, byte[] ba) {
if (ba == null || size != ba.length)
ba = new byte[(int) size];
bytes.read(ba);
return ba;
}
}
@@ -1,160 +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.serialization.impl;
import net.openhft.chronicle.map.serialization.AgileBytesMarshaller;
import net.openhft.chronicle.map.serialization.Hasher;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.io.NativeBytes;
import net.openhft.lang.io.serialization.*;
import net.openhft.lang.io.serialization.impl.AllocateInstanceObjectFactory;
import net.openhft.lang.model.Byteable;
import org.jetbrains.annotations.NotNull;
public class ByteableMarshaller<E extends Byteable> implements AgileBytesMarshaller<E> {
private static final long serialVersionUID = 0L;
public static <E extends Byteable> ByteableMarshaller<E> of(@NotNull Class<E> eClass) {
return new ByteableMarshaller<E>(eClass);
}
public static <E extends Byteable> ByteableMarshaller<E> of(@NotNull Class<E> eClass,
ObjectFactory<E> factory) {
if (factory instanceof AllocateInstanceObjectFactory) {
Class allocatedClass = ((AllocateInstanceObjectFactory) factory).allocatedClass();
return new Default<E>(allocatedClass);
} else {
return new WithCustomFactory<E>(eClass, factory);
}
}
@NotNull
private final Class<E> tClass;
private long size;
private ByteableMarshaller(@NotNull Class<E> tClass) {
this.tClass = tClass;
}
void initSize() {
try {
size = getInstance().maxSize();
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
@Override
public long size(E e) {
return size;
}
@Override
public int sizeEncodingSize(long size) {
return 0;
}
@Override
public void writeSize(Bytes bytes, long size) {
// do nothing
}
@Override
public boolean startsWith(Bytes bytes, E e) {
Bytes input = e.bytes();
long pos = bytes.position(), inputPos = e.offset();
int i = 0;
for (; i < size - 7; i += 8) {
if (bytes.readLong(pos + i) != input.readLong(inputPos + i))
return false;
}
for (; i < size; i++) {
if (bytes.readByte(pos + i) != input.readByte(inputPos + i))
return false;
}
return true;
}
@Override
public long hash(E e) {
return Hasher.hash(e.bytes(), e.offset(), e.offset() + size);
}
@Override
public void write(Bytes bytes, E e) {
bytes.write(e.bytes(), e.offset(), size);
}
@Override
public long readSize(Bytes bytes) {
return size;
}
@Override
public E read(Bytes bytes, long size) {
return read(bytes, size, null);
}
@Override
public E read(Bytes bytes, long size, E e) {
try {
if (e == null)
e = getInstance();
e.bytes(bytes, bytes.position());
bytes.skip(size);
return e;
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
@SuppressWarnings("unchecked")
@NotNull
E getInstance() throws Exception {
return (E) NativeBytes.UNSAFE.allocateInstance(tClass);
}
private static class Default<E extends Byteable> extends ByteableMarshaller<E> {
private static final long serialVersionUID = 0L;
Default(@NotNull Class<E> tClass) {
super(tClass);
initSize();
}
}
private static class WithCustomFactory<E extends Byteable> extends ByteableMarshaller<E> {
private static final long serialVersionUID = 0L;
@NotNull
private final ObjectFactory<E> factory;
WithCustomFactory(@NotNull Class<E> tClass,
@NotNull ObjectFactory<E> factory) {
super(tClass);
this.factory = factory;
initSize();
}
@NotNull
@Override
E getInstance() throws Exception {
return factory.create();
}
}
}
@@ -1,117 +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.serialization.impl;
import net.openhft.chronicle.map.serialization.BytesReader;
import net.openhft.lang.io.AbstractBytes;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.pool.CharSequenceInterner;
import net.openhft.chronicle.map.threadlocal.StatefulCopyable;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.ObjectStreamException;
import java.io.Serializable;
public final class CharSequenceReader<S extends CharSequence>
implements BytesReader<S>, StatefulCopyable<CharSequenceReader<S>> {
private static final long serialVersionUID = 0L;
private enum NoInterning implements CharSequenceInterner<String> {
INSTANCE;
@Override
public String intern(CharSequence cs) {
return cs.toString();
}
}
private static final CharSequenceReader<String> DEFAULT_READER =
new CharSequenceReader<String>(NoInterning.INSTANCE, NoInterning.INSTANCE);
public static CharSequenceReader<String> of() {
return DEFAULT_READER;
}
public static <S extends CharSequence, I extends CharSequenceInterner<S> & Serializable>
CharSequenceReader<S> of(@NotNull I interner, @NotNull Serializable stateIdentity) {
return new CharSequenceReader<S>(interner, stateIdentity);
}
private final StringBuilder sb = new StringBuilder(128);
@NotNull
private final CharSequenceInterner<S> interner;
@NotNull
private final Serializable identity;
private CharSequenceReader(@NotNull CharSequenceInterner<S> interner,
@NotNull Serializable identity) {
this.interner = interner;
this.identity = identity;
}
@Override
public S read(Bytes bytes, long size) {
sb.setLength(0);
try {
AbstractBytes.readUTF0(bytes, sb, (int) size);
} catch (IOException e) {
throw new IllegalStateException(e);
}
return interner.intern(sb);
}
@Override
public S read(Bytes bytes, long size, S s) {
Appendable appendable;
if (s instanceof Appendable) {
appendable = (Appendable) s;
if (s instanceof StringBuilder) {
((StringBuilder) s).setLength(0);
} else if (s instanceof StringBuffer) {
((StringBuffer) s).setLength(0);
}
} else {
sb.setLength(0);
appendable = sb;
}
try {
AbstractBytes.readUTF0(bytes, appendable, (int) size);
} catch (IOException e) {
throw new IllegalStateException(e);
}
if (appendable == s)
return s;
return interner.intern(sb);
}
@Override
public Object stateIdentity() {
return identity;
}
@Override
public CharSequenceReader<S> copy() {
return new CharSequenceReader<S>(interner, identity);
}
private Object readResolve() throws ObjectStreamException {
if (interner == NoInterning.INSTANCE)
return DEFAULT_READER;
return this;
}
}
@@ -1,48 +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.serialization.impl;
import net.openhft.lang.io.AbstractBytes;
import net.openhft.lang.io.Bytes;
import net.openhft.chronicle.map.serialization.BytesWriter;
import java.io.ObjectStreamException;
public final class CharSequenceWriter<CS extends CharSequence> implements BytesWriter<CS> {
private static final long serialVersionUID = 0L;
private static final CharSequenceWriter INSTANCE = new CharSequenceWriter();
public static <CS extends CharSequence> CharSequenceWriter<CS> instance() {
return INSTANCE;
}
private CharSequenceWriter() {}
@Override
public long size(CS s) {
return AbstractBytes.findUTFLength(s, s.length());
}
@Override
public void write(Bytes bytes, CS s) {
AbstractBytes.writeUTF0(bytes, s, s.length());
}
private Object readResolve() throws ObjectStreamException {
return INSTANCE;
}
}
@@ -1,72 +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.serialization.impl;
import net.openhft.chronicle.map.serialization.AgileBytesMarshaller;
import net.openhft.chronicle.map.serialization.Hasher;
import net.openhft.lang.io.Bytes;
import static java.lang.Double.doubleToLongBits;
public enum DoubleMarshaller implements AgileBytesMarshaller<Double> {
INSTANCE;
@Override
public long size(Double e) {
return 8L;
}
@Override
public int sizeEncodingSize(long size) {
return 0;
}
@Override
public void writeSize(Bytes bytes, long size) {
// do nothing
}
@Override
public boolean startsWith(Bytes bytes, Double e) {
return doubleToLongBits(e) == bytes.readLong(0);
}
@Override
public long hash(Double e) {
return Hasher.hash(doubleToLongBits(e));
}
@Override
public void write(Bytes bytes, Double e) {
bytes.writeLong(doubleToLongBits(e));
}
@Override
public long readSize(Bytes bytes) {
return 8L;
}
@Override
public Double read(Bytes bytes, long size) {
return Double.longBitsToDouble(bytes.readLong());
}
@Override
public Double read(Bytes bytes, long size, Double e) {
return read(bytes, size);
}
}
@@ -1,70 +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.serialization.impl;
import net.openhft.chronicle.map.serialization.AgileBytesMarshaller;
import net.openhft.chronicle.map.serialization.Hasher;
import net.openhft.lang.io.Bytes;
public enum IntegerMarshaller implements AgileBytesMarshaller<Integer> {
INSTANCE;
@Override
public long size(Integer e) {
return 4L;
}
@Override
public int sizeEncodingSize(long size) {
return 0;
}
@Override
public void writeSize(Bytes bytes, long size) {
// do nothing
}
@Override
public boolean startsWith(Bytes bytes, Integer e) {
return e == bytes.readInt(0);
}
@Override
public long hash(Integer e) {
return Hasher.hash(e);
}
@Override
public void write(Bytes bytes, Integer e) {
bytes.writeInt(e);
}
@Override
public long readSize(Bytes bytes) {
return 4L;
}
@Override
public Integer read(Bytes bytes, long size) {
return bytes.readInt();
}
@Override
public Integer read(Bytes bytes, long size, Integer e) {
return bytes.readInt();
}
}
@@ -1,70 +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.serialization.impl;
import net.openhft.chronicle.map.serialization.AgileBytesMarshaller;
import net.openhft.chronicle.map.serialization.Hasher;
import net.openhft.lang.io.Bytes;
public enum LongMarshaller implements AgileBytesMarshaller<Long> {
INSTANCE;
@Override
public long size(Long e) {
return 8L;
}
@Override
public int sizeEncodingSize(long size) {
return 0;
}
@Override
public void writeSize(Bytes bytes, long size) {
// do nothing
}
@Override
public boolean startsWith(Bytes bytes, Long e) {
return e == bytes.readLong(0);
}
@Override
public long hash(Long e) {
return Hasher.hash(e);
}
@Override
public void write(Bytes bytes, Long e) {
bytes.writeLong(e);
}
@Override
public long readSize(Bytes bytes) {
return 8L;
}
@Override
public Long read(Bytes bytes, long size) {
return bytes.readLong();
}
@Override
public Long read(Bytes bytes, long size, Long e) {
return bytes.readLong();
}
}
@@ -1,71 +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.serialization.impl;
import net.openhft.chronicle.map.serialization.AgileBytesMarshaller;
import net.openhft.lang.io.Bytes;
public enum VoidMarshaller implements AgileBytesMarshaller<Void> {
INSTANCE;
@Override
public long size(Void e) {
return 0L;
}
@Override
public int sizeEncodingSize(long size) {
return 0;
}
@Override
public void writeSize(Bytes bytes, long size) {
// do nothing
}
@Override
public boolean startsWith(Bytes bytes, Void e) {
return false;
}
@Override
public long hash(Void e) {
return 0;
}
@Override
public void write(Bytes bytes, Void e) {
// do nothing;
}
@Override
public long readSize(Bytes bytes) {
return 0L;
}
@Override
public Void read(Bytes bytes, long size) {
// Void nothing;
return null;
}
@Override
public Void read(Bytes bytes, long size, Void aVoid) {
return null;
}
}
@@ -1,85 +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.threadlocal;
public abstract class Provider<T> {
public static <T> Provider<T> of(Class<T> tClass) {
if (StatefulCopyable.class.isAssignableFrom(tClass)) {
return StatefulProvider.INSTANCE;
}
return StatelessProvider.INSTANCE;
}
public abstract T get(ThreadLocalCopies copies, T original);
public abstract ThreadLocalCopies getCopies(ThreadLocalCopies copies);
private static final class StatefulProvider<T extends StatefulCopyable<T>> extends Provider<T> {
private static final Provider INSTANCE = new StatefulProvider();
@Override
public ThreadLocalCopies getCopies(ThreadLocalCopies copies) {
if (copies != null)
return copies;
return ThreadLocalCopies.get();
}
@Override
public T get(ThreadLocalCopies copies, T original) {
if (copies.currentlyAccessed.compareAndSet(false, true)) {
Object id = original.stateIdentity();
int m = copies.mask;
Object[] tab = copies.table;
int i = System.identityHashCode(id) & m;
T copy;
while (true) {
Object idInTable = tab[i];
if (idInTable == id) {
copy = (T) tab[i + 1];
break;
} else if (idInTable == null) {
tab[i] = id;
tab[i + 1] = copy = original.copy();
copies.postInsert();
break;
}
i = (i + 2) & m;
}
copies.currentlyAccessed.set(false);
return copy;
} else {
throw new IllegalStateException(
"Concurrent or recursive access to ThreadLocalCopies is not allowed");
}
}
}
private static final class StatelessProvider<M> extends Provider<M> {
private static final Provider INSTANCE = new StatelessProvider();
@Override
public M get(ThreadLocalCopies copies, M original) {
return original;
}
@Override
public ThreadLocalCopies getCopies(ThreadLocalCopies copies) {
return copies;
}
}
}
@@ -1,23 +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.threadlocal;
public interface StatefulCopyable<S extends StatefulCopyable<S>> {
Object stateIdentity();
S copy();
}
@@ -1,73 +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.threadlocal;
import java.util.concurrent.atomic.AtomicBoolean;
public final class ThreadLocalCopies {
private static ThreadLocal<ThreadLocalCopies> states = new ThreadLocal<ThreadLocalCopies>() {
@Override
protected ThreadLocalCopies initialValue() {
return new ThreadLocalCopies();
}
};
static ThreadLocalCopies get() {
return states.get();
}
AtomicBoolean currentlyAccessed = new AtomicBoolean(false);
Object[] table;
int size = 0, sizeLimit, mask;
private ThreadLocalCopies() {
init(32); // 16 entries
}
void init(int doubledCapacity) {
table = new Object[doubledCapacity];
sizeLimit = doubledCapacity / 3; // 0.66 fullness
mask = (doubledCapacity - 1) & ~1;
}
void rehash() {
Object[] oldTab = this.table;
int oldCapacity = oldTab.length;
if (oldCapacity == (1 << 30))
throw new IllegalStateException("Hash is full");
init(oldCapacity << 1);
int m = mask;
Object[] tab = this.table;
for (int oldI = 0; oldI < oldCapacity; oldI += 2) {
Object id = oldTab[oldI];
if (id != null) {
int i = System.identityHashCode(id) & m;
while (tab[i] != null) {
i = (i + 2) & m;
}
tab[i] = id;
tab[i + 1] = oldTab[oldI + 1];
}
}
}
void postInsert() {
if (++size > sizeLimit)
rehash();
}
}