Skip to content

Commit

Permalink
Defer index creation
Browse files Browse the repository at this point in the history
Signed-off-by: Prabhu Subramanian <[email protected]>
  • Loading branch information
prabhu committed Mar 17, 2024
1 parent 5210b26 commit 259f43b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 208 deletions.
198 changes: 0 additions & 198 deletions test/test_db.py

This file was deleted.

2 changes: 1 addition & 1 deletion vdb/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def main():
for s in sources:
LOG.info("Refreshing %s", s.__class__.__name__)
s.refresh()
db_lib.close_all()
db_lib.optimize_and_close_all()
elif args.sync:
for s in (GitHubSource(),):
LOG.info("Syncing %s", s.__class__.__name__)
Expand Down
21 changes: 12 additions & 9 deletions vdb/lib/db6.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,8 @@ def ensure_schemas(db_conn: sqlite3.Connection, index_conn: sqlite3.Connection):
"""Create the sqlite tables and indexes in case they don't exist"""
db_conn.execute(
"CREATE TABLE if not exists cve_data(cve_id TEXT NOT NULL, type TEXT NOT NULL, namespace TEXT, name TEXT NOT NULL, source_data JSON NOT NULL, override_data JSON);")
db_conn.execute(
"CREATE INDEX if not exists idx1 on cve_data(cve_id, type);")
index_conn.execute(
"CREATE TABLE if not exists cve_index(cve_id TEXT NOT NULL, type TEXT NOT NULL, namespace TEXT, name TEXT NOT NULL, versions JSON NOT NULL);")
index_conn.execute(
"CREATE INDEX if not exists cidx1 on cve_index(cve_id);")
index_conn.execute(
"CREATE INDEX if not exists cidx2 on cve_index(type, namespace, name);")
index_conn.execute(
"CREATE INDEX if not exists cidx3 on cve_index(namespace, name);")


def get(db_file: str = config.VDB_BIN_FILE, index_file: str = config.VDB_BIN_INDEX, read_only=False) -> (
Expand Down Expand Up @@ -57,12 +49,23 @@ def clear_all():
index_conn.commit()


def close_all():
def optimize_and_close_all():
"""
Safely close the connections by creating indexes and vacuuming if needed.
"""
if db_conn:
db_conn.execute(
"CREATE INDEX if not exists idx1 on cve_data(cve_id, type);")
db_conn.execute("VACUUM;")
db_conn.commit()
db_conn.close()
if index_conn:
index_conn.execute(
"CREATE INDEX if not exists cidx1 on cve_index(cve_id);")
index_conn.execute(
"CREATE INDEX if not exists cidx2 on cve_index(type, namespace, name);")
index_conn.execute(
"CREATE INDEX if not exists cidx3 on cve_index(namespace, name);")
index_conn.execute("VACUUM;")
index_conn.commit()
index_conn.close()

0 comments on commit 259f43b

Please sign in to comment.