-
Notifications
You must be signed in to change notification settings - Fork 11
/
encode.go
311 lines (267 loc) · 6.9 KB
/
encode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package triplestore
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"io"
"net/url"
"strings"
)
type Encoder interface {
Encode(tris ...Triple) error
}
type StreamEncoder interface {
StreamEncode(context.Context, <-chan Triple) error
}
func NewContext() *Context {
return &Context{Prefixes: make(map[string]string)}
}
type Context struct {
Base string
Prefixes map[string]string
}
var RDFContext = &Context{
Prefixes: map[string]string{
"xsd": "http://www.w3.org/2001/XMLSchema#",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
},
}
type wordLength uint32
const (
resourceTypeEncoding = uint8(0)
literalTypeEncoding = uint8(1)
bnodeTypeEncoding = uint8(2)
literalWithLangEncoding = uint8(3)
)
type binaryEncoder struct {
w io.Writer
}
func NewBinaryStreamEncoder(w io.Writer) StreamEncoder {
return &binaryEncoder{w}
}
func NewBinaryEncoder(w io.Writer) Encoder {
return &binaryEncoder{w}
}
func (enc *binaryEncoder) StreamEncode(ctx context.Context, triples <-chan Triple) error {
if triples == nil {
return nil
}
var buf bytes.Buffer
for {
select {
case tri, ok := <-triples:
if !ok {
return nil
}
if err := enc.writeTriple(tri, &buf); err != nil {
return err
}
case <-ctx.Done():
return nil
}
}
}
func (enc *binaryEncoder) Encode(tris ...Triple) error {
var buf bytes.Buffer
for _, t := range tris {
if err := enc.writeTriple(t, &buf); err != nil {
return err
}
}
return nil
}
func (enc *binaryEncoder) writeTriple(t Triple, buf *bytes.Buffer) error {
if err := encodeBinTriple(t, buf); err != nil {
return err
}
if _, err := enc.w.Write(buf.Bytes()); err != nil {
return err
}
buf.Reset()
return nil
}
func encodeBinTriple(t Triple, buff *bytes.Buffer) error {
sub, pred := t.Subject(), t.Predicate()
binary.Write(buff, binary.BigEndian, t.(*triple).isSubBnode)
binary.Write(buff, binary.BigEndian, wordLength(len(sub)))
buff.WriteString(sub)
binary.Write(buff, binary.BigEndian, wordLength(len(pred)))
buff.WriteString(pred)
obj := t.Object()
if lit, isLit := obj.Literal(); isLit {
if lang := lit.Lang(); len(lang) > 0 {
binary.Write(buff, binary.BigEndian, literalWithLangEncoding)
binary.Write(buff, binary.BigEndian, wordLength(len(lang)))
buff.WriteString(string(lang))
} else {
binary.Write(buff, binary.BigEndian, literalTypeEncoding)
typ := lit.Type()
binary.Write(buff, binary.BigEndian, wordLength(len(typ)))
buff.WriteString(string(typ))
}
litVal := lit.Value()
if lit.Type() == XsdString {
litVal = escapeStringLiteral(litVal)
}
binary.Write(buff, binary.BigEndian, wordLength(len(litVal)))
buff.WriteString(litVal)
} else if bnode, isBnode := obj.Bnode(); isBnode {
binary.Write(buff, binary.BigEndian, bnodeTypeEncoding)
binary.Write(buff, binary.BigEndian, wordLength(len(bnode)))
buff.WriteString(bnode)
} else {
binary.Write(buff, binary.BigEndian, resourceTypeEncoding)
res, _ := obj.Resource()
binary.Write(buff, binary.BigEndian, wordLength(len(res)))
buff.WriteString(res)
}
return nil
}
type ntriplesEncoder struct {
w io.Writer
c *Context
}
func NewLenientNTStreamEncoder(w io.Writer) StreamEncoder {
return &ntriplesEncoder{w: w}
}
func NewLenientNTEncoder(w io.Writer) Encoder {
return &ntriplesEncoder{w: w}
}
func NewLenientNTEncoderWithContext(w io.Writer, c *Context) Encoder {
return &ntriplesEncoder{w: w, c: c}
}
func (enc *ntriplesEncoder) StreamEncode(ctx context.Context, triples <-chan Triple) error {
if triples == nil {
return nil
}
var buf bytes.Buffer
finalWrite := func() error {
_, err := enc.w.Write(buf.Bytes())
return err
}
for {
select {
case tri, ok := <-triples:
if !ok {
return finalWrite()
}
encodeNTriple(tri, enc.c, &buf)
case <-ctx.Done():
return finalWrite()
}
}
}
func (enc *ntriplesEncoder) Encode(tris ...Triple) error {
var buff bytes.Buffer
for _, t := range tris {
encodeNTriple(t, enc.c, &buff)
}
_, err := enc.w.Write(buff.Bytes())
return err
}
func encodeNTriple(t Triple, ctx *Context, buff *bytes.Buffer) {
var sub string
if tt := t.(*triple); tt.isSubBnode {
sub = "_:" + buildIRI(ctx, t.Subject())
} else {
sub = "<" + buildIRI(ctx, t.Subject()) + ">"
}
buff.WriteString(sub + " <" + buildIRI(ctx, t.Predicate()) + "> ")
if bnode, isBnode := t.Object().Bnode(); isBnode {
buff.WriteString("_:" + bnode)
} else {
if rid, ok := t.Object().Resource(); ok {
buff.WriteString("<" + buildIRI(ctx, rid) + ">")
} else if lit, ok := t.Object().Literal(); ok {
if lit.Lang() != "" {
buff.WriteString("\"" + escapeStringLiteral(lit.Value()) + "\"@" + lit.Lang())
} else {
switch lit.Type() {
case XsdString:
// namespace empty as per spec
buff.WriteString("\"" + escapeStringLiteral(lit.Value()) + "\"")
default:
if ctx != nil {
if _, ok := ctx.Prefixes["xsd"]; ok {
buff.WriteString("\"" + lit.Value() + "\"^^<" + lit.Type().NTriplesNamespaced() + ">")
}
} else {
buff.WriteString("\"" + lit.Value() + "\"^^<" + string(lit.Type()) + ">")
}
}
}
}
}
buff.Write([]byte(" .\n"))
}
func buildIRI(ctx *Context, id string) string {
if ctx != nil {
if ctx.Prefixes != nil {
for k, uri := range ctx.Prefixes {
prefix := k + ":"
if strings.HasPrefix(id, prefix) {
id = uri + url.QueryEscape(strings.TrimPrefix(id, prefix))
continue
}
}
}
if !strings.HasPrefix(id, "http") && ctx.Base != "" {
id = ctx.Base + url.QueryEscape(id)
}
}
return id
}
type dotGraphEncoder struct {
pred string
w io.Writer
}
func NewDotGraphEncoder(w io.Writer, predicate string) Encoder {
return &dotGraphEncoder{w: w, pred: predicate}
}
func (dg *dotGraphEncoder) Encode(tris ...Triple) error {
src := NewSource()
src.Add(tris...)
snap := src.Snapshot()
all := snap.WithPredicate(dg.pred)
queryDone := make(map[string][]string)
getTypes := func(ref string) ([]string, bool) {
if all, ok := queryDone[ref]; ok {
return all, true
} else {
fresh := snap.WithSubjPred(ref, "rdf:type")
for _, typ := range fresh {
val, _ := typ.Object().Resource()
queryDone[ref] = append(queryDone[ref], val)
}
return queryDone[ref], false
}
}
fmt.Fprintf(dg.w, "digraph \"%s\" {\n", dg.pred)
for _, tri := range all {
sub := tri.Subject()
res, ok := tri.Object().Resource()
if ok {
fmt.Fprintf(dg.w, "\"%s\" -> \"%s\";\n", sub, res)
subTypes, done := getTypes(sub)
if !done {
for _, typ := range subTypes {
fmt.Fprintf(dg.w, "\"%s\" [label=\"%s<%s>\"];\n", sub, sub, typ)
}
}
resTypes, done := getTypes(res)
if !done {
for _, typ := range resTypes {
fmt.Fprintf(dg.w, "\"%s\" [label=\"%s<%s>\"];\n", res, res, typ)
}
}
}
}
fmt.Fprintf(dg.w, "}")
return nil
}
var escaper = strings.NewReplacer("\n", "\\n", "\r", "\\r")
func escapeStringLiteral(s string) string {
return escaper.Replace(s)
}