/* * 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; import net.openhft.chronicle.bytes.Bytes; import net.openhft.chronicle.wire.WireIn; import net.openhft.chronicle.wire.WireOut; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.IOException; import java.io.ObjectInputStream; import java.util.*; import static net.openhft.chronicle.hash.serialization.StatefulCopyable.copyIfNeeded; /** * Marshaller of {@code Set}. Uses {@link HashSet} (hence default element objects' equality and * {@code hashCode()}) as the set implementation to deserialize into. * *

This marshaller supports multimap emulation on top of Chronicle Map, that is possible but * inefficient. See the * README section. * *

Usage:

{@code
 * SetMarshaller valueMarshaller = new SetMarshaller<>(
 *     new StringBytesReader(), CharSequenceBytesWriter.INSTANCE);
 * ChronicleMap> map = ChronicleMap
 *     .of(String.class, (Class>) (Class) Set.class)
 *     .entries(10_000)
 *     .averageKey("fruits")
 *     .valueMarshaller(valueMarshaller)
 *     .averageValue(ImmutableSet.of("apples", "bananas", "grapes"))
 *     .create();
 * }
* *

Look for pre-defined element marshallers in {@link * net.openhft.chronicle.hash.serialization.impl} package. This package is not included into * Javadocs, but present in Chronicle Map distribution. If there are no existing marshallers for * your {@code Set} element type, define {@link BytesReader} and {@link BytesWriter} yourself. * * @param the element type of serialized Sets * @see ListMarshaller * @see MapMarshaller */ public final class SetMarshaller implements BytesReader>, BytesWriter>, StatefulCopyable> { // Config fields private BytesReader elementReader; private BytesWriter elementWriter; /** Cache field */ private transient Deque orderedElements; public SetMarshaller(BytesReader elementReader, BytesWriter elementWriter) { this.elementReader = elementReader; this.elementWriter = elementWriter; initTransients(); } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); initTransients(); } private void initTransients() { orderedElements = new ArrayDeque<>(); } @NotNull @Override public Set read(Bytes in, @Nullable Set using) { int size = in.readInt(); if (using == null) { using = new HashSet<>((int) (size / 0.75)); for (int i = 0; i < size; i++) { using.add(elementReader.read(in, null)); } } else { orderedElements.addAll(using); using.clear(); for (int i = 0; i < size; i++) { using.add(elementReader.read(in, orderedElements.pollFirst())); } orderedElements.clear(); // for GC, avoid zombie object links } return using; } @Override public void write(Bytes out, @NotNull Set toWrite) { out.writeInt(toWrite.size()); toWrite.forEach(e -> elementWriter.write(out, e)); } @Override public SetMarshaller copy() { return new SetMarshaller<>(copyIfNeeded(elementReader), copyIfNeeded(elementWriter)); } @Override public void readMarshallable(@NotNull WireIn wireIn) { elementReader = wireIn.read(() -> "elementReader").typedMarshallable(); elementWriter = wireIn.read(() -> "elementWriter").typedMarshallable(); initTransients(); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { wireOut.write(() -> "elementReader").typedMarshallable(elementReader); wireOut.write(() -> "elementWriter").typedMarshallable(elementWriter); } }