mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-24 12:41:02 +02:00
Remove legacy
This commit is contained in:
parent
190cae845a
commit
ca86601c2d
1 changed files with 68 additions and 167 deletions
|
|
@ -24,13 +24,7 @@ class KnowledgeGraph:
|
||||||
self.keyspace = keyspace
|
self.keyspace = keyspace
|
||||||
self.username = username
|
self.username = username
|
||||||
|
|
||||||
# Multi-table schema design for optimal performance
|
# Optimized multi-table schema
|
||||||
self.use_legacy = os.getenv('CASSANDRA_USE_LEGACY', 'false').lower() == 'true'
|
|
||||||
|
|
||||||
if self.use_legacy:
|
|
||||||
self.table = "triples" # Legacy single table
|
|
||||||
else:
|
|
||||||
# New optimized tables
|
|
||||||
self.subject_table = "triples_s"
|
self.subject_table = "triples_s"
|
||||||
self.po_table = "triples_p"
|
self.po_table = "triples_p"
|
||||||
self.object_table = "triples_o"
|
self.object_table = "triples_o"
|
||||||
|
|
@ -47,8 +41,6 @@ class KnowledgeGraph:
|
||||||
_active_clusters.append(self.cluster)
|
_active_clusters.append(self.cluster)
|
||||||
|
|
||||||
self.init()
|
self.init()
|
||||||
|
|
||||||
if not self.use_legacy:
|
|
||||||
self.prepare_statements()
|
self.prepare_statements()
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
|
|
@ -70,38 +62,8 @@ class KnowledgeGraph:
|
||||||
""");
|
""");
|
||||||
|
|
||||||
self.session.set_keyspace(self.keyspace)
|
self.session.set_keyspace(self.keyspace)
|
||||||
|
|
||||||
if self.use_legacy:
|
|
||||||
self.init_legacy_schema()
|
|
||||||
else:
|
|
||||||
self.init_optimized_schema()
|
self.init_optimized_schema()
|
||||||
|
|
||||||
def init_legacy_schema(self):
|
|
||||||
"""Initialize legacy single-table schema for backward compatibility"""
|
|
||||||
self.session.execute(f"""
|
|
||||||
create table if not exists {self.table} (
|
|
||||||
collection text,
|
|
||||||
s text,
|
|
||||||
p text,
|
|
||||||
o text,
|
|
||||||
PRIMARY KEY (collection, s, p, o)
|
|
||||||
);
|
|
||||||
""");
|
|
||||||
|
|
||||||
self.session.execute(f"""
|
|
||||||
create index if not exists {self.table}_s
|
|
||||||
ON {self.table} (s);
|
|
||||||
""");
|
|
||||||
|
|
||||||
self.session.execute(f"""
|
|
||||||
create index if not exists {self.table}_p
|
|
||||||
ON {self.table} (p);
|
|
||||||
""");
|
|
||||||
|
|
||||||
self.session.execute(f"""
|
|
||||||
create index if not exists {self.table}_o
|
|
||||||
ON {self.table} (o);
|
|
||||||
""");
|
|
||||||
|
|
||||||
def init_optimized_schema(self):
|
def init_optimized_schema(self):
|
||||||
"""Initialize optimized multi-table schema for performance"""
|
"""Initialize optimized multi-table schema for performance"""
|
||||||
|
|
@ -192,13 +154,6 @@ class KnowledgeGraph:
|
||||||
logger.info("Prepared statements initialized for optimal performance")
|
logger.info("Prepared statements initialized for optimal performance")
|
||||||
|
|
||||||
def insert(self, collection, s, p, o):
|
def insert(self, collection, s, p, o):
|
||||||
|
|
||||||
if self.use_legacy:
|
|
||||||
self.session.execute(
|
|
||||||
f"insert into {self.table} (collection, s, p, o) values (%s, %s, %s, %s)",
|
|
||||||
(collection, s, p, o)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
# Batch write to all three tables for consistency
|
# Batch write to all three tables for consistency
|
||||||
batch = BatchStatement()
|
batch = BatchStatement()
|
||||||
|
|
||||||
|
|
@ -214,12 +169,6 @@ class KnowledgeGraph:
|
||||||
self.session.execute(batch)
|
self.session.execute(batch)
|
||||||
|
|
||||||
def get_all(self, collection, limit=50):
|
def get_all(self, collection, limit=50):
|
||||||
if self.use_legacy:
|
|
||||||
return self.session.execute(
|
|
||||||
f"select s, p, o from {self.table} where collection = %s limit {limit}",
|
|
||||||
(collection,)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
# Use subject table for get_all queries
|
# Use subject table for get_all queries
|
||||||
return self.session.execute(
|
return self.session.execute(
|
||||||
self.get_all_stmt,
|
self.get_all_stmt,
|
||||||
|
|
@ -227,12 +176,6 @@ class KnowledgeGraph:
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_s(self, collection, s, limit=10):
|
def get_s(self, collection, s, limit=10):
|
||||||
if self.use_legacy:
|
|
||||||
return self.session.execute(
|
|
||||||
f"select p, o from {self.table} where collection = %s and s = %s limit {limit}",
|
|
||||||
(collection, s)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
# Optimized: Direct partition access with (collection, s)
|
# Optimized: Direct partition access with (collection, s)
|
||||||
return self.session.execute(
|
return self.session.execute(
|
||||||
self.get_s_stmt,
|
self.get_s_stmt,
|
||||||
|
|
@ -240,12 +183,6 @@ class KnowledgeGraph:
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_p(self, collection, p, limit=10):
|
def get_p(self, collection, p, limit=10):
|
||||||
if self.use_legacy:
|
|
||||||
return self.session.execute(
|
|
||||||
f"select s, o from {self.table} where collection = %s and p = %s limit {limit}",
|
|
||||||
(collection, p)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
# Optimized: Use po_table for direct partition access
|
# Optimized: Use po_table for direct partition access
|
||||||
return self.session.execute(
|
return self.session.execute(
|
||||||
self.get_p_stmt,
|
self.get_p_stmt,
|
||||||
|
|
@ -253,12 +190,6 @@ class KnowledgeGraph:
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_o(self, collection, o, limit=10):
|
def get_o(self, collection, o, limit=10):
|
||||||
if self.use_legacy:
|
|
||||||
return self.session.execute(
|
|
||||||
f"select s, p from {self.table} where collection = %s and o = %s limit {limit}",
|
|
||||||
(collection, o)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
# Optimized: Use object_table for direct partition access
|
# Optimized: Use object_table for direct partition access
|
||||||
return self.session.execute(
|
return self.session.execute(
|
||||||
self.get_o_stmt,
|
self.get_o_stmt,
|
||||||
|
|
@ -266,12 +197,6 @@ class KnowledgeGraph:
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_sp(self, collection, s, p, limit=10):
|
def get_sp(self, collection, s, p, limit=10):
|
||||||
if self.use_legacy:
|
|
||||||
return self.session.execute(
|
|
||||||
f"select o from {self.table} where collection = %s and s = %s and p = %s limit {limit}",
|
|
||||||
(collection, s, p)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
# Optimized: Use subject_table with clustering key access
|
# Optimized: Use subject_table with clustering key access
|
||||||
return self.session.execute(
|
return self.session.execute(
|
||||||
self.get_sp_stmt,
|
self.get_sp_stmt,
|
||||||
|
|
@ -279,12 +204,6 @@ class KnowledgeGraph:
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_po(self, collection, p, o, limit=10):
|
def get_po(self, collection, p, o, limit=10):
|
||||||
if self.use_legacy:
|
|
||||||
return self.session.execute(
|
|
||||||
f"select s from {self.table} where collection = %s and p = %s and o = %s limit {limit} allow filtering",
|
|
||||||
(collection, p, o)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
# CRITICAL OPTIMIZATION: Use po_table - NO MORE ALLOW FILTERING!
|
# CRITICAL OPTIMIZATION: Use po_table - NO MORE ALLOW FILTERING!
|
||||||
return self.session.execute(
|
return self.session.execute(
|
||||||
self.get_po_stmt,
|
self.get_po_stmt,
|
||||||
|
|
@ -292,12 +211,6 @@ class KnowledgeGraph:
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_os(self, collection, o, s, limit=10):
|
def get_os(self, collection, o, s, limit=10):
|
||||||
if self.use_legacy:
|
|
||||||
return self.session.execute(
|
|
||||||
f"select p from {self.table} where collection = %s and o = %s and s = %s limit {limit} allow filtering",
|
|
||||||
(collection, o, s)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
# Optimized: Use subject_table with clustering access (no more ALLOW FILTERING)
|
# Optimized: Use subject_table with clustering access (no more ALLOW FILTERING)
|
||||||
return self.session.execute(
|
return self.session.execute(
|
||||||
self.get_os_stmt,
|
self.get_os_stmt,
|
||||||
|
|
@ -305,12 +218,6 @@ class KnowledgeGraph:
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_spo(self, collection, s, p, o, limit=10):
|
def get_spo(self, collection, s, p, o, limit=10):
|
||||||
if self.use_legacy:
|
|
||||||
return self.session.execute(
|
|
||||||
f"""select s as x from {self.table} where collection = %s and s = %s and p = %s and o = %s limit {limit}""",
|
|
||||||
(collection, s, p, o)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
# Optimized: Use subject_table for exact key lookup
|
# Optimized: Use subject_table for exact key lookup
|
||||||
return self.session.execute(
|
return self.session.execute(
|
||||||
self.get_spo_stmt,
|
self.get_spo_stmt,
|
||||||
|
|
@ -319,12 +226,6 @@ class KnowledgeGraph:
|
||||||
|
|
||||||
def delete_collection(self, collection):
|
def delete_collection(self, collection):
|
||||||
"""Delete all triples for a specific collection"""
|
"""Delete all triples for a specific collection"""
|
||||||
if self.use_legacy:
|
|
||||||
self.session.execute(
|
|
||||||
f"delete from {self.table} where collection = %s",
|
|
||||||
(collection,)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
# Delete from all three tables
|
# Delete from all three tables
|
||||||
self.session.execute(
|
self.session.execute(
|
||||||
f"delete from {self.subject_table} where collection = %s",
|
f"delete from {self.subject_table} where collection = %s",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue