Implemented AgileBytesMarshaller for Integer

This commit is contained in:
leventov
2014-10-12 16:54:08 +04:00
parent 573b91464a
commit cc12bba67a
3 changed files with 95 additions and 29 deletions
@@ -94,6 +94,8 @@ final class SerializationBuilder<E> implements Cloneable {
agileMarshaller((AgileBytesMarshaller<E>) LongMarshaller.INSTANCE, factory);
} else if (eClass == Double.class) {
agileMarshaller((AgileBytesMarshaller<E>) DoubleMarshaller.INSTANCE, factory);
} else if (eClass == Integer.class) {
agileMarshaller((AgileBytesMarshaller<E>) IntegerMarshaller.INSTANCE, factory);
} else if (eClass == byte[].class) {
reader((BytesReader<E>) ByteArrayMarshaller.INSTANCE, factory);
interop((BytesInterop<E>) ByteArrayMarshaller.INSTANCE);
@@ -113,8 +115,6 @@ final class SerializationBuilder<E> implements Cloneable {
return new BytesMarshallableMarshaller(classForMarshaller);
if (Externalizable.class.isAssignableFrom(eClass))
return new ExternalizableMarshaller(classForMarshaller);
if (eClass == Integer.class)
return (BytesMarshaller<E>) IntegerMarshaller.INSTANCE;
return null;
}
@@ -330,27 +330,6 @@ final class SerializationBuilder<E> implements Cloneable {
}
}
private enum IntegerMarshaller implements BytesMarshaller<Integer> {
INSTANCE;
@Override
public void write(Bytes bytes, Integer v) {
bytes.writeInt(v);
}
@Nullable
@Override
public Integer read(Bytes bytes) {
return bytes.readInt();
}
@Nullable
@Override
public Integer read(Bytes bytes, @Nullable Integer v) {
return bytes.readInt();
}
}
private static class SerializableMarshaller implements BytesMarshaller {
private static final long serialVersionUID = 0L;
@@ -20,8 +20,20 @@ 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());
}
@@ -30,11 +42,11 @@ public final class Hasher {
long h = 0;
long i = offset;
for (; i < limit - 7; i += 8)
h = 1011001110001111L * h + bytes.readLong(i);
h = LONG_LEVEL_PRIME_MULTIPLE * h + bytes.readLong(i);
for (; i < limit - 1; i += 2)
h = 101111 * h + bytes.readShort(i);
h = SHORT_LEVEL_PRIME_MULTIPLE * h + bytes.readShort(i);
if (i < limit)
h = 2111 * h + bytes.readByte(i);
h = BYTE_LEVEL_PRIME_MULTIPLE * h + bytes.readByte(i);
return hash(h);
}
@@ -46,14 +58,19 @@ public final class Hasher {
long i = 0;
long limit = ((long) length) * scale;
for (; i < limit - 7; i += 8)
h = 1011001110001111L * h + unsafe.getLong(array, base + i);
h = LONG_LEVEL_PRIME_MULTIPLE * h + unsafe.getLong(array, base + i);
for (; i < limit - 1; i += 2)
h = 101111 * h + unsafe.getShort(array, base + i);
h = SHORT_LEVEL_PRIME_MULTIPLE * h + unsafe.getShort(array, base + i);
if (i < limit)
h = 2111 * h + unsafe.getByte(array, base + i);
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);
@@ -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 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();
}
}