-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
65 lines (55 loc) · 1.19 KB
/
index.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
package dieci
import (
lru "github.com/hashicorp/golang-lru"
)
// cache is in memory lookup store
type cache struct {
lru *lru.TwoQueueCache
}
func newCache(size int) *cache {
l, _ := lru.New2Q(size)
return &cache{lru: l}
}
func (c *cache) Add(key Key, score Score) {
cacheKey := key.String()
c.lru.Add(cacheKey, score)
}
func (c *cache) Get(key Key) (score Score, ok bool) {
cacheKey := key.String()
value, ok := c.lru.Get(cacheKey)
if ok {
score = value.(Score)
}
return score, ok
}
// Index represents an index of a datalog file
type Index struct {
cache *cache
backend Backend
}
// NewIndex returns a new index
func NewIndex(b Backend) *Index {
cache := newCache(20000)
return &Index{cache: cache, backend: b}
}
// Read is a read callback
func (idx *Index) Read(key Key) (Score, error) {
if score, ok := idx.cache.Get(key); ok {
return score, nil
}
score, err := idx.backend.Read(key)
if err == nil {
idx.cache.Add(key, score)
}
return score, err
}
// Write is a write callback
func (idx *Index) Write(score Score) (Key, error) {
key := NewKey()
err := idx.backend.Write(key, score)
if err != nil {
return Key{}, err
}
idx.cache.Add(key, score)
return key, nil
}