generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
iterator.go
321 lines (266 loc) · 8.29 KB
/
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
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
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package coherence
import (
"container/list"
"context"
"errors"
pb "github.com/oracle/coherence-go-client/proto"
"google.golang.org/protobuf/types/known/wrapperspb"
"io"
"sync"
)
var (
_ keyPageIterator[int, string] = &streamedKeyIterator[int, string]{}
_ entryPageIterator[int, string] = &streamedEntryIterator[int, string]{}
_ valuePageIterator[int, string] = &streamedValueIterator[int, string]{}
// ErrDone indicates that there are no more entries to return.
ErrDone = errors.New("iterator done")
)
// keyPageIterator defines an iterator of type K which is used to return
// values from KeySet() function. This iterator pages data internally so that
// if this iterator is called on a large cache it does not end up returning
// every key at once and potentially causing memory pressure.
//
// You can keep calling .Next() until err returns coherence.ErrDone, which indicates no more keys to iterate.
// This is an internal type only.
type keyPageIterator[K comparable, V any] interface {
// Next returns the next key and error, if error is ErrDone then this means
// there are no more keys, otherwise it means an actual error.
Next() (*K, error)
}
type streamedKeyIterator[K comparable, V any] struct {
exhausted bool
dataList *list.List
ctx context.Context
bc *baseClient[K, V]
cookie []byte
sync.RWMutex
}
func newKeyPageIterator[K comparable, V any](ctx context.Context, bc *baseClient[K, V]) keyPageIterator[K, V] {
iter := &streamedKeyIterator[K, V]{
exhausted: false,
dataList: list.New(),
ctx: ctx,
bc: bc,
cookie: make([]byte, 0),
}
return iter
}
func (it *streamedKeyIterator[K, V]) Next() (*K, error) {
it.Lock()
defer it.Unlock()
// attempt to get next page of data if we have none
if it.dataList.Len() == 0 && !it.exhausted {
if err := it.getNextPage(); err != nil {
// could not get next page of data
return nil, err
}
}
// if we are exhausted and no data, indicate this in error
if it.exhausted && it.dataList.Len() == 0 {
return nil, ErrDone
}
// we have at least 1 entry in dataList
first := it.dataList.Front()
key := first.Value.(K)
// remove the first entry
it.dataList.Remove(first)
return &key, nil
}
// getNextPage retrieves the next page of data from the gRPC connection and sets the
// current state to reflect the result of the operation.
func (it *streamedKeyIterator[K, V]) getNextPage() error {
var (
err = it.bc.ensureClientConnection()
client pb.NamedCacheService_NextKeySetPageClient
m = new(wrapperspb.BytesValue)
response *K
first = true
)
if err != nil {
return err
}
newCtx, cancel := it.bc.session.ensureContext(it.ctx)
if cancel != nil {
defer cancel()
}
request := &pb.PageRequest{Scope: it.bc.sessionOpts.Scope, Cache: it.bc.name, Format: it.bc.format, Cookie: it.cookie}
if client, err = it.bc.client.NextKeySetPage(newCtx, request); err != nil {
return err
}
// reset the data
it.dataList = list.New()
for {
m, err = client.Recv()
if err == io.EOF {
// end of stream
break
} else if err != nil {
return err
}
if first {
// first entry will always be the cookie so store back in 'it.cookie' to be sent with next request
it.cookie = m.Value
if it.cookie == nil {
it.exhausted = true
}
first = false
} else {
// no error so deserialize the key
if response, err = it.bc.keySerializer.Deserialize(m.Value); err != nil {
return err
}
it.dataList.PushBack(*response)
}
}
if it.dataList.Len() == 0 {
it.exhausted = true
}
return nil
}
// entryPageIterator defines an iterator of type Entry which is used to return
// entries from EntrySet() function. This iterator pages data internally so that
// if this iterator is called on a large cache it does not end up returning
// every entry at once and potentially causing memory pressure.
//
// You can keep calling .Next() until err returns coherence.ErrDone, which indicates no more entries to iterate.
// This is an internal type only.
type entryPageIterator[K comparable, V any] interface {
// Next returns the next entry and error, if error is ErrDone then this means
// there are no more entries, otherwise it means an actual error.
Next() (*Entry[K, V], error)
}
func newEntryPageIterator[K comparable, V any](ctx context.Context, bc *baseClient[K, V]) entryPageIterator[K, V] {
iter := &streamedEntryIterator[K, V]{
exhausted: false,
dataList: list.New(),
ctx: ctx,
bc: bc,
cookie: make([]byte, 0),
}
return iter
}
type streamedEntryIterator[K comparable, V any] struct {
exhausted bool
dataList *list.List
ctx context.Context
bc *baseClient[K, V]
cookie []byte
sync.RWMutex
}
func (it *streamedEntryIterator[K, V]) Next() (*Entry[K, V], error) {
it.Lock()
defer it.Unlock()
// attempt to get next page of data if we have none
if it.dataList.Len() == 0 && !it.exhausted {
if err := it.getNextPage(); err != nil {
// could not get next page of data
return nil, err
}
}
// if we are exhausted and no data, indicate this in error
if it.exhausted && it.dataList.Len() == 0 {
return nil, ErrDone
}
// we have at least 1 entry in dataList
first := it.dataList.Front()
entry := first.Value
entryValue := entry.(Entry[K, V])
// remove the first entry
it.dataList.Remove(first)
return &entryValue, nil
}
// getNextPage retrieves the next page of data from the gRPC connection and sets the
// current state to reflect the result of the operation.
func (it *streamedEntryIterator[K, V]) getNextPage() error {
var (
err = it.bc.ensureClientConnection()
client pb.NamedCacheService_NextEntrySetPageClient
m = new(pb.EntryResult)
key *K
value *V
first = true
)
if err != nil {
return err
}
newCtx, cancel := it.bc.session.ensureContext(it.ctx)
if cancel != nil {
defer cancel()
}
request := &pb.PageRequest{Scope: it.bc.sessionOpts.Scope, Cache: it.bc.name, Format: it.bc.format, Cookie: it.cookie}
if client, err = it.bc.client.NextEntrySetPage(newCtx, request); err != nil {
return err
}
// reset the data
it.dataList = list.New()
for {
m, err = client.Recv()
if err == io.EOF {
// end of stream
break
} else if err != nil {
return err
}
if first {
// first entry will always be the cookie so store back in 'it.cookie' to be sent with next request
it.cookie = m.Cookie
if it.cookie == nil {
it.exhausted = true
}
first = false
} else {
// no error so deserialize the key
if key, err = it.bc.keySerializer.Deserialize(m.Key); err != nil {
return err
}
if value, err = it.bc.valueSerializer.Deserialize(m.Value); err != nil {
return err
}
it.dataList.PushBack(Entry[K, V]{Key: *key, Value: *value})
}
}
if it.dataList.Len() == 0 {
it.exhausted = true
}
return nil
}
// valuePageIterator defines an iterator of type V which is used to return
// values from Values() function. This iterator pages data internally so that
// if this iterator is called on a large cache it does not end up returning
// every value at once and potentially causing memory pressure.
//
// You can keep calling .Next() until err returns coherence.ErrDone, which indicates no more entries to iterate.
// This is an internal type only.
type valuePageIterator[K comparable, V any] interface {
// Next returns the next value and error, if error is ErrDone then this means
// there are no more keys, otherwise it means an actual error.
Next() (*V, error)
}
type streamedValueIterator[K comparable, V any] struct {
entryIterator *streamedEntryIterator[K, V]
}
func newValuePageIterator[K comparable, V any](ctx context.Context, bc *baseClient[K, V]) valuePageIterator[K, V] {
internalIter := &streamedEntryIterator[K, V]{
exhausted: false,
dataList: list.New(),
ctx: ctx,
bc: bc,
cookie: make([]byte, 0),
}
// use an streamedEntryIterator but only return the value
iter := &streamedValueIterator[K, V]{entryIterator: internalIter}
return iter
}
func (it *streamedValueIterator[K, V]) Next() (*V, error) {
// call the entry iterator but only return the value
entry, err := it.entryIterator.Next()
if err == ErrDone {
return nil, ErrDone
}
return &entry.Value, nil
}