Skip to content

Commit 403ac44

Browse files
sigsegvdevTomasz Wejroch
andauthored
Changes to init syntax (#460)
Co-authored-by: Tomasz Wejroch <twejroch@dbzero.io>
1 parent 66771e8 commit 403ac44

18 files changed

Lines changed: 84 additions & 84 deletions

‎dbzero/dbzero/__init__.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from .storage_api import *
77
from .atomic import *
88
from .locked import *
9-
from .connection import *
109
from .utilities import taggify
1110
from .decorators import *
1211
from .select import *
13-
from .compare import *
12+
from .compare import *
13+
from .initialization import init

‎dbzero/dbzero/connection.py‎

Lines changed: 0 additions & 62 deletions
This file was deleted.

‎dbzero/dbzero/initialization.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""dbzero initialization functions"""
2+
from .dbzero import _init, open as dbzero_open
3+
4+
def init(dbzero_root: str, **kwargs) -> None:
5+
"""Initialize the dbzero environment in a specified directory and apply global configurations.
6+
7+
This function sets up the underlying state management engine.
8+
It must be called once before interacting with any data.
9+
If you need to switch to a different working directory, you should first call
10+
dbzero.close() and then dbzero.init() again with the new path.
11+
12+
Parameters
13+
----------
14+
dbzero_root : str
15+
The path to dbzero data files directory. If the directory doesn't exist,
16+
it will be created.
17+
**kwargs : dict
18+
Additional keyword arguments:
19+
* prefix (str) shortcut to open a prefix after initialization
20+
* read_write (bool, default True) set the open mode for the prefix
21+
22+
Configure global dbzero behavior:
23+
* autocommit (bool, default True) to enable automatic commits
24+
* autocommit_interval (int, default 250) for commit interval in milliseconds
25+
* cache_size (int, default 2 GiB) for main object cache size in bytes
26+
* lang_cache_size (int, default 1024) for language model data cache size
27+
* lock_flags (dict) to configure locking behavior when opening the prefix in read-write mode
28+
29+
Lock flags (dict):
30+
* blocking (bool, default False) wait when trying to acquire the lock
31+
* timeout (int) maximum waiting time in seconds when blocking wait is enabled
32+
* force_unlock (bool, default False) force unlocking of existing lock
33+
"""
34+
35+
init_kwargs = {}
36+
37+
config_keys = ("autocommit", "autocommit_interval", "cache_size", "lang_cache_size")
38+
config = {}
39+
for key in config_keys:
40+
if key in kwargs:
41+
config[key] = kwargs[key]
42+
43+
if config:
44+
init_kwargs["config"] = config
45+
46+
if "lock_flags" in kwargs:
47+
init_kwargs["lock_flags"] = kwargs["lock_flags"]
48+
49+
_init(dbzero_root, **init_kwargs)
50+
51+
if "prefix" in kwargs:
52+
open_mode = "rw" if kwargs.get("read_write", True) else "r"
53+
dbzero_open(kwargs["prefix"], open_mode=open_mode)

‎dbzero/dbzero/storage_api.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def get_prefixes() -> Iterator[PrefixMetaData]:
1818
Discovering and opening all available prefixes:
1919
2020
>>> # First, initialize the dbzero environment
21-
>>> dbzero.init(path="/path/to/my/data")
21+
>>> dbzero.init(dbzero_root="/path/to/my/data")
2222
>>>
2323
>>> # Discover all available prefixes
2424
>>> all_prefixes = dbzero.get_prefixes()

‎python_tests/conftest.py‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ def db0_slab_size(request):
5151
shutil.rmtree(DB0_DIR)
5252
# create empty directory
5353
os.mkdir(DB0_DIR)
54-
db0.init(DB0_DIR, config = {
55-
"autocommit": request.param.get("autocommit", True),
56-
"autocommit_interval": request.param.get("autocommit_interval", 250),
57-
})
54+
db0.init(
55+
DB0_DIR,
56+
autocommit=request.param.get("autocommit", True),
57+
autocommit_interval=request.param.get("autocommit_interval", 250),
58+
)
5859
db0.open("my-test-prefix", slab_size=request.param["slab_size"])
5960
yield db0
6061
gc.collect()
@@ -72,7 +73,7 @@ def db0_autocommit_fixture(request):
7273
shutil.rmtree(DB0_DIR)
7374
# create empty directory
7475
os.mkdir(DB0_DIR)
75-
db0.init(DB0_DIR, config = {"autocommit": True, "autocommit_interval": request.param})
76+
db0.init(DB0_DIR, autocommit=True, autocommit_interval=request.param)
7677
db0.open("my-test-prefix")
7778
yield db0
7879
gc.collect()
@@ -91,7 +92,7 @@ def db0_no_autocommit():
9192
# create empty directory
9293
os.mkdir(DB0_DIR)
9394
# disable autocommit on all prefixes
94-
db0.init(DB0_DIR, config = {"autocommit": False})
95+
db0.init(DB0_DIR, autocommit=False)
9596
db0.open("my-test-prefix")
9697
yield db0
9798
db0.close()
@@ -160,7 +161,7 @@ def db0_metaio_fixture():
160161
shutil.rmtree(DB0_DIR)
161162
# create empty directory
162163
os.mkdir(DB0_DIR)
163-
db0.init(DB0_DIR, config = {"autocommit": False})
164+
db0.init(DB0_DIR, autocommit=False)
164165
db0.open("my-test-prefix", meta_io_step_size=16)
165166
yield db0
166167
gc.collect()

‎python_tests/migration2_init.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(self):
3333

3434
def start():
3535
# Configure the dbzero connection without connecting yet
36-
db0.init(os.path.join(os.getcwd(), "app-data"), config=config)
36+
db0.init(os.path.join(os.getcwd(), "app-data"), **config)
3737
db0.open(config["prefix"])
3838
root = Root([])
3939
for _ in range(10):

‎python_tests/migration2_test.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(self):
3535

3636
def start():
3737
# Configure the dbzero connection without connecting yet
38-
db0.init(os.path.join(os.getcwd(), "app-data"), config=config)
38+
db0.init(os.path.join(os.getcwd(), "app-data"), **config)
3939
db0.open(config["prefix"])
4040
root = Root()
4141
print(f"Root has: {len(root.value)} items")

‎python_tests/migration_init.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, value):
2323

2424
def start():
2525
# Configure the dbzero connection without connecting yet
26-
db0.init(os.path.join(os.getcwd(), "app-data"), config=config)
26+
db0.init(os.path.join(os.getcwd(), "app-data") **config)
2727
db0.open(config["prefix"])
2828
obj = MySingleton(123)
2929
db0.close()

‎python_tests/migration_test_1.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def items(self):
3333

3434
def start():
3535
# Configure the dbzero connection without connecting yet
36-
db0.init(os.path.join(os.getcwd(), "app-data"), config=config)
36+
db0.init(os.path.join(os.getcwd(), "app-data"), **config)
3737
db0.open(config["prefix"])
3838
obj = MySingleton(123)
3939
print(f"int param: {obj.int_param}")

‎python_tests/migration_test_2.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def str_items(self):
4343

4444
def start():
4545
# Configure the dbzero connection without connecting yet
46-
db0.init(os.path.join(os.getcwd(), "app-data"), config=config)
46+
db0.init(os.path.join(os.getcwd(), "app-data"), **config)
4747
db0.open(config["prefix"])
4848
obj = MySingleton(123)
4949
print(f"int param: {obj.int_param}")

0 commit comments

Comments
 (0)