Search in sources :

Example 41 with Employee

use of com.hazelcast.query.SampleObjects.Employee 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());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.query.SampleObjects.Employee) MapConfig(com.hazelcast.config.MapConfig) Config(com.hazelcast.config.Config) MapIndexConfig(com.hazelcast.config.MapIndexConfig) SqlPredicate(com.hazelcast.query.SqlPredicate) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 42 with Employee

use of com.hazelcast.query.SampleObjects.Employee in project hazelcast by hazelcast.

the class QueryBasicTest method doFunctionalSQLQueryTest.

public static void doFunctionalSQLQueryTest(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);
    entries = map.entrySet(new SqlPredicate("active=true and age=23"));
    assertEquals(3, entries.size());
    for (Map.Entry entry : entries) {
        Employee employee = (Employee) entry.getValue();
        assertEquals(employee.getAge(), 23);
        assertTrue(employee.isActive());
    }
    map.remove("2");
    entries = map.entrySet(new SqlPredicate("active=true and age=23"));
    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!=33"));
    for (Map.Entry entry : entries) {
        Employee employee = (Employee) entry.getValue();
        assertTrue(employee.getAge() != 33);
    }
    entries = map.entrySet(new SqlPredicate("active!=false"));
    for (Map.Entry entry : entries) {
        Employee employee = (Employee) entry.getValue();
        assertTrue(employee.isActive());
    }
}
Also used : Employee(com.hazelcast.query.SampleObjects.Employee) SqlPredicate(com.hazelcast.query.SqlPredicate) Map(java.util.Map) IMap(com.hazelcast.core.IMap)

Example 43 with Employee

use of com.hazelcast.query.SampleObjects.Employee in project hazelcast by hazelcast.

the class MapTransactionTest method testValues_withPagingPredicate.

@Test(expected = IllegalArgumentException.class)
public void testValues_withPagingPredicate() throws TransactionException {
    final int nodeCount = 1;
    final String mapName = randomMapName("testValuesWithPagingPredicate");
    final Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(nodeCount);
    final HazelcastInstance node = factory.newHazelcastInstance(config);
    final IMap<Integer, Employee> map = node.getMap(mapName);
    final Employee emp = new Employee("name", 77, true, 10D);
    map.put(1, emp);
    node.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            final TransactionalMap<Integer, Employee> txMap = context.getMap(mapName);
            PagingPredicate<Integer, Employee> predicate = new PagingPredicate<Integer, Employee>(5);
            txMap.values(predicate);
            return true;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) PagingPredicate(com.hazelcast.query.PagingPredicate) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.query.SampleObjects.Employee) TransactionException(com.hazelcast.transaction.TransactionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 44 with Employee

use of com.hazelcast.query.SampleObjects.Employee in project hazelcast by hazelcast.

the class MapTransactionTest method testValues_shouldNotDeduplicateEntriesWhenGettingByPredicate.

@Test
public void testValues_shouldNotDeduplicateEntriesWhenGettingByPredicate() throws TransactionException {
    final int nodeCount = 1;
    final String mapName = randomMapName();
    final Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(nodeCount);
    final HazelcastInstance node = factory.newHazelcastInstance(config);
    final IMap map = node.getMap(mapName);
    final Employee emp = new Employee("name", 77, true, 10D);
    map.put(1, emp);
    node.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            final TransactionalMap<Integer, Employee> txMap = context.getMap(mapName);
            txMap.put(2, emp);
            Collection<Employee> coll = txMap.values(new SqlPredicate("age = 77"));
            assertEquals(2, coll.size());
            return true;
        }
    });
    node.shutdown();
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) SqlPredicate(com.hazelcast.query.SqlPredicate) IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.query.SampleObjects.Employee) TransactionException(com.hazelcast.transaction.TransactionException) Collection(java.util.Collection) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 45 with Employee

use of com.hazelcast.query.SampleObjects.Employee in project hazelcast by hazelcast.

the class IndexesTest method testAndWithSingleEntry.

@Test
public void testAndWithSingleEntry() throws Exception {
    Indexes indexes = new Indexes(serializationService, Extractors.empty());
    indexes.addOrGetIndex("name", false);
    indexes.addOrGetIndex("age", true);
    indexes.addOrGetIndex("salary", true);
    for (int i = 0; i < 100; i++) {
        Employee employee = new Employee(i + "Name", i % 80, (i % 2 == 0), 100 + (i % 1000));
        indexes.saveEntryIndex(new QueryEntry(serializationService, toData(i), employee, Extractors.empty()), null);
    }
    int count = 10;
    Set<String> ages = new HashSet<String>(count);
    for (int i = 0; i < count; i++) {
        ages.add(String.valueOf(i));
    }
    EntryObject entryObject = new PredicateBuilder().getEntryObject();
    PredicateBuilder predicate = entryObject.get("name").equal("0Name").and(entryObject.get("age").in(ages.toArray(new String[count])));
    Set<QueryableEntry> results = indexes.query(predicate);
    assertEquals(1, results.size());
}
Also used : Employee(com.hazelcast.query.SampleObjects.Employee) EntryObject(com.hazelcast.query.EntryObject) PredicateBuilder(com.hazelcast.query.PredicateBuilder) HashSet(java.util.HashSet) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

Employee (com.hazelcast.query.SampleObjects.Employee)49 Test (org.junit.Test)41 QuickTest (com.hazelcast.test.annotation.QuickTest)38 HazelcastInstance (com.hazelcast.core.HazelcastInstance)36 ParallelTest (com.hazelcast.test.annotation.ParallelTest)35 Config (com.hazelcast.config.Config)28 SqlPredicate (com.hazelcast.query.SqlPredicate)23 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)21 MapIndexConfig (com.hazelcast.config.MapIndexConfig)18 MapStoreConfig (com.hazelcast.config.MapStoreConfig)17 PortableEmployee (com.hazelcast.query.SampleObjects.PortableEmployee)14 IMap (com.hazelcast.core.IMap)11 MapConfig (com.hazelcast.config.MapConfig)10 Collection (java.util.Collection)8 Map (java.util.Map)8 EntryObject (com.hazelcast.query.EntryObject)6 Predicate (com.hazelcast.query.Predicate)6 PredicateBuilder (com.hazelcast.query.PredicateBuilder)6 HashMap (java.util.HashMap)6 TransactionalMap (com.hazelcast.core.TransactionalMap)5