Search in sources :

Example 16 with Predicate

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

the class BetweenVisitorTest method whenPredicateIsExclusive_thenIsNotUsedToBuildBetween.

@Test
public void whenPredicateIsExclusive_thenIsNotUsedToBuildBetween() {
    //(age >= 5 and age < 6)  -->  (age >= 5 and age < 6)
    Predicate left = greaterThan("attribute", 5);
    Predicate right = lessEqual("attribute", 6);
    Predicate and = and(left, right);
    Predicate result = visitor.visit((AndPredicate) and, mockIndexes);
    assertSame(and, result);
}
Also used : Predicate(com.hazelcast.query.Predicate) FalsePredicate(com.hazelcast.query.impl.FalsePredicate) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 17 with Predicate

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

the class BetweenVisitorTest method whenPredicatesOtherThenGreatLess_thenDoNotAttemptToEliminateThem.

@Test
public void whenPredicatesOtherThenGreatLess_thenDoNotAttemptToEliminateThem() {
    //(age >= 5 and age <= 6 and age <> 4)  -->  ( (age between 5 6) and (age <> 5) )
    Predicate left = greaterEqual("attribute", 5);
    Predicate right = lessEqual("attribute", 6);
    Predicate other = notEqual("attribute", 4);
    Predicate and = and(left, right, other);
    AndPredicate result = (AndPredicate) visitor.visit((AndPredicate) and, mockIndexes);
    Predicate[] inners = result.predicates;
    assertThat(inners, hasItemInArray(other));
    BetweenPredicate betweenPredicate = findFirstBetweenPredicate(inners);
    assertBetweenPredicate(betweenPredicate, 5, 6);
}
Also used : Predicate(com.hazelcast.query.Predicate) FalsePredicate(com.hazelcast.query.impl.FalsePredicate) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 18 with Predicate

use of com.hazelcast.query.Predicate in project microservices by pwillhan.

the class AverageCityPopulationCallable method call.

@Override
public Integer call() throws Exception {
    System.err.println("Running task on: " + hz.getCluster().getLocalMember().toString());
    IMap<CityKey, City> cities = hz.getMap("cities");
    Predicate countryCityPredicate = Predicates.equal("country", country);
    Collection<City> countryCities = cities.values(countryCityPredicate);
    int totalPopulation = 0;
    for (City countryCity : countryCities) {
        totalPopulation += countryCity.getPopulation();
    }
    return totalPopulation / countryCities.size();
}
Also used : City(data.City) CityKey(data.CityKey) Predicate(com.hazelcast.query.Predicate)

Example 19 with Predicate

use of com.hazelcast.query.Predicate in project microservices by pwillhan.

the class AggregationCountryCityPopulation method main.

