Skip to content

Commit

Permalink
map.remove_all implementation (#566)
Browse files Browse the repository at this point in the history
map.remove_all is implemented
  • Loading branch information
feysahin authored Aug 19, 2022
1 parent 7843a07 commit 06a48e8
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
18 changes: 18 additions & 0 deletions examples/map/map_remove_all_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import hazelcast

from hazelcast.predicate import between

client = hazelcast.HazelcastClient()

predicate_map = client.get_map("predicate-map").blocking()

for i in range(10):
predicate_map.put("key" + str(i), i)

predicate = between("this", 3, 5)

predicate_map.remove_all(predicate)

print(predicate_map.values())

client.shutdown()
17 changes: 17 additions & 0 deletions hazelcast/protocol/codec/map_remove_all_codec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from hazelcast.protocol.client_message import OutboundMessage, REQUEST_HEADER_SIZE, create_initial_buffer
from hazelcast.protocol.builtin import StringCodec
from hazelcast.protocol.builtin import DataCodec

# hex: 0x013E00
_REQUEST_MESSAGE_TYPE = 81408
# hex: 0x013E01
_RESPONSE_MESSAGE_TYPE = 81409

_REQUEST_INITIAL_FRAME_SIZE = REQUEST_HEADER_SIZE


def encode_request(name, predicate):
buf = create_initial_buffer(_REQUEST_INITIAL_FRAME_SIZE, _REQUEST_MESSAGE_TYPE)
StringCodec.encode(buf, name)
DataCodec.encode(buf, predicate, True)
return OutboundMessage(buf, False)
26 changes: 26 additions & 0 deletions hazelcast/proxy/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
map_put_transient_with_max_idle_codec,
map_set_with_max_idle_codec,
map_remove_interceptor_codec,
map_remove_all_codec,
)
from hazelcast.proxy.base import (
Proxy,
Expand Down Expand Up @@ -1283,6 +1284,28 @@ def remove(self, key: KeyType) -> Future[typing.Optional[ValueType]]:

return self._remove_internal(key_data)

def remove_all(self, predicate: Predicate) -> Future[None]:
"""Removes all entries which match with the supplied predicate
Args:
predicate: Used to select entries to be removed from map.
Returns:
None
"""

check_not_none(predicate, "predicate can't be None")

try:
predicate_data = self._to_data(predicate)

except SchemaNotReplicatedError as e:
return self._send_schema_and_retry(e, self.remove_all, predicate)

request = map_remove_all_codec.encode_request(self.name, predicate_data)

return self._invoke(request)

def remove_if_same(self, key: KeyType, value: ValueType) -> Future[bool]:
"""Removes the entry for a key only if it is currently mapped to a
given value.
Expand Down Expand Up @@ -2215,6 +2238,9 @@ def remove( # type: ignore[override]
) -> typing.Optional[ValueType]:
return self._wrapped.remove(key).result()

def remove_all(self, predicate: Predicate) -> None: # type: ignore[override]
return self._wrapped.remove_all(predicate).result()

def remove_if_same( # type: ignore[override]
self,
key: KeyType,
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/backward_compatible/proxy/map_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,15 @@ def test_remove(self):
self.assertEqual(0, self.map.size())
self.assertFalse(self.map.contains_key("key"))

def test_remove_all_with_none_predicate(self):

This comment has been minimized.

Copy link
@srknzl

srknzl Sep 8, 2022

Member

@mdumandag These tests are failing in server compatibility tests. https://github.com/hazelcast/client-compatibility-suites/runs/8247095922?check_suite_focus=true

can you add a version check here in this test ?

This comment has been minimized.

Copy link
@srknzl

srknzl Sep 8, 2022

Member

map_nearcache_test fails as well, possibly due to extending this test.

This comment has been minimized.

Copy link
@mdumandag

mdumandag Sep 9, 2022

Contributor

I know about this, but if I do that, the compact tests will start to fail in server compatibility tests. So, I will do that once we have the 5.2 compact implementation ready

This comment has been minimized.

Copy link
@srknzl

srknzl Sep 9, 2022

Member

I did not understand why. Can't you add a version check just for this tests, not the test suite? So remove_all tests would only run in master, since it is added very recently.

This comment has been minimized.

Copy link
@mdumandag

mdumandag Sep 9, 2022

Contributor

master is at v5.1 right now, we can make it 5.2 and add the check but that would make compact tests to fail (they are run with 5.2 server and 5.2 client)

with self.assertRaises(AssertionError):
self.map.remove_all(None)

def test_remove_all(self):
self.fill_map()
self.map.remove_all(predicate=sql("__key > 'key-7'"))
self.assertEqual(self.map.size(), 8)

def test_remove_if_same_when_same(self):
self.map.put("key", "value")

Expand Down

0 comments on commit 06a48e8

Please sign in to comment.