HCOLL-85 HCOLL-142 JLANG-29 Featured serializers

This commit is contained in:
leventov
2014-09-24 22:12:39 +04:00
parent 6d17ce29d2
commit 38cda58032
35 changed files with 2366 additions and 940 deletions
@@ -0,0 +1,20 @@
/*
* 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 {
}
@@ -0,0 +1,35 @@
/*
* 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);
}
@@ -0,0 +1,28 @@
/*
* 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);
}
@@ -0,0 +1,48 @@
/*
* 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 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() {}
}
@@ -0,0 +1,28 @@
/*
* 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);
}
@@ -0,0 +1,242 @@
/*
* 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.DirectBytes;
import net.openhft.lang.io.DirectStore;
import net.openhft.lang.io.serialization.BytesMarshaller;
import net.openhft.lang.io.serialization.JDKObjectSerializer;
import net.openhft.chronicle.map.threadlocal.Provider;
import net.openhft.chronicle.map.threadlocal.StatefulCopyable;
import net.openhft.chronicle.map.threadlocal.ThreadLocalCopies;
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 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();
}
}
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) {
return Hasher.hash(buffer.buffer);
}
@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();
}
private 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);
}
}
}
}
@@ -0,0 +1,58 @@
/*
* 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;
}
}
@@ -0,0 +1,50 @@
/*
* 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;
}
}
@@ -0,0 +1,58 @@
/*
* 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.DirectBytes;
import net.openhft.lang.io.DirectStore;
import net.openhft.lang.io.serialization.JDKObjectSerializer;
import net.openhft.chronicle.map.threadlocal.StatefulCopyable;
final class DirectBytesBuffer implements StatefulCopyable<DirectBytesBuffer> {
private final Object identity;
private DirectBytes buffer;
DirectBytesBuffer(Object identity) {
this.identity = identity;
}
private Bytes buffer(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 {
return buffer = new DirectStore(JDKObjectSerializer.INSTANCE, maxSize, false).bytes();
}
}
@Override
public Object stateIdentity() {
return identity;
}
@Override
public DirectBytesBuffer copy() {
return new DirectBytesBuffer(identity);
}
}
@@ -0,0 +1,62 @@
/*
* 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.AbstractBytes;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.io.NativeBytes;
public final class Hasher {
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 = 1011001110001111L * h + bytes.readLong(i);
for (; i < limit - 1; i += 2)
h = 101111 * h + bytes.readShort(i);
if (i < limit)
h = 2111 * h + bytes.readByte(i);
return hash(h);
}
public static long hash(byte[] ba) {
long h = 0;
long i = 0;
long base = NativeBytes.UNSAFE.arrayBaseOffset(byte[].class);
long limit = ba.length;
for (; i < limit - 7; i += 8)
h = 1011001110001111L * h + NativeBytes.UNSAFE.getLong(ba, base + i);
for (; i < limit - 1; i += 2)
h = 101111 * h + NativeBytes.UNSAFE.getShort(ba, base + i);
if (i < limit)
h = 2111 * h + NativeBytes.UNSAFE.getByte(ba, base + i);
return hash(h);
}
public static long hash(long value) {
value *= 11018881818881011L;
value ^= (value >>> 41) ^ (value >>> 21);
return value;
}
private Hasher() {}
}
@@ -0,0 +1,27 @@
/*
* 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);
}
@@ -0,0 +1,27 @@
/*
* 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);
}
@@ -0,0 +1,28 @@
/*
* 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);
}
@@ -0,0 +1,30 @@
/*
* 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);
}
@@ -0,0 +1,51 @@
/*
* 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 final class SizeMarshallers {
public static SizeMarshaller stopBit() {
return StopBit.INSTANCE;
}
private enum StopBit implements SizeMarshaller {
INSTANCE;
@Override
public int sizeEncodingSize(long size) {
if (size <= 127)
return 1;
// numberOfLeadingZeros is cheap intrinsic on modern CPUs
// integral division is not... but there is no choice
return ((70 - Long.numberOfLeadingZeros(size)) / 7);
}
@Override
public void writeSize(Bytes bytes, long size) {
bytes.writeStopBit(size);
}
@Override
public long readSize(Bytes bytes) {
return bytes.readStopBit();
}
}
private SizeMarshallers() {}
}
@@ -0,0 +1,66 @@
/*
* 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);
}
@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 (size != ba.length)
ba = new byte[(int) size];
bytes.read(ba);
return ba;
}
}
@@ -0,0 +1,147 @@
/*
* 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 ByteableMarshaller<E>(allocatedClass);
} else {
return new WithCustomFactory<E>(eClass, factory);
}
}
@NotNull
final Class<E> tClass;
final long size;
ByteableMarshaller(@NotNull Class<E> tClass) {
this.tClass = tClass;
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 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;
}
@NotNull
@Override
E getInstance() throws Exception {
return factory.create();
}
}
}
@@ -0,0 +1,117 @@
/*
* 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;
}
}
@@ -0,0 +1,48 @@
/*
* 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;
}
}
@@ -0,0 +1,70 @@
/*
* 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 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, 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 4L;
}
@Override
public Long read(Bytes bytes, long size) {
return bytes.readLong();
}
@Override
public Long read(Bytes bytes, long size, Long e) {
return bytes.readLong();
}
}