2019-08-01 06:00:12 +08:00
|
|
|
from typing import Dict, List, Optional, Sequence, Tuple
|
2019-07-26 16:44:33 +08:00
|
|
|
|
|
|
|
from .pb import rpc_pb2
|
|
|
|
|
|
|
|
|
2019-07-27 11:27:47 +08:00
|
|
|
class CacheEntry:
|
2019-07-26 16:44:33 +08:00
|
|
|
|
2019-07-27 11:27:47 +08:00
|
|
|
mid: Tuple[bytes, bytes]
|
|
|
|
topics: List[str]
|
2019-07-26 16:44:33 +08:00
|
|
|
|
2019-07-27 11:27:47 +08:00
|
|
|
"""
|
|
|
|
A logical representation of an entry in the mcache's _history_.
|
|
|
|
"""
|
2019-08-01 06:00:12 +08:00
|
|
|
|
2019-07-27 11:27:47 +08:00
|
|
|
def __init__(self, mid: Tuple[bytes, bytes], topics: Sequence[str]) -> None:
|
2019-05-07 11:44:13 +08:00
|
|
|
"""
|
2019-07-27 11:27:47 +08:00
|
|
|
Constructor.
|
|
|
|
:param mid: (seqno, from_id) of the msg
|
|
|
|
:param topics: list of topics this message was sent on
|
2019-05-07 11:44:13 +08:00
|
|
|
"""
|
2019-07-27 11:27:47 +08:00
|
|
|
self.mid = mid
|
|
|
|
self.topics = list(topics)
|
|
|
|
|
|
|
|
|
|
|
|
class MessageCache:
|
|
|
|
|
2019-07-26 16:44:33 +08:00
|
|
|
window_size: int
|
|
|
|
history_size: int
|
|
|
|
|
|
|
|
msgs: Dict[Tuple[bytes, bytes], rpc_pb2.Message]
|
|
|
|
|
2019-07-27 11:27:47 +08:00
|
|
|
history: List[List[CacheEntry]]
|
2019-07-26 16:44:33 +08:00
|
|
|
|
|
|
|
def __init__(self, window_size: int, history_size: int) -> None:
|
2019-05-07 11:44:13 +08:00
|
|
|
"""
|
|
|
|
Constructor.
|
|
|
|
:param window_size: Size of the window desired.
|
|
|
|
:param history_size: Size of the history desired.
|
|
|
|
:return: the MessageCache
|
|
|
|
"""
|
|
|
|
self.window_size = window_size
|
|
|
|
self.history_size = history_size
|
|
|
|
|
|
|
|
# (seqno, from_id) -> rpc message
|
|
|
|
self.msgs = dict()
|
|
|
|
|
|
|
|
# max length of history_size. each item is a list of CacheEntry.
|
|
|
|
# messages lost upon shift().
|
2019-08-01 06:00:12 +08:00
|
|
|
self.history = [[] for _ in range(history_size)]
|
2019-05-07 11:44:13 +08:00
|
|
|
|
2019-07-26 16:44:33 +08:00
|
|
|
def put(self, msg: rpc_pb2.Message) -> None:
|
2019-05-07 11:44:13 +08:00
|
|
|
"""
|
|
|
|
Put a message into the mcache.
|
|
|
|
:param msg: The rpc message to put in. Should contain seqno and from_id
|
|
|
|
"""
|
2019-07-26 16:44:33 +08:00
|
|
|
mid: Tuple[bytes, bytes] = (msg.seqno, msg.from_id)
|
2019-05-07 11:44:13 +08:00
|
|
|
self.msgs[mid] = msg
|
|
|
|
|
2019-07-27 11:27:47 +08:00
|
|
|
self.history[0].append(CacheEntry(mid, msg.topicIDs))
|
2019-05-07 11:44:13 +08:00
|
|
|
|
2019-07-26 16:44:33 +08:00
|
|
|
def get(self, mid: Tuple[bytes, bytes]) -> Optional[rpc_pb2.Message]:
|
2019-05-07 11:44:13 +08:00
|
|
|
"""
|
|
|
|
Get a message from the mcache.
|
|
|
|
:param mid: (seqno, from_id) of the message to get.
|
|
|
|
:return: The rpc message associated with this mid
|
|
|
|
"""
|
|
|
|
if mid in self.msgs:
|
|
|
|
return self.msgs[mid]
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2019-07-26 16:44:33 +08:00
|
|
|
def window(self, topic: str) -> List[Tuple[bytes, bytes]]:
|
2019-05-07 11:44:13 +08:00
|
|
|
"""
|
|
|
|
Get the window for this topic.
|
|
|
|
:param topic: Topic whose message ids we desire.
|
|
|
|
:return: List of mids in the current window.
|
|
|
|
"""
|
2019-07-26 16:44:33 +08:00
|
|
|
mids: List[Tuple[bytes, bytes]] = []
|
2019-05-07 11:44:13 +08:00
|
|
|
|
|
|
|
for entries_list in self.history[: self.window_size]:
|
|
|
|
for entry in entries_list:
|
|
|
|
for entry_topic in entry.topics:
|
|
|
|
if entry_topic == topic:
|
|
|
|
mids.append(entry.mid)
|
|
|
|
|
|
|
|
return mids
|
|
|
|
|
2019-07-26 16:44:33 +08:00
|
|
|
def shift(self) -> None:
|
2019-05-07 11:44:13 +08:00
|
|
|
"""
|
|
|
|
Shift the window over by 1 position, dropping the last element of the history.
|
|
|
|
"""
|
2019-07-26 16:44:33 +08:00
|
|
|
last_entries: List[CacheEntry] = self.history[len(self.history) - 1]
|
2019-05-07 11:44:13 +08:00
|
|
|
|
|
|
|
for entry in last_entries:
|
|
|
|
del self.msgs[entry.mid]
|
|
|
|
|
2019-07-26 16:44:33 +08:00
|
|
|
i: int = len(self.history) - 2
|
2019-05-07 11:44:13 +08:00
|
|
|
|
|
|
|
while i >= 0:
|
|
|
|
self.history[i + 1] = self.history[i]
|
|
|
|
i -= 1
|
|
|
|
|
|
|
|
self.history[0] = []
|