Coverage report: /development/source/library/org/datagraph/spocq-shard/src/core/encoding/sparql-results-term-number.lisp

KindCoveredAll%
expression0171 0.0
branch018 0.0
Key
Not instrumented
Conditionalized out
Executed
Not executed
 
Both branches taken
One branch taken
Neither branch taken
1
 ;;; -*- Mode: lisp; Syntax: ansi-common-lisp; Base: 10; Package: org.datagraph.spocq.implementation; -*-
2
 
3
 (in-package :org.datagraph.spocq.implementation)
4
 
5
 ;;; results+json-columns serializer
6
 ;;; ideosyncratic to dydra
7
 
8
 ;;; no reading
9
 
10
 
11
 ;;; writing - either boolean or bindings                             
12
 
13
 (defgeneric write-sparql-results+term-number (results stream)
14
   (:documentation "Encode the result field to the stream as results+term-number.
15
  The results are successive lines. The first line is a '#' comment, followed by just the request form.
16
  The second line is the variable names, separated by tabs. Each successive line is the sequence of
17
  respective values. If a value is not bound, the entry is the +null-term+, like '0'.
18
  Each line ends with a linefeed.")
19
   
20
   (:method ((result spocq:boolean) (stream t))
21
     (format stream "~a~%" (spocq:literal-lexical-form result))
22
     (incf-stat *statements-returned*))
23
   
24
   (:method ((results cons) (stream t))
25
     (let* ((variables (first results))
26
            (solutions (rest results))
27
            (index 0)
28
            (start (or (response-offset) 0))
29
            (end (response-end)))
30
       (format stream "~{~a~^    ~}~%" variables)
31
       (dolist (result solutions)
32
         (when (>= index start)
33
           (when (and end (>= index end))
34
             (return))
35
           (loop for value in result
36
                 for first = t then nil
37
                 do (progn (unless first (write-char #\tab stream))
38
                           (encode-turtle-object value stream))))
39
         (incf index)
40
         (terpri stream))
41
       (incf-stat *statements-returned* index)))
42
   
43
   (:method ((results boolean-generator) (stream t))
44
     (let* ((channel (boolean-generator-channel results)))
45
       (format stream "~a~%" (spocq:literal-lexical-form (if (get-field-page channel) spocq.a:|true| spocq.a:|false|)))
46
       (incf-stat *statements-returned*)))
47
   
48
   (:method ((results solution-generator) (stream t))
49
     (let* ((dimensions (solution-generator-dimensions results))
50
            (channel (solution-generator-channel results))
51
            (variable-count (length dimensions))
52
            (index 0)
53
            (start (or (response-offset) 0))
54
            (end (response-end)))
55
       (log-debug "write-sparql-results+term-number: ~s: ~s -- ~s" dimensions start end)
56
       (format stream "~{~a~^    ~}~%" dimensions)
57
       (do-pages (page channel)
58
         (if (and end (>= index end))
59
           (return)
60
           (if (>= (+ index (array-dimension page 0)) start)
61
             (cond ((= variable-count (array-dimension page 1))
62
                    (trace-algebra write-sparql-results+term-number dimensions (term-value-field page))
63
                    (setf index (write-sparql-results-field+term-number page stream index start end)))
64
                   (t
65
                    (log-warn "field width mismatch: ~s : ~s."
66
                              dimensions (array-dimension page 1))
67
                    (incf index (array-dimension page 0))))
68
             ; otherwise skip the entire page
69
             (incf index (array-dimension page 0)))))
70
       (incf-stat *statements-returned* index))))
71
 
72
 (defun write-sparql-results-field+term-number (page stream &optional (index 0) (start 0) end)
73
   (flet ((term-aspect-encoder (term-type term-literal term-language-tag term-datatype)
74
            (encode-turtle-term-aspects term-type term-literal term-language-tag term-datatype stream)))
75
     (declare (dynamic-extent #'term-aspect-encoder))
76
     (let ((variable-count (array-dimension page 1)))
77
       (dotimes (page-index (array-dimension page 0))
78
         (when (>= index start)
79
           (when (and end (>= index end))
80
             (return))
81
           (loop for value-index from 0 below variable-count
82
                 do (let ((term-number (aref page page-index value-index)))
83
                      (unless (zerop value-index) (write-char #\tab stream))
84
                      (prin1 term-number stream))))
85
         (incf index)
86
         (terpri stream))))
87
   index)
88
 
89
 
90
 (defmethod send-error-message ((body t) (stream t) (content-type mime:application/sparql-results+term-number))
91
   "Send an error response encoded as xml"
92
   (send-error-message body stream mime:application/sparql-query+sse))
93
 
94
 
95
 (defmethod send-response-message (operation (message-body t) (stream t)
96
                                             (content-type mime:application/sparql-results+term-number))
97
   "Given a MESSAGE, and a STREAM with the application/sparql-results+term-number CONTENT-TYPE, encode as a
98
  tab-sepearated-values table comprising either as term numbers for persistent terms or turtle externalizations
99
  for transient terms."
100
   (when *encoding-trace-output*
101
     (setf stream (make-broadcast-stream *encoding-trace-output* stream)))
102
   (write-sparql-results+term-number message-body stream))
103
 
104
 #+(or) ;; superseded by mode-specific send-response-header-type
105
 (defmethod send-response-message :around (operation (message-body ABSTRACT-SOLUTION-FIELD) (stream t) (content-type t))
106
   (cond (*emit-task-operation*
107
          (let ((*emit-task-operation* nil)) ; once only
108
            (format stream "# ~(~a~)~%" *task-operation*)
109
            (call-next-method)))
110
         (t
111
          (call-next-method))))
112
 
113
 
114
 #|
115
 (defparameter *io-lock* (bt:make-lock "IO"))
116
 
117
 (defun print-term-number (term-number stream)
118
   (prin1 term-number stream))
119
 
120
 ;;;!!! if skolemizing is enabled, this needs to check if the terms is a blank node and
121
 ;;;!!! generate a new one with a prefixed label
122
 (defun write-sparql-results-field+term-number (page stream)
123
   (let ((string-stream (make-string-output-stream)))
124
     (flet ((term-aspect-encoder (term-type term-literal term-language-tag term-datatype)
125
              (encode-turtle-term-aspects term-type term-literal term-language-tag term-datatype string-stream)))
126
       (declare (dynamic-extent #'term-aspect-encoder))
127
       (let ((term-deconstructor (repository-term-deconstructor *transaction*)))
128
         (let ((variable-count (array-dimension page 1)))
129
           (dotimes (page-index (array-dimension page 0))
130
             (loop for value-index from 0 below variable-count
131
                   do (let ((term-number (aref page page-index value-index)))
132
                        (unless (zerop value-index) (write-char #\tab string-stream))
133
                        (print-term-number term-number string-stream)))
134
             (bt:with-lock-held (*io-lock*)
135
               (let ((out (get-output-stream-string string-stream)))
136
                 (format stream "'~a' [~d,~a]~%" out (length out) *thread-name*)
137
                 (finish-output stream)))
138
             ;; (break "row ~a" page-index)
139
             ))))))
140
 
141
 (defmethod send-response-message (operation (message-body t) (stream t)
142
                                             (content-type mime:application/sparql-results+term-number))
143
   "Given a MESSAGE, and a STREAM with the application/sparql-results+term-number CONTENT-TYPE, encode as a
144
  tab-sepearated-values table comprising either as term numbers for persistent terms or turtle externalizations
145
  for transient terms."
146
   (when *encoding-trace-output*
147
     (setf stream (make-broadcast-stream *encoding-trace-output* stream)))
148
   (bt:with-lock-held (*io-lock*)
149
     (print :send-response-message *trace-output*)
150
     (finish-output *trace-output*))
151
   (write-sparql-results+term-number message-body stream))
152
 |#
153
 
154
   
155
 
156
 #|
157
 
158
 (write-sparql-results+term-number '((?::a ?::s ?::z) (1 2 3) (<http://example/1> <http://example/2> <http://example/3>))
159
                                    *trace-output*)
160
 
161
 (write-sparql-results+term-number spocq.a:|true| *trace-output*)
162
 
163
 (send-response-message :test
164
                        '((?::a ?::s ?::z) (1 2 3) (<http://example/1> <http://example/2> <http://example/3>))
165
                        *trace-output*
166
                        mime:application/sparql-results+term-number)
167
       
168
 |#