Skip to content

Commit 68d6342

Browse files
authored
Go over the predicate implementations (#239)
* Go over the predicate implementations As described in #233, our predicate implementations had some problems. This is an attempt to solve all of these. The changes done are listed below. - It is moved from `hazelcast.serialization.predicate` to `hazelcast.predicate` package. - `Predicate` and `PagingPredicate` interfaces are properly defined and documented. - All implementations are made protected. - Factory-like functions are provided for each predicate implementation with proper documentatiın instead of aliases. Also, some explicit names are shortened. - `is_equal_to` -> `equal` - `is_not_equal_to` -> `not_equal` - `is_instance_of` -> `instance_of` - `is_like` -> `like` - `is_ilike` -> `ilike` - `is_greater_than` -> `greater_than` - `is_greater_than_or_equal_to` -> `greater_equal` - `is_less_than` -> `less_than` - `is_less_than_or_equal_to` -> `less_equal` - `is_between` -> `between` - `is_in` -> `in_` - `is_not` -> `not_` - `matches_regex` -> `regex` * Renam less* and greater* predicates According to review comments, the followings are renamed. - `greater_than` -> `greater` - `greater_equal` -> `greter_or_equal` - `less_than` -> `less` - `less_equal` -> `less_or_eqaul`
1 parent 84a50db commit 68d6342

20 files changed

+861
-470
lines changed

README.rst

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ less than 6 is shown below:
10781078
.. code:: python
10791079
10801080
# Get the objects whose age is less than 6
1081-
result = json_map.values(is_less_than_or_equal_to("age", 6))
1081+
result = json_map.values(less_or_equal("age", 6))
10821082
print("Retrieved %s values whose age is less than 6." % len(result))
10831083
print("Entry is", result[0].to_string())
10841084
@@ -2695,34 +2695,34 @@ since only filtered data is sent to the requester.
26952695

26962696
**Predicate Module Operators**
26972697

2698-
The ``Predicate`` module offered by the Python client includes many
2698+
The ``predicate`` module offered by the Python client includes many
26992699
operators for your query requirements. Some of them are explained below.
27002700

2701-
- ``is_equal_to``: Checks if the result of an expression is equal to a
2701+
- ``equal``: Checks if the result of an expression is equal to a
27022702
given value.
2703-
- ``is_not_equal_to``: Checks if the result of an expression is not
2703+
- ``not_equal``: Checks if the result of an expression is not
27042704
equal to a given value.
2705-
- ``is_instance_of``: Checks if the result of an expression has a
2705+
- ``instance_of``: Checks if the result of an expression has a
27062706
certain type.
2707-
- ``is_like``: Checks if the result of an expression matches some
2707+
- ``like``: Checks if the result of an expression matches some
27082708
string pattern. ``%`` (percentage sign) is the placeholder for many
27092709
characters, ``_`` (underscore) is placeholder for only one character.
2710-
- ``is_ilike``: Checks if the result of an expression matches some
2710+
- ``ilike``: Checks if the result of an expression matches some
27112711
string pattern in a case-insensitive manner.
2712-
- ``is_greater_than``: Checks if the result of an expression is greater
2712+
- ``greater``: Checks if the result of an expression is greater
27132713
than a certain value.
2714-
- ``is_greater_than_or_equal_to``: Checks if the result of an
2714+
- ``greater_or_equal``: Checks if the result of an
27152715
expression is greater than or equal to a certain value.
2716-
- ``is_less_than``: Checks if the result of an expression is less than
2716+
- ``less``: Checks if the result of an expression is less than
27172717
a certain value.
2718-
- ``is_less_than_or_equal_to``: Checks if the result of an expression
2718+
- ``less_or_equal``: Checks if the result of an expression
27192719
is less than or equal to a certain value.
2720-
- ``is_between``: Checks if the result of an expression is between two
2720+
- ``between``: Checks if the result of an expression is between two
27212721
values (this is inclusive).
2722-
- ``is_in``: Checks if the result of an expression is an element of a
2722+
- ``in_``: Checks if the result of an expression is an element of a
27232723
certain list.
2724-
- ``is_not``: Checks if the result of an expression is false.
2725-
- ``matches_regex``: Checks if the result of an expression matches some
2724+
- ``not_``: Checks if the result of an expression is false.
2725+
- ``regex``: Checks if the result of an expression matches some
27262726
regular expression.
27272727
- ``true``: Creates an always true predicate that will pass all items.
27282728
- ``false``: Creates an always false predicate that will filter out all
@@ -2793,11 +2793,11 @@ operators, as shown in the below example.
27932793

27942794
.. code:: python
27952795
2796-
from hazelcast.serialization.predicate import and_, is_equal_to, is_less_than
2796+
from hazelcast.predicate import and_, equal, less
27972797
27982798
employee_map = client.get_map("employee")
27992799
2800-
predicate = and_(is_equal_to('active', True), is_less_than('age', 30))
2800+
predicate = and_(equal('active', True), less('age', 30))
28012801
28022802
employees = employee_map.values(predicate).result()
28032803
@@ -2818,7 +2818,7 @@ following example:
28182818

28192819
.. code:: python
28202820
2821-
from hazelcast.serialization.predicate import sql
2821+
from hazelcast.predicate import sql
28222822
28232823
employee_map = client.get_map("employee")
28242824
@@ -2882,7 +2882,7 @@ the entry keys. See the following example:
28822882

28832883
.. code:: python
28842884
2885-
from hazelcast.serialization.predicate import sql
2885+
from hazelcast.predicate import sql
28862886
28872887
person_map = client.get_map("persons").blocking()
28882888
@@ -2904,15 +2904,15 @@ the entry values. See the following example:
29042904

29052905
.. code:: python
29062906
2907-
from hazelcast.serialization.predicate import is_greater_than_or_equal_to
2907+
from hazelcast.predicate import greater_or_equal
29082908
29092909
person_map = client.get_map("persons").blocking()
29102910
29112911
person_map.put("John", 28)
29122912
person_map.put("Mary", 23)
29132913
person_map.put("Judy", 30)
29142914
2915-
predicate = is_greater_than_or_equal_to("this", 27)
2915+
predicate = greater_or_equal("this", 27)
29162916
29172917
persons = person_map.values(predicate)
29182918
@@ -2946,7 +2946,7 @@ Hazelcast query methods explained in this section.
29462946
# From JSON serializable object
29472947
id_person_map.put(3, HazelcastJsonValue(person3))
29482948
2949-
people_under_21 = id_person_map.values(is_less_than("age", 21))
2949+
people_under_21 = id_person_map.values(less("age", 21))
29502950
29512951
When running the queries, Hazelcast treats values extracted from the
29522952
JSON documents as Java types so they can be compared with the query
@@ -2982,7 +2982,7 @@ objects using the ``Predicate``\ s.
29822982
# }
29832983
# The following query finds all the departments that have a person named "Peter" working in them.
29842984
2985-
department_with_peter = departments.values(is_equal_to("people[any].name", "Peter"))
2985+
department_with_peter = departments.values(equal("people[any].name", "Peter"))
29862986
29872987
``HazelcastJsonValue`` is a lightweight wrapper around your JSON
29882988
strings. It is used merely as a way to indicate that the contained
@@ -3044,7 +3044,7 @@ details.
30443044

30453045
In the example code below:
30463046

3047-
- The ``is_greater_than_or_equal_to`` predicate gets values from the
3047+
- The ``greater_or_equal`` predicate gets values from the
30483048
``students`` map. This predicate has a filter to retrieve the objects
30493049
with an ``age`` greater than or equal to ``18``.
30503050

@@ -3058,12 +3058,12 @@ In the example code below:
30583058

30593059
.. code:: python
30603060
3061-
from hazelcast.serialization.predicate import paging, is_greater_than_or_equal_to
3061+
from hazelcast.predicate import paging, greater_or_equal
30623062
30633063
...
30643064
30653065
m = client.get_map("students").blocking()
3066-
predicate = paging(is_greater_than_or_equal_to("age", 18), 5)
3066+
predicate = paging(greater_or_equal("age", 18), 5)
30673067
30683068
# Retrieve the first page
30693069
values = m.values(predicate)

docs/hazelcast.predicate.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Predicate
2+
=========
3+
4+
.. automodule:: hazelcast.predicate

docs/hazelcast.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Hazelcast Python Client
1313
hazelcast.future
1414
hazelcast.lifecycle
1515
hazelcast.partition
16+
hazelcast.predicate
1617
hazelcast.proxy
1718
hazelcast.serialization
1819
hazelcast.transaction

examples/hazelcast-json-value/hazelcast_json_value_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import hazelcast
22

33
from hazelcast.core import HazelcastJsonValue
4-
from hazelcast.serialization.predicate import and_, is_greater_than, sql
4+
from hazelcast.predicate import and_, greater, sql
55

66
client = hazelcast.HazelcastClient()
77
employees_map = client.get_map("employees").blocking()
@@ -18,7 +18,7 @@
1818
employees_map.put(2, HazelcastJsonValue(bob))
1919

2020
# Employees whose name starts with 'A' and age is greater than 30
21-
predicate = and_(sql("name like A%"), is_greater_than("age", 30))
21+
predicate = and_(sql("name like A%"), greater("age", 30))
2222

2323
values = employees_map.values(predicate)
2424

examples/map/map_paging_predicate_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import hazelcast
22
from hazelcast.serialization.api import IdentifiedDataSerializable
3-
from hazelcast.serialization.predicate import paging, true
3+
from hazelcast.predicate import paging, true
44

55
client = hazelcast.HazelcastClient()
66

examples/map/map_portable_query_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import hazelcast
33

44
from hazelcast.serialization.api import Portable
5-
from hazelcast.serialization.predicate import sql
5+
from hazelcast.predicate import sql
66

77

88
class Employee(Portable):

examples/map/map_predicate_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import hazelcast
22

3-
from hazelcast.serialization.predicate import is_between
3+
from hazelcast.predicate import between
44

55
client = hazelcast.HazelcastClient()
66

77
predicate_map = client.get_map("predicate-map").blocking()
88
for i in range(10):
99
predicate_map.put("key" + str(i), i)
1010

11-
predicate = is_between("this", 3, 5)
11+
predicate = between("this", 3, 5)
1212

1313
entry_set = predicate_map.entry_set(predicate)
1414

examples/org-website/query_sample.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import hazelcast
22

33
from hazelcast.serialization.api import Portable
4-
from hazelcast.serialization.predicate import sql, and_, is_between, is_equal_to
4+
from hazelcast.predicate import sql, and_, between, equal
55

66

77
class User(Portable):
@@ -52,7 +52,7 @@ def generate_users(users):
5252
# Create a Predicate from a String (a SQL like Where clause)
5353
sql_query = sql("active AND age BETWEEN 18 AND 21)")
5454
# Creating the same Predicate as above but with a builder
55-
criteria_query = and_(is_equal_to("active", True), is_between("age", 18, 21))
55+
criteria_query = and_(equal("active", True), between("age", 18, 21))
5656
# Get result collections using the two different Predicates
5757
result1 = users_map.values(sql_query)
5858
result2 = users_map.values(criteria_query)

0 commit comments

Comments
 (0)