Search in sources :

Example 1 with SqlPredicate

use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.

the class MapMBean method entrySet.

@ManagedAnnotation(value = "entrySet", operation = true)
public String entrySet(String query) {
    Set<Map.Entry> entrySet;
    if (query != null && !query.isEmpty()) {
        Predicate predicate = new SqlPredicate(query);
        entrySet = managedObject.entrySet(predicate);
    } else {
        entrySet = managedObject.entrySet();
    }
    StringBuilder buf = new StringBuilder();
    if (entrySet.size() == 0) {
        buf.append("Empty");
    } else {
        buf.append("[");
        for (Map.Entry entry : entrySet) {
            buf.append("{key:");
            buf.append(entry.getKey());
            buf.append(", value:");
            buf.append(entry.getValue());
            buf.append("}, ");
        }
        buf.replace(buf.length() - 1, buf.length(), "]");
    }
    return buf.toString();
}
Also used : SqlPredicate(com.hazelcast.query.SqlPredicate) IMap(com.hazelcast.core.IMap) Map(java.util.Map) Predicate(com.hazelcast.query.Predicate) SqlPredicate(com.hazelcast.query.SqlPredicate)

Example 2 with SqlPredicate

use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.

the class MapListenerTest method main.

public static void main(String[] args) throws InterruptedException {
    // create Hazelcast instance
    Config config = new Config();
    config.setInstanceName("hz-maplistener");
    config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
    config.getNetworkConfig().getInterfaces().setInterfaces(Arrays.asList(new String[] { "127.0.0.1" }));
    HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
    IMap<String, Person> map = hz.getMap("map");
    MapListener listener = new AllListener();
    map.addEntryListener(listener, new SqlPredicate("age > " + AGE_THRESHOLD), true);
    MapRandomizer mapRandomizer = new MapRandomizer(map);
    Thread t = new Thread(mapRandomizer);
    t.start();
    // let it run for 1 minute
    Thread.sleep(60000);
    mapRandomizer.setRunning(false);
    // assertions
    assertCount(ENTRIES, ENTRIES_OBSERVED, "entries");
    assertCount(EXITS, EXITS_OBSERVED, "exits");
    // dumpMap(map);
    hz.shutdown();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapListener(com.hazelcast.map.listener.MapListener) Config(com.hazelcast.config.Config) SqlPredicate(com.hazelcast.query.SqlPredicate)

Example 3 with SqlPredicate

use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.

the class ClientMapBasicTest method testValues_withPredicate.

@Test
public void testValues_withPredicate() {
    int max = 27;
    IMap<Integer, String> map = client.getMap(randomString());
    Set<String> expected = new TreeSet<String>();
    for (int key = 0; key < max; key++) {
        String value = key + "value";
        map.put(key, value);
    }
    expected.add("4value");
    Collection<String> collection = map.values(new SqlPredicate("this == 4value"));
    Set<String> resultSet = new TreeSet<String>(collection);
    assertEquals(expected, resultSet);
}
Also used : TreeSet(java.util.TreeSet) SqlPredicate(com.hazelcast.query.SqlPredicate) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with SqlPredicate

use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.

the class ClientMapBasicTest method testEntrySet_withPredicate.

@Test
public void testEntrySet_withPredicate() {
    int max = 44;
    IMap<Integer, String> map = client.getMap(randomString());
    for (int key = 0; key < max; key++) {
        String value = key + "value";
        map.put(key, value);
    }
    Set<Map.Entry<Integer, String>> entrySet = map.entrySet(new SqlPredicate("this == 1value"));
    assertEquals(1, entrySet.size());
    Map.Entry<Integer, String> entry = entrySet.iterator().next();
    assertEquals(1, (int) entry.getKey());
    assertEquals("1value", entry.getValue());
}
Also used : SqlPredicate(com.hazelcast.query.SqlPredicate) HashMap(java.util.HashMap) Map(java.util.Map) IMap(com.hazelcast.core.IMap) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with SqlPredicate

use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.

the class ClientQueryCacheEventLostListenerTest method testListenerNotified_onEventLoss.

@Test
public void testListenerNotified_onEventLoss() throws Exception {
    int count = 30;
    String mapName = randomString();
    String queryCacheName = randomString();
    IMap<Integer, Integer> mapNode = node.getMap(mapName);
    HazelcastInstance client = factory.newHazelcastClient();
    IMap<Integer, Integer> mapClient = client.getMap(mapName);
    setTestSequencer(mapClient, 9);
    // expecting one lost event publication per partition.
    final CountDownLatch lostEventCount = new CountDownLatch(1);
    QueryCache queryCache = mapClient.getQueryCache(queryCacheName, new SqlPredicate("this > 20"), true);
    queryCache.addEntryListener(new EventLostListener() {

        @Override
        public void eventLost(EventLostEvent event) {
            lostEventCount.countDown();
        }
    }, false);
    for (int i = 0; i < count; i++) {
        mapNode.put(i, i);
    }
    assertOpenEventually(lostEventCount);
}
Also used : QueryCache(com.hazelcast.map.QueryCache) HazelcastInstance(com.hazelcast.core.HazelcastInstance) EventLostEvent(com.hazelcast.map.EventLostEvent) SqlPredicate(com.hazelcast.query.SqlPredicate) CountDownLatch(java.util.concurrent.CountDownLatch) EventLostListener(com.hazelcast.map.listener.EventLostListener) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

SqlPredicate (com.hazelcast.query.SqlPredicate)53 QuickTest (com.hazelcast.test.annotation.QuickTest)40 Test (org.junit.Test)40 ParallelTest (com.hazelcast.test.annotation.ParallelTest)35 Employee (com.hazelcast.mapreduce.helpers.Employee)28 HazelcastInstance (com.hazelcast.core.HazelcastInstance)26 IMap (com.hazelcast.core.IMap)5 Map (java.util.Map)5 AssertTask (com.hazelcast.test.AssertTask)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Config (com.hazelcast.config.Config)3 QueryCacheConfig (com.hazelcast.config.QueryCacheConfig)3 EventLostEvent (com.hazelcast.map.EventLostEvent)3 QueryCache (com.hazelcast.map.QueryCache)3 EventLostListener (com.hazelcast.map.listener.EventLostListener)3 Predicate (com.hazelcast.query.Predicate)3 ClientConfig (com.hazelcast.client.config.ClientConfig)2 TimeStep (com.hazelcast.simulator.test.annotations.TimeStep)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2