|
3 | 3 | import utils |
4 | 4 | from os import makedirs |
5 | 5 | from easyrocks import RocksDB, WriteBatch, CompressionType |
| 6 | +from easyrocks.utils import int_to_padded_bytes |
6 | 7 | from threading import Lock |
7 | 8 | import logging |
8 | 9 | from typing import Dict |
9 | 10 |
|
| 11 | +UINT_BYTES = 8 |
| 12 | +MAX_UINT = 2**(UINT_BYTES * 8) - 1 |
| 13 | + |
10 | 14 |
|
11 | 15 | class PartitionItem: |
12 | 16 | def __init__(self, |
@@ -39,6 +43,10 @@ def _load_from_dict(self, value: Dict): |
39 | 43 |
|
40 | 44 |
|
41 | 45 | class Partition: |
| 46 | + MESSAGE = b'\x00' |
| 47 | + INDEX = b'\x01' |
| 48 | + OFFSET = b'\x02' |
| 49 | + |
42 | 50 | def __init__(self, |
43 | 51 | stream: str, |
44 | 52 | number: int, |
@@ -114,7 +122,7 @@ def set_offset(self, receiver: str, offset: int): |
114 | 122 | offset_key = self._get_offset_key(receiver) |
115 | 123 | self._store.put(offset_key, offset) |
116 | 124 |
|
117 | | - def prune(self, ttl): |
| 125 | + def prune(self, ttl: int): |
118 | 126 | ttl *= 1000 # milliseconds |
119 | 127 |
|
120 | 128 | current_timestamp = utils.get_timestamp_ms() |
@@ -149,43 +157,40 @@ def _get_by_index(self, index: int) -> bytes: |
149 | 157 | partition_item = PartitionItem(item_dict=value) |
150 | 158 | return partition_item |
151 | 159 |
|
152 | | - def _get_index(self): |
153 | | - index_key = self._get_index_key() |
| 160 | + def _get_index(self) -> int: |
| 161 | + index_key = Partition.INDEX |
154 | 162 | index = self._store.get(index_key) |
155 | 163 | if index is None: |
156 | 164 | index = -1 |
157 | 165 | return index |
158 | 166 |
|
159 | | - def _increase_index(self, write_batch): |
160 | | - index_key = self._get_index_key() |
161 | | - index = self._get_index() |
162 | | - index += 1 |
163 | | - self._store.put(index_key, index, write_batch=write_batch) |
164 | | - |
165 | | - def _get_offset(self, receiver: str): |
| 167 | + def _get_offset(self, receiver: str) -> int: |
166 | 168 | offset_key = self._get_offset_key(receiver) |
167 | 169 | offset = self._store.get(offset_key) |
168 | 170 | if offset is None: |
169 | 171 | offset = -1 |
170 | 172 | return offset |
171 | 173 |
|
| 174 | + def _increase_index(self, write_batch: WriteBatch): |
| 175 | + next_index = self._get_index() + 1 |
| 176 | + if next_index > MAX_UINT: |
| 177 | + raise ValueError(next_index) |
| 178 | + index_key = Partition.INDEX |
| 179 | + self._store.put(index_key, next_index, write_batch=write_batch) |
| 180 | + |
172 | 181 | def _increase_offset(self, receiver: str): |
| 182 | + next_offset = self._get_offset(receiver) + 1 |
| 183 | + if next_offset > MAX_UINT: |
| 184 | + raise ValueError(next_offset) |
173 | 185 | offset_key = self._get_offset_key(receiver) |
174 | | - offset = self._get_offset(receiver) |
175 | | - offset += 1 |
176 | | - self._store.put(offset_key, offset) |
177 | | - |
178 | | - @staticmethod |
179 | | - def _get_index_key(): |
180 | | - index_key = utils.get_padded_string('', prefix='_index:') |
181 | | - return index_key |
| 186 | + self._store.put(offset_key, next_offset) |
182 | 187 |
|
183 | 188 | @staticmethod |
184 | | - def _get_offset_key(receiver): |
185 | | - offset_key = utils.get_padded_string(receiver, prefix='_offset:') |
| 189 | + def _get_offset_key(receiver: str) -> bytes: |
| 190 | + offset_key = Partition.OFFSET + bytes(receiver, 'utf-8') |
186 | 191 | return offset_key |
187 | 192 |
|
188 | 193 | @staticmethod |
189 | | - def _get_message_key(index): |
190 | | - message_key = utils.get_padded_string(str(index), prefix='message:') |
| 194 | + def _get_message_key(index: int) -> bytes: |
| 195 | + message_key = Partition.MESSAGE + int_to_padded_bytes(index, UINT_BYTES) |
191 | 196 | return message_key |
0 commit comments