-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.go
More file actions
212 lines (176 loc) · 3.96 KB
/
index.go
File metadata and controls
212 lines (176 loc) · 3.96 KB
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
package memdb
import (
"strings"
"time"
)
// Index implements IndexSearcher and represents a list of indexes
type Index struct {
IndexSearcher
n int
id string
fields []string
store *Store
unique bool
}
// FieldKey represents the key for an item within a field
type FieldKey []string
// NewFieldKey returns a FieldKey from a field representation string [ FieldKey.String() ]
func NewFieldKey(from string) FieldKey {
return FieldKey(strings.Split(from, "\000"))
}
// Keys are the keys contained in the FieldKey
// It can be used like store.In("field").One(fieldKey.Keys()...)
func (fk FieldKey) Keys() []string {
return []string(fk)
}
// String returns a representation string for the FieldKey [ can supply to NewFieldKey() ]
func (fk FieldKey) String() string {
return strings.Join(fk.Keys(), "\000")
}
// FieldKey returns the used key value for the given item for this index
func (idx *Index) FieldKey(a interface{}) FieldKey {
components := make([]string, len(idx.fields))
for i, field := range idx.fields {
components[i] = idx.store.GetField(a, field)
}
return FieldKey(components)
}
// Each calls iterator for every matched element
// Items are not guaranteed to be in any particular order
func (idx *Index) Each(cb Iterator, keys ...string) {
if idx == nil {
return
}
idx.store.RLock()
defer idx.store.RUnlock()
values := idx.find(keys)
if values == nil {
return
}
now := time.Now()
for _, wrapped := range values {
wrapped.stats.read(now)
idx.store.happens <- &happening{
event: Access,
old: wrapped.item,
new: wrapped.item,
stats: wrapped.stats,
}
if !cb(wrapped.item) {
return
}
}
}
// One is like Lookup, except just returns the first item found
func (idx *Index) One(keys ...string) interface{} {
if idx == nil {
return nil
}
idx.store.RLock()
defer idx.store.RUnlock()
now := time.Now()
values := idx.find(keys)
if len(values) > 0 {
wrapped := values[0]
wrapped.stats.read(now)
idx.store.happens <- &happening{
event: Access,
old: wrapped.item,
new: wrapped.item,
stats: wrapped.stats,
}
return wrapped.item
}
return nil
}
// Lookup returns the list of items from the index that match given key
// Returned items are not guaranteed to be in any particular order
func (idx *Index) Lookup(keys ...string) []interface{} {
if idx == nil {
return nil
}
idx.store.RLock()
defer idx.store.RUnlock()
values := idx.find(keys)
if values == nil {
return nil
}
now := time.Now()
c := make([]interface{}, len(values))
for i, wrapped := range values {
c[i] = wrapped.item
wrapped.stats.read(now)
idx.store.happens <- &happening{
event: Access,
old: wrapped.item,
new: wrapped.item,
stats: wrapped.stats,
}
}
return c
}
// Stats returns the stats for all items in the index without modifying access time
func (idx *Index) Stats(keys ...string) []Stats {
if idx == nil {
return nil
}
idx.store.RLock()
defer idx.store.RUnlock()
values := idx.find(keys)
n := len(values)
if n > 0 {
c := make([]Stats, n)
for i, wrapped := range values {
c[i] = wrapped.stats
}
return c
}
return nil
}
// All returns the all items from the index
func (idx *Index) All() []interface{} {
if idx == nil {
return nil
}
idx.store.RLock()
defer idx.store.RUnlock()
done := map[string]bool{}
items := []interface{}{}
if index, ok := idx.store.index[idx.id]; ok {
for _, idx := range index {
for _, wrap := range idx {
uid := wrap.uid.String()
if d, ok := done[uid]; !ok || !d {
items = append(items, wrap.item)
done[uid] = true
}
}
}
}
return items
}
func (idx *Index) _id() string {
if idx == nil {
return ""
}
return idx.id
}
func (idx *Index) find(keys []string) []*wrap {
if idx == nil {
return nil
}
if len(keys) != len(idx.fields) {
return nil
}
s := idx.store
index, ok := s.index[idx.id]
if !ok {
return nil
}
key := strings.Join(keys, "\000")
values, ok := index[key]
if !ok {
return nil
}
return values
}