-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
278 lines (248 loc) · 6.48 KB
/
util.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
package styx
import (
"encoding/base64"
"encoding/binary"
"encoding/json"
"io"
"regexp"
"strings"
badger "github.com/dgraph-io/badger/v2"
ld "github.com/piprate/json-gold/ld"
rdf "github.com/underlay/go-rdfjs"
)
// ID is the type of terms of the index tuples
type ID string
// NIL is empty ID
var NIL ID = ""
type iri string
var proc = ld.NewJsonLdProcessor()
var patternInteger = regexp.MustCompile("^[\\-+]?[0-9]+$")
var patternDouble = regexp.MustCompile("^(\\+|-)?([0-9]+(\\.[0-9]*)?|\\.[0-9]+)([Ee](\\+|-)?[0-9]+)?$")
const max6Byte uint64 = 16777216
const max8Byte uint64 = 281474976710656
func fromUint64(id uint64) iri {
var res []byte
if id < max6Byte {
res = make([]byte, 4)
tmp := make([]byte, 4)
binary.BigEndian.PutUint32(tmp, uint32(id))
base64.StdEncoding.Encode(res, tmp[1:])
} else if id < max8Byte {
res = make([]byte, 8)
tmp := make([]byte, 8)
binary.BigEndian.PutUint64(tmp, id)
base64.StdEncoding.Encode(res, tmp[2:])
}
l := len(res)
if res[l-2] == '=' {
return iri(res[:l-2])
} else if res[l-1] == '=' {
return iri(res[:l-1])
} else {
return iri(res)
}
}
func escape(str string) string {
str = strings.Replace(str, "\\", "\\\\", -1)
str = strings.Replace(str, "\"", "\\\"", -1)
str = strings.Replace(str, "\n", "\\n", -1)
str = strings.Replace(str, "\r", "\\r", -1)
str = strings.Replace(str, "\t", "\\t", -1)
return str
}
func unescape(str string) string {
str = strings.Replace(str, "\\\\", "\\", -1)
str = strings.Replace(str, "\\\"", "\"", -1)
str = strings.Replace(str, "\\n", "\n", -1)
str = strings.Replace(str, "\\r", "\r", -1)
str = strings.Replace(str, "\\t", "\t", -1)
return str
}
// assembleKey concatenates the passed slices
func assembleKey(prefix byte, tail bool, terms ...ID) []byte {
l := 0
for _, term := range terms {
l += 1 + len(term)
}
if tail {
l++
}
key := make([]byte, l)
key[0] = prefix
i := 1
for _, term := range terms {
copy(key[i:i+len(term)], term)
i += len(term)
if i < l {
key[i] = '\t'
i++
}
}
return key
}
// setSafe writes the entry and returns a new transaction if the old one was full.
func setSafe(key, val []byte, txn *badger.Txn, db *badger.DB) (*badger.Txn, error) {
e := badger.NewEntry(key, val).WithMeta(key[0])
err := txn.SetEntry(e)
if err == badger.ErrTxnTooBig {
err = txn.Commit()
if err != nil {
return nil, err
}
txn = db.NewTransaction(true)
err = txn.SetEntry(e)
}
return txn, err
}
// deleteSafe deletes the entry and returns a new transaction if the old one was full.
func deleteSafe(key []byte, txn *badger.Txn, db *badger.DB) (*badger.Txn, error) {
err := txn.Delete(key)
if err == badger.ErrTxnTooBig {
err = txn.Commit()
if err != nil {
return nil, err
}
txn = db.NewTransaction(true)
err = txn.Delete(key)
}
return txn, err
}
// matrix is a type for 3x3 permutators
type matrix [3][3]uint8
// permute permutes the given ids by the specified permutation
func (m matrix) permute(permutation Permutation, ids [3]ID) (ID, ID, ID) {
row := m[permutation]
return ids[row[0]], ids[row[1]], ids[row[2]]
}
// major indexes the major permutations
var major = matrix{
[3]uint8{0, 1, 2},
[3]uint8{1, 2, 0},
[3]uint8{2, 0, 1},
}
// minor indexes the minor permutations
var minor = matrix{
[3]uint8{0, 2, 1},
[3]uint8{1, 0, 2},
[3]uint8{2, 1, 0},
}
func getDataset(input interface{}, opts *ld.JsonLdOptions) (dataset *ld.RDFDataset, err error) {
var document interface{}
switch input := input.(type) {
case []byte:
err = json.Unmarshal(input, &document)
case string:
err = json.Unmarshal([]byte(input), &document)
case io.Reader:
err = json.NewDecoder(input).Decode(&document)
case map[string]interface{}:
document = input
case []interface{}:
document = input
default:
err = ErrInvalidInput
}
if err != nil {
return
}
var rdf interface{}
rdf, err = proc.ToRDF(document, opts)
if err != nil {
return
}
switch result := rdf.(type) {
case *ld.RDFDataset:
return result, err
default:
err = ErrInvalidInput
}
return
}
func fromLdDataset(dataset *ld.RDFDataset, base string) []*rdf.Quad {
result := []*rdf.Quad{}
for _, quads := range dataset.Graphs {
for _, quad := range quads {
result = append(result, fromLdQuad(quad, base))
}
}
return result
}
func fromLdQuad(quad *ld.Quad, base string) *rdf.Quad {
return rdf.NewQuad(
fromLdNode(quad.Subject, base),
fromLdNode(quad.Predicate, base),
fromLdNode(quad.Object, base),
fromLdNode(quad.Graph, base),
)
}
var blankNodePrefix = "_:"
func fromLdNode(node ld.Node, base string) rdf.Term {
if node == nil {
return rdf.Default
}
switch node := node.(type) {
case *ld.IRI:
if base != "" && strings.HasPrefix(node.Value, base) {
return rdf.NewVariable(node.Value[len(base):])
}
return rdf.NewNamedNode(node.Value)
case *ld.BlankNode:
if node.Attribute == "" || node.Attribute == "@default" {
return rdf.Default
} else if node.Attribute[:len(blankNodePrefix)] == blankNodePrefix {
return rdf.NewBlankNode(node.Attribute[len(blankNodePrefix):])
} else {
return rdf.NewBlankNode(node.Attribute)
}
case *ld.Literal:
if node.Language != "" {
return rdf.NewLiteral(node.Value, node.Language, rdf.RDFLangString)
} else if node.Datatype != "" && node.Datatype != ld.XSDString {
return rdf.NewLiteral(node.Value, "", rdf.NewNamedNode(node.Datatype))
} else {
return rdf.NewLiteral(node.Value, "", nil)
}
}
return nil
}
func toLdNode(term rdf.Term) ld.Node {
switch term := term.(type) {
case *rdf.NamedNode:
return ld.NewIRI(term.Value())
case *rdf.BlankNode:
return ld.NewBlankNode(blankNodePrefix + term.Value())
case *rdf.Literal:
if term.Datatype() == nil {
return ld.NewLiteral(term.Value(), "", "")
}
return ld.NewLiteral(term.Value(), term.Datatype().Value(), term.Language())
case *rdf.DefaultGraph:
return ld.NewBlankNode("@default")
default:
return nil
}
}
func toLdQuad(quad *rdf.Quad) *ld.Quad {
return &ld.Quad{
Subject: toLdNode(quad.Subject()),
Predicate: toLdNode(quad.Predicate()),
Object: toLdNode(quad.Object()),
Graph: toLdNode(quad.Graph()),
}
}
// ToRDFDataset transforms the slice of quads into an *ld.RDFDataset
func ToRDFDataset(quads []*rdf.Quad) *ld.RDFDataset {
dataset := ld.NewRDFDataset()
for _, quad := range quads {
label := quad.Graph().String()
if label == "" {
label = "@default"
}
if graph, has := dataset.Graphs[label]; has {
dataset.Graphs[label] = append(graph, toLdQuad(quad))
} else {
dataset.Graphs[label] = []*ld.Quad{toLdQuad(quad)}
}
}
return dataset
}