-
Notifications
You must be signed in to change notification settings - Fork 11
/
source.go
220 lines (183 loc) · 4.33 KB
/
source.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
package triplestore
import (
"fmt"
"reflect"
"sort"
"strings"
"sync"
"sync/atomic"
)
// A source is a persistent yet mutable source or container of triples.
type Source interface {
Add(...Triple)
Remove(...Triple)
Snapshot() RDFGraph
CopyTriples() []Triple
}
// A RDFGraph is an immutable set of triples. It is a snapshot of a source and it is queryable.
type RDFGraph interface {
Contains(Triple) bool
Triples() []Triple
Count() int
WithSubject(s string) []Triple
WithPredicate(p string) []Triple
WithObject(o Object) []Triple
WithSubjObj(s string, o Object) []Triple
WithSubjPred(s, p string) []Triple
WithPredObj(p string, o Object) []Triple
}
type Triples []Triple
func (ts Triples) Equal(others Triples) bool {
if len(ts) != len(others) {
return false
}
this := make(map[string]struct{})
for _, tri := range ts {
this[tri.(*triple).key()] = struct{}{}
}
other := make(map[string]struct{})
for _, tri := range others {
other[tri.(*triple).key()] = struct{}{}
}
return reflect.DeepEqual(this, other)
}
func (ts Triples) Sort() {
sort.Slice(ts, func(i, j int) bool { return ts[i].(*triple).key() > ts[j].(*triple).key() })
}
func (ts Triples) Map(fn func(Triple) string) (out []string) {
for _, t := range ts {
out = append(out, fn(t))
}
return
}
func (ts Triples) String() string {
joined := strings.Join(ts.Map(
func(t Triple) string { return fmt.Sprint(t) },
), "\n")
return fmt.Sprintf("[%s]", joined)
}
type source struct {
latestSnap atomic.Value
updated uint32 // atomic
mu sync.RWMutex
triples map[string]Triple
}
// A source is a persistent yet mutable source or container of triples
func NewSource() Source {
s := &source{
triples: make(map[string]Triple),
}
s.latestSnap.Store(newGraph(0))
return s
}
func (s *source) isUpdated() bool {
return atomic.LoadUint32(&s.updated) > 0
}
func (s *source) update() {
atomic.StoreUint32(&s.updated, uint32(1))
}
func (s *source) reset() {
atomic.StoreUint32(&s.updated, uint32(0))
}
func (s *source) Add(ts ...Triple) {
s.mu.Lock()
defer s.mu.Unlock()
defer s.update()
for _, t := range ts {
tr := t.(*triple)
s.triples[tr.key()] = t
}
}
func (s *source) Remove(ts ...Triple) {
s.mu.Lock()
defer s.mu.Unlock()
defer s.update()
for _, t := range ts {
tr := t.(*triple)
delete(s.triples, tr.key())
}
}
func (s *source) CopyTriples() (out []Triple) {
s.mu.RLock()
defer s.mu.RUnlock()
for _, t := range s.triples {
out = append(out, t.(*triple).clone())
}
return
}
func (s *source) Snapshot() RDFGraph {
if !s.isUpdated() {
return s.latestSnap.Load().(RDFGraph)
}
s.mu.RLock()
defer s.mu.RUnlock()
gph := newGraph(len(s.triples))
for k, t := range s.triples {
objKey := t.Object().(object).key()
sub, pred := t.Subject(), t.Predicate()
gph.s[sub] = append(gph.s[sub], t)
gph.p[pred] = append(gph.p[pred], t)
gph.o[objKey] = append(gph.o[objKey], t)
sp := sub + pred
gph.sp[sp] = append(gph.sp[sp], t)
so := sub + objKey
gph.so[so] = append(gph.so[so], t)
po := pred + objKey
gph.po[po] = append(gph.po[po], t)
gph.spo[k] = t
}
s.latestSnap.Store(gph)
s.reset()
return gph
}
type graph struct {
once sync.Once
unique []Triple
s, p, o map[string][]Triple
sp, so, po map[string][]Triple
spo map[string]Triple
}
func newGraph(cap int) *graph {
return &graph{
s: make(map[string][]Triple, cap),
p: make(map[string][]Triple, cap),
o: make(map[string][]Triple, cap),
sp: make(map[string][]Triple, cap),
so: make(map[string][]Triple, cap),
po: make(map[string][]Triple, cap),
spo: make(map[string]Triple, cap),
}
}
func (g *graph) Contains(t Triple) bool {
_, ok := g.spo[t.(*triple).key()]
return ok
}
func (g *graph) Triples() []Triple {
g.once.Do(func() {
for _, t := range g.spo {
g.unique = append(g.unique, t)
}
})
return g.unique
}
func (g *graph) Count() int {
return len(g.spo)
}
func (g *graph) WithSubject(s string) []Triple {
return g.s[s]
}
func (g *graph) WithPredicate(p string) []Triple {
return g.p[p]
}
func (g *graph) WithObject(o Object) []Triple {
return g.o[o.(object).key()]
}
func (g *graph) WithSubjObj(s string, o Object) []Triple {
return g.so[s+o.(object).key()]
}
func (g *graph) WithSubjPred(s, p string) []Triple {
return g.sp[s+p]
}
func (g *graph) WithPredObj(p string, o Object) []Triple {
return g.po[p+o.(object).key()]
}