use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.
the class QueryIndexTest method testPredicateNotEqualWithIndex.
private void testPredicateNotEqualWithIndex(IMap<Integer, Value> map, boolean ordered) {
map.addIndex("name", ordered);
map.put(1, new Value("abc", 1));
map.put(2, new Value("xyz", 2));
map.put(3, new Value("aaa", 3));
assertEquals(3, map.values(new SqlPredicate("name != 'aac'")).size());
assertEquals(2, map.values(new SqlPredicate("index != 2")).size());
assertEquals(3, map.values(new SqlPredicate("name <> 'aac'")).size());
assertEquals(2, map.values(new SqlPredicate("index <> 2")).size());
assertEquals(3, map.values(new PredicateBuilder().getEntryObject().get("name").notEqual("aac")).size());
assertEquals(2, map.values(new PredicateBuilder().getEntryObject().get("index").notEqual(2)).size());
}
use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.
the class QueryBasicTest method testSqlQueryUsingNested__KeyField.
@Test
public void testSqlQueryUsingNested__KeyField() {
Config config = getConfig();
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
factory.newHazelcastInstance(config);
HazelcastInstance instance = factory.newHazelcastInstance(config);
IMap<Object, Object> map = instance.getMap(randomMapName());
Object key = new CustomAttribute(12, 123L);
Object value = "value";
map.put(key, value);
Collection<Object> values = map.values(new SqlPredicate("__key.age = 12 and __key.height = 123"));
assertEquals(1, values.size());
assertEquals(value, values.iterator().next());
}
use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.
the class QueryBasicTest method doFunctionalQueryTest.
public static void doFunctionalQueryTest(IMap<String, Employee> map) {
map.put("1", new Employee("joe", 33, false, 14.56));
map.put("2", new Employee("ali", 23, true, 15.00));
for (int i = 3; i < 103; i++) {
map.put(String.valueOf(i), new Employee("name" + i, i % 60, ((i & 1) == 1), i));
}
Set<Map.Entry<String, Employee>> entries = map.entrySet();
assertEquals(102, entries.size());
int entryCount = 0;
for (Map.Entry entry : entries) {
Employee employee = (Employee) entry.getValue();
assertNotNull(employee);
entryCount++;
}
assertEquals(102, entryCount);
EntryObject entryObject = new PredicateBuilder().getEntryObject();
Predicate predicate = entryObject.is("active").and(entryObject.get("age").equal(23));
entries = map.entrySet(predicate);
for (Map.Entry entry : entries) {
Employee employee = (Employee) entry.getValue();
assertEquals(employee.getAge(), 23);
assertTrue(employee.isActive());
}
map.remove("2");
entries = map.entrySet(predicate);
assertEquals(2, entries.size());
for (Map.Entry entry : entries) {
Employee employee = (Employee) entry.getValue();
assertEquals(employee.getAge(), 23);
assertTrue(employee.isActive());
}
entries = map.entrySet(new SqlPredicate(" (age >= " + 30 + ") AND (age <= " + 40 + ")"));
assertEquals(23, entries.size());
for (Map.Entry entry : entries) {
Employee employee = (Employee) entry.getValue();
assertTrue(employee.getAge() >= 30);
assertTrue(employee.getAge() <= 40);
}
}
use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.
the class QueryBasicTest method testInvalidSqlPredicate.
@Test(timeout = 1000 * 60)
public void testInvalidSqlPredicate() {
Config cfg = getConfig();
TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(1);
HazelcastInstance instance = nodeFactory.newHazelcastInstance(cfg);
IMap<Integer, Employee> map = instance.getMap("employee");
map.put(1, new Employee("e", 1, false, 0));
map.put(2, new Employee("e2", 1, false, 0));
try {
map.values(new SqlPredicate("invalid_sql"));
fail("Should fail because of invalid SQL!");
} catch (RuntimeException e) {
assertContains(e.getMessage(), "There is no suitable accessor for 'invalid_sql'");
}
try {
map.values(new SqlPredicate("invalid sql"));
fail("Should fail because of invalid SQL!");
} catch (RuntimeException e) {
assertContains(e.getMessage(), "Invalid SQL: [invalid sql]");
}
try {
map.values(new SqlPredicate("invalid and sql"));
fail("Should fail because of invalid SQL!");
} catch (RuntimeException e) {
assertContains(e.getMessage(), "There is no suitable accessor for 'invalid'");
}
try {
map.values(new SqlPredicate("invalid sql and"));
fail("Should fail because of invalid SQL!");
} catch (RuntimeException e) {
assertContains(e.getMessage(), "There is no suitable accessor for 'invalid'");
}
try {
map.values(new SqlPredicate(""));
fail("Should fail because of invalid SQL!");
} catch (RuntimeException e) {
assertContains(e.getMessage(), "Invalid SQL: []");
}
assertEquals(2, map.values(new SqlPredicate("age=1 and name like 'e%'")).size());
}
use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.
the class QueryBasicTest method testSqlQueryUsing__KeyField.
@Test
public void testSqlQueryUsing__KeyField() {
Config config = getConfig();
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
HazelcastInstance instance1 = factory.newHazelcastInstance(config);
HazelcastInstance instance2 = factory.newHazelcastInstance(config);
IMap<Object, Object> map = instance2.getMap(randomMapName());
Object key = generateKeyOwnedBy(instance1);
Object value = "value";
map.put(key, value);
Collection<Object> values = map.values(new SqlPredicate("__key = '" + key + "'"));
assertEquals(1, values.size());
assertEquals(value, values.iterator().next());
}
Aggregations