-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmerge_operator.go
More file actions
277 lines (235 loc) · 7.93 KB
/
Copy pathmerge_operator.go
File metadata and controls
277 lines (235 loc) · 7.93 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package rockyardkv
// merge_operator.go implements merge operator.
//
// MergeOperator allows users to define custom merge semantics for
// atomic read-modify-write operations like counters and append-only lists.
//
// Reference: RocksDB v10.7.5
// - include/rocksdb/merge_operator.h
// MergeOperator is the interface for user-defined merge operations.
//
// A MergeOperator specifies the semantics of a merge operation, which only
// the client knows. It could be numeric addition, list append, string
// concatenation, or any custom operation.
//
// RocksDB calls the merge operator during:
// - Get operations (to compute the final value)
// - Compaction (to combine merge operands)
// - Iteration (to compute values on the fly)
//
// There are two types of merge operators:
// 1. AssociativeMergeOperator - for simple operations like addition
// 2. MergeOperator - for complex operations requiring full control
type MergeOperator interface {
// Name returns a unique identifier for this merge operator.
// Used to check compatibility when opening an existing database.
Name() string
// FullMerge performs a merge operation.
//
// Parameters:
// - key: The key associated with this merge operation
// - existingValue: The existing value (nil if key doesn't exist)
// - operands: List of merge operands to apply, oldest first
//
// Returns:
// - newValue: The result of the merge
// - ok: Whether the merge succeeded
//
// If ok is false, the merge is considered failed and treated as an error.
FullMerge(key []byte, existingValue []byte, operands [][]byte) (newValue []byte, ok bool)
// PartialMerge merges two operands into a single operand.
// This is an optimization that allows combining operands before FullMerge.
//
// Parameters:
// - key: The key associated with this merge operation
// - leftOperand: The first operand
// - rightOperand: The second operand
//
// Returns:
// - newOperand: The combined operand
// - ok: Whether the partial merge succeeded
//
// If ok is false, the operands cannot be combined and both must be kept.
// PartialMerge is optional - returning (nil, false) is always valid.
PartialMerge(key []byte, leftOperand, rightOperand []byte) (newOperand []byte, ok bool)
}
// AssociativeMergeOperator is a simplified interface for associative operations.
// Use this when merging is associative: Merge(Merge(a, b), c) == Merge(a, Merge(b, c))
// Examples: numeric addition, string concatenation, set union
type AssociativeMergeOperator interface {
// Name returns a unique identifier for this merge operator.
Name() string
// Merge merges a new value with an existing value.
// If existingValue is nil, treat it as the identity element for the operation.
Merge(key []byte, existingValue, value []byte) ([]byte, bool)
}
// =============================================================================
// Built-in Merge Operators
// =============================================================================
// UInt64AddOperator is a merge operator that treats values as uint64 and adds them.
type UInt64AddOperator struct{}
// Name returns the name of this merge operator.
func (o *UInt64AddOperator) Name() string {
return "UInt64AddOperator"
}
// FullMerge adds all operands to the existing value.
func (o *UInt64AddOperator) FullMerge(key []byte, existingValue []byte, operands [][]byte) ([]byte, bool) {
var result uint64
// Parse existing value
if existingValue != nil {
if len(existingValue) != 8 {
return nil, false
}
result = decodeUint64(existingValue)
}
// Add all operands
for _, op := range operands {
if len(op) != 8 {
return nil, false
}
result += decodeUint64(op)
}
return encodeUint64(result), true
}
// PartialMerge adds two operands together.
func (o *UInt64AddOperator) PartialMerge(key []byte, left, right []byte) ([]byte, bool) {
if len(left) != 8 || len(right) != 8 {
return nil, false
}
result := decodeUint64(left) + decodeUint64(right)
return encodeUint64(result), true
}
// StringAppendOperator is a merge operator that concatenates strings with a delimiter.
type StringAppendOperator struct {
Delimiter string
}
// Name returns the name of this merge operator.
func (o *StringAppendOperator) Name() string {
return "StringAppendOperator"
}
// FullMerge concatenates all operands with the delimiter.
func (o *StringAppendOperator) FullMerge(key []byte, existingValue []byte, operands [][]byte) ([]byte, bool) {
var result []byte
if existingValue != nil {
result = make([]byte, len(existingValue))
copy(result, existingValue)
}
for _, op := range operands {
if len(result) > 0 && len(op) > 0 {
result = append(result, []byte(o.Delimiter)...)
}
result = append(result, op...)
}
return result, true
}
// PartialMerge concatenates two operands with the delimiter.
func (o *StringAppendOperator) PartialMerge(key []byte, left, right []byte) ([]byte, bool) {
if len(left) == 0 {
return right, true
}
if len(right) == 0 {
return left, true
}
result := make([]byte, 0, len(left)+len(o.Delimiter)+len(right))
result = append(result, left...)
result = append(result, []byte(o.Delimiter)...)
result = append(result, right...)
return result, true
}
// MaxOperator is a merge operator that keeps the maximum value.
type MaxOperator struct{}
// Name returns the name of this merge operator.
func (o *MaxOperator) Name() string {
return "MaxOperator"
}
// FullMerge returns the maximum of all values.
func (o *MaxOperator) FullMerge(key []byte, existingValue []byte, operands [][]byte) ([]byte, bool) {
var maxVal []byte
if existingValue != nil {
maxVal = make([]byte, len(existingValue))
copy(maxVal, existingValue)
}
for _, op := range operands {
if maxVal == nil || compareBytes(op, maxVal) > 0 {
maxVal = make([]byte, len(op))
copy(maxVal, op)
}
}
return maxVal, true
}
// PartialMerge returns the maximum of two operands.
func (o *MaxOperator) PartialMerge(key []byte, left, right []byte) ([]byte, bool) {
if compareBytes(left, right) >= 0 {
result := make([]byte, len(left))
copy(result, left)
return result, true
}
result := make([]byte, len(right))
copy(result, right)
return result, true
}
// =============================================================================
// Helper Adapter
// =============================================================================
// WrapAssociativeMergeOperator adapts an AssociativeMergeOperator into a MergeOperator.
func WrapAssociativeMergeOperator(op AssociativeMergeOperator) MergeOperator {
return &associativeMergeOperatorAdapter{op: op}
}
type associativeMergeOperatorAdapter struct {
op AssociativeMergeOperator
}
func (a *associativeMergeOperatorAdapter) Name() string {
return a.op.Name()
}
func (a *associativeMergeOperatorAdapter) FullMerge(key []byte, existingValue []byte, operands [][]byte) ([]byte, bool) {
result := existingValue
for _, operand := range operands {
var ok bool
result, ok = a.op.Merge(key, result, operand)
if !ok {
return nil, false
}
}
return result, true
}
func (a *associativeMergeOperatorAdapter) PartialMerge(key []byte, left, right []byte) ([]byte, bool) {
return a.op.Merge(key, left, right)
}
// =============================================================================
// Helper functions
// =============================================================================
func decodeUint64(b []byte) uint64 {
_ = b[7] // bounds check
return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
}
func encodeUint64(v uint64) []byte {
b := make([]byte, 8)
b[0] = byte(v)
b[1] = byte(v >> 8)
b[2] = byte(v >> 16)
b[3] = byte(v >> 24)
b[4] = byte(v >> 32)
b[5] = byte(v >> 40)
b[6] = byte(v >> 48)
b[7] = byte(v >> 56)
return b
}
func compareBytes(a, b []byte) int {
minLen := min(len(b), len(a))
for i := range minLen {
if a[i] < b[i] {
return -1
}
if a[i] > b[i] {
return 1
}
}
if len(a) < len(b) {
return -1
}
if len(a) > len(b) {
return 1
}
return 0
}