<s> from random import randint <EOL> from hazelcast . protocol . codec import replicated_map_clear_codec , replicated_map_add_entry_listener_codec , replicated_map_add_entry_listener_to_key_codec , replicated_map_add_entry_listener_to_key_with_predicate_codec , replicated_map_add_entry_listener_with_predicate_codec , replicated_map_contains_key_codec , replicated_map_contains_value_codec , replicated_map_entry_set_codec , replicated_map_get_codec , replicated_map_is_empty_codec , replicated_map_key_set_codec , replicated_map_put_all_codec , replicated_map_put_codec , replicated_map_remove_codec , replicated_map_remove_entry_listener_codec , replicated_map_size_codec , replicated_map_values_codec <EOL> from hazelcast . proxy . base import Proxy , default_response_handler , EntryEvent , EntryEventType <EOL> from hazelcast . util import to_millis , check_not_none <EOL> class ReplicatedMap ( Proxy ) : <EOL> _partition_id = None <EOL> def add_entry_listener ( self , key = None , predicate = None , added = None , removed = None , updated = None , <EOL> evicted = None , clear_all = None ) : <EOL> if key and predicate : <EOL> key_data = self . _to_data ( key ) <EOL> predicate_data = self . _to_data ( predicate ) <EOL> request = replicated_map_add_entry_listener_to_key_with_predicate_codec . encode_request ( self . name , key_data , <EOL> predicate_data , <EOL> False ) <EOL> elif key and not predicate : <EOL> key_data = self . _to_data ( key ) <EOL> request = replicated_map_add_entry_listener_to_key_codec . encode_request ( self . name , key_data , False ) <EOL> elif not key and predicate : <EOL> predicate = self . _to_data ( predicate ) <EOL> request = replicated_map_add_entry_listener_with_predicate_codec . encode_request ( self . name , predicate , <EOL> False ) <EOL> else : <EOL> request = replicated_map_add_entry_listener_codec . encode_request ( self . name , False ) <EOL> def handle_event_entry ( ** _kwargs ) : <EOL> event = EntryEvent ( self . _to_object , ** _kwargs ) <EOL> if event . event_type == EntryEventType . added and added : <EOL> added ( event ) <EOL> elif event . event_type == EntryEventType . removed and removed : <EOL> removed ( event ) <EOL> elif event . event_type == EntryEventType . updated and updated : <EOL> updated ( event ) <EOL> elif event . event_type == EntryEventType . evicted and evicted : <EOL> evicted ( event ) <EOL> elif event . event_type == EntryEventType . clear_all and clear_all : <EOL> clear_all ( event ) <EOL> return self . _start_listening ( request , <EOL> lambda m : replicated_map_add_entry_listener_codec . handle ( m , <EOL> handle_event_entry ) , <EOL> lambda r : replicated_map_add_entry_listener_codec . decode_response ( r ) [ <EOL> '<STR_LIT>' ] ) <EOL> def clear ( self ) : <EOL> return self . _encode_invoke ( replicated_map_clear_codec ) <EOL> def contains_key ( self , key ) : <EOL> check_not_none ( key , "<STR_LIT>" ) <EOL> key_data = self . _to_data ( key ) <EOL> return self . _encode_invoke_on_key ( replicated_map_contains_key_codec , key_data , key = key_data ) <EOL> def contains_value ( self , value ) : <EOL> check_not_none ( value , "<STR_LIT>" ) <EOL> return self . _encode_invoke_on_target_partition ( replicated_map_contains_value_codec , value = self . _to_data ( value ) ) <EOL> def entry_set ( self ) : <EOL> return self . _encode_invoke_on_target_partition ( replicated_map_entry_set_codec ) <EOL> def get ( self , key ) : <EOL> check_not_none ( key , "<STR_LIT>" ) <EOL> key_data = self . _to_data ( key ) <EOL> return self . _encode_invoke_on_key ( replicated_map_get_codec , key_data , key = key_data ) <EOL> def is_empty ( self ) : <EOL> return self . _encode_invoke_on_target_partition ( replicated_map_is_empty_codec ) <EOL> def key_set ( self ) : <EOL> return self . _encode_invoke_on_target_partition ( replicated_map_key_set_codec ) <EOL> def put ( self , key , value , ttl = <NUM_LIT:0> ) : <EOL> check_not_none ( key , "<STR_LIT>" ) <EOL> check_not_none ( key , "<STR_LIT>" ) <EOL> key_data = self . _to_data ( key ) <EOL> value_data = self . _to_data ( value ) <EOL> return self . _encode_invoke_on_key ( replicated_map_put_codec , key_data , key = key_data , value = value_data , <EOL> ttl = to_millis ( ttl ) ) <EOL> def put_all ( self , map ) : <EOL> entries = { } <EOL> for key , value in map . iteritems ( ) : <EOL> check_not_none ( key , "<STR_LIT>" ) <EOL> check_not_none ( value , "<STR_LIT>" ) <EOL> entries [ self . _to_data ( key ) ] = self . _to_data ( value ) <EOL> self . _encode_invoke ( replicated_map_put_all_codec , entries = entries ) <EOL> def remove ( self , key ) : <EOL> check_not_none ( key , "<STR_LIT>" ) <EOL> key_data = self . _to_data ( key ) <EOL> return self . _encode_invoke_on_key ( replicated_map_remove_codec , key_data , key = key_data ) <EOL> def remove_entry_listener ( self , registration_id ) : <EOL> return self . _stop_listening ( registration_id , <EOL> lambda i : replicated_map_remove_entry_listener_codec . encode_request ( self . name , i ) ) <EOL> def size ( self ) : <EOL> return self . _encode_invoke_on_target_partition ( replicated_map_size_codec ) <EOL> def values ( self ) : <EOL> return self . _encode_invoke_on_target_partition ( replicated_map_values_codec ) <EOL> def _get_partition_id ( self ) : <EOL> if not self . _partition_id : <EOL> self . _partition_id = randint ( <NUM_LIT:0> , self . _client . partition_service . get_partition_count ( ) - <NUM_LIT:1> ) <EOL> return self . _partition_id <EOL> def _encode_invoke_on_target_partition ( self , codec , response_handler = default_response_handler , ** kwargs ) : <EOL> return self . _encode_invoke_on_partition ( codec , self . _get_partition_id ( ) , response_handler , ** kwargs ) </s>
