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

cache: Switch to apsw for cache SQLite db #58

Merged
merged 1 commit into from
May 23, 2024
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
11 changes: 4 additions & 7 deletions getter/cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import shutil
import sqlite3
import apsw
import hashlib

DATABASE_NAME = "cache_datagetter.db"
Expand All @@ -12,15 +12,14 @@ class DatagetterCacheError(Exception):

def setup_database():
try:
con = sqlite3.connect(DATABASE_NAME)
con = apsw.Connection(DATABASE_NAME)
cur = con.cursor()
cur.execute(
"""CREATE TABLE IF NOT EXISTS cache
(original_file_name TEXT NOT NULL UNIQUE,
hash TEXT NOT NULL UNIQUE,
json_file TEXT NOT NULL UNIQUE);"""
)
con.commit()
con.close()
except Exception as e:
raise DatagetterCacheError(e)
Expand Down Expand Up @@ -52,7 +51,7 @@ def hash_file(original_file_path):

def get_file(file_hash_str):
try:
con = sqlite3.connect(DATABASE_NAME)
con = apsw.Connection(DATABASE_NAME)
cur = con.cursor()
cur.execute("SELECT json_file FROM cache WHERE hash = ?", (file_hash_str,))
row = cur.fetchone()
Expand All @@ -76,7 +75,7 @@ def update_cache(json_file_name, file_hash_str, file_identifier, file_type):
# Todo clean up cache functionality for orphaned files or to reset the cache

try:
con = sqlite3.connect(DATABASE_NAME)
con = apsw.Connection(DATABASE_NAME)
cur = con.cursor()

cur.execute(
Expand All @@ -96,8 +95,6 @@ def update_cache(json_file_name, file_hash_str, file_identifier, file_type):
),
)

con.commit()

shutil.copy(json_file_name, CACHE_DIR)
except Exception as e:
raise DatagetterCacheError(e)
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ jsonschema
strict-rfc3339
rfc3987
requests[socks]
apsw>=3.35,<4

# Compatibility with CoVE
attrs>=20.3.0
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#
# pip-compile
#
apsw==3.45.3.0
# via -r requirements.in
attrs==21.2.0
# via
# -r requirements.in
Expand Down
Loading