use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.
the class ClientMapTest method testBasicPredicate.
@Test
public void testBasicPredicate() {
IMap<String, String> map = createMap();
fillMap(map);
Collection collection = map.values(new SqlPredicate("this == value1"));
assertEquals("value1", collection.iterator().next());
Set<String> set = map.keySet(new SqlPredicate("this == value1"));
assertEquals("key1", set.iterator().next());
Set<Map.Entry<String, String>> set1 = map.entrySet(new SqlPredicate("this == value1"));
assertEquals("key1", set1.iterator().next().getKey());
assertEquals("value1", set1.iterator().next().getValue());
}
use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.
the class MapMBean method values.
@ManagedAnnotation(value = "values", operation = true)
public String values(String query) {
Collection coll;
if (query != null && !query.isEmpty()) {
Predicate predicate = new SqlPredicate(query);
coll = managedObject.values(predicate);
} else {
coll = managedObject.values();
}
StringBuilder buf = new StringBuilder();
if (coll.size() == 0) {
buf.append("Empty");
} else {
buf.append("[");
for (Object obj : coll) {
buf.append(obj);
buf.append(", ");
}
buf.replace(buf.length() - 1, buf.length(), "]");
}
return buf.toString();
}
use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.
the class QueryAdvancedTest method testQueryWithTTL.
@Test
@SuppressWarnings("deprecation")
public void testQueryWithTTL() throws Exception {
Config config = getConfig();
String mapName = "default";
config.getMapConfig(mapName).setTimeToLiveSeconds(5);
HazelcastInstance instance = createHazelcastInstance(config);
IMap<String, Employee> map = instance.getMap(mapName);
map.addIndex("name", false);
map.addIndex("age", false);
map.addIndex("active", true);
int passiveEmployees = 5;
int activeEmployees = 5;
int allEmployees = passiveEmployees + activeEmployees;
final CountDownLatch latch = new CountDownLatch(allEmployees);
map.addEntryListener(new EntryAdapter() {
@Override
public void entryEvicted(EntryEvent event) {
latch.countDown();
}
}, false);
for (int i = 0; i < activeEmployees; i++) {
Employee employee = new Employee("activeEmployee" + i, 60, true, i);
map.put("activeEmployee" + i, employee);
}
for (int i = 0; i < passiveEmployees; i++) {
Employee employee = new Employee("passiveEmployee" + i, 60, false, i);
map.put("passiveEmployee" + i, employee);
}
// check the query result before eviction
Collection values = map.values(new SqlPredicate("active"));
assertEquals(activeEmployees, values.size());
// wait until eviction is completed
assertOpenEventually(latch);
// check the query result after eviction
values = map.values(new SqlPredicate("active"));
assertEquals(0, values.size());
}
use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.
the class QueryAdvancedTest method testTwoMembersWithIndexesAndShutdown2.
@Test
public void testTwoMembersWithIndexesAndShutdown2() {
Config config = getConfig();
TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);
IMap<String, Employee> map = instance1.getMap("employees");
map.addIndex("name", false);
map.addIndex("age", true);
map.addIndex("active", false);
QueryBasicTest.doFunctionalQueryTest(map);
assertEquals(101, map.size());
instance1.getLifecycleService().shutdown();
map = instance2.getMap("employees");
assertEquals(101, map.size());
Set<Map.Entry<String, Employee>> entries = map.entrySet(new SqlPredicate("active and age=23"));
assertEquals(2, entries.size());
for (Map.Entry<String, Employee> entry : entries) {
Employee employee = entry.getValue();
assertEquals(employee.getAge(), 23);
assertTrue(employee.isActive());
}
}
use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.
the class QueryAdvancedTest method testUnknownPortableField_notCausesQueryException_withIndex.
@Test
public // see: https://github.com/hazelcast/hazelcast/issues/3927
void testUnknownPortableField_notCausesQueryException_withIndex() {
String mapName = "default";
Config config = getConfig();
config.getSerializationConfig().addPortableFactory(666, new PortableFactory() {
public Portable create(int classId) {
return new PortableEmployee();
}
});
config.getMapConfig(mapName).addMapIndexConfig(new MapIndexConfig("notExist", false)).addMapIndexConfig(new MapIndexConfig("n", false));
HazelcastInstance hazelcastInstance = createHazelcastInstance(config);
IMap<Integer, PortableEmployee> map = hazelcastInstance.getMap(mapName);
for (int i = 0; i < 5; i++) {
map.put(i, new PortableEmployee(i, "name_" + i));
}
Collection values = map.values(new SqlPredicate("n = name_2 OR notExist = name_0"));
assertEquals(1, values.size());
}
Aggregations