-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection+query.go
296 lines (264 loc) · 8.63 KB
/
collection+query.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
// Copyright 2023-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package rosmar
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"fmt"
"regexp"
"strings"
sgbucket "github.com/couchbase/sg-bucket"
)
//////// Interface QueryableStore
func (c *Collection) CanQueryIn(language sgbucket.QueryLanguage) bool {
return language == sgbucket.SQLiteLanguage
}
// Implementation of Query. Supports `SQLiteLanguage` only, of course.
// Use `$_keyspace` to refer to the collection in the `FROM` clause.
// The table columns are `id` (document ID or key), `body`, `xattrs`.
// You can use SQLite's `->` and `->>` syntax to refer to JSON properties in body and xattrs.
// The `consistency` and `adhoc` parameters are currently ignored.
func (c *Collection) Query(
language sgbucket.QueryLanguage,
statement string,
args map[string]any,
consistency sgbucket.ConsistencyMode,
adhoc bool,
) (iter sgbucket.QueryResultIterator, err error) {
traceEnter("Query", "%q, query: %s)", language, statement)
defer func() { traceExit("Query", err, "iterator") }()
if language != sgbucket.SQLiteLanguage {
err = fmt.Errorf("unsupported query language")
return
}
// Replace `$_keyspace` with a sub-query matching documents in this collection:
statement, sqlArgs := c.prepareQuery(statement, args)
// TODO: Save a sql.Stmt if adhoc==false
rows, err := c.db().Query(statement, sqlArgs...)
if err != nil {
err = fmt.Errorf("SQLite query failed: %w", err)
return
}
it := &queryIterator{rows: rows}
if c.bucket.inMemory {
// An in-memory database has only a single connection, so it's dangerous to leave the
// sql.Rows object open -- any other database call will block until it's closed. This
// can easily lead to deadlock. As a workaround, read all the rows now, close the
// Rows object, and return an iterator over the in-memory rows.
iter = preRecord(it)
} else {
iter = it
}
return
}
// Implementation of CreateIndex.
// The table columns are `id` (document ID or key), `body`, `xattrs`.
// You can use SQLite's `->` and `->>` syntax to refer to JSON properties in body and xattrs.
func (c *Collection) CreateIndex(indexName string, expression string, filterExpression string) (err error) {
traceEnter("CreateIndex", "%q, %q, %q)", indexName, expression, filterExpression)
defer func() { traceExit("CreateIndex", err, "ok") }()
// Rename the `id` and `body` columns to their actual names in the schema:
re := regexp.MustCompile(`\bid\b`)
expression = re.ReplaceAllString(expression, `key`)
filterExpression = re.ReplaceAllString(filterExpression, `key`)
re = regexp.MustCompile(`\bbody\b`)
expression = re.ReplaceAllString(expression, `value`)
filterExpression = re.ReplaceAllString(filterExpression, `value`)
// Note that we prepend `id` to the index columns, since the Query interface always
// evaluates a SELECT statement that matches the `id` with the specified Collection.
stmt := fmt.Sprintf(`CREATE INDEX %s ON documents (id, %s) WHERE value NOT NULL`,
indexName, expression)
if filterExpression != "" {
stmt += ` AND ` + filterExpression
}
_, err = c.db().Exec(stmt)
if err != nil && strings.Contains(err.Error(), "already exists") {
err = sgbucket.ErrIndexExists
}
return err
}
// Returns a description of the query plan, created by SQLITE's `EXPLAIN QUERY PLAN`.
// The format of the result is: a single key "plan" whose value is an array of steps.
// Each step is an array with two integers (node id and parent id) and a string description.
// Example: `{"plan":[[3,0,"SEARCH documents USING INDEX docs_cas (collection=?)"]]}`
// For details, see https://sqlite.org/eqp.html
func (c *Collection) ExplainQuery(statement string, args map[string]any) (plan map[string]any, err error) {
statement, sqlArgs := c.prepareQuery(statement, args)
rows, err := c.db().Query(`EXPLAIN QUERY PLAN `+statement, sqlArgs...)
if err != nil {
return
}
result := []any{}
for rows.Next() {
var node, parent, unused int
var description string
if err = rows.Scan(&node, &parent, &unused, &description); err != nil {
return
}
result = append(result, []any{node, parent, description})
}
if err = rows.Close(); err != nil {
return
}
return map[string]any{"plan": result}, nil
}
func (c *Collection) prepareQuery(statement string, args map[string]any) (string, []any) {
// Replace `$_keyspace` with a sub-query matching documents in this collection:
statement = strings.Replace(statement, sgbucket.KeyspaceQueryToken, "_keyspace", -1)
statement = fmt.Sprintf(`WITH _keyspace as (SELECT key as id, value as body, xattrs
FROM documents WHERE collection=%d AND value NOT NULL) %s`,
c.id, statement)
// Convert the args to an array of sql.NamedArg values:
sqlArgs := make([]any, 0, len(args))
for key, arg := range args {
sqlArgs = append(sqlArgs, sql.Named(key, arg))
}
return statement, sqlArgs
}
//////// Interface QueryResultIterator:
type queryIterator struct {
rows *sql.Rows
columnNames [][]byte
columnVals []sql.NullString
columnValPtrs []any
err error
}
// returns the next row as a JSON string.
func (iter *queryIterator) NextBytes() []byte {
if iter.err != nil {
return nil
}
if !iter.rows.Next() {
return nil
}
// Initialize column arrays:
if iter.columnVals == nil {
var colNames []string
if colNames, iter.err = iter.rows.Columns(); iter.err != nil {
return nil
}
// Convert each column name to a JSON string, including quotes:
nCols := len(colNames)
iter.columnNames = make([][]byte, nCols)
for i, name := range colNames {
iter.columnNames[i], _ = json.Marshal(name)
}
// Create array of column values, and the pointers thereto:
iter.columnVals = make([]sql.NullString, nCols)
iter.columnValPtrs = make([]any, nCols)
for i := range iter.columnNames {
iter.columnValPtrs[i] = &iter.columnVals[i]
}
}
// Read the row's columns into i.columnVals:
iter.err = iter.rows.Scan(iter.columnValPtrs...)
if iter.err != nil {
return nil
}
// Generate JSON. Each column value must be a JSON string.
row := bytes.Buffer{}
row.WriteByte('{')
first := true
for i, val := range iter.columnVals {
if val.Valid {
if first {
first = false
} else {
row.WriteByte(',')
}
row.Write(iter.columnNames[i])
row.WriteByte(':')
row.WriteString(val.String)
}
}
row.WriteByte('}')
return row.Bytes()
}
// unmarshals the query row into the pointed-to struct.
func (iter *queryIterator) Next(_ context.Context, valuePtr any) bool {
bytes := iter.NextBytes()
if bytes == nil {
return false
}
if err := json.Unmarshal(bytes, valuePtr); err != nil {
iter.err = fmt.Errorf("failed to unmarshal query result: %w; raw result is: %s", err, bytes)
return false
}
return true
}
func (iter *queryIterator) One(ctx context.Context, valuePtr any) error {
if !iter.Next(ctx, valuePtr) {
iter.err = sgbucket.ErrNoRows
}
return iter.Close()
}
func (iter *queryIterator) Close() error {
if iter.rows != nil {
if closeErr := iter.rows.Close(); closeErr != nil && iter.err == nil {
iter.err = closeErr
}
iter.rows = nil
}
return iter.err
}
//////// Pre-recorded query iterator:
type preRecordedQueryIterator struct {
rows [][]byte
err error
}
func preRecord(iter *queryIterator) *preRecordedQueryIterator {
var rows [][]byte
for {
if row := iter.NextBytes(); row != nil {
rows = append(rows, row)
} else {
break
}
}
return &preRecordedQueryIterator{
rows: rows,
err: iter.Close(),
}
}
func (iter *preRecordedQueryIterator) NextBytes() []byte {
if len(iter.rows) == 0 || iter.err != nil {
return nil
}
result := iter.rows[0]
iter.rows = iter.rows[1:]
return result
}
func (iter *preRecordedQueryIterator) Next(_ context.Context, valuePtr any) bool {
bytes := iter.NextBytes()
if bytes == nil {
return false
}
if err := json.Unmarshal(bytes, valuePtr); err != nil {
iter.err = fmt.Errorf("failed to unmarshal query result: %w; raw result is: %s", err, bytes)
return false
}
return true
}
func (iter *preRecordedQueryIterator) One(ctx context.Context, valuePtr any) error {
if !iter.Next(ctx, valuePtr) {
iter.err = sgbucket.ErrNoRows
}
return iter.Close()
}
func (iter *preRecordedQueryIterator) Close() error {
iter.rows = nil
return iter.err
}
var (
// Enforce interface conformance:
_ sgbucket.QueryableStore = &Collection{}
_ sgbucket.QueryResultIterator = &queryIterator{}
_ sgbucket.QueryResultIterator = &preRecordedQueryIterator{}
)