0

I am using oficial Riak Java client v2.0.2. When I update previously written value (with 2i indexes), the secondary indexes are not preserved.

This is how I do update:

Location location = new Location(this.namespace, key);
UpdateValue updateOp = new UpdateValue.Builder(location)
        .withFetchOption(FetchValue.Option.DELETED_VCLOCK, true)
        .withUpdate(new RiakKVUpdateValue(values))
        .build();

And this is my update class:

public class RiakKVUpdateValue extends Update<Map<String, String>> {

    private final Map<String, String> value;

    public RiakKVUpdateValue(HashMap<String, ByteIterator> values) {
        this.value = StringByteIterator.getStringMap(values);
    }

    @Override
    public Map<String, String> apply(Map<String, String> original) {
        return this.value;
    }
}

I haven't found anything in the docs about updating objects with 2i indexes.

  • Am I doing someting wrong?
  • Should I do manual Read/Modify/Write?
NefariousOctopus
  • 807
  • 2
  • 10
  • 18

1 Answers1

3

You have to fetch the index and write it back every time you update the value. See 2i Indexing an Object.

I would suggest to create a field to hold the index and annotate it with @RiakIndex. A field annotated with this annotation is populated with 2i values automatically by the Java client when fetched. Then, copy its value in RiakKVUpdateValue.apply() to retain it. Alternatively, fetch and then write back in two separate commands as you already mentioned. This will allow you to control metadata you want to write back. Don't forget to populate the VClocks.

P.S. Retaining 2i automatically can be a bad idea since it's not obvious that a user will want to keep old 2i values. I believe that's why it is left up to the user to decide.

vempo
  • 3,093
  • 1
  • 14
  • 16