寻找实体内数组属性中的可比值

huangapple go评论62阅读模式
英文:

Find a comparable value inside an array property within an Entity

问题

如何判断一个作为数组的属性是否包含某个值,在Xodus中,默认提供了txn.find(entityType, propertyName, comparableValue)来查找值,然而,要找出数组属性是否包含某个值,最佳方法是什么呢?假设我有以下代码:

public class EmbeddedArrayIterable implements Serializable, ByteIterable {
}

我需要比较给定的值,例如"cat",是否在数组内。应该如何实现EmbeddedArrayIterable,使其能够返回包含这个"cat"字符串的实体呢?

因此,我们可以进行如下操作:

// 这个属性是一个`EmbeddedArrayIterable`
// 它是一个类似于序列化JSON的数组,内容为["cat", "tiger", "lion"]
String propertyName = "keywords";
String comparableValue = "cat";
EntityIterable cats = txn.find(entityType, propertyName, comparableValue);
英文:

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);

答案1

得分: 1

如果您定义了自定义属性类型,那么您不能仅通过其值的一部分来查找实体。在您的情况下,使用开箱即用的 ComparableSet 值类型是有意义的。将属性值设置为一组项目:

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

然后您可以使用 Entity#getProperty(..) 获取该值,将其转换为 ComparableSet<String> 并在需要时修改集合。您还可以仅通过集合中的单个项来查找实体:

EntityIterable cats = txn.find(entityType, "keywords", "cat");
英文:

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&lt;String&gt; propValue = new ComparableSet&lt;&gt;();
propValue.addItem(&quot;cat&quot;);
propValue.addItem(&quot;tiger&quot;);
propValue.addItem(&quot;lion&quot;);
entity.setProperty(&quot;keywords&quot;, propValue);

You can then get the value using Entity#getProperty(..), cast to ComparableSet&lt;String&gt; 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, &quot;keywords&quot;, &quot;cat&quot;);

huangapple
  • 本文由 发表于 2020年10月17日 03:46:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64395507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定