0

How do you find if a property that is an array contains a value, what is available by default to Xodus is txn.find(entityType, propertyName, comparableValue) to find a value, however, what is the best way to find if an array property contains a value. Suppose I have this:

public class EmbeddedArrayIterable implements Serializable, ByteIterable {
}

In which I need to compare if a given value, example "cat", is inside the array. What should be the implementation of EmbeddedArrayIterable as such it will be able to return the Entity to which this "cat" string is in the array of the entity.

As such we can do:

// This property is a `EmbeddedArrayIterable` 
// which is a serialized JSON like `["cat","tiger","lion"]`
String propertyName = "keywords"; 
String comparableValue = "cat";
EntityIterable cats = txn.find(entityType, propertyName, comparableValue);
quarks
  • 33,478
  • 73
  • 290
  • 513

1 Answers1

1

If you define a custom property type, then you cannot find an entity by only a part of its value. In your case, it makes sense to use ComparableSet value type provided out-of-the-box. Set the property value as a set of items:

final ComparableSet<String> propValue = new ComparableSet<>();
propValue.addItem("cat");
propValue.addItem("tiger");
propValue.addItem("lion");
entity.setProperty("keywords", propValue);

You can then get the value using Entity#getProperty(..), cast to ComparableSet<String> and modify the set if necessary. You can also find the entity by only a single item of the set:

EntityIterable cats = txn.find(entityType, "keywords", "cat");
Vyacheslav Lukianov
  • 1,913
  • 8
  • 12