def unlock(self, key): check_not_none(key, "key can't be None") key_data = self._to_data(key) return self._encode_invoke_on_key(map_unlock_codec, key_data, key=key_data, thread_id=thread_id())
def remove(self, key, value): check_not_none(key, "key can't be None") check_not_none(key, "value can't be None") key_data = self._to_data(key) value_data = self._to_data(value) return self._encode_invoke_on_key(multi_map_remove_entry_codec, key_data, key=key_data, value=value_data, thread_id=thread_id())
def try_remove(self, key, timeout=0): check_not_none(key, "key can't be None") key_data = self._to_data(key) return self._encode_invoke_on_key(map_try_remove_codec, key_data, key=key_data, thread_id=thread_id(), timeout=to_millis(timeout))
def put_transient(self, key, value, ttl=-1): check_not_none(key, "key can't be None") check_not_none(value, "value can't be None") key_data = self._to_data(key) value_data = self._to_data(value) return self._encode_invoke_on_key(map_put_transient_codec, key_data, key=key_data, value=value_data, thread_id=thread_id(), ttl=to_millis(ttl))
def contains_key(self, key): """ :param key: :return: """ check_not_none(key, "key can't be None") key_data = self._to_data(key) return self._encode_invoke_on_key(map_contains_key_codec, key_data, key=key_data, thread_id=thread_id())
def try_put(self, key, value, timeout=0): check_not_none(key, "key can't be None") check_not_none(value, "value can't be None") key_data = self._to_data(key) value_data = self._to_data(value) return self._encode_invoke_on_key(map_try_put_codec, key_data, key=key_data, value=value_data, thread_id=thread_id(), timeout=to_millis(timeout))
def replace_if_same(self, key, old_value, new_value): check_not_none(key, "key can't be None") check_not_none(old_value, "old_value can't be None") check_not_none(new_value, "new_value can't be None") key_data = self._to_data(key) old_value_data = self._to_data(old_value) new_value_data = self._to_data(new_value) return self._encode_invoke_on_key(map_replace_if_same_codec, key_data, key=key_data, test_value=old_value_data, value=new_value_data, thread_id=thread_id())
def set(self, key, value, ttl=-1): """ :param key: :param value: :param ttl: :return: """ check_not_none(key, "key can't be None") check_not_none(value, "value can't be None") key_data = self._to_data(key) value_data = self._to_data(value) return self._encode_invoke_on_key(map_set_codec, key_data, key=key_data, value=value_data, thread_id=thread_id(), ttl=to_millis(ttl))
def unlock(self, key): """ Releases the lock for the specified key. It never blocks and returns immediately. **Warning: This method uses __hash__ and __eq__ methods of binary form of the key, not the actual implementations of __hash__ and __eq__ defined in key's class.** :param key: (object), the key to lock. """ check_not_none(key, "key can't be None") key_data = self._to_data(key) request = multi_map_unlock_codec.encode_request( self.name, key_data, thread_id(), self._reference_id_generator.get_and_increment()) return self._invoke_on_key(request, key_data)
def value_count(self, key): """ Transactional implementation of :func:`MultiMap.value_count(key) <hazelcast.proxy.multi_map.MultiMap.value_count>` :param key: (object), the key whose number of values is to be returned. :return: (int), the number of values matching the given key in the multimap. """ check_not_none(key, "key can't be none") key_data = self._to_data(key) request = transactional_multi_map_value_count_codec.encode_request( self.name, self.transaction.id, thread_id(), key_data) return self._invoke( request, transactional_multi_map_value_count_codec.decode_response)
def contains_key(self, key): """ Determines whether this multimap contains an entry with the key. **Warning: This method uses __hash__ and __eq__ methods of binary form of the key, not the actual implementations of __hash__ and __eq__ defined in key's class.** :param key: (object), the specified key. :return: (bool), ``true`` if this multimap contains an entry for the specified key. """ check_not_none(key, "key can't be None") key_data = self._to_data(key) return self._encode_invoke_on_key(multi_map_contains_key_codec, key_data, key=key_data, thread_id=thread_id())
def value_count(self, key): """ Returns the number of values that match the given key in the multimap. **Warning: This method uses __hash__ and __eq__ methods of binary form of the key, not the actual implementations of __hash__ and __eq__ defined in key's class.** :param key: (object), the key whose values count is to be returned. :return: (int), the number of values that match the given key in the multimap. """ check_not_none(key, "key can't be None") key_data = self._to_data(key) return self._encode_invoke_on_key(multi_map_value_count_codec, key_data, key=key_data, thread_id=thread_id())
def contains_key(self, key): """Transactional implementation of :func:`Map.contains_key(key) <hazelcast.proxy.map.Map.contains_key>` Args: key: The specified key. Returns: hazelcast.future.Future[bool]: ``True`` if this map contains an entry for the specified key, ``False`` otherwise. """ check_not_none(key, "key can't be none") key_data = self._to_data(key) request = transactional_map_contains_key_codec.encode_request( self.name, self.transaction.id, thread_id(), key_data ) return self._invoke(request, transactional_map_contains_key_codec.decode_response)
def remove(self, key, value): """ Removes the given key-value tuple from the multimap. **Warning: This method uses __hash__ and __eq__ methods of binary form of the key, not the actual implementations of __hash__ and __eq__ defined in key's class.** :param key: (object), the key of the entry to remove. :param value: (object), the value of the entry to remove. :return: (bool), ``true`` if the size of the multimap changed after the remove operation, ``false`` otherwise. """ check_not_none(key, "key can't be None") check_not_none(key, "value can't be None") key_data = self._to_data(key) value_data = self._to_data(value) return self._encode_invoke_on_key(multi_map_remove_entry_codec, key_data, key=key_data, value=value_data, thread_id=thread_id())
def contains_entry(self, key, value): """ Returns whether the multimap contains an entry with the value. :param key: (object), the specified key. :param value: (object), the specified value. :return: (bool), ``true`` if this multimap contains the key-value tuple. """ check_not_none(key, "key can't be None") check_not_none(value, "value can't be None") key_data = self._to_data(key) value_data = self._to_data(value) return self._encode_invoke_on_key(multi_map_contains_entry_codec, key_data, key=key_data, value=value_data, thread_id=thread_id())
def unlock(self): """Releases the lock if the lock is currently held by the current thread. Returns: hazelcast.future.Future[None]: Raises: LockOwnershipLostError: If the underlying CP session was closed before the client releases the lock IllegalMonitorStateError: If the lock is not held by the current thread """ current_thread_id = thread_id() session_id = self._get_session_id() # the order of the following checks is important try: self._verify_locked_session_id_if_present(current_thread_id, session_id, False) except LockOwnershipLostError as e: return ImmediateExceptionFuture(e) if session_id == _NO_SESSION_ID: self._lock_session_ids.pop(current_thread_id, None) return ImmediateExceptionFuture( self._new_illegal_monitor_state_error()) def check_response(f): try: still_locked_by_the_current_thread = f.result() if still_locked_by_the_current_thread: self._lock_session_ids[current_thread_id] = session_id else: self._lock_session_ids.pop(current_thread_id, None) self._release_session(session_id) except SessionExpiredError: self._invalidate_session(session_id) self._lock_session_ids.pop(current_thread_id, None) raise self._new_lock_ownership_lost_error(session_id) except IllegalMonitorStateError as e: self._lock_session_ids.pop(current_thread_id, None) raise e return self._request_unlock(session_id, current_thread_id, uuid.uuid4()).continue_with(check_response)
def peek(self, timeout=0): """Transactional implementation of :func:`Queue.peek(timeout) <hazelcast.proxy.queue.Queue.peek>` Args: timeout (int): Maximum time in seconds to wait for addition. Returns: hazelcast.future.Future[any]: The head of this queue, or ``None`` if this queue is empty or specified timeout elapses before an item is added to the queue. """ def handler(message): return self._to_object( transactional_queue_peek_codec.decode_response(message)) request = transactional_queue_peek_codec.encode_request( self.name, self.transaction.id, thread_id(), to_millis(timeout)) return self._invoke(request, handler)
def get(self, key): """ Transactional implementation of :func:`Map.get(key) <hazelcast.proxy.map.Map.get>` :param key: (object), the specified key. :return: (object), the value for the specified key. """ check_not_none(key, "key can't be none") def handler(message): return self._to_object( transactional_map_get_codec.decode_response(message)) key_data = self._to_data(key) request = transactional_map_get_codec.encode_request( self.name, self.transaction.id, thread_id(), key_data) return self._invoke(request, handler)
def value_count(self, key): """Returns the number of values that match the given key in the multimap. Warning: This method uses ``__hash__`` and ``__eq__`` methods of binary form of the key, not the actual implementations of ``__hash__`` and ``__eq__`` defined in key's class. Args: key: The key whose values count is to be returned. Returns: hazelcast.future.Future[int]: The number of values that match the given key in the multimap. """ check_not_none(key, "key can't be None") key_data = self._to_data(key) request = multi_map_value_count_codec.encode_request(self.name, key_data, thread_id()) return self._invoke_on_key(request, key_data, multi_map_value_count_codec.decode_response)
def offer(self, item, timeout=0): """Transactional implementation of :func:`Queue.offer(item, timeout) <hazelcast.proxy.queue.Queue.offer>` Args: item: The item to be added. timeout (int): Maximum time in seconds to wait for addition. Returns: hazelcast.future.Future[bool]: ``True`` if the element was added to this queue, ``False`` otherwise. """ check_not_none(item, "item can't be none") item_data = self._to_data(item) request = transactional_queue_offer_codec.encode_request( self.name, self.transaction.id, thread_id(), item_data, to_millis(timeout)) return self._invoke(request, transactional_queue_offer_codec.decode_response)
def delete(self, key): """Transactional implementation of :func:`Map.delete(key) <hazelcast.proxy.map.Map.delete>` The object to be deleted will be removed from only the current transaction context until the transaction is committed. Args: key: Key of the mapping to be deleted. Returns: hazelcast.future.Future[None]: """ check_not_none(key, "key can't be none") key_data = self._to_data(key) request = transactional_map_delete_codec.encode_request( self.name, self.transaction.id, thread_id(), key_data) return self._invoke(request)
def put(self, key, value): """ Transactional implementation of :func:`MultiMap.put(key, value) <hazelcast.proxy.multi_map.MultiMap.put>` :param key: (object), the key to be stored. :param value: (object), the value to be stored. :return: (bool), ``true`` if the size of the multimap is increased, ``false`` if the multimap already contains the key-value tuple. """ check_not_none(key, "key can't be none") check_not_none(value, "value can't be none") key_data = self._to_data(key) value_data = self._to_data(value) request = transactional_multi_map_put_codec.encode_request( self.name, self.transaction.id, thread_id(), key_data, value_data) return self._invoke(request, transactional_multi_map_put_codec.decode_response)
def remove_all(self, key): """ Removes all the entries with the given key and returns the value list associated with this key. **Warning: This method uses __hash__ and __eq__ methods of binary form of the key, not the actual implementations of __hash__ and __eq__ defined in key's class.** **Warning-2: The returned list is NOT backed by the map, so changes to the map are NOT reflected in the list, and vice-versa.** :param key: (object), the key of the entries to remove. :return: (Sequence), the collection of removed values associated with the given key. """ check_not_none(key, "key can't be None") key_data = self._to_data(key) return self._encode_invoke_on_key(multi_map_remove_codec, key_data, key=key_data, thread_id=thread_id())
def set(self, key, value): """ Transactional implementation of :func:`Map.set(key, value) <hazelcast.proxy.map.Map.set>` The object to be set will be accessible only in the current transaction context till the transaction is committed. :param key: (object), key of the entry. :param value: (object), value of the entry. """ check_not_none(key, "key can't be none") check_not_none(value, "value can't be none") key_data = self._to_data(key) value_data = self._to_data(value) request = transactional_map_set_codec.encode_request( self.name, self.transaction.id, thread_id(), key_data, value_data) return self._invoke(request)
def put(self, key, value): """ Stores a key-value tuple in the multimap. **Warning: This method uses __hash__ and __eq__ methods of binary form of the key, not the actual implementations of __hash__ and __eq__ defined in key's class.** :param key: (object), the key to be stored. :param value: (object), the value to be stored. :return: (bool), ``true`` if size of the multimap is increased, ``false`` if the multimap already contains the key-value tuple. """ check_not_none(key, "key can't be None") check_not_none(value, "value can't be None") key_data = self._to_data(key) value_data = self._to_data(value) return self._encode_invoke_on_key(multi_map_put_codec, key_data, key=key_data, value=value_data, thread_id=thread_id())
def get(self, key): """ Transactional implementation of :func:`MultiMap.get(key) <hazelcast.proxy.multi_map.MultiMap.get>` :param key: (object), the key whose associated values are returned. :return: (Sequence), the collection of the values associated with the key. """ check_not_none(key, "key can't be none") def handler(message): return ImmutableLazyDataList( transactional_multi_map_get_codec.decode_response(message), self._to_object) key_data = self._to_data(key) request = transactional_multi_map_get_codec.encode_request( self.name, self.transaction.id, thread_id(), key_data) return self._invoke(request, handler)
def poll(self, timeout: float = 0) -> typing.Optional[ItemType]: """Transactional implementation of :func:`Queue.poll(timeout) <hazelcast.proxy.queue.Queue.poll>` Args: timeout: Maximum time in seconds to wait for addition. Returns: The head of this queue, or ``None`` if this queue is empty or specified timeout elapses before an item is added to the queue. """ def handler(message): return self._to_object( transactional_queue_poll_codec.decode_response(message)) request = transactional_queue_poll_codec.encode_request( self.name, self.transaction.id, thread_id(), to_millis(timeout)) return self._invoke(request, handler)
def get(self, key): """ Returns the list of values associated with the key. ``None`` if this map does not contain this key. **Warning: This method uses hashCode and equals of the binary form of the key, not the actual implementations of hashCode and equals defined in the key's class.** **Warning-2: The list is NOT backed by the multimap, so changes to the map are list reflected in the collection, and vice-versa.** :param key: (object), the specified key. :return: (Sequence), the list of the values associated with the specified key. """ check_not_none(key, "key can't be None") key_data = self._to_data(key) return self._encode_invoke_on_key(multi_map_get_codec, key_data, key=key_data, thread_id=thread_id())
def remove(self, key, value): """ Transactional implementation of :func:`MultiMap.remove(key, value) <hazelcast.proxy.multi_map.MultiMap.remove>` :param key: (object), the key of the entry to remove. :param value: (object), the value of the entry to remove. :return: (bool), True if the item is removed, False otherwise """ check_not_none(key, "key can't be none") check_not_none(value, "value can't be none") key_data = self._to_data(key) value_data = self._to_data(value) request = transactional_multi_map_remove_entry_codec.encode_request( self.name, self.transaction.id, thread_id(), key_data, value_data) return self._invoke( request, transactional_multi_map_remove_entry_codec.decode_response)
def try_lock(self, timeout=0, lease_time=-1): """ Tries to acquire the lock. When the lock is not available, * If timeout is not provided, the current thread doesn't wait and returns ``false`` immediately. * If a timeout is provided, the current thread becomes disabled for thread scheduling purposes and lies dormant until one of the followings happens: * the lock is acquired by the current thread, or * the specified waiting time elapses. If lease time is provided, lock will be released after this time elapses. :param timeout: (long), maximum time in seconds to wait for the lock (optional). :param lease_time: (long), time in seconds to wait before releasing the lock (optional). :return: (bool), ``true`` if the lock was acquired and otherwise, ``false``. """ return self._encode_invoke(lock_try_lock_codec, invocation_timeout=MAX_SIZE, lease=to_millis(lease_time), thread_id=thread_id(), timeout=to_millis(timeout), reference_id=self.reference_id_generator.get_and_increment())
def unlock(self, key): """Releases the lock for the specified key. It never blocks and returns immediately. Warning: This method uses ``__hash__`` and ``__eq__`` methods of binary form of the key, not the actual implementations of ``__hash__`` and ``__eq__`` defined in key's class. Args: key: The key to lock. Returns: hazelcast.future.Future[None]: """ check_not_none(key, "key can't be None") key_data = self._to_data(key) request = multi_map_unlock_codec.encode_request( self.name, key_data, thread_id(), self._reference_id_generator.get_and_increment() ) return self._invoke_on_key(request, key_data)
def contains_key(self, key): """Determines whether this multimap contains an entry with the key. Warning: This method uses ``__hash__`` and ``__eq__`` methods of binary form of the key, not the actual implementations of ``__hash__`` and ``__eq__`` defined in key's class. Args: key: The specified key. Returns: hazelcast.future.Future[bool]: ``True`` if this multimap contains an entry for the specified key, ``False`` otherwise. """ check_not_none(key, "key can't be None") key_data = self._to_data(key) request = multi_map_contains_key_codec.encode_request(self.name, key_data, thread_id()) return self._invoke_on_key(request, key_data, multi_map_contains_key_codec.decode_response)
def _do_change_permits(self, delta): current_thread_id = thread_id() invocation_uuid = uuid.uuid4() def do_change_permits_once(session_id): session_id = session_id.result() def check_response(response): try: response.result() except SessionExpiredError as e: self._invalidate_session(session_id) raise self._new_illegal_state_error(e) finally: self._release_session(session_id) return self._request_change(session_id, current_thread_id, invocation_uuid, delta).continue_with( check_response) return self._acquire_session().continue_with(do_change_permits_once)
def remove(self, key): """ Transactional implementation of :func:`Map.remove(key) <hazelcast.proxy.map.Map.remove>` The object to be removed will be removed from only the current transaction context until the transaction is committed. :param key: (object), key of the mapping to be deleted. :return: (object), the previous value associated with key, or ``None`` if there was no mapping for key. """ check_not_none(key, "key can't be none") def handler(message): return self._to_object( transactional_map_remove_codec.decode_response(message)) key_data = self._to_data(key) request = transactional_map_remove_codec.encode_request( self.name, self.transaction.id, thread_id(), key_data) return self._invoke(request, handler)
def delete(self, key: KeyType) -> None: """Transactional implementation of :func:`Map.delete(key) <hazelcast.proxy.map.Map.delete>` The object to be deleted will be removed from only the current transaction context until the transaction is committed. Args: key: Key of the mapping to be deleted. """ check_not_none(key, "key can't be none") try: key_data = self._to_data(key) except SchemaNotReplicatedError as e: self._send_schema(e) return self.delete(key) request = transactional_map_delete_codec.encode_request( self.name, self.transaction.id, thread_id(), key_data) return self._invoke(request)
def release(self, permits=1): check_true(permits > 0, "Permits must be positive") session_id = self._get_session_id() if session_id == _NO_SESSION_ID: return ImmediateExceptionFuture(self._new_illegal_state_error()) current_thread_id = thread_id() invocation_uuid = uuid.uuid4() def check_response(response): try: response.result() except SessionExpiredError as e: self._invalidate_session(session_id) raise self._new_illegal_state_error(e) finally: self._release_session(session_id, permits) return self._request_release(session_id, current_thread_id, invocation_uuid, permits).continue_with( check_response)
def remove(self, key, value): """Transactional implementation of :func:`MultiMap.remove(key, value) <hazelcast.proxy.multi_map.MultiMap.remove>` Args: key: The key of the entry to remove. value: The value of the entry to remove. Returns: hazelcast.future.Future[bool]: ``True`` if the item is removed, ``False`` otherwise """ check_not_none(key, "key can't be none") check_not_none(value, "value can't be none") key_data = self._to_data(key) value_data = self._to_data(value) request = transactional_multi_map_remove_entry_codec.encode_request( self.name, self.transaction.id, thread_id(), key_data, value_data ) return self._invoke(request, transactional_multi_map_remove_entry_codec.decode_response)
def begin(self): if hasattr(self._locals, "transaction_exists") and self._locals.transaction_exists: raise TransactionError("Nested transactions are not allowed.") if self.state != _STATE_NOT_STARTED: raise TransactionError("Transaction has already been started.") self._locals.transaction_exists = True self.start_time = time.time() self.thread_id = thread_id() try: request = transaction_create_codec.encode_request( timeout=self.timeout * 1000, durability=self.durability, transaction_type=self.transaction_type, thread_id=self.thread_id, ) response = self.client.invoker.invoke_on_connection(request, self.connection).result() self.id = transaction_create_codec.decode_response(response)["response"] self.state = _STATE_ACTIVE except: self._locals.transaction_exists = False raise
def _encode_invoke(self, codec, response_handler=default_response_handler, **kwargs): request = codec.encode_request(name=self.name, txn_id=self.transaction.id, thread_id=thread_id(), **kwargs) return self.transaction.client.invoker.invoke_on_connection(request, self.transaction.connection).continue_with( response_handler, codec, self._to_object)
def remove_all(self, key): check_not_none(key, "key can't be None") key_data = self._to_data(key) return self._encode_invoke_on_key(multi_map_remove_codec, key_data, key=key_data, thread_id=thread_id())
def _check_thread(self): if not thread_id() == self.thread_id: raise TransactionError("Transaction cannot span multiple threads.")
def lock(self, key, lease_time=-1): check_not_none(key, "key can't be None") key_data = self._to_data(key) return self._encode_invoke_on_key(multi_map_lock_codec, key_data, key=key_data, thread_id=thread_id(), ttl=to_millis(lease_time))
def _try_put_internal(self, key_data, value_data, timeout): return self._encode_invoke_on_key(map_try_put_codec, key_data, key=key_data, value=value_data, thread_id=thread_id(), timeout=to_millis(timeout))
def _execute_on_key_internal(self, key_data, entry_processor): return self._encode_invoke_on_key(map_execute_on_key_codec, key_data, key=key_data, entry_processor=self._to_data(entry_processor), thread_id=thread_id())
def _replace_if_same_internal(self, key_data, old_value_data, new_value_data): return self._encode_invoke_on_key(map_replace_if_same_codec, key_data, key=key_data, test_value=old_value_data, value=new_value_data, thread_id=thread_id())
def _replace_internal(self, key_data, value_data): return self._encode_invoke_on_key(map_replace_codec, key_data, key=key_data, value=value_data, thread_id=thread_id())
def get_entry_view(self, key): check_not_none(key, "key can't be None") key_data = self._to_data(key) return self._encode_invoke_on_key(map_get_entry_view_codec, key_data, key=key_data, thread_id=thread_id())
def execute_on_key(self, key, entry_processor): check_not_none(key, "key can't be None") key_data = self._to_data(key) return self._encode_invoke_on_key(map_execute_on_key_codec, key_data, key=key_data, entry_processor=self._to_data(entry_processor), thread_id=thread_id())
def _contains_key_internal(self, key_data): return self._encode_invoke_on_key(map_contains_key_codec, key_data, key=key_data, thread_id=thread_id())
def _put_if_absent_internal(self, key_data, value_data, ttl): return self._encode_invoke_on_key(map_put_if_absent_codec, key_data, key=key_data, value=value_data, thread_id=thread_id(), ttl=to_millis(ttl))
def _evict_internal(self, key_data): return self._encode_invoke_on_key(map_evict_codec, key_data, key=key_data, thread_id=thread_id())