Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dbzero/dbzero/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from .storage_api import *
from .atomic import *
from .locked import *
from .connection import *
from .utilities import taggify
from .decorators import *
from .select import *
from .compare import *
from .compare import *
from .initialization import init
62 changes: 0 additions & 62 deletions dbzero/dbzero/connection.py

This file was deleted.

53 changes: 53 additions & 0 deletions dbzero/dbzero/initialization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""dbzero initialization functions"""
from .dbzero import _init, open as dbzero_open

def init(dbzero_root: str, **kwargs) -> None:
"""Initialize the dbzero environment in a specified directory and apply global configurations.

This function sets up the underlying state management engine.
It must be called once before interacting with any data.
If you need to switch to a different working directory, you should first call
dbzero.close() and then dbzero.init() again with the new path.

Parameters
----------
dbzero_root : str
The path to dbzero data files directory. If the directory doesn't exist,
it will be created.
**kwargs : dict
Additional keyword arguments:
* prefix (str) shortcut to open a prefix after initialization
* read_write (bool, default True) set the open mode for the prefix

Configure global dbzero behavior:
* autocommit (bool, default True) to enable automatic commits
* autocommit_interval (int, default 250) for commit interval in milliseconds
* cache_size (int, default 2 GiB) for main object cache size in bytes
* lang_cache_size (int, default 1024) for language model data cache size
* lock_flags (dict) to configure locking behavior when opening the prefix in read-write mode

