-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyx.go
204 lines (180 loc) · 4.34 KB
/
styx.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
package styx
import (
"bytes"
"encoding/binary"
"log"
"strings"
badger "github.com/dgraph-io/badger/v2"
uuid "github.com/google/uuid"
ld "github.com/piprate/json-gold/ld"
rdf "github.com/underlay/go-rdfjs"
)
// DefaultPath is the default path for the Badger database
const tmpPath = "/tmp/styx"
// A Store is a database instance
type Store struct {
Badger *badger.DB
Config *Config
}
// Config contains the initialization options passed to Styx
type Config struct {
TagScheme TagScheme
Dictionary DictionaryFactory
QuadStore QuadStore
}
// Close the database
func (s *Store) Close() (err error) {
if s == nil {
return
}
if s.Config.Dictionary != nil {
err = s.Config.Dictionary.Close()
if err != nil {
return
}
}
if s.Badger != nil {
err = s.Badger.Close()
if err != nil {
return
}
}
return
}
// NewMemoryStore opens a styx database in memory
func NewMemoryStore(config *Config) (*Store, error) {
opts := badger.DefaultOptions("").WithInMemory(true)
db, err := badger.Open(opts)
if err != nil {
return nil, err
}
return NewStore(config, db)
}
// NewStore opens a styx database
func NewStore(config *Config, db *badger.DB) (*Store, error) {
if config == nil {
config = &Config{}
}
if config.TagScheme == nil {
config.TagScheme = nilTagScheme{}
}
if config.Dictionary == nil {
config.Dictionary = StringDictionary
}
if config.QuadStore == nil {
config.QuadStore = MakeEmptyStore()
}
return &Store{
Config: config,
Badger: db,
}, nil
}
// QueryJSONLD exposes a JSON-LD query interface
func (s *Store) QueryJSONLD(query interface{}) (*Iterator, error) {
opts := ld.NewJsonLdOptions("")
opts.ProduceGeneralizedRdf = true
id, err := uuid.NewRandom()
if err != nil {
return nil, err
}
base := "urn:uuid:" + id.String() + "?"
opts.ExpandContext = map[string]interface{}{"?": base}
dataset, err := getDataset(query, opts)
if err != nil {
return nil, err
}
quads := fromLdDataset(dataset, base)
return s.Query(quads, nil, nil)
}
// Query satisfies the Styx interface
func (s *Store) Query(pattern []*rdf.Quad, domain []rdf.Term, index []rdf.Term) (*Iterator, error) {
txn := s.Badger.NewTransaction(false)
dictionary := s.Config.Dictionary.Open(false)
iter, err := newIterator(pattern, domain, index, s.Config.TagScheme, txn, dictionary)
if err != nil {
iter.Close()
}
if err == badger.ErrKeyNotFound || err == ErrEmptyInterset {
err = nil
iter.top = true
}
return iter, err
}
// Log will print the *entire database contents* to log
func (s *Store) Log() {
txn := s.Badger.NewTransaction(false)
defer txn.Discard()
iter := txn.NewIterator(badger.DefaultIteratorOptions)
defer iter.Close()
var i int
for iter.Seek(nil); iter.Valid(); iter.Next() {
item := iter.Item()
key := item.KeyCopy(nil)
val, err := item.ValueCopy(nil)
if err != nil {
log.Println(err)
return
}
prefix := key[0]
if bytes.Equal(key, SequenceKey) {
log.Printf("Sequence: %02d\n", binary.BigEndian.Uint64(val))
} else if prefix == ValueToIDPrefix {
// Value key
value := string(key[1:])
if err != nil {
log.Println(err)
return
}
log.Printf("Value to ID: %s -> %s\n", value, string(val))
} else if prefix == IDToValuePrefix {
// Value key
id := iri(key[1:])
if err != nil {
log.Println(err)
return
}
log.Printf("ID to Value: %s <- %s\n", id, string(val))
} else if 'a' <= prefix && prefix <= 'c' {
// Ternary key
log.Println(
"Ternary entry:",
string(prefix),
strings.Replace(string(key[1:]), "\t", " ", -1),
"->",
"|"+strings.Replace(strings.Replace(string(val), "\t", " ", -1), "\n", "|", -1),
)
} else if 'i' <= prefix && prefix <= 'n' {
// Binary key
log.Println(
"Binary entry:",
string(prefix),
strings.Replace(string(key[1:]), "\t", " ", -1),
"->",
binary.BigEndian.Uint32(val),
)
} else if prefix == DatasetPrefix {
log.Printf("Dataset: %s\n", string(key[1:]))
} else if prefix == UnaryPrefix {
if len(val) != 24 {
log.Println("Unexpected index value", val)
return
}
index := &[6]uint32{}
for i := 0; i < 6; i++ {
index[i] = binary.BigEndian.Uint32(val[i*4 : (i+1)*4])
}
log.Println(
"Unary entry:",
string(prefix),
string(key[1:]),
"->",
*index,
)
} else {
// Some other key...
}
i++
}
log.Printf("Printed %02d database entries\n", i)
return
}