public static void main(String[] args) throws Exception {
    Config conf = new Config();
    HazelcastInstance hz = Hazelcast.newHazelcastInstance(conf);
    IMap<CityKey, City> cities = hz.getMap("cities");
    if (cities.isEmpty()) {
        cities.put(new CityKey("London", "GB"), new City("London", "GB", 8416535, 1572));
        cities.put(new CityKey("Southampton", "GB"), new City("Southampton", "GB", 242100, 51));
        cities.put(new CityKey("Chicago", "US"), new City("Chicago", "US", 2718782, 606));
        cities.put(new CityKey("Washington DC", "US"), new City("Washington DC", "US", 658893, 177));
        cities.put(new CityKey("Seattle", "US"), new City("Seattle", "US", 652405, 370));
    }
    Supplier<CityKey, City, Integer> gbCityPopulation6 = Supplier.fromPredicate(new Predicate<CityKey, City>() {

        @Override
        public boolean apply(Map.Entry<CityKey, City> entry) {
            return "GB".equals(entry.getValue().getCountry());
        }
    }, Supplier.<CityKey, City, Integer>all(new PropertyExtractor<City, Integer>() {

        @Override
        public Integer extract(City city) {
            return city.getPopulation();
        }
    }));
    int result6 = cities.aggregate(gbCityPopulation6, Aggregations.<CityKey, City>integerSum());
    System.err.println(result6);
    Supplier<CityKey, City, Integer> gbCityPopulation8 = Supplier.fromPredicate(entry -> "GB".equals(entry.getValue().getCountry()), Supplier.all(city -> city.getPopulation()));
    int result8 = cities.aggregate(gbCityPopulation8, Aggregations.integerSum());
    System.err.println(result8);
}
Also used : CityKey(data.CityKey) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) City(data.City) Supplier(com.hazelcast.mapreduce.aggregation.Supplier) IMap(com.hazelcast.core.IMap) Hazelcast(com.hazelcast.core.Hazelcast) PropertyExtractor(com.hazelcast.mapreduce.aggregation.PropertyExtractor) Aggregations(com.hazelcast.mapreduce.aggregation.Aggregations) Map(java.util.Map) Predicate(com.hazelcast.query.Predicate) Config(com.hazelcast.config.Config) City(data.City) HazelcastInstance(com.hazelcast.core.HazelcastInstance) PropertyExtractor(com.hazelcast.mapreduce.aggregation.PropertyExtractor) CityKey(data.CityKey) IMap(com.hazelcast.core.IMap) Map(java.util.Map)

Example 20 with Predicate

use of com.hazelcast.query.Predicate in project microservices by pwillhan.

the class MapSearchPredicateExample method main.

public static void main(String[] args) {
    HazelcastInstance hz = Hazelcast.newHazelcastInstance();
    IMap<String, City> capitals = hz.getMap("capitals");
    capitals.addIndex("name", false);
    capitals.addIndex("population", true);
    capitals.put("GB", new City("London", "GB", 8174100));
    capitals.put("FR", new City("Paris", "FR", 2268265));
    capitals.put("US", new City("Washington DC", "US", 601723));
    capitals.put("AU", new City("Canberra", "AU", 354644));
    EntryObject c = new PredicateBuilder().getEntryObject();
    Predicate londonPredicate = c.get("name").equal("London");
    Collection<City> possibleLondons = capitals.values(londonPredicate);
    System.err.println(possibleLondons);
    Predicate largeCityPredicate = Predicates.greaterThan("population", 1000000);
    Collection<City> largeCities = capitals.values(largeCityPredicate);
    System.err.println(largeCities);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) EntryObject(com.hazelcast.query.EntryObject) PredicateBuilder(com.hazelcast.query.PredicateBuilder) City(data.City) Predicate(com.hazelcast.query.Predicate)

Aggregations

Predicate (com.hazelcast.query.Predicate)248 Test (org.junit.Test)165 QuickTest (com.hazelcast.test.annotation.QuickTest)159 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)105 HazelcastInstance (com.hazelcast.core.HazelcastInstance)38 MapListener (com.hazelcast.map.listener.MapListener)17 ParallelTest (com.hazelcast.test.annotation.ParallelTest)17 EntryListener (com.hazelcast.core.EntryListener)16 FalsePredicate (com.hazelcast.query.impl.FalsePredicate)15 ArrayList (java.util.ArrayList)15 EntryObject (com.hazelcast.query.PredicateBuilder.EntryObject)14 QueryableEntry (com.hazelcast.query.impl.QueryableEntry)14 Value (com.hazelcast.query.SampleTestObjects.Value)10 SqlPredicate (com.hazelcast.query.SqlPredicate)10 Config (com.hazelcast.config.Config)9 PredicateTestUtils.createMockVisitablePredicate (com.hazelcast.query.impl.predicates.PredicateTestUtils.createMockVisitablePredicate)9 EntryEvent (com.hazelcast.core.EntryEvent)8 Data (com.hazelcast.internal.serialization.Data)8 MapListenerAdapter (com.hazelcast.map.impl.MapListenerAdapter)8 Indexes (com.hazelcast.query.impl.Indexes)8