forked from go-kivik/couchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rows.go
188 lines (169 loc) · 3.91 KB
/
rows.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
package couchdb
import (
"bytes"
"encoding/json"
"fmt"
"io"
"github.com/flimzy/kivik"
"github.com/flimzy/kivik/driver"
"github.com/flimzy/kivik/errors"
)
type rows struct {
offset int64
totalRows int64
updateSeq string
warning string
bookmark string
body io.ReadCloser
dec *json.Decoder
// closed is true after all rows have been processed
closed bool
// isFindRows is set to true if this result set is from the _find interface.
isFindRows bool
}
var _ driver.Rows = &rows{}
func newRows(r io.ReadCloser) *rows {
return &rows{
body: r,
}
}
func (r *rows) Offset() int64 {
return r.offset
}
func (r *rows) TotalRows() int64 {
return r.totalRows
}
func (r *rows) Warning() string {
return r.warning
}
func (r *rows) Bookmark() string {
return r.bookmark
}
func (r *rows) UpdateSeq() string {
return r.updateSeq
}
func (r *rows) Close() error {
return r.body.Close()
}
func (r *rows) Next(row *driver.Row) error {
if r.closed {
return io.EOF
}
if r.dec == nil {
// We haven't begun yet
r.dec = json.NewDecoder(r.body)
// consume the first '{'
if err := consumeDelim(r.dec, json.Delim('{')); err != nil {
return err
}
if err := r.begin(); err != nil {
return errors.WrapStatus(kivik.StatusBadResponse, err)
}
}
err := r.nextRow(row)
if err != nil {
r.closed = true
if err == io.EOF {
return r.finish()
}
}
return err
}
// begin parses the top-level of the result object; until rows
func (r *rows) begin() error {
for {
t, err := r.dec.Token()
if err != nil {
// I can't find a test case to trigger this, so it remains uncovered.
return err
}
key, ok := t.(string)
if !ok {
// The JSON parser should never permit this
return fmt.Errorf("Unexpected token: (%T) %v", t, t)
}
if key == "rows" || key == "docs" {
r.isFindRows = key == "docs"
// Consume the first '['
return consumeDelim(r.dec, json.Delim('['))
}
if err := r.parseMeta(key); err != nil {
return err
}
}
}
func (r *rows) finish() error {
for {
t, err := r.dec.Token()
if err != nil {
return err
}
switch v := t.(type) {
case json.Delim:
if v != json.Delim('}') {
// This should never happen, as the JSON parser should prevent it.
return fmt.Errorf("Unexpected JSON delimiter: %c", v)
}
case string:
if err := r.parseMeta(v); err != nil {
return err
}
default:
// This should never happen, as the JSON parser would never get
// this far.
return fmt.Errorf("Unexpected JSON token: (%T) '%s'", t, t)
}
}
}
// parseMeta parses result metadata
func (r *rows) parseMeta(key string) error {
switch key {
case "update_seq":
return r.readUpdateSeq()
case "offset":
return r.dec.Decode(&r.offset)
case "total_rows":
return r.dec.Decode(&r.totalRows)
case "warning":
return r.dec.Decode(&r.warning)
case "bookmark":
return r.dec.Decode(&r.bookmark)
}
return errors.Statusf(kivik.StatusBadResponse, "Unexpected key: %s", key)
}
func (r *rows) readUpdateSeq() error {
var raw json.RawMessage
if err := r.dec.Decode(&raw); err != nil {
return err
}
r.updateSeq = string(bytes.Trim(raw, `""`))
return nil
}
func (r *rows) nextRow(row *driver.Row) error {
if !r.dec.More() {
if err := consumeDelim(r.dec, json.Delim(']')); err != nil {
return err
}
return io.EOF
}
if r.isFindRows {
return r.dec.Decode(&row.Doc)
}
return r.dec.Decode(row)
}
// consumeDelim consumes the expected delimiter from the stream, or returns an
// error if an unexpected token was found.
func consumeDelim(dec *json.Decoder, expectedDelim json.Delim) error {
t, err := dec.Token()
if err != nil {
return errors.WrapStatus(kivik.StatusBadResponse, errors.Wrap(err, "no closing delimiter"))
}
d, ok := t.(json.Delim)
if !ok {
return errors.Statusf(kivik.StatusBadResponse, "Unexpected token %T: %v", t, t)
}
if d != expectedDelim {
return errors.Statusf(kivik.StatusBadResponse, "Unexpected JSON delimiter: %c", d)
}
return nil
}