-
Notifications
You must be signed in to change notification settings - Fork 1
/
index_filters.go
53 lines (42 loc) 路 1.26 KB
/
index_filters.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
package kvas
import "sync"
// filter returns a set of keys matching a provided filter func
func (idx index) filter(f func(record) bool, mtx *sync.Mutex) []string {
keys := make([]string, 0, len(idx))
mtx.Lock()
defer mtx.Unlock()
for k, ir := range idx {
if (f != nil && f(ir)) || f == nil {
keys = append(keys, k)
}
}
return keys
}
// Keys returns all keys of a localKeyValues
func (idx index) Keys(mtx *sync.Mutex) []string {
return idx.filter(nil, mtx)
}
// CreatedAfter returns keys of values created on or after provided timestamp
func (idx index) CreatedAfter(timestamp int64, mtx *sync.Mutex) []string {
return idx.filter(func(ir record) bool {
return ir.Created >= timestamp
}, mtx)
}
// ModifiedAfter returns keys of values modified on or after provided timestamp
// that were created earlier
func (idx index) ModifiedAfter(timestamp int64, strictlyModified bool, mtx *sync.Mutex) []string {
return idx.filter(func(ir record) bool {
if strictlyModified && ir.Modified == ir.Created {
return false
}
return ir.Modified >= timestamp
}, mtx)
}
func (idx index) IsModifiedAfter(key string, timestamp int64, mtx *sync.Mutex) bool {
mtx.Lock()
defer mtx.Unlock()
if ir, ok := idx[key]; ok {
return ir.Modified > timestamp
}
return false
}