forked from rethinkdb/rethinkdb-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor.go
467 lines (399 loc) · 9.88 KB
/
cursor.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package gorethink
import (
"encoding/json"
"errors"
"reflect"
"sync/atomic"
"github.com/dancannon/gorethink/encoding"
p "github.com/dancannon/gorethink/ql2"
)
var (
errCursorClosed = errors.New("connection closed, cannot read cursor")
)
func newCursor(conn *Connection, cursorType string, token int64, term *Term, opts map[string]interface{}) *Cursor {
if cursorType == "" {
cursorType = "Cursor"
}
cursor := &Cursor{
conn: conn,
token: token,
cursorType: cursorType,
term: term,
opts: opts,
}
return cursor
}
// Cursor is the result of a query. Its cursor starts before the first row
// of the result set. A Cursor is not thread safe and should only be accessed
// by a single goroutine at any given time. Use Next to advance through the
// rows:
//
// cursor, err := query.Run(session)
// ...
// defer cursor.Close()
//
// var response interface{}
// for cursor.Next(&response) {
// ...
// }
// err = cursor.Err() // get any error encountered during iteration
// ...
type Cursor struct {
releaseConn func(error)
conn *Connection
token int64
cursorType string
term *Term
opts map[string]interface{}
lastErr error
fetching bool
closed int32
finished bool
isAtom bool
buffer queue
responses queue
profile interface{}
}
// Profile returns the information returned from the query profiler.
func (c *Cursor) Profile() interface{} {
return c.profile
}
// Type returns the cursor type (by default "Cursor")
func (c *Cursor) Type() string {
return c.cursorType
}
// Err returns nil if no errors happened during iteration, or the actual
// error otherwise.
func (c *Cursor) Err() error {
return c.lastErr
}
// Close closes the cursor, preventing further enumeration. If the end is
// encountered, the cursor is closed automatically. Close is idempotent.
func (c *Cursor) Close() error {
var err error
if c.closed != 0 || !atomic.CompareAndSwapInt32(&c.closed, 0, 1) {
return nil
}
conn := c.conn
if conn == nil {
return nil
}
if conn.conn == nil {
return nil
}
// Stop any unfinished queries
if c.closed == 0 && !c.finished {
q := Query{
Type: p.Query_STOP,
Token: c.token,
}
_, _, err = conn.Query(q)
}
if c.releaseConn != nil {
c.releaseConn(err)
}
c.conn = nil
c.buffer.elems = nil
c.responses.elems = nil
return err
}
// Next retrieves the next document from the result set, blocking if necessary.
// This method will also automatically retrieve another batch of documents from
// the server when the current one is exhausted, or before that in background
// if possible.
//
// Next returns true if a document was successfully unmarshalled onto result,
// and false at the end of the result set or if an error happened.
// When Next returns false, the Err method should be called to verify if
// there was an error during iteration.
//
// Also note that you are able to reuse the same variable multiple times as
// `Next` zeroes the value before scanning in the result.
func (c *Cursor) Next(dest interface{}) bool {
if c.closed != 0 {
return false
}
hasMore, err := c.loadNext(dest)
if c.handleError(err) != nil {
c.Close()
return false
}
if !hasMore {
c.Close()
}
return hasMore
}
func (c *Cursor) loadNext(dest interface{}) (bool, error) {
for c.lastErr == nil {
// Check if response is closed/finished
if c.buffer.Len() == 0 && c.responses.Len() == 0 && c.closed != 0 {
return false, errCursorClosed
}
if c.buffer.Len() == 0 && c.responses.Len() == 0 && !c.finished {
err := c.fetchMore()
if err != nil {
return false, err
}
}
if c.buffer.Len() == 0 && c.responses.Len() == 0 && c.finished {
return false, nil
}
if c.buffer.Len() == 0 && c.responses.Len() > 0 {
if response, ok := c.responses.Pop().(json.RawMessage); ok {
var value interface{}
err := json.Unmarshal(response, &value)
if err != nil {
return false, err
}
value, err = recursivelyConvertPseudotype(value, c.opts)
if err != nil {
return false, err
}
// If response is an ATOM then try and convert to an array
if data, ok := value.([]interface{}); ok && c.isAtom {
for _, v := range data {
c.buffer.Push(v)
}
} else if value == nil {
c.buffer.Push(nil)
} else {
c.buffer.Push(value)
}
}
}
if c.buffer.Len() > 0 {
data := c.buffer.Pop()
err := encoding.Decode(dest, data)
if err != nil {
return false, err
}
return true, nil
}
}
return false, c.lastErr
}
// All retrieves all documents from the result set into the provided slice
// and closes the cursor.
//
// The result argument must necessarily be the address for a slice. The slice
// may be nil or previously allocated.
//
// Also note that you are able to reuse the same variable multiple times as
// `All` zeroes the value before scanning in the result. It also attempts
// to reuse the existing slice without allocating any more space by either
// resizing or returning a selection of the slice if necessary.
func (c *Cursor) All(result interface{}) error {
resultv := reflect.ValueOf(result)
if resultv.Kind() != reflect.Ptr || resultv.Elem().Kind() != reflect.Slice {
panic("result argument must be a slice address")
}
slicev := resultv.Elem()
slicev = slicev.Slice(0, slicev.Cap())
elemt := slicev.Type().Elem()
i := 0
for {
if slicev.Len() == i {
elemp := reflect.New(elemt)
if !c.Next(elemp.Interface()) {
break
}
slicev = reflect.Append(slicev, elemp.Elem())
slicev = slicev.Slice(0, slicev.Cap())
} else {
if !c.Next(slicev.Index(i).Addr().Interface()) {
break
}
}
i++
}
resultv.Elem().Set(slicev.Slice(0, i))
if err := c.Err(); err != nil {
c.Close()
return err
}
if err := c.Close(); err != nil {
return err
}
return nil
}
// One retrieves a single document from the result set into the provided
// slice and closes the cursor.
//
// Also note that you are able to reuse the same variable multiple times as
// `One` zeroes the value before scanning in the result.
func (c *Cursor) One(result interface{}) error {
if c.IsNil() {
c.Close()
return ErrEmptyResult
}
hasResult := c.Next(result)
if err := c.Err(); err != nil {
c.Close()
return err
}
if err := c.Close(); err != nil {
return err
}
if !hasResult {
return ErrEmptyResult
}
return nil
}
// Listen listens for rows from the database and sends the result onto the given
// channel. The type that the row is scanned into is determined by the element
// type of the channel.
//
// Also note that this function returns immediately.
//
// cursor, err := r.Expr([]int{1,2,3}).Run(session)
// if err != nil {
// panic(err)
// }
//
// ch := make(chan int)
// cursor.Listen(ch)
// <- ch // 1
// <- ch // 2
// <- ch // 3
func (c *Cursor) Listen(channel interface{}) {
go func() {
channelv := reflect.ValueOf(channel)
if channelv.Kind() != reflect.Chan {
panic("input argument must be a channel")
}
elemt := channelv.Type().Elem()
for {
elemp := reflect.New(elemt)
if !c.Next(elemp.Interface()) {
break
}
channelv.Send(elemp.Elem())
}
c.Close()
channelv.Close()
}()
}
// IsNil tests if the current row is nil.
func (c *Cursor) IsNil() bool {
if c.buffer.Len() > 0 {
bufferedItem := c.buffer.Peek()
if bufferedItem == nil {
return true
}
return false
}
if c.responses.Len() > 0 {
response := c.responses.Peek()
if response == nil {
return true
}
if response, ok := response.(json.RawMessage); ok {
if string(response) == "null" {
return true
}
}
return false
}
return true
}
// fetchMore fetches more rows from the database.
//
// If wait is true then it will wait for the database to reply otherwise it
// will return after sending the continue query.
func (c *Cursor) fetchMore() error {
var err error
if !c.fetching {
c.fetching = true
if c.closed != 0 {
return errCursorClosed
}
q := Query{
Type: p.Query_CONTINUE,
Token: c.token,
}
_, _, err = c.conn.Query(q)
c.handleError(err)
}
return err
}
// handleError sets the value of lastErr to err if lastErr is not yet set.
func (c *Cursor) handleError(err error) error {
return c.handleErrorLocked(err)
}
// handleError sets the value of lastErr to err if lastErr is not yet set.
func (c *Cursor) handleErrorLocked(err error) error {
if c.lastErr == nil {
c.lastErr = err
}
return c.lastErr
}
// extend adds the result of a continue query to the cursor.
func (c *Cursor) extend(response *Response) {
for _, response := range response.Responses {
c.responses.Push(response)
}
c.finished = response.Type != p.Response_SUCCESS_PARTIAL
c.fetching = false
c.isAtom = response.Type == p.Response_SUCCESS_ATOM
putResponse(response)
}
// Queue structure used for storing responses
type queue struct {
elems []interface{}
nelems, popi, pushi int
}
func (q *queue) Len() int {
if len(q.elems) == 0 {
return 0
}
return q.nelems
}
func (q *queue) Push(elem interface{}) {
if q.nelems == len(q.elems) {
q.expand()
}
q.elems[q.pushi] = elem
q.nelems++
q.pushi = (q.pushi + 1) % len(q.elems)
}
func (q *queue) Pop() (elem interface{}) {
if q.nelems == 0 {
return nil
}
elem = q.elems[q.popi]
q.elems[q.popi] = nil // Help GC.
q.nelems--
q.popi = (q.popi + 1) % len(q.elems)
return elem
}
func (q *queue) Peek() (elem interface{}) {
if q.nelems == 0 {
return nil
}
return q.elems[q.popi]
}
func (q *queue) expand() {
curcap := len(q.elems)
var newcap int
if curcap == 0 {
newcap = 8
} else if curcap < 1024 {
newcap = curcap * 2
} else {
newcap = curcap + (curcap / 4)
}
elems := make([]interface{}, newcap)
if q.popi == 0 {
copy(elems, q.elems)
q.pushi = curcap
} else {
newpopi := newcap - (curcap - q.popi)
copy(elems, q.elems[:q.popi])
copy(elems[newpopi:], q.elems[q.popi:])
q.popi = newpopi
}
for i := range q.elems {
q.elems[i] = nil // Help GC.
}
q.elems = elems
}