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
5 changes: 3 additions & 2 deletions src_cpp/py_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ PyDatabase::PyDatabase(const std::string& databasePath, std::optional<uint64_t>
// Start from the engine defaults and only apply explicitly set settings,
// so engine-side default changes propagate automatically.
auto systemConfig = SystemConfig();
if (bufferPoolSize.has_value()) {
// The SystemConfig constructor reads 0 as "use the default"; assigning it here would store 0.
if (bufferPoolSize.has_value() && bufferPoolSize.value() != 0) {
systemConfig.bufferPoolSize = bufferPoolSize.value();
}
if (maxNumThreads.has_value()) {
if (maxNumThreads.has_value() && maxNumThreads.value() != 0) {
systemConfig.maxNumThreads = maxNumThreads.value();
}
if (compression.has_value()) {
Expand Down
6 changes: 3 additions & 3 deletions src_py/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ def __init__(
will be created in memory.

buffer_pool_size : int, optional
The maximum size of buffer pool in bytes. If None (default), the
engine default (~80% of system memory) is used.
The maximum size of buffer pool in bytes. If None (default) or 0,
the engine default (~80% of system memory) is used.

max_num_threads : int, optional
The maximum number of threads to use for executing queries. If None
(default), the engine default is used.
(default) or 0, the engine default is used.

compression : bool, optional
Enable database compression. If None (default), the engine default
Expand Down
59 changes: 59 additions & 0 deletions test/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,65 @@ def test_database_checkpoint_threshold_config(tmp_path: Path) -> None:
assert result.get_next()[0] == "1234"


THREADS_QUERY = "CALL current_setting('threads') RETURN *"


def print_settings_in_subprocess(build_dir: Path, body: str) -> list[str]:
# A subprocess turns a hang into a failure and keeps these databases out of
# the suite's address space.
code = dedent(f"""
import sys
sys.path.append(r"{build_dir!s}")

import ladybug as lb


def read(query, **kwargs):
with lb.Database(":memory:", **kwargs) as db, lb.Connection(db) as conn:
return conn.execute(query).get_next()[0]
""") + dedent(body)
try:
result = subprocess.run(
[sys.executable, "-c", code], capture_output=True, text=True, timeout=60
)
except subprocess.TimeoutExpired:
pytest.fail("a query did not return within 60 s")
assert result.returncode == 0, result.stderr
return result.stdout.split()


def test_database_max_num_threads_config(build_dir: Path) -> None:
default, explicit = print_settings_in_subprocess(
build_dir,
f"""
default = int(read({THREADS_QUERY!r}))
print(default)
print(read({THREADS_QUERY!r}, max_num_threads=default + 1))
""",
)
assert int(explicit) == int(default) + 1


@pytest.mark.parametrize(
("setting", "query"),
[
("max_num_threads", THREADS_QUERY),
("buffer_pool_size", "CALL bm_info() RETURN mem_limit"),
],
)
def test_database_zero_setting_uses_engine_default(
setting: str, query: str, build_dir: Path
) -> None:
default, zero = print_settings_in_subprocess(
build_dir,
f"""
print(read({query!r}))
print(read({query!r}, {setting}=0))
""",
)
assert zero == default


def test_database_throw_on_wal_replay_failure_config(tmp_path: Path) -> None:
database_path = get_db_file_path(tmp_path)
wal_file_path = str(database_path) + ".wal"
Expand Down
Loading