MCPcopy Create free account
hub / github.com/cisco/mindmeld / QueryCache

Class QueryCache

mindmeld/query_cache.py:31–270  ·  view source on GitHub ↗

QueryCache stores ProcessedQuerys and associated metadata in a sqlite3 backed cache to save processing time on reloading the examples later.

Source from the content-addressed store, hash-verified

29
30
31class QueryCache:
32 """
33 QueryCache stores ProcessedQuerys and associated metadata in a sqlite3 backed
34 cache to save processing time on reloading the examples later.
35 """
36
37 @staticmethod
38 def copy_db(source, dest):
39 cursor = dest.cursor()
40 for statement in source.iterdump():
41 cursor.execute(statement)
42 dest.commit()
43
44 def __init__(self, app_path, schema_version_hash):
45 # make generated directory if necessary
46 self.schema_version_hash = schema_version_hash
47 gen_folder = GEN_FOLDER.format(app_path=app_path)
48 if not os.path.isdir(gen_folder):
49 os.makedirs(gen_folder)
50
51 db_file_location = QUERY_CACHE_DB_PATH.format(app_path=app_path)
52 self.disk_connection = sqlite3.connect(db_file_location)
53 self.batch_write_size = int(os.environ.get("MM_QUERY_CACHE_WRITE_SIZE", "1000"))
54
55 cursor = self.disk_connection.cursor()
56
57 if not self.compatible_version():
58 cursor.execute("""
59 DROP TABLE IF EXISTS queries;
60 """)
61 cursor.execute("""
62 DROP TABLE IF EXISTS version;
63 """)
64 # Create table to store queries
65 cursor.execute("""
66 CREATE TABLE IF NOT EXISTS queries
67 (hash_id TEXT PRIMARY KEY, query TEXT, raw_query TEXT, domain TEXT, intent TEXT);
68 """)
69 # Create table to store the data version
70 cursor.execute("""
71 CREATE TABLE IF NOT EXISTS version
72 (schema_version_hash TEXT PRIMARY KEY);
73 """)
74 cursor.execute("""
75 INSERT OR IGNORE INTO version values (?);
76 """, (self.schema_version_hash,))
77 self.disk_connection.commit()
78
79 in_memory = bool(strtobool(os.environ.get("MM_QUERY_CACHE_IN_MEMORY", "1").lower()))
80
81 if in_memory:
82 logger.info("Loading query cache into memory")
83 self.memory_connection = sqlite3.connect(":memory:")
84 self.copy_db(self.disk_connection, self.memory_connection)
85 self.batch_writes = []
86 else:
87 self.memory_connection = None
88 self.batch_writes = None

Callers 5

test_disk_query_cacheFunction · 0.90
test_memory_query_cacheFunction · 0.90
test_flatten_query_treeFunction · 0.90
query_cacheMethod · 0.85

Calls

no outgoing calls

Tested by 4

test_disk_query_cacheFunction · 0.72
test_memory_query_cacheFunction · 0.72
test_flatten_query_treeFunction · 0.72