MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / writeQueryRows

Method writeQueryRows

rest/functions_api.go:112–151  ·  view source on GitHub ↗

Subroutine to write N1QL query results to a response

(rows sgbucket.QueryResultIterator)

Source from the content-addressed store, hash-verified

110
111// Subroutine to write N1QL query results to a response
112func (h *handler) writeQueryRows(rows sgbucket.QueryResultIterator) error {
113 // Use iterator to write results one at a time to the response:
114 defer func() {
115 if rows != nil {
116 _ = rows.Close()
117 }
118 }()
119
120 // Write the query results to the response, as a JSON array of objects.
121 h.setHeader("Content-Type", "application/json")
122 var err error
123 if _, err = h.response.Write([]byte(`[`)); err != nil {
124 return err
125 }
126 first := true
127 var row interface{}
128 for rows.Next(h.ctx(), &row) {
129 if first {
130 first = false
131 } else {
132 if _, err = h.response.Write([]byte(`,`)); err != nil {
133 return err
134 }
135 }
136 if err = h.addJSON(row); err != nil {
137 return err
138 }
139 // The iterator streams results as the query engine produces them, so this loop may take most of the query's time; check for timeout after each iteration:
140 if err = db.CheckTimeout(h.ctx()); err != nil {
141 return err
142 }
143 }
144 err = rows.Close()
145 rows = nil // prevent 'defer' from closing again
146 if err != nil {
147 return err
148 }
149 _, err = h.response.Write([]byte("]\n"))
150 return err
151}

Callers 1

handleFunctionCallMethod · 0.95

Calls 7

setHeaderMethod · 0.95
ctxMethod · 0.95
addJSONMethod · 0.95
CheckTimeoutFunction · 0.92
CloseMethod · 0.65
WriteMethod · 0.45
NextMethod · 0.45

Tested by

no test coverage detected