1

I have the following map definition where map.containsKey() clearly doesn't work:

ChronicleMapBuilder<String, String> cmBuilder = ChronicleMapBuilder.of(String.class, String.class);
cmBuilder.constantKeySizeBySample("abcde")
        .averageValueSize(128)
        .entries(1024)
        .keyMarshaller(new MyStringMarshaller())
        .valueMarshaller(new MyStringMarshaller());
Map myMap = cmBuilder.createPersistedTo(new File("/tmp/test-map"));
myMap.put("abc", "123");
System.out.println(myMap.containsKey("abc")); // prints false

...

public static class MyStringMarshaller implements BytesMarshaller<String> {
    @Override
    public void write(Bytes bytes, String str) {
        bytes.writeUTF(str);
    }

    @Override
    public String read(Bytes bytes) {
        return read(bytes, null);
    }

    @Override
    public String read(Bytes bytes, String str) {
        return bytes.readUTF();
    }
}

I use Chronicle Map 2.4.17 and it's too difficult to migrate to the version 3 in my project.

Right after putting the entry with the key "abc", the method containsKey() returns false for the key "abc". I'm confused why it wouldn't work. The String type is the default Java string that has correct hashCode() method.

Soid
  • 2,585
  • 1
  • 30
  • 42

1 Answers1

1

You don't have to use a keyMarshaller if you just want to create a chronicle map containing a string. But since this is the question that you are asking, I suggest you replace your MyStringMarshaller with an instance of StringMarshaller, to see the source code for this see https://github.com/OpenHFT/Java-Lang/blob/master/lang/src/main/java/net/openhft/lang/io/serialization/impl/StringMarshaller.java

Rob Austin
  • 620
  • 3
  • 5
  • 1
    are you just looking to create a ChronicleMap because if this is all you want to do, there are simpler ways to build the map, which I can show you, or do you have to use the KeyMarshaller for another reason – Rob Austin Feb 16 '17 at 11:44
  • I found that if I don't use constantKeySizeBySample() and averageValueSize() than it works. It might be related to this issue that's been open for a year for CM 2.x: https://github.com/OpenHFT/Chronicle-Map/issues/86 – Soid Feb 21 '17 at 19:10