Skip to content

Commit

Permalink
Avoid false sharing of databases between instances
Browse files Browse the repository at this point in the history
  • Loading branch information
dspinellis committed Jan 21, 2025
1 parent ad8d91d commit 9226930
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/alexandria3k/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@

# pylint: disable=too-many-lines

TMP_DB_URI = "file:/shared-tmp?vfs=memdb"

SINGLE_PARTITION_INDEX = "SINGLE_PARTITION"
"""str: denote a table with a single partition by setting or comparing for
equality an index value to this reference."""
Expand Down Expand Up @@ -549,6 +547,7 @@ class DataSource:
# pylint: disable=too-many-instance-attributes

uri_configured = False
instance_id = 0

def __init__(
self,
Expand All @@ -568,8 +567,11 @@ def __init__(
self.tables = tables
self.table_dict = {t.get_name(): t for t in tables}

# A named in-memory database; it can be attached by name to others
self.vdb = apsw.Connection(TMP_DB_URI)
# A unique per-instance named in-memory database
# It can be attached by name to other databases.
self.vdb_uri = f"file:/shared-tmp-{DataSource.instance_id}?vfs=memdb"
self.vdb = apsw.Connection(self.vdb_uri)
DataSource.instance_id += 1
self.cursor = self.vdb.cursor()
# Register the module as filesource
self.data_source = data_source
Expand Down Expand Up @@ -751,7 +753,7 @@ def query(self, query, partition=False):
partition = apsw.Connection(":memory:", apsw.SQLITE_OPEN_READWRITE)
partition.create_module("filesource", self.data_source)
partition.execute(
log_sql(f"ATTACH DATABASE '{TMP_DB_URI}' AS virtual")
log_sql(f"ATTACH DATABASE '{self.vdb_uri}' AS virtual")
)

# Also attach databases to the partition
Expand Down

0 comments on commit 9226930

Please sign in to comment.