|
| 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) |
0 commit comments