forked from Cepave/graph
/
store.go
151 lines (127 loc) · 2.9 KB
/
store.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
package store
import (
"container/list"
"hash/crc32"
"log"
"sync"
cmodel "github.com/Cepave/common/model"
"github.com/Cepave/graph/g"
)
var GraphItems *GraphItemMap
type GraphItemMap struct {
sync.RWMutex
A []map[string]*SafeLinkedList
Size int
}
func (this *GraphItemMap) Get(key string) (*SafeLinkedList, bool) {
this.RLock()
defer this.RUnlock()
idx := hashKey(key) % uint32(this.Size)
val, ok := this.A[idx][key]
return val, ok
}
func (this *GraphItemMap) Getitems(idx int) map[string]*SafeLinkedList {
this.RLock()
defer this.RUnlock()
items := this.A[idx]
this.A[idx] = make(map[string]*SafeLinkedList)
return items
}
func (this *GraphItemMap) Set(key string, val *SafeLinkedList) {
this.Lock()
defer this.Unlock()
idx := hashKey(key) % uint32(this.Size)
this.A[idx][key] = val
}
func (this *GraphItemMap) Len() int {
this.RLock()
defer this.RUnlock()
var l int
for i := 0; i < this.Size; i++ {
l += len(this.A[i])
}
return l
}
func (this *GraphItemMap) First(key string) *cmodel.GraphItem {
this.RLock()
defer this.RUnlock()
idx := hashKey(key) % uint32(this.Size)
L, ok := this.A[idx][key]
if !ok {
return nil
}
first := L.Front()
if first == nil {
return nil
}
return first.Value.(*cmodel.GraphItem)
}
func (this *GraphItemMap) PopAll(key string) []*cmodel.GraphItem {
this.Lock()
defer this.Unlock()
idx := hashKey(key) % uint32(this.Size)
L, ok := this.A[idx][key]
if !ok {
return []*cmodel.GraphItem{}
}
return L.PopAll()
}
func (this *GraphItemMap) FetchAll(key string) []*cmodel.GraphItem {
this.RLock()
defer this.RUnlock()
idx := hashKey(key) % uint32(this.Size)
L, ok := this.A[idx][key]
if !ok {
return []*cmodel.GraphItem{}
}
return L.FetchAll()
}
func hashKey(key string) uint32 {
if len(key) < 64 {
var scratch [64]byte
copy(scratch[:], key)
return crc32.ChecksumIEEE(scratch[:len(key)])
}
return crc32.ChecksumIEEE([]byte(key))
}
func getWts(key string, now int64) int64 {
interval := int64(g.CACHE_TIME)
return now + interval - (int64(hashKey(key)) % interval)
}
func (this *GraphItemMap) PushFront(key string, val *cmodel.GraphItem) {
if linkedList, exists := this.Get(key); exists {
linkedList.PushFront(val)
} else {
//log.Println("new key:", key)
NL := list.New()
NL.PushFront(val)
safeList := &SafeLinkedList{L: NL}
this.Set(key, safeList)
}
}
func (this *GraphItemMap) KeysByIndex(idx int) []string {
this.RLock()
defer this.RUnlock()
count := len(this.A[idx])
if count == 0 {
return []string{}
}
keys := make([]string, 0, count)
for key := range this.A[idx] {
keys = append(keys, key)
}
return keys
}
func init() {
size := g.CACHE_TIME / g.FLUSH_DISK_STEP
if size < 0 {
log.Panicf("store.init, bad size %d\n", size)
}
GraphItems = &GraphItemMap{
A: make([]map[string]*SafeLinkedList, size),
Size: size,
}
for i := 0; i < size; i++ {
GraphItems.A[i] = make(map[string]*SafeLinkedList)
}
}