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

Add multithreading compatibility #558

Merged
merged 1 commit into from
Jul 5, 2024
Merged
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
73 changes: 38 additions & 35 deletions src/galois/_databases/_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,32 @@
import sqlite3
import sys
from pathlib import Path
from threading import Lock


class DatabaseInterface:
"""
An abstract class to interface with SQLite databases.
"""

singleton = None
_lock = Lock()
_singleton = None
file: Path

def __new__(cls):
if cls.singleton is None:
cls.singleton = super().__new__(cls)
cls.conn = sqlite3.connect(cls.file)
cls.cursor = cls.conn.cursor()
return cls.singleton
with cls._lock:
if cls._singleton is None:
cls._singleton = super().__new__(cls)
cls.conn = sqlite3.connect(cls.file, check_same_thread=False)
cls.cursor = cls.conn.cursor()
return cls._singleton


class PrimeFactorsDatabase(DatabaseInterface):
"""
A class to interface with the prime factors database.
"""

singleton = None
file = Path(__file__).parent / "prime_factors.db"

def fetch(self, n: int) -> tuple[list[int], list[int], int]:
Expand All @@ -51,15 +53,16 @@ def fetch(self, n: int) -> tuple[list[int], list[int], int]:
n = str(n)
sys.set_int_max_str_digits(default_limit)

self.cursor.execute(
"""
SELECT factors, multiplicities, composite
FROM factorizations
WHERE value=?
""",
(str(n),),
)
result = self.cursor.fetchone()
with self._lock:
self.cursor.execute(
"""
SELECT factors, multiplicities, composite
FROM factorizations
WHERE value=?
""",
(str(n),),
)
result = self.cursor.fetchone()

if result is None:
raise LookupError(f"The prime factors database does not contain an entry for {n}.")
Expand All @@ -76,7 +79,6 @@ class IrreduciblePolyDatabase(DatabaseInterface):
A class to interface with the irreducible polynomials database.
"""

singleton = None
file = Path(__file__).parent / "irreducible_polys.db"

def fetch(self, characteristic: int, degree: int) -> tuple[list[int], list[int]]:
Expand All @@ -90,14 +92,15 @@ def fetch(self, characteristic: int, degree: int) -> tuple[list[int], list[int]]
Returns:
A tuple containing the non-zero degrees and coefficients of the irreducible polynomial.
"""
self.cursor.execute(
"""
SELECT nonzero_degrees, nonzero_coeffs
FROM polys
WHERE characteristic=? AND degree=?""",
(characteristic, degree),
)
result = self.cursor.fetchone()
with self._lock:
self.cursor.execute(
"""
SELECT nonzero_degrees, nonzero_coeffs
FROM polys
WHERE characteristic=? AND degree=?""",
(characteristic, degree),
)
result = self.cursor.fetchone()

if result is None:
raise LookupError(
Expand All @@ -116,7 +119,6 @@ class ConwayPolyDatabase(DatabaseInterface):
A class to interface with the Conway polynomials database.
"""

singleton = None
file = Path(__file__).parent / "conway_polys.db"

def fetch(self, characteristic: int, degree: int) -> tuple[list[int], list[int]]:
Expand All @@ -130,15 +132,16 @@ def fetch(self, characteristic: int, degree: int) -> tuple[list[int], list[int]]
Returns:
A tuple containing the non-zero degrees and coefficients of the Conway polynomial.
"""
self.cursor.execute(
"""
SELECT nonzero_degrees, nonzero_coeffs
FROM polys
WHERE characteristic=? AND degree=?
""",
(characteristic, degree),
)
result = self.cursor.fetchone()
with self._lock:
self.cursor.execute(
"""
SELECT nonzero_degrees, nonzero_coeffs
FROM polys
WHERE characteristic=? AND degree=?
""",
(characteristic, degree),
)
result = self.cursor.fetchone()

if result is None:
raise LookupError(
Expand Down
Loading