Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use in-memory database with shared cache #719

Merged
merged 16 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from 12 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- #710, #561 Implement `except*` syntax (@lieryan)
- #711 allow building documentation without having rope module installed (@kloczek)
- #719 Allows the in-memory db to be shared across threads (@tkrabel)

# Release 1.10.0

Expand Down
20 changes: 16 additions & 4 deletions rope/contrib/autoimport/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import contextlib
import json
import random
import re
import sqlite3
import sys
Expand Down Expand Up @@ -151,12 +152,23 @@ def create_database_connection(
"""
if not memory and project is None:
raise Exception("if memory=False, project must be provided")
db_path: str
if memory or project is None or project.ropefolder is None:
db_path = ":memory:"
# Allows the in-memory db to be shared across threads
# See https://www.sqlite.org/inmemorydb.html
project_hash: int
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about moving this into its own function, but that would add another hop for understanding what's happening, increasing obscurity of the code base. So I decided against it. I follow the principle of "small interfaces" and "deep modules"

if project is None:
project_hash = random.randint(100_000_000, 999_999_999)
elif project.ropefolder is None:
project_hash = hash(project.address)
else:
project_hash = hash(project.ropefolder.real_path)
return sqlite3.connect(
f"file:memdb{project_hash}:?mode=memory&cache=shared", uri=True
)
else:
db_path = str(Path(project.ropefolder.real_path) / "autoimport.db")
return sqlite3.connect(db_path)
return sqlite3.connect(
str(Path(project.ropefolder.real_path) / "autoimport.db")
)

def _setup_db(self):
models.Metadata.create_table(self.connection)
Expand Down
7 changes: 7 additions & 0 deletions ropetest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ def project():
testutils.remove_project(project)


@pytest.fixture
def project2():
project = testutils.sample_project("another_project")
yield project
testutils.remove_project(project)


@pytest.fixture
def project_path(project):
yield pathlib.Path(project.address)
Expand Down
16 changes: 16 additions & 0 deletions ropetest/contrib/autoimport/autoimporttest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sqlite3

from contextlib import closing, contextmanager
from textwrap import dedent
from unittest.mock import ANY, patch
Expand Down Expand Up @@ -26,6 +28,20 @@ def database_list(connection):
return list(connection.execute("PRAGMA database_list"))


def test_in_memory_database_share_cache(project, project2):
ai_1 = AutoImport(project, memory=True)
ai_2 = AutoImport(project, memory=True)

ai_3 = AutoImport(project2, memory=True)

with ai_1.connection:
ai_1.connection.execute("CREATE TABLE shared(data)")
ai_1.connection.execute("INSERT INTO shared VALUES(28)")
assert ai_2.connection.execute("SELECT data FROM shared").fetchone() == (28,)
with pytest.raises(sqlite3.OperationalError, match="no such table: shared"):
ai_3.connection.execute("SELECT data FROM shared").fetchone()


def test_autoimport_connection_parameter_with_in_memory(
project: Project,
autoimport: AutoImport,
Expand Down
Loading