-
Notifications
You must be signed in to change notification settings - Fork 4
/
common_api.go
192 lines (161 loc) · 4.44 KB
/
common_api.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
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
// Copyright (c) 2015-2021, NVIDIA CORPORATION.
// SPDX-License-Identifier: Apache-2.0
// Package sortedmap provides sorted maps implemented as either a Left-Leaning Red-Black Tree (in memory) or a B+Tree (pageable)
package sortedmap
import (
"bytes"
"fmt"
"strings"
"time"
)
type Key interface{}
type Value interface{}
type Compare func(key1 Key, key2 Key) (result int, err error) // returns <0 if key1 < key2, 0 if key1 == key2, >0 if key1 > key2
func CompareInt(key1 Key, key2 Key) (result int, err error) {
key1Int, ok := key1.(int)
if !ok {
err = fmt.Errorf("CompareInt(non-int,) not supported")
return
}
key2Int, ok := key2.(int)
if !ok {
err = fmt.Errorf("CompareInt(int, non-int) not supported")
return
}
result = key1Int - key2Int
err = nil
return
}
func CompareUint16(key1 Key, key2 Key) (result int, err error) {
key1Uint16, ok := key1.(uint16)
if !ok {
err = fmt.Errorf("CompareUint16(non-uint16,) not supported")
return
}
key2Uint16, ok := key2.(uint16)
if !ok {
err = fmt.Errorf("CompareUint16(uint16, non-uint16) not supported")
return
}
if key1Uint16 < key2Uint16 {
result = -1
} else if key1Uint16 == key2Uint16 {
result = 0
} else { // key1Uint16 > key2Uint16
result = 1
}
err = nil
return
}
func CompareUint32(key1 Key, key2 Key) (result int, err error) {
key1Uint32, ok := key1.(uint32)
if !ok {
err = fmt.Errorf("CompareUint32(non-uint32,) not supported")
return
}
key2Uint32, ok := key2.(uint32)
if !ok {
err = fmt.Errorf("CompareUint32(uint32, non-uint32) not supported")
return
}
if key1Uint32 < key2Uint32 {
result = -1
} else if key1Uint32 == key2Uint32 {
result = 0
} else { // key1Uint32 > key2Uint32
result = 1
}
err = nil
return
}
func CompareUint64(key1 Key, key2 Key) (result int, err error) {
key1Uint64, ok := key1.(uint64)
if !ok {
err = fmt.Errorf("CompareUint64(non-uint64,) not supported")
return
}
key2Uint64, ok := key2.(uint64)
if !ok {
err = fmt.Errorf("CompareUint64(uint64, non-uint64) not supported")
return
}
if key1Uint64 < key2Uint64 {
result = -1
} else if key1Uint64 == key2Uint64 {
result = 0
} else { // key1Uint64 > key2Uint64
result = 1
}
err = nil
return
}
func CompareString(key1 Key, key2 Key) (result int, err error) {
key1String, ok := key1.(string)
if !ok {
err = fmt.Errorf("CompareString(non-string,) not supported")
return
}
key2String, ok := key2.(string)
if !ok {
err = fmt.Errorf("CompareString(string, non-string) not supported")
return
}
result = strings.Compare(key1String, key2String)
err = nil
return
}
func CompareByteSlice(key1 Key, key2 Key) (result int, err error) {
key1Slice, ok := key1.([]byte)
if !ok {
err = fmt.Errorf("CompareByteSlice(non-[]byte,) not supported")
return
}
key2Slice, ok := key2.([]byte)
if !ok {
err = fmt.Errorf("CompareByteSlice([]byte, non-[]byte) not supported")
return
}
result = bytes.Compare(key1Slice, key2Slice)
err = nil
return
}
func CompareTime(key1 Key, key2 Key) (result int, err error) {
key1Time, ok := key1.(time.Time)
if !ok {
err = fmt.Errorf("CompareTime(non-time.Time,) not supported")
return
}
key2Time, ok := key2.(time.Time)
if !ok {
err = fmt.Errorf("CompareTime(time.Time, non-time.Time) not supported")
return
}
if key1Time.Before(key2Time) {
result = -1
} else if key1Time.After(key2Time) {
result = 1
} else { // key1Time == key2Time
result = 0
}
err = nil
return
}
type SortedMap interface {
BisectLeft(key Key) (index int, found bool, err error) // Returns index of matching key:value pair or, if no match, index is to key:value just before where this key would go
BisectRight(key Key) (index int, found bool, err error) // Returns index of matching key:value pair or, if no match, index is to key:value just after where this key would go
DeleteByIndex(index int) (ok bool, err error)
DeleteByKey(key Key) (ok bool, err error)
Dump() (err error)
GetByIndex(index int) (key Key, value Value, ok bool, err error)
GetByKey(key Key) (value Value, ok bool, err error)
Len() (numberOfItems int, err error)
PatchByIndex(index int, value Value) (ok bool, err error)
PatchByKey(key Key, value Value) (ok bool, err error)
Put(key Key, value Value) (ok bool, err error)
Validate() (err error)
}
// DumpCallbacks specifies the interface to a set of callbacks provided by the client
type DumpCallbacks interface {
DumpKey(key Key) (keyAsString string, err error)
DumpValue(value Value) (valueAsString string, err error)
}