renamed common to hash

This commit is contained in:
Rob Austin
2014-10-29 12:11:24 +00:00
parent ebecafe54a
commit 2f73cf1f39
54 changed files with 105 additions and 107 deletions
@@ -0,0 +1,68 @@
/*
* 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.serialization.impl;
import net.openhft.chronicle.hash.serialization.BytesInterop;
import net.openhft.chronicle.hash.serialization.BytesReader;
import net.openhft.chronicle.hash.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;
}
}
@@ -0,0 +1,174 @@
/*
* 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.serialization.impl;
import net.openhft.chronicle.hash.serialization.AgileBytesMarshaller;
import net.openhft.chronicle.hash.serialization.Hasher;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.io.MultiStoreBytes;
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();
setBytesAndOffset(e, bytes);
bytes.skip(size);
return e;
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
public static void setBytesAndOffset(Byteable e, Bytes bytes) {
// use the unshifting underlying Bytes (the position never changes)
// if you use a Bytes where the position changes, you risk concurrency issues.
if (bytes instanceof MultiStoreBytes) {
MultiStoreBytes msb = (MultiStoreBytes) bytes;
e.bytes(msb.underlyingBytes(), msb.underlyingOffset() + msb.position());
} else {
e.bytes(bytes, bytes.position());
}
}
@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();
}
}
}
@@ -0,0 +1,68 @@
/*
* 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.serialization.impl;
import net.openhft.chronicle.hash.serialization.BytesInterop;
import net.openhft.chronicle.hash.serialization.BytesReader;
import net.openhft.chronicle.hash.serialization.Hasher;
import net.openhft.lang.io.Bytes;
public enum CharArrayMarshaller implements BytesInterop<char[]>, BytesReader<char[]> {
INSTANCE;
@Override
public boolean startsWith(Bytes bytes, char[] chars) {
long pos = bytes.position();
for (int i = 0; i < chars.length; i++) {
if (bytes.readChar(pos + (i * 2L)) != chars[i])
return false;
}
return true;
}
@Override
public long hash(char[] chars) {
return Hasher.hash(chars, chars.length);
}
@Override
public char[] read(Bytes bytes, long size) {
char[] chars = new char[(int) size];
bytes.readFully(chars);
return chars;
}
@Override
public char[] read(Bytes bytes, long size, char[] chars) {
if (chars == null || chars.length != size)
chars = new char[(int) size];
bytes.readFully(chars);
return chars;
}
@Override
public long size(char[] chars) {
return chars.length * 2L;
}
@Override
public void write(Bytes bytes, char[] chars) {
bytes.write(chars);
}
}
@@ -0,0 +1,119 @@
/*
* 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.serialization.impl;
import net.openhft.chronicle.hash.serialization.BytesReader;
import net.openhft.lang.io.AbstractBytes;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.pool.CharSequenceInterner;
import net.openhft.chronicle.hash.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,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.hash.serialization.impl;
import net.openhft.chronicle.hash.serialization.BytesWriter;
import net.openhft.lang.io.AbstractBytes;
import net.openhft.lang.io.Bytes;
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,74 @@
/*
* 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.serialization.impl;
import net.openhft.chronicle.hash.serialization.AgileBytesMarshaller;
import net.openhft.chronicle.hash.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);
}
}
@@ -0,0 +1,72 @@
/*
* 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.serialization.impl;
import net.openhft.chronicle.hash.serialization.AgileBytesMarshaller;
import net.openhft.chronicle.hash.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();
}
}
@@ -0,0 +1,72 @@
/*
* 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.serialization.impl;
import net.openhft.chronicle.hash.serialization.AgileBytesMarshaller;
import net.openhft.chronicle.hash.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();
}
}
@@ -0,0 +1,73 @@
/*
* 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.serialization.impl;
import net.openhft.chronicle.hash.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;
}
}