-
Notifications
You must be signed in to change notification settings - Fork 1
/
multiblock_iterator.go
166 lines (134 loc) · 3.57 KB
/
multiblock_iterator.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
/*
* Copyright (C) 2023 Intergral GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package vparquet
import (
"bytes"
"context"
"io"
"github.com/segmentio/parquet-go"
"github.com/intergral/deep/pkg/deepdb/encoding/common"
"github.com/pkg/errors"
)
type iterable interface {
parquet.Row | *Snapshot | *uint8
}
type combineFn[T iterable] func([]T) (T, error)
type MultiBlockIterator[T iterable] struct {
bookmarks []*bookmark[T]
combine combineFn[T]
}
func newMultiblockIterator[T iterable](bookmarks []*bookmark[T]) *MultiBlockIterator[T] {
return &MultiBlockIterator[T]{
bookmarks: bookmarks,
}
}
func (m *MultiBlockIterator[T]) Next(ctx context.Context) (common.ID, T, error) {
if m.done(ctx) {
return nil, nil, io.EOF
}
var (
lowestID common.ID
lowestBookmark *bookmark[T]
)
// find the lowest ID of the new object
for _, b := range m.bookmarks {
id, err := b.peekID(ctx)
if err != nil && err != io.EOF {
return nil, nil, err
}
if id == nil {
continue
}
comparison := bytes.Compare(id, lowestID)
if len(lowestID) == 0 || comparison == -1 {
lowestID = id
lowestBookmark = b
}
}
_, obj, err := lowestBookmark.current(ctx)
if err != nil && err != io.EOF {
return nil, nil, err
}
if obj == nil {
// this should never happen. id was non-nil above
return nil, nil, errors.New("unexpected nil object from lowest bookmark")
}
lowestBookmark.clear()
return lowestID, obj, nil
}
func (m *MultiBlockIterator[T]) Close() {
for _, b := range m.bookmarks {
b.close()
}
}
func (m *MultiBlockIterator[T]) done(ctx context.Context) bool {
for _, b := range m.bookmarks {
if !b.done(ctx) {
return false
}
}
return true
}
type bookmark[T iterable] struct {
iter bookmarkIterator[T]
currentID common.ID
currentObject T
currentErr error
}
type bookmarkIterator[T iterable] interface {
Next(ctx context.Context) (common.ID, T, error)
Close()
peekNextID(ctx context.Context) (common.ID, error)
}
func newBookmark[T iterable](iter bookmarkIterator[T]) *bookmark[T] {
return &bookmark[T]{
iter: iter,
}
}
func (b *bookmark[T]) peekID(ctx context.Context) (common.ID, error) {
nextID, err := b.iter.peekNextID(ctx)
if err != common.ErrUnsupported {
return nextID, err
}
id, _, err := b.current(ctx)
return id, err
}
func (b *bookmark[T]) current(ctx context.Context) (common.ID, T, error) {
if b.currentErr != nil {
return nil, nil, b.currentErr
}
if b.currentObject != nil {
return b.currentID, b.currentObject, nil
}
b.currentID, b.currentObject, b.currentErr = b.iter.Next(ctx)
return b.currentID, b.currentObject, b.currentErr
}
func (b *bookmark[T]) done(ctx context.Context) bool {
nextID, err := b.iter.peekNextID(ctx)
if err != common.ErrUnsupported {
return nextID == nil || err != nil
}
_, obj, err := b.current(ctx)
return obj == nil || err != nil
}
func (b *bookmark[T]) clear() {
b.currentID = nil
b.currentObject = nil
}
func (b *bookmark[T]) close() {
b.iter.Close()
}