|
| 1 | +# Copyright 2016 OpenMarket Ltd |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from typing import List, Optional, Tuple |
| 15 | + |
| 16 | +from synapse.storage.database import LoggingDatabaseConnection |
| 17 | +from synapse.storage.util.id_generators import AbstractStreamIdTracker, _load_current_id |
| 18 | + |
| 19 | + |
| 20 | +class SlavedIdTracker(AbstractStreamIdTracker): |
| 21 | + """Tracks the "current" stream ID of a stream with a single writer. |
| 22 | +
|
| 23 | + See `AbstractStreamIdTracker` for more details. |
| 24 | +
|
| 25 | + Note that this class does not work correctly when there are multiple |
| 26 | + writers. |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self, |
| 31 | + db_conn: LoggingDatabaseConnection, |
| 32 | + table: str, |
| 33 | + column: str, |
| 34 | + extra_tables: Optional[List[Tuple[str, str]]] = None, |
| 35 | + step: int = 1, |
| 36 | + ): |
| 37 | + self.step = step |
| 38 | + self._current = _load_current_id(db_conn, table, column, step) |
| 39 | + if extra_tables: |
| 40 | + for table, column in extra_tables: |
| 41 | + self.advance(None, _load_current_id(db_conn, table, column)) |
| 42 | + |
| 43 | + def advance(self, instance_name: Optional[str], new_id: int) -> None: |
| 44 | + self._current = (max if self.step > 0 else min)(self._current, new_id) |
| 45 | + |
| 46 | + def get_current_token(self) -> int: |
| 47 | + return self._current |
| 48 | + |
| 49 | + def get_current_token_for_writer(self, instance_name: str) -> int: |
| 50 | + return self.get_current_token() |
0 commit comments