operation_data.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. from typing import Any
  10. from psycopg import Cursor
  11. class OperationData:
  12. def __init__(self, cursor: Cursor, worker_id: int):
  13. self._data: dict[str, Any] = dict()
  14. self._data["cursor"] = cursor
  15. self._data["worker_id"] = worker_id
  16. def cursor(self) -> Cursor:
  17. return self._data["cursor"]
  18. def worker_id(self) -> Cursor:
  19. return self._data["worker_id"]
  20. def push(self, key: str, value: Any) -> None:
  21. self._data[key] = value
  22. def remove(self, key: str) -> None:
  23. self._data.pop(key, None)
  24. def get(self, key: str) -> Any:
  25. if key not in self._data.keys():
  26. raise RuntimeError(f"Key does not exist: {key}")
  27. return self._data[key]
  28. def validate_requirements(
  29. self, expected_keys: set[str], required_by: type[Any], requirement: str
  30. ) -> None:
  31. for key in expected_keys:
  32. if key not in self._data.keys():
  33. raise RuntimeError(
  34. f"{required_by.__name__} {requirement} '{key}' but got only: {self._data.keys()}"
  35. )