Lock flags (dict):
* blocking (bool, default False) wait when trying to acquire the lock
* timeout (int) maximum waiting time in seconds when blocking wait is enabled
* force_unlock (bool, default False) force unlocking of existing lock
"""

init_kwargs = {}

config_keys = ("autocommit", "autocommit_interval", "cache_size", "lang_cache_size")
config = {}
for key in config_keys:
if key in kwargs:
config[key] = kwargs[key]

if config:
init_kwargs["config"] = config

if "lock_flags" in kwargs:
init_kwargs["lock_flags"] = kwargs["lock_flags"]

_init(dbzero_root, **init_kwargs)

if "prefix" in kwargs:
open_mode = "rw" if kwargs.get("read_write", True) else "r"
dbzero_open(kwargs["prefix"], open_mode=open_mode)
2 changes: 1 addition & 1 deletion dbzero/dbzero/storage_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def get_prefixes() -> Iterator[PrefixMetaData]:
Discovering and opening all available prefixes:

>>> # First, initialize the dbzero environment
>>> dbzero.init(path="/path/to/my/data")
>>> dbzero.init(dbzero_root="/path/to/my/data")
>>>
>>> # Discover all available prefixes
>>> all_prefixes = dbzero.get_prefixes()
Expand Down
15 changes: 8 additions & 7 deletions python_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ def db0_slab_size(request):
shutil.rmtree(DB0_DIR)
# create empty directory
os.mkdir(DB0_DIR)
db0.init(DB0_DIR, config = {
"autocommit": request.param.get("autocommit", True),
"autocommit_interval": request.param.get("autocommit_interval", 250),
})
db0.init(
DB0_DIR,
autocommit=request.param.get("autocommit", True),
autocommit_interval=request.param.get("autocommit_interval", 250),
)
db0.open("my-test-prefix", slab_size=request.param["slab_size"])
yield db0
gc.collect()
Expand All @@ -72,7 +73,7 @@ def db0_autocommit_fixture(request):
shutil.rmtree(DB0_DIR)
# create empty directory
os.mkdir(DB0_DIR)
db0.init(DB0_DIR, config = {"autocommit": True, "autocommit_interval": request.param})
db0.init(DB0_DIR, autocommit=True, autocommit_interval=request.param)
db0.open("my-test-prefix")
yield db0
gc.collect()
Expand All @@ -91,7 +92,7 @@ def db0_no_autocommit():
# create empty directory
os.mkdir(DB0_DIR)
# disable autocommit on all prefixes
db0.init(DB0_DIR, config = {"autocommit": False})
db0.init(DB0_DIR, autocommit=False)
db0.open("my-test-prefix")
yield db0
db0.close()
Expand Down Expand Up @@ -160,7 +161,7 @@ def db0_metaio_fixture():
shutil.rmtree(DB0_DIR)
# create empty directory
os.mkdir(DB0_DIR)
db0.init(DB0_DIR, config = {"autocommit": False})
db0.init(DB0_DIR, autocommit=False)
db0.open("my-test-prefix", meta_io_step_size=16)
yield db0
gc.collect()
Expand Down
2 changes: 1 addition & 1 deletion python_tests/migration2_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self):

def start():
# Configure the dbzero connection without connecting yet
db0.init(os.path.join(os.getcwd(), "app-data"), config=config)
db0.init(os.path.join(os.getcwd(), "app-data"), **config)
db0.open(config["prefix"])
root = Root([])
for _ in range(10):
Expand Down
2 changes: 1 addition & 1 deletion python_tests/migration2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self):

def start():
# Configure the dbzero connection without connecting yet
db0.init(os.path.join(os.getcwd(), "app-data"), config=config)
db0.init(os.path.join(os.getcwd(), "app-data"), **config)
db0.open(config["prefix"])
root = Root()
print(f"Root has: {len(root.value)} items")
Expand Down
2 changes: 1 addition & 1 deletion python_tests/migration_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, value):

def start():
# Configure the dbzero connection without connecting yet
db0.init(os.path.join(os.getcwd(), "app-data"), config=config)
db0.init(os.path.join(os.getcwd(), "app-data") **config)
db0.open(config["prefix"])
obj = MySingleton(123)
db0.close()
Expand Down
2 changes: 1 addition & 1 deletion python_tests/migration_test_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def items(self):

def start():
# Configure the dbzero connection without connecting yet
db0.init(os.path.join(os.getcwd(), "app-data"), config=config)
db0.init(os.path.join(os.getcwd(), "app-data"), **config)
db0.open(config["prefix"])
obj = MySingleton(123)
print(f"int param: {obj.int_param}")
Expand Down
2 changes: 1 addition & 1 deletion python_tests/migration_test_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def str_items(self):

def start():
# Configure the dbzero connection without connecting yet
db0.init(os.path.join(os.getcwd(), "app-data"), config=config)
db0.init(os.path.join(os.getcwd(), "app-data"), **config)
db0.open(config["prefix"])
obj = MySingleton(123)
print(f"int param: {obj.int_param}")
Expand Down
4 changes: 2 additions & 2 deletions python_tests/test_autocommit.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ def test_autocommit_config(db0_fixture):
assert cfg['autocommit_interval'] == 250

db0.close()
db0.init(DB0_DIR, config={'autocommit': False, 'autocommit_interval': 1000})
db0.init(DB0_DIR, autocommit=False, autocommit_interval=1000)
cfg = db0.get_config()
assert cfg['autocommit'] == False
assert cfg['autocommit_interval'] == 1000

db0.close()
db0.init(DB0_DIR, config={'autocommit': False})
db0.init(DB0_DIR, autocommit=False)
cfg = db0.get_config()
assert cfg['autocommit'] == False
assert cfg['autocommit_interval'] == 250
Expand Down
2 changes: 1 addition & 1 deletion python_tests/test_crash_recovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def rand_chars(length):

# start a child process that will change the singleton
def generator_process(op_size, op_count, crash_after):
db0.init(DB0_DIR, config = {"autocommit": False})
db0.init(DB0_DIR, autocommit=False)
db0.open(px_name, "rw")
root = MemoTestSingleton()
next_id = len(root.value)
Expand Down
2 changes: 1 addition & 1 deletion python_tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def test_cache_size_can_be_specified_on_init(db0_fixture):
db0.close()
db0.init(DB0_DIR, config={"cache_size": 123456, "lang_cache_size": 9876})
db0.init(DB0_DIR, cache_size=123456, lang_cache_size=9876)
db0.open("my-test-prefix")
assert db0.get_lang_cache_stats()["capacity"] == 9876

Expand Down
4 changes: 4 additions & 0 deletions python_tests/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ def test_load_set_of_objects(db0_fixture):
assert loaded == [{"value": "a"}, {"value": "b"}]

class TestObject:
__test__ = False

def __init__(self, value, other_value):
self.value = value
self.other_value = other_value
Expand All @@ -260,6 +262,8 @@ def test_load_python_object(db0_fixture):

@db0.memo
class TestObjectNoLoad:
__test__ = False

def __init__(self, value, other_value):
self.value = value
self.other_value = other_value
Expand Down
4 changes: 4 additions & 0 deletions python_tests/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,16 @@ def test_select_new_miltiple_prefixes(db0_fixture, memo_tags):

@db0.memo(prefix="some/test/prefix")
class TestClassWithPrefix:
__test__ = False

def __init__(self, value):
self.value = value


@db0.memo(prefix="/some/test/prefix/x")
class TestClassWithPrefixStartingWithSlash:
__test__ = False

def __init__(self, value):
self.value = value

Expand Down
2 changes: 1 addition & 1 deletion samples/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __main__():
parser.add_argument('--uuid', default=None, type=str, help="UUID of the object to be shown")
args = parser.parse_args()

db0.init(path=args.path)
db0.init(dbzero_root=args.path)
try:
for prefix in db0.get_prefixes():
# open prefix to make it the default one
Expand Down
2 changes: 1 addition & 1 deletion samples/explore_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __main__():
print(f"Query args: {query_args}")

try:
db0.init(path=args.path)
db0.init(dbzero_root=args.path)
db0.init_fast_query(prefix="fq_cache")
# open fq_cache as the initially default prefix
db0.open("fq_cache")
Expand Down
2 changes: 1 addition & 1 deletion src/dbzero/bindings/python/dbzero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace py = db0::python;

static PyMethodDef dbzero_methods[] =
{
{"init", (PyCFunction)&py::PyAPI_init, METH_VARARGS | METH_KEYWORDS, "Initialize dbzero CE workspace at a specific root path"},
{"_init", (PyCFunction)&py::PyAPI_init, METH_VARARGS | METH_KEYWORDS, "Initialize dbzero workspace at a specific root path"},
{"open", (PyCFunction)&py::PyAPI_open, METH_VARARGS | METH_KEYWORDS, "Open or create a prefix for read or read/write"},
{"close", &py::PyAPI_close, METH_VARARGS, ""},
{"drop", &py::PyAPI_drop, METH_VARARGS, "Drop prefix (if exists)"},
Expand Down