-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference_test.go
More file actions
165 lines (149 loc) · 3.55 KB
/
reference_test.go
File metadata and controls
165 lines (149 loc) · 3.55 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
package kv_test
import (
"bytes"
"fmt"
"iter"
"slices"
"strings"
"github.com/phiryll/kv"
)
// reference serves as an expected value to compare against while testing,
// and a source of entries from which to create a new Store.
// This implementation is meant to be as trivially correct as possible.
type reference struct {
entries map[string]byte
ascKeys [][]byte
dirty bool
}
func newReference() *reference {
return &reference{
entries: map[string]byte{},
}
}
func (r *reference) makeDirty() {
r.ascKeys = nil
r.dirty = true
}
func (r *reference) refresh() {
if !r.dirty {
return
}
var keys [][]byte
for key := range r.entries {
keys = append(keys, []byte(key))
}
slices.SortFunc(keys, bytes.Compare)
r.ascKeys = keys
r.dirty = false
}
func (r *reference) Get(key []byte) (byte, bool) {
if key == nil {
panic("key must be non-nil")
}
value, ok := r.entries[string(key)]
return value, ok
}
func (r *reference) Set(key []byte, value byte) (byte, bool) {
if key == nil {
panic("key must be non-nil")
}
prev, ok := r.entries[string(key)]
r.entries[string(key)] = value
r.makeDirty()
return prev, ok
}
func (r *reference) Delete(key []byte) (byte, bool) {
if key == nil {
panic("key must be non-nil")
}
value, ok := r.entries[string(key)]
if ok {
delete(r.entries, string(key))
r.makeDirty()
}
return value, ok
}
func (r *reference) Range(bounds *Bounds) iter.Seq2[[]byte, byte] {
bounds = bounds.Clone()
if bounds.IsReverse {
// Desc behaves differently than Range, with low inclusive and high exclusive.
low, high := bounds.End, bounds.Begin
if low != nil {
low = nextKey(low)
}
if high != nil {
high = nextKey(high)
}
return r.Desc(low, high)
}
return r.Asc(bounds.Begin, bounds.End)
}
// Future methods on Store, replacing Range.
func (r *reference) All() iter.Seq2[[]byte, byte] {
return func(yield func([]byte, byte) bool) {
for k, v := range r.entries {
if !yield([]byte(k), v) {
return
}
}
}
}
//nolint:revive
func (r *reference) between(low, high []byte) (int, int) {
lowIndex, highIndex := 0, len(r.ascKeys)
if low != nil {
lowIndex, _ = slices.BinarySearchFunc(r.ascKeys, low, bytes.Compare)
}
if high != nil {
highIndex, _ = slices.BinarySearchFunc(r.ascKeys[lowIndex:], high, bytes.Compare)
highIndex += lowIndex
}
return lowIndex, highIndex
}
func (r *reference) Asc(low, high []byte) iter.Seq2[[]byte, byte] {
if low != nil && high != nil && bytes.Compare(low, high) >= 0 {
panic("low >= high")
}
return func(yield func([]byte, byte) bool) {
r.refresh()
lowIndex, highIndex := r.between(low, high)
for i := lowIndex; i < highIndex; i++ {
key := r.ascKeys[i]
if !yield(key, r.entries[string(key)]) {
return
}
}
}
}
func (r *reference) Desc(low, high []byte) iter.Seq2[[]byte, byte] {
if low != nil && high != nil && bytes.Compare(low, high) >= 0 {
panic("low >= high")
}
return func(yield func([]byte, byte) bool) {
r.refresh()
lowIndex, highIndex := r.between(low, high)
for i := highIndex - 1; i >= lowIndex; i-- {
key := r.ascKeys[i]
if !yield(key, r.entries[string(key)]) {
return
}
}
}
}
func (r *reference) String() string {
var s strings.Builder
s.WriteString("{\n")
fmt.Fprintf(&s, " dirty: %v\n", r.dirty)
s.WriteString(" entries: {")
for key, value := range r.entries {
fmt.Fprintf(&s, "%s:%02X, ", kv.KeyName([]byte(key)), value)
}
s.WriteString(" }\n")
s.WriteString(" ascKeys: {")
for _, key := range r.ascKeys {
fmt.Fprintf(&s, "%s, ", kv.KeyName(key))
}
s.WriteString(" }\n")
s.WriteString("}")
return s.String()
}