/
storage.go
381 lines (304 loc) · 7.86 KB
/
storage.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Package happy is the main backend storage for graphie. happy stores
// graphs efficiently and supports high-write throughput with a write complexity
// of O(1).
package happy
import (
"container/list"
"crypto/md5"
"crypto/rand"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"sort"
"sync"
"github.com/flosch/graphie"
)
const (
maxWorkers = 4
)
var (
ErrNotFound = errors.New("Node not found")
)
type nodetables []*nodetable
func (nt nodetables) Len() int {
return len(nt)
}
func (nt nodetables) Less(i, j int) bool {
return nt[i].created > nt[j].created
}
func (nt nodetables) Swap(i, j int) {
nt[i], nt[j] = nt[j], nt[i]
}
type storage struct {
g *graphie.Graph
path string
wg sync.WaitGroup
counterNodes uint64
counterLabels uint16
lock sync.RWMutex
labelIndex map[string]uint16
memtable map[uint64]*node
memtableQueueLock sync.Mutex
memtableQueue *list.List
memtableWorkersChan chan *list.Element
tables nodetables
}
func init() {
graphie.RegisterDriver("happy", registerHappy)
runtime.GOMAXPROCS(runtime.NumCPU())
}
func registerHappy(g *graphie.Graph) (graphie.Storage, error) {
return &storage{
g: g,
labelIndex: make(map[string]uint16),
memtable: make(map[uint64]*node),
memtableQueue: list.New(),
memtableWorkersChan: make(chan *list.Element),
}, nil
}
func (s *storage) Start(attrs string, dbname string) error {
// Load all table indexes (bitmaps)
path, err := filepath.Abs(attrs)
if err != nil {
return err
}
s.path = path
fi, err := os.Stat(path)
if os.IsNotExist(err) {
err = os.MkdirAll(path, 0700)
if err != nil {
return err
}
} else if err != nil {
return err
} else {
if !fi.IsDir() {
return fmt.Errorf("Path '%s' is not a directory.", attrs)
}
}
// Start all workers
for i := 0; i < maxWorkers; i++ {
s.wg.Add(1)
go s.memtableWorker()
}
return nil
}
func (s *storage) Stop() error {
s.lock.RLock()
if len(s.memtable) > 0 {
s.lock.RUnlock()
s.memtableFlush()
} else {
s.lock.RUnlock()
}
close(s.memtableWorkersChan)
s.wg.Wait()
return nil
}
// memtable_flush manages the lock itself!
func (s *storage) memtableFlush() {
s.lock.Lock()
// Make old one persistent
oldMemtable := s.memtable
// Make the memtables accessible while they are being written on disk
s.memtableQueueLock.Lock()
el := s.memtableQueue.PushBack(oldMemtable)
s.memtableQueueLock.Unlock()
// Create an empty memtable for new nodes
s.memtable = make(map[uint64]*node)
s.lock.Unlock()
// This blocks when all workers are busy and there's no chance to full the memory
// Otherwise it will pass the memtable to a worker who will write the memtable down
// in nodetable format
s.memtableWorkersChan <- el
}
func (s *storage) Add(labels []string, attrs graphie.Attrs) (graphie.NodeID, error) {
// Add to memtable
s.lock.Lock()
s.counterNodes++
n := &node{
s: s,
id: s.counterNodes,
attrs: attrs,
labels: make([]uint16, 0, len(labels)),
linksOut: make([]*link, 0, 10), // some guesses with 10 outlinks on avg
linksIn: make([]*link, 0, 10), // same
}
for _, lbl := range labels {
n.labels = append(n.labels, s.labelindex(lbl))
}
s.memtable[s.counterNodes] = n
s.lock.Unlock()
// Check if we reached the treshold
if len(s.memtable) >= nodetablePersistentThreshold {
s.memtableFlush()
}
return graphie.NodeID(s.counterNodes), nil
}
// s.lock must be held outside of get()
func (s *storage) get(id graphie.NodeID) (*node, error) {
// First search for the node
n, err := s.getRaw(id)
if err != nil {
return nil, err
}
// Second, make a copy of this node
newNode := &node{
s: s,
id: n.id,
attrs: make(graphie.Attrs),
linksOut: make([]*link, 0, len(n.linksOut)),
linksIn: make([]*link, 0, len(n.linksIn)),
}
copy(newNode.labels, n.labels)
for _, lnk := range n.linksOut {
newNode.linksOut = append(newNode.linksOut, &link{
other: lnk.other,
attrs: lnk.attrs,
})
}
for _, lnk := range n.linksIn {
newNode.linksIn = append(newNode.linksIn, &link{
other: lnk.other,
attrs: lnk.attrs,
})
}
for k, v := range n.attrs {
newNode.attrs[k] = v
}
return newNode, nil
}
func (s *storage) getRaw(id graphie.NodeID) (*node, error) {
// First, check current memtable
n, has := s.memtable[uint64(id)]
if has {
// We were lucky
return n, nil
}
// Second, check all remaining memtables in the persisting-queue
s.memtableQueueLock.Lock()
f := s.memtableQueue.Front()
for f != nil {
m := f.Value.(map[uint64]*node)
n, has = m[uint64(id)]
if has {
s.memtableQueueLock.Unlock()
return n, nil
}
f = f.Next()
}
s.memtableQueueLock.Unlock()
// Last, check all persistent nodetables; (1) using bitmap (2) using binary search on disk
// something like
panic("not implemented yet")
}
func (s *storage) Unlink(from, to graphie.NodeID, attrs graphie.Attrs) error {
panic("not implemented")
}
func (s *storage) Link(from, to graphie.NodeID, attrs graphie.Attrs) error {
// Get both nodes
// TODO: Do locking on a per node-id basis, not using a global lock
s.lock.Lock()
defer s.lock.Unlock()
// Receive a copy of the node's data
nodeFrom, err := s.get(from)
if err != nil {
return err
}
nodeTo, err := s.get(to)
if err != nil {
return err
}
nodeFrom.linksOut = append(nodeFrom.linksOut, &link{
other: nodeTo.id,
attrs: attrs,
})
nodeTo.linksIn = append(nodeTo.linksIn, &link{
other: nodeFrom.id,
attrs: attrs,
})
// Both nodes must be rewritten, add them to the memtable
s.memtable[nodeFrom.id] = nodeFrom
s.memtable[nodeTo.id] = nodeTo
return nil
}
func (s *storage) memtableWorker() {
defer s.wg.Done()
for el := range s.memtableWorkersChan {
mem := el.Value.(map[uint64]*node)
// Make the memtable persistent
h := md5.New()
_, err := io.CopyN(h, rand.Reader, 32)
if err != nil {
panic(err)
}
nt, err := createNodetable(mem, filepath.Join(s.path, fmt.Sprintf("%x.nt", h.Sum(nil))))
if err != nil {
panic(err)
}
// Remove it now from the memtable-queue and add it to the nodetables atomically
s.lock.Lock()
s.memtableQueueLock.Lock()
s.memtableQueue.Remove(el)
s.memtableQueueLock.Unlock()
s.tables = append(s.tables, nt)
// It is important to keep all tables sorted by their creation date
sort.Sort(s.tables)
s.lock.Unlock()
}
}
// Does not hold s.lock; must be held outside
func (s *storage) labelindex(l string) uint16 {
i, has := s.labelIndex[l]
if has {
return i
}
if s.counterLabels >= 2^16-1 {
panic("Too many labels; max supported by happy is 2^16-1")
}
s.counterLabels++
s.labelIndex[l] = s.counterLabels
return s.counterLabels
}
func (s *storage) Merge(labels []string, attrs graphie.Attrs) (graphie.NodeID, error) {
return s.Add(labels, attrs)
}
func (s *storage) Remove(id graphie.NodeID) error {
s.lock.Lock()
defer s.lock.Unlock()
// TODO (important):
// To keep the database consistent, we have to remove all references to this
// node (outgoing and incoming nodes in other nodes and we have to write them
// again)
// TODO: We're currently ignoring that the node might not exist
s.memtable[uint64(id)] = nil
return nil
}
func (s *storage) EnsureIndexNodes(labels []string, attrName string) error {
return nil
}
func (s *storage) EnsureIndexLinks(labels []string, attrName string) error {
return nil
}
func (s *storage) In(id graphie.NodeID) ([]*graphie.Link, error) {
return nil, nil
}
func (s *storage) Out(id graphie.NodeID) ([]*graphie.Link, error) {
return nil, nil
}
// Attribute handling
func (s *storage) Set(id graphie.NodeID, key string, value interface{}) error {
return nil
}
func (s *storage) Get(id graphie.NodeID, key string) (interface{}, error) {
return nil, nil
}
func (s *storage) Has(id graphie.NodeID, key string) (bool, error) {
return false, nil
}
func (s *storage) Attrs(id graphie.NodeID) (graphie.Attrs, error) {
return nil, nil
}