MultiMap

class MultiMap(service_name, name, context)[source]

Bases: Proxy[BlockingMultiMap], Generic[KeyType, ValueType]

A specialized map whose keys can be associated with multiple values.

add_entry_listener(include_value: bool = False, key: Optional[KeyType] = None, added_func: Optional[Callable[[EntryEvent[KeyType, ValueType]], None]] = None, removed_func: Optional[Callable[[EntryEvent[KeyType, ValueType]], None]] = None, clear_all_func: Optional[Callable[[EntryEvent[KeyType, ValueType]], None]] = None) Future[str][source]

Adds an entry listener for this multimap.

The listener will be notified for all multimap add/remove/clear-all events.

Parameters
  • include_value – Whether received event should include the value or not.

  • key – Key for filtering the events.

  • added_func – Function to be called when an entry is added to map.

  • removed_func – Function to be called when an entry is removed from map.

  • clear_all_func – Function to be called when entries are cleared from map.

Returns

A registration id which is used as a key to remove the listener.

contains_key(key: KeyType) Future[bool][source]

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.

Parameters

key – The specified key.

Returns

True if this multimap contains an entry for the specified key, False otherwise.

contains_value(value: ValueType) Future[bool][source]

Determines whether this map contains one or more keys for the specified value.

Parameters

value – The specified value.

Returns

True if this multimap contains an entry for the specified value, False otherwise.

contains_entry(key: KeyType, value: ValueType) Future[bool][source]

Returns whether the multimap contains an entry with the value.

Parameters
  • key – The specified key.

  • value – The specified value.

Returns

True if this multimap contains the key-value tuple, False otherwise.

clear() Future[None][source]

Clears the multimap. Removes all key-value tuples.

entry_set() Future[List[Tuple[KeyType, ValueType]]][source]

Returns the list of key-value tuples in the multimap.

Warning

The list is NOT backed by the map, so changes to the map are NOT reflected in the list, and vice-versa.

Returns

The list of key-value tuples in the multimap.

get(key: KeyType) Future[Optional[List[ValueType]]][source]

Returns the list of values associated with the key. None if this map does not contain this key.

Warning

This method uses __hash__ and __eq__ of the binary form of the key, not the actual implementations of __hash__ and __eq__ defined in the key’s class.

Warning

The list is NOT backed by the multimap, so changes to the map are list reflected in the collection, and vice-versa.

Parameters

key – The specified key.

Returns

The list of the values associated with the specified key.

is_locked(key: KeyType) Future[bool][source]

Checks the lock for the specified 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.

Parameters

key – The key that is checked for lock.

Returns

True if lock is acquired, False otherwise.

force_unlock(key: KeyType) Future[None][source]

Releases the lock for the specified key regardless of the lock owner.

It always successfully unlocks the key, 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.

Parameters

key – The key to lock.

key_set() Future[List[KeyType]][source]

Returns the list of keys in the multimap.

Warning

The list is NOT backed by the map, so changes to the map are NOT reflected in the list, and vice-versa.

Returns

A list of the clone of the keys.

lock(key: KeyType, lease_time: Optional[float] = None) Future[None][source]

Acquires the lock for the specified key infinitely or for the specified lease time if provided.

If the lock is not available, the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired.

Scope of the lock is this map only. Acquired lock is only for the key in this map.

Locks are re-entrant; so, if the key is locked N times, it should be unlocked N times before another thread can acquire it.

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.

Parameters
  • key – The key to lock.

  • lease_time – Time in seconds to wait before releasing the lock.

remove(key: KeyType, value: ValueType) Future[bool][source]

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.

Parameters
  • key – The key of the entry to remove.

  • value – The value of the entry to remove.

Returns

True if the size of the multimap changed after the remove operation, False otherwise.

remove_all(key: KeyType) Future[List[ValueType]][source]

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

The returned list is NOT backed by the map, so changes to the map are NOT reflected in the list, and vice-versa.

Parameters

key – The key of the entries to remove.

Returns

The collection of removed values associated with the given key.

put(key: KeyType, value: ValueType) Future[bool][source]

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.

Parameters
  • key – The key to be stored.

  • value – The value to be stored.

Returns

True if size of the multimap is increased, False if the multimap already contains the key-value tuple.

remove_entry_listener(registration_id: str) Future[bool][source]

Removes the specified entry listener.

Returns silently if there is no such listener added before.

Parameters

registration_id – Id of registered listener.

Returns

True if registration is removed, False otherwise.

size() Future[int][source]

Returns the number of entries in this multimap.

Returns

Number of entries in this multimap.

value_count(key: KeyType) Future[int][source]

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.

Parameters

key – The key whose values count is to be returned.

Returns

The number of values that match the given key in the multimap.

values() Future[List[ValueType]][source]

Returns the list of values in the multimap.

Warning

The returned list is NOT backed by the map, so changes to the map are NOT reflected in the list, and vice-versa.

Returns

The list of values in the multimap.

try_lock(key: KeyType, lease_time: Optional[float] = None, timeout: float = 0) Future[bool][source]

Tries to acquire the lock for the specified key.

When the lock is not available:

  • If the timeout is not provided, the current thread doesn’t wait and returns False immediately.

  • If the 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 the lease time is provided, lock will be released after this time elapses.

Parameters
  • key – Key to lock in this map.

  • lease_time – Time in seconds to wait before releasing the lock.

  • timeout – Maximum time in seconds to wait for the lock.

Returns

True if the lock was acquired, False otherwise.

unlock(key: KeyType) Future[None][source]

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.

Parameters

key – The key to lock.

blocking() BlockingMultiMap[KeyType, ValueType][source]

