mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-04-25 00:16:23 +02:00
SPARQL service supports batching/streaming (#755)
This commit is contained in:
parent
d9dc4cbab5
commit
ee65d90fdd
5 changed files with 169 additions and 95 deletions
|
|
@ -812,6 +812,31 @@ class SocketFlowInstance:
|
|||
else:
|
||||
yield response
|
||||
|
||||
def sparql_query_stream(
|
||||
self,
|
||||
query: str,
|
||||
user: str = "trustgraph",
|
||||
collection: str = "default",
|
||||
limit: int = 10000,
|
||||
batch_size: int = 20,
|
||||
**kwargs: Any
|
||||
) -> Iterator[Dict[str, Any]]:
|
||||
"""Execute a SPARQL query with streaming batches."""
|
||||
request = {
|
||||
"query": query,
|
||||
"user": user,
|
||||
"collection": collection,
|
||||
"limit": limit,
|
||||
"streaming": True,
|
||||
"batch-size": batch_size,
|
||||
}
|
||||
request.update(kwargs)
|
||||
|
||||
for response in self.client._send_request_sync(
|
||||
"sparql", self.flow_id, request, streaming_raw=True
|
||||
):
|
||||
yield response
|
||||
|
||||
def rows_query(
|
||||
self,
|
||||
query: str,
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ class SparqlQueryRequestTranslator(MessageTranslator):
|
|||
collection=data.get("collection", "default"),
|
||||
query=data.get("query", ""),
|
||||
limit=int(data.get("limit", 10000)),
|
||||
streaming=data.get("streaming", False),
|
||||
batch_size=int(data.get("batch-size", 20)),
|
||||
)
|
||||
|
||||
def encode(self, obj: SparqlQueryRequest) -> Dict[str, Any]:
|
||||
|
|
@ -24,6 +26,8 @@ class SparqlQueryRequestTranslator(MessageTranslator):
|
|||
"collection": obj.collection,
|
||||
"query": obj.query,
|
||||
"limit": obj.limit,
|
||||
"streaming": obj.streaming,
|
||||
"batch-size": obj.batch_size,
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -108,4 +112,4 @@ class SparqlQueryResponseTranslator(MessageTranslator):
|
|||
def encode_with_completion(
|
||||
self, obj: SparqlQueryResponse
|
||||
) -> Tuple[Dict[str, Any], bool]:
|
||||
return self.encode(obj), True
|
||||
return self.encode(obj), obj.is_final
|
||||
|
|
|
|||
|
|
@ -20,6 +20,8 @@ class SparqlQueryRequest:
|
|||
collection: str = ""
|
||||
query: str = "" # SPARQL query string
|
||||
limit: int = 10000 # Safety limit on results
|
||||
streaming: bool = False # Enable streaming mode
|
||||
batch_size: int = 20 # Bindings per batch in streaming mode
|
||||
|
||||
@dataclass
|
||||
class SparqlQueryResponse:
|
||||
|
|
@ -36,5 +38,7 @@ class SparqlQueryResponse:
|
|||
# For CONSTRUCT/DESCRIBE queries
|
||||
triples: list[Triple] = field(default_factory=list)
|
||||
|
||||
is_final: bool = True # False for intermediate batches in streaming
|
||||
|
||||
sparql_query_request_queue = queue('sparql-query', cls='request')
|
||||
sparql_query_response_queue = queue('sparql-query', cls='response')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue