This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
merged.go
324 lines (268 loc) · 6.35 KB
/
merged.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
Copyright 2020 Google LLC
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd
*/
package reftable
import (
"fmt"
"log"
)
// pqEntry is an entry of the priority queue
type pqEntry struct {
rec record
index int
}
func (e pqEntry) String() string {
return fmt.Sprintf("%s,%d", e.rec.key(), e.index)
}
func pqLess(a, b pqEntry) bool {
if a.rec.key() == b.rec.key() {
return a.index > b.index
}
return a.rec.key() < b.rec.key()
}
// a min heap
type mergedIterPQueue struct {
heap []pqEntry
}
func (pq *mergedIterPQueue) isEmpty() bool {
return len(pq.heap) == 0
}
func (pq *mergedIterPQueue) top() pqEntry {
return pq.heap[0]
}
func (pq *mergedIterPQueue) check() {
for i := 1; i < len(pq.heap); i++ {
par := (i - 1) / 2
if !pqLess(pq.heap[par], pq.heap[i]) {
log.Panicf("%v %v %d", pq.heap[par], pq.heap[i], i)
}
}
}
func (pq *mergedIterPQueue) remove() pqEntry {
e := pq.heap[0]
pq.heap[0] = pq.heap[len(pq.heap)-1]
pq.heap = pq.heap[:len(pq.heap)-1]
i := 0
for i < len(pq.heap) {
min := i
j, k := 2*i+1, 2*i+2
if j < len(pq.heap) && pqLess(pq.heap[j], pq.heap[i]) {
min = j
}
if k < len(pq.heap) && pqLess(pq.heap[k], pq.heap[min]) {
min = k
}
if min == i {
break
}
pq.heap[i], pq.heap[min] = pq.heap[min], pq.heap[i]
i = min
}
return e
}
func (pq *mergedIterPQueue) add(e pqEntry) {
pq.heap = append(pq.heap, e)
i := len(pq.heap) - 1
for i > 0 {
j := (i - 1) / 2
if pqLess(pq.heap[j], pq.heap[i]) {
break
}
pq.heap[j], pq.heap[i] = pq.heap[i], pq.heap[j]
i = j
}
}
// Merged is a stack of reftables.
type Merged struct {
stack []Table
hashID HashID
suppressDeletions bool
}
func (m *Merged) HashID() HashID {
return m.hashID
}
// NewMerged creates a reader for a merged reftable.
func NewMerged(tabs []Table, hashID [4]byte) (*Merged, error) {
var last Table
for i, t := range tabs {
if last != nil && last.MaxUpdateIndex() >= t.MinUpdateIndex() {
return nil, fmt.Errorf("reftable: table %d has min %d, table %d has max %d; indices must be increasing.", i, t.MinUpdateIndex(), i-1, last.MaxUpdateIndex())
}
if t.HashID() != hashID {
return nil, fmt.Errorf("reftable: table %d has hash ID %q want hash ID %q", i,
t.HashID(), hashID)
}
last = t
}
return &Merged{
stack: tabs,
hashID: hashID,
}, nil
}
// MaxUpdateIndex implements the Table interface.
func (m *Merged) MaxUpdateIndex() uint64 {
return m.stack[len(m.stack)].MaxUpdateIndex()
}
// MinUpdateIndex implements the Table interface.
func (m *Merged) MinUpdateIndex() uint64 {
return m.stack[0].MinUpdateIndex()
}
// RefsFor returns the refs that point to the given oid
func (m *Merged) RefsFor(oid []byte) (*Iterator, error) {
mit := &mergedIter{
typ: blockTypeRef,
}
for _, t := range m.stack {
it, err := t.RefsFor(oid)
if err != nil {
return nil, err
}
mit.stack = append(mit.stack, it.impl)
}
if err := mit.init(); err != nil {
return nil, err
}
return &Iterator{&filteringRefIterator{
tab: m,
oid: oid,
it: mit,
doubleCheck: true,
}}, nil
}
func (m *Merged) Name() string {
var ss []string
for _, t := range m.stack {
ss = append(ss, t.Name())
}
return fmt.Sprintf("%s", ss)
}
// Seek returns an iterator positioned before the wanted record.
func (m *Merged) SeekLog(refname string, updateIndex uint64) (*Iterator, error) {
log := LogRecord{
RefName: refname,
UpdateIndex: updateIndex,
}
impl, err := m.seekRecord(&log)
if err != nil {
return nil, err
}
return &Iterator{impl}, nil
}
func (m *Merged) SeekRef(name string) (*Iterator, error) {
ref := RefRecord{
RefName: name,
}
impl, err := m.seekRecord(&ref)
if err != nil {
return nil, err
}
return &Iterator{impl}, nil
}
func (m *Merged) seekRecord(rec record) (iterator, error) {
var its []iterator
var names []string
for _, t := range m.stack {
iter, err := t.seekRecord(rec)
if err != nil {
return nil, fmt.Errorf("reftable: seek %s: %v", t.Name(), err)
}
its = append(its, iter)
names = append(names, t.Name())
}
merged := &mergedIter{
typ: rec.typ(),
suppressDeletions: m.suppressDeletions,
stack: its,
names: names,
}
if err := merged.init(); err != nil {
return nil, err
}
return merged, nil
}
// in the stack obscure lower entries.
type mergedIter struct {
// TODO: We could short-circuit the entries coming from the
// base stack
typ byte
pq mergedIterPQueue
suppressDeletions bool
// Fixed side arrays, matched with merged.stack
stack []iterator
names []string
}
func (it *mergedIter) init() error {
for i, sub := range it.stack {
rec := newRecord(it.typ, "")
ok, err := sub.Next(rec)
if err != nil {
return fmt.Errorf("init %s: %v", it.names[i], err)
}
if ok {
it.pq.add(pqEntry{
rec: rec,
index: i,
})
} else {
it.stack[i] = nil
}
}
return nil
}
// advance one iterator, putting its result into the priority queue.
func (m *mergedIter) advanceSubIter(index int) error {
if m.stack[index] == nil {
return nil
}
r := newRecord(m.typ, "")
ok, err := m.stack[index].Next(r)
if err != nil {
return fmt.Errorf("next %s: %v", m.names[index], err)
}
if !ok {
m.stack[index] = nil
return nil
}
m.pq.add(pqEntry{r, index})
return nil
}
func (m *mergedIter) Next(rec record) (bool, error) {
for {
ok, err := m.nextEntry(rec)
if ok && rec.IsDeletion() && m.suppressDeletions {
continue
}
return ok, err
}
}
func (m *mergedIter) nextEntry(rec record) (bool, error) {
if m.pq.isEmpty() {
return false, nil
}
entry := m.pq.remove()
if err := m.advanceSubIter(entry.index); err != nil {
return false, err
}
// One can also use reftable as datacenter-local storage, where the
// ref database is maintained in globally consistent database
// (eg. CockroachDB or Spanner). In this scenario, replication
// delays together with compaction may cause newer tables to
// contain older entries. In such a deployment, the loop below
// must be changed to collect all entries for the same key,
// and return new the newest one.
for !m.pq.isEmpty() {
top := m.pq.top()
if top.rec.key() > entry.rec.key() {
break
}
m.pq.remove()
if err := m.advanceSubIter(top.index); err != nil {
return false, err
}
}
rec.copyFrom(entry.rec)
return true, nil
}