-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwrap.go
More file actions
85 lines (69 loc) · 1.4 KB
/
wrap.go
File metadata and controls
85 lines (69 loc) · 1.4 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
package memdb
import (
"github.com/google/btree"
"fmt"
"sync"
"time"
)
// Stats contains the access statistics for a stored item
type Stats struct {
Created time.Time
Accessed time.Time
Modified time.Time
Reads uint64
Writes uint64
Size uint64
w *wrap
}
func (s *Stats) read(t time.Time) {
s.w.Lock()
defer s.w.Unlock()
s.Accessed = t
s.Reads++
}
func (s *Stats) written(t time.Time) {
s.w.Lock()
defer s.w.Unlock()
s.Modified = t
s.Writes++
}
func (s *Stats) set(from Stats) {
s.w.Lock()
defer s.w.Unlock()
s.Created = from.Created
s.Accessed = from.Accessed
s.Modified = from.Modified
s.Reads = from.Reads
s.Writes = from.Writes
s.Size = from.Size
}
// IsZero returns whether the statistic has an item or not
func (s *Stats) IsZero() bool {
return s.w == nil
}
type wrap struct {
sync.RWMutex
storer Storer
uid UID
item interface{}
values []string
stats Stats
}
// UID generates a unique UID for a wrap instance
func (w *wrap) UID() UID {
if w.uid == "" {
w.uid = NewUID()
}
return w.uid
}
func (w *wrap) Less(than btree.Item) bool {
a := w.item
if wb, ok := than.(*wrap); ok {
return w.storer.Less(a, wb.item)
}
return false
}
// Unsure calculates if one of different typed objects is less than another in an arbitrary but consistent way.
func Unsure(a interface{}, b interface{}) bool {
return fmt.Sprintf("%#v", a) < fmt.Sprintf("%#v", b)
}