| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | |
| 7 | from .api import * # noqa: F403 |
| 8 | from .config import * # noqa: F403 |
| 9 | |
| 10 | |
| 11 | def kvstore_dependencies(): |
| 12 | return ["aiosqlite", "psycopg2-binary", "redis"] |
| 13 | |
| 14 | |
| 15 | class InmemoryKVStoreImpl(KVStore): |
| 16 | def __init__(self): |
| 17 | self._store = {} |
| 18 | |
| 19 | async def initialize(self) -> None: |
| 20 | pass |
| 21 | |
| 22 | async def get(self, key: str) -> Optional[str]: |
| 23 | return self._store.get(key) |
| 24 | |
| 25 | async def set(self, key: str, value: str) -> None: |
| 26 | self._store[key] = value |
| 27 | |
| 28 | async def range(self, start_key: str, end_key: str) -> List[str]: |
| 29 | return [ |
| 30 | self._store[key] |
| 31 | for key in self._store.keys() |
| 32 | if key >= start_key and key < end_key |
| 33 | ] |
| 34 | |
| 35 | |
| 36 | async def kvstore_impl(config: KVStoreConfig) -> KVStore: |
| 37 | if config.type == KVStoreType.redis.value: |
| 38 | from .redis import RedisKVStoreImpl |
| 39 | |
| 40 | impl = RedisKVStoreImpl(config) |
| 41 | elif config.type == KVStoreType.sqlite.value: |
| 42 | from .sqlite import SqliteKVStoreImpl |
| 43 | |
| 44 | impl = SqliteKVStoreImpl(config) |
| 45 | elif config.type == KVStoreType.postgres.value: |
| 46 | raise NotImplementedError() |
| 47 | else: |
| 48 | raise ValueError(f"Unknown kvstore type {config.type}") |
| 49 | |
| 50 | await impl.initialize() |
| 51 | return impl |