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
32 changes: 20 additions & 12 deletions dbzero/dbzero/dbzero.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ from .interfaces import (

# Core workspace management functions

def init(path: str, config: Optional[Dict[str, Any]] = None) -> None:
def init(path: str, config: Optional[Dict[str, Any]] = None, lock_flags: Optional[Dict[str, Any]] = None) -> None:
"""Initialize the dbzero environment in a specified directory and apply global configurations.

This function sets up the underlying state management engine.
Expand All @@ -29,8 +29,15 @@ def init(path: str, config: Optional[Dict[str, Any]] = None) -> None:

* autocommit (bool, default True) to enable automatic commits
* autocommit_interval (int, default 250) for commit interval in milliseconds
* cache_size (int, default 4 GiB) for main object cache size in bytes
* cache_size (int, default 2 GiB) for main object cache size in bytes
* lang_cache_size (int, optional) for language model data cache size
lock_flags : dict, optional
Dictionary with configuration of locking behavior when opening the prefix in read-write mode.

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


Examples
--------
Expand Down Expand Up @@ -66,6 +73,7 @@ def open(prefix_name: str, open_mode: str = "rw", **kwargs: Any) -> None:
* autocommit (bool) to disable automatic commits for this prefix
* slab_size (int) for memory slab allocation size in bytes
* meta_io_step_size (int) for metadata I/O operation chunk size
* lock_flags (dict) to change locking behavior when opening the prefix for read-write

Examples
--------
Expand Down Expand Up @@ -148,20 +156,20 @@ def commit(prefix_name: Optional[str] = None) -> None:

# Object retrieval and management

def fetch(id: Union[str, type], type: Optional[type] = None, prefix: Optional[str] = None) -> Memo:
def fetch(identifier: Union[str, type], expected_type: Optional[type] = None, prefix: Optional[str] = None) -> Memo:
"""Retrieve a single object directly from memory using its unique identifier.

The fastest way to access an object, operating in constant time O(1).
It is guaranteed that only one instance of an object exists in memory for a given UUID.

Parameters
----------
id : str or type
identifier : str or type
The identifier for the object you want to retrieve.

* UUID string: Returns the specific object instance for that UUID
* type (singleton class): Returns the unique instance of that singleton
type : type, optional
expected_type : type, optional
Optional type to validate the retrieved object.
Raises exception if the fetched object is not an instance of this type.
prefix : str, optional
Expand Down Expand Up @@ -202,20 +210,20 @@ def fetch(id: Union[str, type], type: Optional[type] = None, prefix: Optional[st
"""
...

def exists(id: Union[str, type], type: Optional[type] = None, prefix: Optional[str] = None) -> bool:
def exists(identifier: Union[str, type], expected_type: Optional[type] = None, prefix: Optional[str] = None) -> bool:
"""Check if a dbzero object exists.

Can check by UUID or by singleton type.
Allows to verify if an object is still available before trying to retrieve it.

Parameters
----------
id : str or type
The object to check for.
identifier : str or type
The identifier for object to check for.

* str: Check for object with its unique identifier
* type: Check for instance of this singleton type
type : type, optional
expected_type : type, optional
Optional expected type when checking by UUID.
Verifies the found object is an instance of this type.
prefix : str, optional
Expand Down Expand Up @@ -548,7 +556,7 @@ def touch(*objects: Memo) -> None:
"""
...

def rename_field(class_obj: type, old_name: str, new_name: str) -> None:
def rename_field(class_obj: type, from_name: str, to_name: str) -> None:
"""Rename a field for a given Memo class.

Modifies the internal field layout for all existing and future instances
Expand All @@ -558,9 +566,9 @@ def rename_field(class_obj: type, old_name: str, new_name: str) -> None:
----------
class_obj : type
The memo type for which to rename the field.
old_name : str
from_name : str
The current name of the field you want to change.
new_name : str
to_name : str
The new name for the field.

Examples
Expand Down
5 changes: 4 additions & 1 deletion dbzero/dbzero/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def find(self, *query_criteria: Union[Tag, List[Tag], Tuple[Tag], QueryObject, T
QueryObject
An iterable query object.
"""
...

def deserialize(self, data: bytes, /) -> Any:
"""Reconstruct a dbzero object from serialized bytes, withing the snapshot context.
Expand All @@ -236,6 +237,7 @@ def deserialize(self, data: bytes, /) -> Any:
Any
A dbzero object that was encoded in the data bytes.
"""
...

def close(self) -> None:
"""Close dbzero snapshot."""
Expand All @@ -248,4 +250,5 @@ def get_state_num(self) -> int:
-------
int
State number of a snapshot.
"""
"""
...
7 changes: 3 additions & 4 deletions python_tests/migration2_init.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dbzero import db0
from dbzero.connection import Connection
import os
import datetime

Expand Down Expand Up @@ -34,13 +33,13 @@ def __init__(self):

def start():
# Configure the dbzero connection without connecting yet
Connection.setup(os.path.join(os.getcwd(), "app-data"), read_write=True, **config, client_app="migration-test")
Connection.assure_initialized()
db0.init(os.path.join(os.getcwd(), "app-data"), config=config)
db0.open(config["prefix"])
root = Root([])
for _ in range(10):
root.value.append(MigrationTestClass())

Connection.close()
db0.close()


if __name__ == "__main__":
Expand Down
7 changes: 3 additions & 4 deletions python_tests/migration2_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dbzero import db0
from dbzero.connection import Connection
import os
import datetime

Expand Down Expand Up @@ -36,16 +35,16 @@ def __init__(self):

def start():
# Configure the dbzero connection without connecting yet
Connection.setup(os.path.join(os.getcwd(), "app-data"), read_write=True, **config, client_app="migration-test")
Connection.assure_initialized()
db0.init(os.path.join(os.getcwd(), "app-data"), config=config)
db0.open(config["prefix"])
root = Root()
print(f"Root has: {len(root.value)} items")
for _ in range(10):
root.value.append(MigrationTestClass())

for item in root.value:
print(f"Item date_value: {item.date_value}")
Connection.close()
db0.close()


if __name__ == "__main__":
Expand Down
7 changes: 3 additions & 4 deletions python_tests/migration_init.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dbzero import db0
from dbzero.connection import Connection
import os

ORG_NAME = "division-by-zero"
Expand All @@ -24,10 +23,10 @@ def __init__(self, value):

def start():
# Configure the dbzero connection without connecting yet
Connection.setup(os.path.join(os.getcwd(), "app-data"), read_write=True, **config, client_app="migration-test")
Connection.assure_initialized()
db0.init(os.path.join(os.getcwd(), "app-data"), config=config)
db0.open(config["prefix"])
obj = MySingleton(123)
Connection.close()
db0.close()


if __name__ == "__main__":
Expand Down
7 changes: 3 additions & 4 deletions python_tests/migration_test_1.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dbzero import db0
from dbzero.connection import Connection
import os

ORG_NAME = "division-by-zero"
Expand Down Expand Up @@ -34,14 +33,14 @@ def items(self):

def start():
# Configure the dbzero connection without connecting yet
Connection.setup(os.path.join(os.getcwd(), "app-data"), read_write=True, **config, client_app="migration-test")
Connection.assure_initialized()
db0.init(os.path.join(os.getcwd(), "app-data"), config=config)
db0.open(config["prefix"])
obj = MySingleton(123)
print(f"int param: {obj.int_param}")
print(f"str param: {obj.str_param}")
# note that migrated singleton should have 3 items
print(list(obj.items))
Connection.close()
db0.close()


if __name__ == "__main__":
Expand Down
7 changes: 3 additions & 4 deletions python_tests/migration_test_2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dbzero import db0
from dbzero.connection import Connection
import os

ORG_NAME = "division-by-zero"
Expand Down Expand Up @@ -44,14 +43,14 @@ def str_items(self):

def start():
# Configure the dbzero connection without connecting yet
Connection.setup(os.path.join(os.getcwd(), "app-data"), read_write=True, **config, client_app="migration-test")
Connection.assure_initialized()
db0.init(os.path.join(os.getcwd(), "app-data"), config=config)
db0.open(config["prefix"])
obj = MySingleton(123)
print(f"int param: {obj.int_param}")
print(f"str param: {obj.str_param}")
print(list(obj.items))
print(dict(obj.str_items))
Connection.close()
db0.close()


if __name__ == "__main__":
Expand Down
15 changes: 0 additions & 15 deletions python_tests/test_connection.py

This file was deleted.

12 changes: 6 additions & 6 deletions python_tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import os


def test_lang_cache_size_can_be_specified_on_init():
if os.path.exists(DB0_DIR):
shutil.rmtree(DB0_DIR)
os.mkdir(DB0_DIR)
db0.init(DB0_DIR, config={"lang_cache_size": 9876})
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.open("my-test-prefix")
assert db0.get_lang_cache_stats()["capacity"] == 9876
db0.close()

stats = db0.get_cache_stats()
assert stats["capacity"] == 123456
Loading