Returns a version of this proxy with only blocking method calls.

class BlockingMultiMap(wrapped: MultiMap[KeyType, ValueType])[source]

Bases: MultiMap[KeyType, ValueType]

name
service_name
add_entry_listener(include_value: bool = False, key: Optional[KeyType] = None, added_func: Optional[Callable[[EntryEvent[KeyType, ValueType]], None]] = None, removed_func: Optional[Callable[[EntryEvent[KeyType, ValueType]], None]] = None, clear_all_func: Optional[Callable[[EntryEvent[KeyType, ValueType]], None]] = None) str[source]

Adds an entry listener for this multimap.

The listener will be notified for all multimap add/remove/clear-all events.

Parameters
  • include_value – Whether received event should include the value or not.

  • key – Key for filtering the events.

  • added_func – Function to be called when an entry is added to map.

  • removed_func – Function to be called when an entry is removed from map.

  • clear_all_func – Function to be called when entries are cleared from map.

Returns

A registration id which is used as a key to remove the listener.

contains_key(key: KeyType) bool[source]

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.

Parameters

key – The specified key.

Returns

True if this multimap contains an entry for the specified key, False otherwise.

contains_value(value: ValueType) bool[source]

Determines whether this map contains one or more keys for the specified value.

Parameters

value – The specified value.

Returns

True if this multimap contains an entry for the specified value, False otherwise.

contains_entry(key: KeyType, value: ValueType) bool[source]

Returns whether the multimap contains an entry with the value.

Parameters
  • key – The specified key.

  • value – The specified value.

Returns

True if this multimap contains the key-value tuple, False otherwise.

clear() None[source]

Clears the multimap. Removes all key-value tuples.

entry_set() List[Tuple[KeyType, ValueType]][source]

Returns the list of key-value tuples in the multimap.

Warning

The list is NOT backed by the map, so changes to the map are NOT reflected in the list, and vice-versa.

Returns

The list of key-value tuples in the multimap.

get(key: KeyType) Optional[List[ValueType]][source]

Returns the list of values associated with the key. None if this map does not contain this key.

Warning

This method uses __hash__ and __eq__ of the binary form of the key, not the actual implementations of __hash__ and __eq__ defined in the key’s class.

Warning

The list is NOT backed by the multimap, so changes to the map are list reflected in the collection, and vice-versa.

Parameters

key – The specified key.

Returns

The list of the values associated with the specified key.

is_locked(key: KeyType) bool[source]

Checks the lock for the specified 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.

Parameters

key – The key that is checked for lock.

Returns

True if lock is acquired, False otherwise.

force_unlock(key: KeyType) None[source]

Releases the lock for the specified key regardless of the lock owner.

It always successfully unlocks the key, 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.

Parameters

key – The key to lock.

key_set() List[KeyType][source]

Returns the list of keys in the multimap.

Warning

The list is NOT backed by the map, so changes to the map are NOT reflected in the list, and vice-versa.

Returns

A list of the clone of the keys.

lock(key: KeyType, lease_time: Optional[float] = None) None[source]

Acquires the lock for the specified key infinitely or for the specified lease time if provided.

If the lock is not available, the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired.

Scope of the lock is this map only. Acquired lock is only for the key in this map.

Locks are re-entrant; so, if the key is locked N times, it should be unlocked N times before another thread can acquire it.

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.

Parameters
  • key – The key to lock.

  • lease_time – Time in seconds to wait before releasing the lock.

remove(key: KeyType, value: ValueType) bool[source]

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.

Parameters
  • key – The key of the entry to remove.

  • value – The value of the entry to remove.

Returns

True if the size of the multimap changed after the remove operation, False otherwise.

remove_all(key: KeyType) List[ValueType][source]

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

The returned list is NOT backed by the map, so changes to the map are NOT reflected in the list, and vice-versa.

Parameters

key – The key of the entries to remove.

Returns

The collection of removed values associated with the given key.

put(key: KeyType, value: ValueType) bool[source]

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.

Parameters
  • key – The key to be stored.

  • value – The value to be stored.

Returns

True if size of the multimap is increased, False if the multimap already contains the key-value tuple.

remove_entry_listener(registration_id: str) bool[source]

Removes the specified entry listener.

Returns silently if there is no such listener added before.

Parameters

registration_id – Id of registered listener.

Returns

True if registration is removed, False otherwise.

size() int[source]

Returns the number of entries in this multimap.

Returns

Number of entries in this multimap.

value_count(key: KeyType) int[source]

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.

Parameters

key – The key whose values count is to be returned.

Returns

The number of values that match the given key in the multimap.

values() List[ValueType][source]

Returns the list of values in the multimap.

Warning

The returned list is NOT backed by the map, so changes to the map are NOT reflected in the list, and vice-versa.

Returns

The list of values in the multimap.

try_lock(key: KeyType, lease_time: Optional[float] = None, timeout: float = 0) bool[source]

Tries to acquire the lock for the specified key.

When the lock is not available:

  • If the timeout is not provided, the current thread doesn’t wait and returns False immediately.

  • If the 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 the lease time is provided, lock will be released after this time elapses.

Parameters
  • key – Key to lock in this map.

  • lease_time – Time in seconds to wait before releasing the lock.

  • timeout – Maximum time in seconds to wait for the lock.

Returns

True if the lock was acquired, False otherwise.

unlock(key: KeyType) None[source]

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.

Parameters

key – The key to lock.

destroy() bool[source]

Destroys this proxy.

Returns

True if this proxy is destroyed successfully, False otherwise.

blocking() BlockingMultiMap[KeyType, ValueType][source]

Returns a version of this proxy with only blocking method calls.