package com.googlecode.cqengine.testutil;
import com.googlecode.cqengine.*;import com.googlecode.cqengine.attribute.*;import com.googlecode.cqengine.resultset.ResultSet;import java.util.Collections;import java.util.Map;import static com.googlecode.cqengine.query.QueryFactory.equal;
public class Person {
static final Attribute<Person, String> FIRSTNAME = new SimpleNullableAttribute<Person, String>() { @Override public String getValue(Person person) { return person.properties.get("PARAM_FIRSTNAME"); } };
final Map<String, String> properties = Collections.emptyMap(); // Might contain PARAM_FIRSTNAME -> Peter
public static void main(String[] args) { IndexedCollection<Person> people = CQEngine.newInstance(); ResultSet<Person> peopleWithFirstNamePeter = people.retrieve(equal(FIRSTNAME, "Peter")); }}package com.googlecode.cqengine.testutil;
import com.googlecode.cqengine.*;
import com.googlecode.cqengine.attribute.*;
import com.googlecode.cqengine.resultset.ResultSet;
import java.util.Collections;
import java.util.Map;
import static com.googlecode.cqengine.query.QueryFactory.equal;
public class Person {
public static Attribute<Person, String> dynamicAttribute(final String param) {
return new SimpleNullableAttribute<Person, String>() {
@Override
public String getValue(Person person) {
return person.properties.get(param);
}
};
}
final Map<String, String> properties = Collections.emptyMap(); // Might contain PARAM_FIRSTNAME -> Peter
public static void main(String[] args) {
IndexedCollection<Person> people = CQEngine.newInstance();
ResultSet<Person> peopleWithFirstNamePeter = people.retrieve(equal(dynamicAttribute("PARAM_FIRSTNAME"), "Peter"));
}
}If you know the full set of possible param names, then iterate them and add an index for each of the dynamic attributes.
Sent from my Android
--
-- You received this message because you are subscribed to the "cqengine-discuss" group.
http://groups.google.com/group/cqengine-discuss
---
You received this message because you are subscribed to the Google Groups "cqengine-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cqengine-discu...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You can do the same for other dynamic attributes too if you wish.
After some further test - i found that yes the search does not work but it still works for 1 indexed param.If i index 2 params - one search for param 1 will never return any results and one search for param 2 will return the expected results.
--
import org.apache.commons.lang.builder.EqualsBuilder;import org.apache.commons.lang.builder.HashCodeBuilder;
import com.googlecode.cqengine.attribute.SimpleNullableAttribute;
// workaround for default SimpleNullableAttribute which has a collision if// you add multiple indexes of string, hence we need to overwrite equals and hashcodepublic abstract class MySimpleNullableAttribute<O, A> extends SimpleNullableAttribute<O, A> {
private String paramName = null; public MySimpleNullableAttribute(String paramName) { this.paramName = paramName; } public String getParamName() { return paramName; } public abstract A getValue(O object);
public int hashCode() { return new HashCodeBuilder(17, 31). // two randomly chosen prime numbers append(paramName). toHashCode(); }
public boolean equals(Object obj) { if (!(obj instanceof SimpleNullableAttribute)) return false; if (obj == this) return true;
MySimpleNullableAttribute<?, ?> rhs = (MySimpleNullableAttribute<?, ?>) obj; return new EqualsBuilder(). // if deriving: appendSuper(super.equals(obj)). append(paramName, rhs.getParamName()). isEquals(); }}
Enter code here..package com.googlecode.cqengine.testutil;
import com.googlecode.cqengine.*;
import com.googlecode.cqengine.attribute.*;
import com.googlecode.cqengine.resultset.ResultSet;
import java.util.Collections;
import java.util.Map;
import static com.googlecode.cqengine.query.QueryFactory.equal;
public class Person {
final Map<String, String> properties = Collections.emptyMap(); // Might contain PARAM_FIRSTNAME -> Peter
// Attributes....
public static Attribute<Person, String> dynamicAttribute(final String param) {
return new ParameterizedAttribute(param);
}
public static class ParameterizedAttribute extends SimpleNullableAttribute<Person, String> {
final String parameter;
public ParameterizedAttribute(String attributeName, String parameter) {
super(attributeName);
this.parameter = parameter;
}
public ParameterizedAttribute(String parameter) {
this.parameter = parameter;
}
@Override
public String getValue(Person person) {
return person.properties.get(parameter);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ParameterizedAttribute)) return false;
if (!super.equals(o)) return false;
ParameterizedAttribute that = (ParameterizedAttribute)o;
return parameter.equals(that.parameter);
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + parameter.hashCode();
return result;
}
}
// Usage...
public static void main(String[] args) {
IndexedCollection<Person> people = CQEngine.newInstance();
ResultSet<Person> peopleWithFirstNamePeter = people.retrieve(equal(dynamicAttribute("PARAM_FIRSTNAME"), "Peter"));
}
}.--
Hi Has,
getType() belongs to the attribute not to the objects in the collection. I expect that it might be called one or possibly a few times during query processing as CQEngine does that to look up indexes.
Sent from my Android
--
--