Skip to content

Commit 1c8c8f4

Browse files
authored
Merge pull request #36 from KenyonY/feat-singleton
feat: Using singleton mode
2 parents b8a2388 + 7b096cb commit 1c8c8f4

File tree

6 files changed

+63
-24
lines changed

6 files changed

+63
-24
lines changed

flaxkv/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919

2020
from .core import LevelDBDict, LMDBDict, RemoteDBDict
2121

22-
__version__ = "0.2.7.1"
22+
__version__ = "0.2.8"
2323

2424
__all__ = [
2525
"FlaxKV",
26+
"Flaxkv",
2627
"dbdict",
2728
"dictdb",
2829
"LMDBDict",
@@ -79,3 +80,4 @@ def FlaxKV(
7980

8081
dbdict = FlaxKV
8182
dictdb = FlaxKV
83+
Flaxkv = FlaxKV

flaxkv/core.py

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,17 @@ def update(self, d: dict):
410410
self._buffered_count = 0
411411
self.write_immediately()
412412

413+
def from_dict(self, d: dict, clear=False):
414+
"""
415+
Updates the buffer with the given dictionary.
416+
417+
Args:
418+
d (dict): A dictionary of key-value pairs to update.
419+
"""
420+
if clear:
421+
self.clear(wait=True)
422+
self.update(d)
423+
413424
# @class_measure_time()
414425
def _write_buffer_to_db(
415426
self,
@@ -831,6 +842,14 @@ class LMDBDict(BaseDBDict):
831842
value: int, float, bool, str, list, dict, and np.ndarray,
832843
"""
833844

845+
_instances = {}
846+
847+
def __new__(cls, db_name: str, root_path: str, rebuild=False, **kwargs):
848+
name = db_name + str(root_path)
849+
if name not in cls._instances:
850+
cls._instances[name] = super().__new__(cls)
851+
return cls._instances[name]
852+
834853
def __init__(
835854
self,
836855
db_name: str,
@@ -839,15 +858,17 @@ def __init__(
839858
rebuild=False,
840859
**kwargs,
841860
):
842-
super().__init__(
843-
"lmdb",
844-
root_path,
845-
db_name,
846-
max_dbs=1,
847-
map_size=map_size,
848-
rebuild=rebuild,
849-
**kwargs,
850-
)
861+
if not hasattr(self, '_initialized'):
862+
super().__init__(
863+
"lmdb",
864+
root_path,
865+
db_name,
866+
max_dbs=1,
867+
map_size=map_size,
868+
rebuild=rebuild,
869+
**kwargs,
870+
)
871+
self._initialized = True
851872

852873
def _iter_db_view(self, view, include_key=True, include_value=True):
853874
"""
@@ -899,14 +920,25 @@ class LevelDBDict(BaseDBDict):
899920
value: int, float, bool, str, list, dict and np.ndarray,
900921
"""
901922

923+
_instances = {}
924+
925+
def __new__(cls, db_name: str, root_path: str, rebuild=False, **kwargs):
926+
name = db_name + str(root_path)
927+
if name not in cls._instances:
928+
cls._instances[name] = super().__new__(cls)
929+
return cls._instances[name]
930+
902931
def __init__(self, db_name: str, root_path: str, rebuild=False, **kwargs):
903-
super().__init__(
904-
"leveldb",
905-
root_path_or_url=root_path,
906-
db_name=db_name,
907-
rebuild=rebuild,
908-
**kwargs,
909-
)
932+
if not hasattr(self, '_initialized'):
933+
super().__init__(
934+
"leveldb",
935+
root_path_or_url=root_path,
936+
db_name=db_name,
937+
rebuild=rebuild,
938+
**kwargs,
939+
)
940+
941+
self._initialized = True
910942

911943
def _iter_db_view(self, view, include_key=True, include_value=True):
912944
"""

flaxkv/manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import time
2424
import traceback
2525
from pathlib import Path
26-
from typing import Dict
26+
from typing import Dict, Literal
2727
from uuid import uuid4
2828

2929
import msgspec

flaxkv/serve/route.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ async def connect(data: AttachRequest) -> Stream:
109109
db_name=data.db_name, backend=data.backend, rebuild=data.rebuild
110110
)
111111
elif data.rebuild:
112-
db.destroy()
112+
db.clear(wait=True)
113113
_db_manager.set_db(
114-
db_name=data.db_name, backend=data.backend, rebuild=False
114+
db_name=data.db_name, backend=data.backend, rebuild=data.rebuild
115115
)
116116

117117
async def stream(client: dict) -> AsyncGenerator:

tests/test_local_db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def temp_db(request):
4747
db = FlaxKV(**request.param)
4848

4949
yield db
50-
db.destroy()
50+
db.clear(wait=True)
5151

5252

5353
def test_set_get_write(temp_db):

tests/test_remote_db.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ def start_server():
3636
@pytest.fixture(
3737
scope="function",
3838
params=[
39-
# dict(db_name="test_server_db", backend="lmdb", rebuild=True, cache=False),
39+
dict(db_name="test_server_db", backend="lmdb", rebuild=True, cache=False),
4040
dict(db_name="test_server_db", backend="leveldb", rebuild=True, cache=False),
41-
# dict(db_name="test_server_db", backend="lmdb", rebuild=True, cache=True),
41+
dict(db_name="test_server_db", backend="lmdb", rebuild=True, cache=True),
4242
dict(db_name="test_server_db", backend="leveldb", rebuild=True, cache=True),
4343
],
4444
)
@@ -57,8 +57,13 @@ def temp_db(request):
5757
db.close(wait=True)
5858

5959

60-
from test_local_db import ( # test_large_value,; test_list_keys_values_items,; test_set_get_write,; test_setdefault,; test_update,
60+
from test_local_db import (
6161
test_buffered_writing,
6262
test_key_checks_and_deletion,
63+
test_large_value,
64+
test_list_keys_values_items,
6365
test_numpy_array,
66+
test_set_get_write,
67+
test_setdefault,
68+
test_update,
6469
)

0 commit comments

Comments
 (0)