diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/impl/AbstractCharSequenceUtf8DataAccess.java b/src/main/java/net/openhft/chronicle/hash/serialization/impl/AbstractCharSequenceUtf8DataAccess.java new file mode 100644 index 000000000..42febce31 --- /dev/null +++ b/src/main/java/net/openhft/chronicle/hash/serialization/impl/AbstractCharSequenceUtf8DataAccess.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2015 higherfrequencytrading.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package net.openhft.chronicle.hash.serialization.impl; + +import net.openhft.chronicle.bytes.Bytes; +import net.openhft.chronicle.bytes.BytesUtil; +import net.openhft.chronicle.bytes.RandomDataInput; +import net.openhft.chronicle.hash.AbstractData; +import net.openhft.chronicle.hash.Data; +import net.openhft.chronicle.hash.serialization.DataAccess; +import net.openhft.chronicle.wire.WireIn; +import net.openhft.chronicle.wire.WireOut; +import org.jetbrains.annotations.NotNull; + +abstract class AbstractCharSequenceUtf8DataAccess extends AbstractData + implements DataAccess, Data { + + /** Cache field */ + private transient Bytes bytes; + + /** State field */ + transient T cs; + + AbstractCharSequenceUtf8DataAccess() { + initTransients(); + } + + private void initTransients() { + bytes = Bytes.allocateElasticDirect(1); + } + + @Override + public Data getData(@NotNull T cs) { + this.cs = cs; + bytes.clear(); + BytesUtil.appendUtf8(bytes, cs); + return this; + } + + @Override + public void uninit() { + cs = null; + } + + @Override + public void readMarshallable(@NotNull WireIn wireIn) { + // no config fields to read + initTransients(); + } + + @Override + public void writeMarshallable(@NotNull WireOut wireOut) { + // no config fields to write + } + + @Override + public RandomDataInput bytes() { + return bytes.bytesStore(); + } + + @Override + public long offset() { + return 0; + } + + @Override + public long size() { + return bytes.readRemaining(); + } + + @Override + public T get() { + return cs; + } +} diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceBytesReader.java b/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceBytesReader.java index 3ab9e088e..a264c2e06 100644 --- a/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceBytesReader.java +++ b/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceBytesReader.java @@ -30,8 +30,8 @@ import java.io.IOException; import java.io.ObjectInputStream; /** - * {@link BytesReader} implementation for {@code CharSequence}, for the primary ChronicleMap's key - * of value type {@link CharSequenceSizedReader} + {@link CharSequenceSizedWriter} are more + * {@link BytesReader} implementation for {@code CharSequence}. For the primary ChronicleMap's key + * or value type {@link CharSequenceSizedReader} + {@link CharSequenceUtf8DataAccess} are more * efficient (because don't store the size twice), so this reader is useful in conjunction with * {@link ListMarshaller} or {@link SetMarshaller}. * diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceBytesWriter.java b/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceBytesWriter.java index d977de8a2..6b92cf993 100644 --- a/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceBytesWriter.java +++ b/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceBytesWriter.java @@ -23,8 +23,8 @@ import net.openhft.chronicle.hash.serialization.SetMarshaller; import org.jetbrains.annotations.NotNull; /** - * {@link BytesWriter} implementation for {@link CharSequence}, for the primary ChronicleMap's - * key or value type {@link CharSequenceSizedWriter} + {@link StringSizedReader} are more + * {@link BytesWriter} implementation for {@link CharSequence}, for the primary ChronicleMap's key + * or value type {@link CharSequenceUtf8DataAccess} + {@link CharSequenceSizedReader} are more * efficient (because don't store the size twice), so this writer is useful in conjunction with * {@link ListMarshaller} or {@link SetMarshaller}. * diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceSizedWriter.java b/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceSizedWriter.java index d72ca9742..4e9cb75f4 100644 --- a/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceSizedWriter.java +++ b/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceSizedWriter.java @@ -21,6 +21,10 @@ import net.openhft.chronicle.bytes.BytesUtil; import net.openhft.chronicle.hash.serialization.SizedWriter; import org.jetbrains.annotations.NotNull; +/** + * @deprecated use one of {@link AbstractCharSequenceUtf8DataAccess} subclasses instead + */ +@Deprecated public enum CharSequenceSizedWriter implements SizedWriter, EnumMarshallable { INSTANCE; diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceUtf8DataAccess.java b/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceUtf8DataAccess.java new file mode 100644 index 000000000..bab67f5df --- /dev/null +++ b/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceUtf8DataAccess.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2015 higherfrequencytrading.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package net.openhft.chronicle.hash.serialization.impl; + +import net.openhft.chronicle.hash.serialization.DataAccess; +import org.jetbrains.annotations.Nullable; + +public final class CharSequenceUtf8DataAccess + extends AbstractCharSequenceUtf8DataAccess { + + @Override + public CharSequence getUsing(@Nullable CharSequence using) { + StringBuilder sb; + if (using instanceof StringBuilder) { + sb = (StringBuilder) using; + sb.setLength(0); + } else { + sb = new StringBuilder(cs.length()); + } + sb.append(cs); + return sb; + } + + @Override + public DataAccess copy() { + return new CharSequenceUtf8DataAccess(); + } +} diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/impl/SerializationBuilder.java b/src/main/java/net/openhft/chronicle/hash/serialization/impl/SerializationBuilder.java index bac685010..f3ba45435 100644 --- a/src/main/java/net/openhft/chronicle/hash/serialization/impl/SerializationBuilder.java +++ b/src/main/java/net/openhft/chronicle/hash/serialization/impl/SerializationBuilder.java @@ -82,13 +82,13 @@ public final class SerializationBuilder implements Cloneable { sizeMarshaller(constant(((Byteable) OS.memory().allocateInstance(tClass)).maxSize())); } else if (tClass == CharSequence.class) { reader((SizedReader) CharSequenceSizedReader.INSTANCE); - writer((SizedWriter) CharSequenceSizedWriter.INSTANCE); + dataAccess((DataAccess) new CharSequenceUtf8DataAccess()); } else if (tClass == StringBuilder.class) { reader((SizedReader) StringBuilderSizedReader.INSTANCE); - writer((SizedWriter) CharSequenceSizedWriter.INSTANCE); + dataAccess((DataAccess) new StringBuilderUtf8DataAccess()); } else if (tClass == String.class) { reader((SizedReader) new StringSizedReader()); - notReusingWriter((SizedWriter) CharSequenceSizedWriter.INSTANCE); + dataAccess((DataAccess) new StringUtf8DataAccess()); } else if (tClass == Boolean.class) { reader((SizedReader) BooleanMarshaller.INSTANCE); notReusingWriter((SizedWriter) BooleanMarshaller.INSTANCE); diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/impl/StringBuilderUtf8DataAccess.java b/src/main/java/net/openhft/chronicle/hash/serialization/impl/StringBuilderUtf8DataAccess.java new file mode 100644 index 000000000..52bfd20c3 --- /dev/null +++ b/src/main/java/net/openhft/chronicle/hash/serialization/impl/StringBuilderUtf8DataAccess.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2015 higherfrequencytrading.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package net.openhft.chronicle.hash.serialization.impl; + +import net.openhft.chronicle.hash.serialization.DataAccess; +import org.jetbrains.annotations.Nullable; + +public final class StringBuilderUtf8DataAccess + extends AbstractCharSequenceUtf8DataAccess { + + @Override + public StringBuilder getUsing(@Nullable StringBuilder using) { + if (using != null) { + using.setLength(0); + } else { + using = new StringBuilder(cs.length()); + } + using.append(cs); + return using; + } + + @Override + public DataAccess copy() { + return new StringBuilderUtf8DataAccess(); + } +} diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/impl/StringBytesReader.java b/src/main/java/net/openhft/chronicle/hash/serialization/impl/StringBytesReader.java index 5392d077a..7a3677212 100644 --- a/src/main/java/net/openhft/chronicle/hash/serialization/impl/StringBytesReader.java +++ b/src/main/java/net/openhft/chronicle/hash/serialization/impl/StringBytesReader.java @@ -27,8 +27,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** - * {@link BytesReader} implementation for String, for the primary ChronicleMap's key of value type - * {@link StringSizedReader} + {@link CharSequenceSizedWriter} are more efficient (because don't + * {@link BytesReader} implementation for String, for the primary ChronicleMap's key or value type + * {@link StringSizedReader} + {@link StringUtf8DataAccess} are more efficient (because don't * store the size twice), so this reader is useful in conjunction with {@link ListMarshaller} or * {@link SetMarshaller}. * diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/impl/StringUtf8DataAccess.java b/src/main/java/net/openhft/chronicle/hash/serialization/impl/StringUtf8DataAccess.java new file mode 100644 index 000000000..a62232eca --- /dev/null +++ b/src/main/java/net/openhft/chronicle/hash/serialization/impl/StringUtf8DataAccess.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2015 higherfrequencytrading.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package net.openhft.chronicle.hash.serialization.impl; + +import net.openhft.chronicle.hash.serialization.DataAccess; +import org.jetbrains.annotations.Nullable; + +public final class StringUtf8DataAccess extends AbstractCharSequenceUtf8DataAccess { + + @Override + public String getUsing(@Nullable String using) { + return cs; + } + + @Override + public DataAccess copy() { + return new StringUtf8DataAccess(); + } +}