Chronicle Queue: SortedMap using Comparator?

53 views
Skip to first unread message

Andrew Oswald

unread,
Jun 14, 2016, 1:29:00 PM6/14/16
to Chronicle
Greetings.

I'd like to try and use a class which extends TreeMap<byte[], byte[]> as a parameter on an interface that CQ would proxy, however, I'm getting an IllegalArgumentException: argument type mismatch due to the SerializationStrategy providing a LinkedHashMap.

So, my question is this: in CQ V4, can I do something to register a custom serialization strategy for my class (or perhaps something altogether different)?  Please see code examples below.

public class MyByteArrayMap extends TreeMap<byte[], byte[]> {
   
public MyByteArrayMap() {
       
super(ByteArrayComparator);
   
}
}


public interface UsesMyByteArrayMap {
   
void use(MyByteArrayMap map);
}

Note: the actual "use" implementation has arity greater than just map.

I'm setting up thusly:

try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(queuePath).build()) {
    UsesMyByteArrayMap user = queue.createAppender().methodWriter(UsesMyByteArrayMap.class);
   
/* etc., etc. using the above user */
}

Thoughts?  Suggestions?

thanks!

Andrew Oswald

unread,
Jun 14, 2016, 2:01:24 PM6/14/16
to Chronicle
Oh, specifically, it's on the unmarshal (read) where it throws IllegalArgumentException.  Apologies for any confusion!

Peter Lawrey

unread,
Jun 14, 2016, 6:27:01 PM6/14/16
to java-ch...@googlegroups.com
It's not a use case we test for but I believe it could work, provided the comparator is also serializable. A challenge would be distiguishing field of the map itself with key/values of the map.

Could you provide a unit test for this, and we will investigate?

Regards,
   Peter.

--
You received this message because you are subscribed to the Google Groups "Chronicle" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-chronicl...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peter Lawrey

unread,
Jun 14, 2016, 6:27:40 PM6/14/16
to java-ch...@googlegroups.com
Out of interest, why are you using byte[] instead of objects?

Andrew Oswald

unread,
Jun 16, 2016, 1:00:08 PM6/16/16
to Chronicle
In this would-be lambda architecture, the data's pre-normalized state happens to be SortedMap<byte[], byte[]>.  Additionally, as it stands now, my "Control System" piece is expecting to be able to use subMap views of it.  I could go ahead and create a DTO that encapsulates those views in ways more conducive to CQ.  That be something you'd recommend?

thanks!

Andrew Oswald

unread,
Jun 16, 2016, 2:13:31 PM6/16/16
to Chronicle
Hopefully quick question:  if I were to go the DTO route, what's the best way to represent a lone byte[]?  Please see below:

public class MyDTO extends AbstractMarshallable {
   
private ??? myByteArray; // what's the best way to represent byte[] in v4 CQ?
}

thanks!

Rob Austin

unread,
Jun 16, 2016, 6:24:11 PM6/16/16
to java-ch...@googlegroups.com
try something like this - see these test cases net.openhft.chronicle.wire.BinaryWire2Test#testByteArrayValueWithRealBytesNegative

@Test
public void testByteArrayValueWithRealBytesNegative() {
Wire wire = createWire();

final byte[] expected = {-1, -2, -3, -4, -5, -6, -7};
wire.writeDocument(false, wir -> wir.writeEventName(() -> "put")
.marshallable(w -> w.write("key").text("1")
.write("value")
.object(expected)));
System.out.println(wire);

wire.readDocument(null, wir -> wire.read(() -> "put")
.marshallable(w -> w.read(() -> "key").object(Object.class, "1", Assert::assertEquals)
.read(() -> "value").object(Object.class, expected, (e, v) -> {
Assert.assertArrayEquals(e, (byte[]) v);
})));
}

@Test
public void testBytesArray() {
Wire wire = createWire();
Random rand = new Random();
for (int i = 0; i < 70000; i += rand.nextInt(i + 1) + 1) {
System.out.println(i);
wire.clear();
final byte[] fromBytes = new byte[i];
wire.writeDocument(false, w -> w.write("bytes").bytes(fromBytes));
Wires.fromSizePrefixedBlobs(wire);
int finalI = i;
wire.readDocument(null, w -> assertEquals(finalI, w.read(() -> "bytes").bytes().length));
}
}

@Test
public void testSmallArray() {
Wire wire = createWire();
wire.writeDocument(false, w -> w.write("index")
.int64array(10));
assertEquals("--- !!data #binary\n" +
"index: [\n" +
" # length: 10, used: 0\n" +
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0\n" +
"]\n", Wires.fromSizePrefixedBlobs(wire.bytes()));
}
Reply all
Reply to author
Forward
0 new messages