forked from vanadium-archive/go.v23
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result_stream.go
95 lines (83 loc) · 2.19 KB
/
result_stream.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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package syncbase
import (
"sync"
"v.io/v23/context"
wire "v.io/v23/services/syncbase"
"v.io/v23/vom"
)
type resultStream struct {
// cancel cancels the RPC resultStream.
cancel context.CancelFunc
mu sync.Mutex
// call is the RPC resultStream object.
call wire.DatabaseExecClientCall
// curr is the currently staged result, or nil if nothing is staged.
curr []*vom.RawBytes
// err is the first error encountered during streaming. It may also be
// populated by a call to Cancel.
err error
// finished records whether we have called call.Finish().
finished bool
}
var _ ResultStream = (*resultStream)(nil)
func newResultStream(cancel context.CancelFunc, call wire.DatabaseExecClientCall) *resultStream {
return &resultStream{
cancel: cancel,
call: call,
}
}
func (rs *resultStream) Advance() bool {
rs.mu.Lock()
defer rs.mu.Unlock()
if rs.finished {
return false
}
// Advance never blocks if the context has been cancelled.
if !rs.call.RecvStream().Advance() {
// TODO(ivanpi): Query tests rely on simpler error formatting in RecvStream
// error vs Finish, so keep the redundant error assignment for now.
rs.err = rs.call.RecvStream().Err()
err := rs.call.Finish()
if rs.err == nil {
rs.err = err
}
rs.cancel()
rs.finished = true
return false
}
rs.curr = rs.call.RecvStream().Value()
return true
}
func (rs *resultStream) ResultCount() int {
rs.mu.Lock()
defer rs.mu.Unlock()
return len(rs.curr)
}
func (rs *resultStream) Result(i int, value interface{}) error {
rs.mu.Lock()
defer rs.mu.Unlock()
return rs.curr[i].ToValue(value)
}
func (rs *resultStream) Err() error {
rs.mu.Lock()
defer rs.mu.Unlock()
return rs.err
}
func (rs *resultStream) Cancel() {
rs.cancel()
rs.mu.Lock()
defer rs.mu.Unlock()
if !rs.finished {
// TODO(ivanpi): Query tests rely on simpler error formatting in RecvStream
// error vs Finish, so keep the redundant error assignment for now.
rs.err = rs.call.RecvStream().Err()
err := rs.call.Finish()
if rs.err == nil {
rs.err = err
}
rs.finished = true
}
}