Skip to main content

BufferMap

This class provides an ordered mapping of message buffers with built-in eviction capabilities. It manages a collection of buffers, tracking the total number of items across all keys and enforcing a maximum size through a least-recently-used (LRU) eviction policy. The class supports adding single items or iterables to specific buffers while automatically handling buffer creation and capacity limits.

Attributes

AttributeTypeDescription
Bufferclass = MessagebufferThe class type used to instantiate new message buffers within the map.
Emptyexception = EmptyException class raised when attempting to take an item from an empty buffer.
maxsizeint = NoneMaximum total number of items allowed across all buffers before eviction is triggered.
totalint = 0The current aggregate count of all items stored across every buffer in the map.
bufmaxsizeint = NoneThe maximum capacity assigned to each individual Messagebuffer instance created by the map.

Constructor

Signature

def BufferMap(
maxsize: int,
iterable: Iterable = None,
bufmaxsize: int = 1000
) - > None

Parameters

NameTypeDescription
maxsizeintThe maximum number of items allowed in the map before eviction occurs.
iterableIterable = NoneAn optional collection of items to initialize the buffer map with.
bufmaxsizeint = 1000The maximum size for individual buffers within the map.

Methods


put()

@classmethod
def put(
key: Any,
item: Any
) - > null

Adds a single item to the buffer associated with the specified key and updates the map's total count. This operation marks the key as the most recently used and triggers eviction if the map exceeds its maximum size.

Parameters

NameTypeDescription
keyAnyThe identifier for the specific buffer where the item should be stored.
itemAnyThe data element to be appended to the buffer.

Returns

TypeDescription
nullNothing is returned.

extend()

@classmethod
def extend(
key: Any,
it: Iterable
) - > null

Appends multiple items from an iterable to the buffer associated with the specified key. The total count is incremented by the number of items added, and eviction is performed if necessary.

Parameters

NameTypeDescription
keyAnyThe identifier for the specific buffer to be extended.
itIterableA collection of items to be added to the buffer.

Returns

TypeDescription
nullNothing is returned.

take()

@classmethod
def take(
key: Any,
*default: Any
) - > Any

Retrieves and removes an item from the buffer associated with the key, marking it as recently used. If the buffer is empty or the key does not exist, it returns the provided default or raises an Empty exception.

Parameters

NameTypeDescription
keyAnyThe identifier for the buffer from which to retrieve an item.
*defaultAnyAn optional value to return if the key is missing or the buffer is empty.

Returns

TypeDescription
AnyThe item removed from the buffer, or the default value if provided.