mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-10 13:52:11 +02:00
Remove Pulsar-specific concepts from application code so that the pub/sub backend is swappable via configuration. Rename translators: - to_pulsar/from_pulsar → decode/encode across all translator classes, dispatch handlers, and tests (55+ files) - from_response_with_completion → encode_with_completion - Remove pulsar.schema.Record from translator base class Queue naming (CLASS:TOPICSPACE:TOPIC): - Replace topic() helper with queue() using new format: flow:tg:name, request:tg:name, response:tg:name, state:tg:name - Queue class implies persistence/TTL (no QoS in names) - Update Pulsar backend map_topic() to parse new format - Librarian queues use flow class (persistent, for chunking) - Config push uses state class (persistent, last-value) - Remove 15 dead topic imports from schema files - Update init_trustgraph.py namespace: config → state Confine Pulsar to pulsar_backend.py: - Delete legacy PulsarClient class from pubsub.py - Move add_args to add_pubsub_args() with standalone flag for CLI tools (defaults to localhost) - PulsarBackendConsumer.receive() catches _pulsar.Timeout, raises standard TimeoutError - Remove Pulsar imports from: async_processor, flow_processor, log_level, all 11 client files, 4 storage writers, gateway service, gateway config receiver - Remove log_level/LoggerLevel from client API - Rewrite tg-monitor-prompts to use backend abstraction - Update tg-dump-queues to use add_pubsub_args Also: pubsub-abstraction.md tech spec covering problem statement, design goals, as-is requirements, candidate broker assessment, approach, and implementation order.
68 lines
No EOL
2.4 KiB
Python
68 lines
No EOL
2.4 KiB
Python
from typing import Dict, Any, Tuple, Optional
|
|
from ...schema import TriplesQueryRequest, TriplesQueryResponse
|
|
from .base import MessageTranslator
|
|
from .primitives import ValueTranslator, SubgraphTranslator
|
|
|
|
|
|
class TriplesQueryRequestTranslator(MessageTranslator):
|
|
"""Translator for TriplesQueryRequest schema objects"""
|
|
|
|
def __init__(self):
|
|
self.value_translator = ValueTranslator()
|
|
|
|
def decode(self, data: Dict[str, Any]) -> TriplesQueryRequest:
|
|
s = self.value_translator.decode(data["s"]) if "s" in data else None
|
|
p = self.value_translator.decode(data["p"]) if "p" in data else None
|
|
o = self.value_translator.decode(data["o"]) if "o" in data else None
|
|
g = data.get("g") # None=default graph, "*"=all graphs
|
|
|
|
return TriplesQueryRequest(
|
|
s=s,
|
|
p=p,
|
|
o=o,
|
|
g=g,
|
|
limit=int(data.get("limit", 10000)),
|
|
user=data.get("user", "trustgraph"),
|
|
collection=data.get("collection", "default"),
|
|
streaming=data.get("streaming", False),
|
|
batch_size=int(data.get("batch-size", 20)),
|
|
)
|
|
|
|
def encode(self, obj: TriplesQueryRequest) -> Dict[str, Any]:
|
|
result = {
|
|
"limit": obj.limit,
|
|
"user": obj.user,
|
|
"collection": obj.collection,
|
|
"streaming": obj.streaming,
|
|
"batch-size": obj.batch_size,
|
|
}
|
|
|
|
if obj.s:
|
|
result["s"] = self.value_translator.encode(obj.s)
|
|
if obj.p:
|
|
result["p"] = self.value_translator.encode(obj.p)
|
|
if obj.o:
|
|
result["o"] = self.value_translator.encode(obj.o)
|
|
if obj.g is not None:
|
|
result["g"] = obj.g
|
|
|
|
return result
|
|
|
|
|
|
class TriplesQueryResponseTranslator(MessageTranslator):
|
|
"""Translator for TriplesQueryResponse schema objects"""
|
|
|
|
def __init__(self):
|
|
self.subgraph_translator = SubgraphTranslator()
|
|
|
|
def decode(self, data: Dict[str, Any]) -> TriplesQueryResponse:
|
|
raise NotImplementedError("Response translation to Pulsar not typically needed")
|
|
|
|
def encode(self, obj: TriplesQueryResponse) -> Dict[str, Any]:
|
|
return {
|
|
"response": self.subgraph_translator.encode(obj.triples)
|
|
}
|
|
|
|
def encode_with_completion(self, obj: TriplesQueryResponse) -> Tuple[Dict[str, Any], bool]:
|
|
"""Returns (response_dict, is_final)"""
|
|
return self.encode(obj), obj.is_final |