-
Notifications
You must be signed in to change notification settings - Fork 67
/
stream.go
172 lines (153 loc) · 4.04 KB
/
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
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
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package bolt
import (
"container/list"
"errors"
"time"
"github.com/neo4j/neo4j-go-driver/v4/neo4j/db"
)
type stream struct {
keys []string
fifo list.List
sum *db.Summary
err error
qid int64
fetchSize int
key int64
}
// Acts on buffered data, first return value indicates if buffering
// is active or not.
func (s *stream) bufferedNext() (bool, *db.Record, *db.Summary, error) {
e := s.fifo.Front()
if e != nil {
s.fifo.Remove(e)
return true, e.Value.(*db.Record), nil, nil
}
if s.err != nil {
return true, nil, nil, s.err
}
if s.sum != nil {
return true, nil, s.sum, nil
}
return false, nil, nil, nil
}
// Delayed error until fifo emptied
func (s *stream) Err() error {
if s.fifo.Len() > 0 {
return nil
}
return s.err
}
func (s *stream) push(rec *db.Record) {
s.fifo.PushBack(rec)
}
type openstreams struct {
curr *stream
num int
key int64
onAssertFail func(e error)
onClose func(s *stream) // Stream succesfully closed
onEmpty func() // All streams closed succesfully or with an error
}
var (
invalidStream = errors.New("Invalid stream handle")
assertDetachUncomplete = errors.New("Assert fail: Detaching incomplete stream")
assertShoudBeNoCurrent = errors.New("Assert fail: Should be no curr stream")
assertShoudBeCurrent = errors.New("Assert fail: Should be curr stream")
)
// Adds a new open stream and sets it as current.
// There should NOT be a current stream .
func (o *openstreams) attach(s *stream) {
if o.curr != nil {
o.onAssertFail(assertShoudBeNoCurrent)
return
}
// Track number of open streams and set the stream as current
o.num++
o.curr = s
s.key = o.key
}
// Detaches the current stream from being current and
// removes it from set of open streams it is no longer open.
// The stream should be either in failed state or completed.
func (o *openstreams) detach(sum *db.Summary, err error) {
if o.curr == nil {
o.onAssertFail(assertShoudBeCurrent)
return
}
if sum != nil {
o.curr.sum = sum
o.onClose(o.curr)
} else if err != nil {
o.curr.err = err
} else {
o.onAssertFail(assertDetachUncomplete)
return
}
o.remove(o.curr)
}
// Streams can be paused when they have received a "has_more" response from server
// Pauses the current stream
func (o *openstreams) pause() {
o.curr = nil
}
// When resuming a stream a new PULL message needs to be sent.
func (o *openstreams) resume(s *stream) {
if o.curr != nil {
o.onAssertFail(assertShoudBeNoCurrent)
return
}
o.curr = s
}
// Removes the stream by disabling its key and removing it from the count of streams.
// If the stream is current the current is set to nil.
func (o *openstreams) remove(s *stream) {
o.num--
s.key = 0
if o.curr == s {
o.curr = nil
}
if o.num == 0 {
o.onEmpty()
}
}
func (o *openstreams) reset() {
o.num = 0
// Might cause onEmpty to be called more than once but makes it more robust
// for any errors on stream counting.
o.onEmpty()
o.curr = nil
o.key = time.Now().UnixNano()
}
// Checks that the handle represents a stream but not necessarily a stream belonging
// to this set of open streams.
func (o *openstreams) getUnsafe(h db.StreamHandle) (*stream, error) {
stream, ok := h.(*stream)
if !ok || stream == nil {
return nil, invalidStream
}
return stream, nil
}
func (o *openstreams) isSafe(s *stream) error {
if s.key == o.key {
return nil
}
return invalidStream
}