-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.go
191 lines (171 loc) · 5.02 KB
/
find.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
// 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 pouchdb
import (
"context"
"encoding/json"
"io"
"net/http"
"strings"
"github.com/gopherjs/gopherjs/js"
kivik "github.com/go-kivik/kivik/v4"
"github.com/go-kivik/kivik/v4/driver"
"github.com/go-kivik/pouchdb/v4/bindings"
)
var _ driver.Finder = &db{}
// buildIndex merges the ddoc and name into the index structure, as reqiured
// by the PouchDB-find plugin.
func buildIndex(ddoc, name string, index interface{}) (*js.Object, error) {
i, err := bindings.Objectify(index)
if err != nil {
return nil, err
}
o := js.Global.Get("Object").New(i)
if ddoc != "" {
o.Set("ddoc", ddoc)
}
if name != "" {
o.Set("name", name)
}
return o, nil
}
func (d *db) CreateIndex(ctx context.Context, ddoc, name string, index interface{}, _ map[string]interface{}) error {
indexObj, err := buildIndex(ddoc, name, index)
if err != nil {
return err
}
_, err = d.db.CreateIndex(ctx, indexObj)
return err
}
func (d *db) GetIndexes(ctx context.Context, _ map[string]interface{}) (indexes []driver.Index, err error) {
defer bindings.RecoverError(&err)
result, err := d.db.GetIndexes(ctx)
if err != nil {
return nil, err
}
// This might not be the most efficient, but it's easy
var final struct {
Indexes []driver.Index `json:"indexes"`
}
err = json.Unmarshal([]byte(js.Global.Get("JSON").Call("stringify", result).String()), &final)
return final.Indexes, err
}
// findIndex attempts to find the requested index definition
func (d *db) findIndex(ctx context.Context, ddoc, name string) (interface{}, error) {
ddoc = "_design/" + strings.TrimPrefix(ddoc, "_design/")
indexes, err := d.GetIndexes(ctx, nil)
if err != nil {
return nil, err
}
for _, idx := range indexes {
if idx.Type == "special" {
continue
}
if idx.DesignDoc == ddoc && idx.Name == name {
return map[string]interface{}{
"ddoc": idx.DesignDoc,
"name": idx.Name,
"type": idx.Type,
"def": idx.Definition,
}, nil
}
}
return nil, &kivik.Error{Status: http.StatusNotFound, Message: "index does not exist"}
}
func (d *db) DeleteIndex(ctx context.Context, ddoc, name string, _ map[string]interface{}) error {
index, err := d.findIndex(ctx, ddoc, name)
if err != nil {
return err
}
_, err = d.db.DeleteIndex(ctx, index)
return err
}
func (d *db) Find(ctx context.Context, query interface{}, _ map[string]interface{}) (driver.Rows, error) {
result, err := d.db.Find(ctx, query)
if err != nil {
return nil, err
}
return &findRows{
Object: result,
}, nil
}
type findRows struct {
*js.Object
}
var _ driver.Rows = &findRows{}
func (r *findRows) Offset() int64 { return 0 }
func (r *findRows) TotalRows() int64 { return 0 }
func (r *findRows) UpdateSeq() string { return "" }
func (r *findRows) Warning() string {
if w := r.Get("warning"); w != js.Undefined {
return w.String()
}
return ""
}
func (r *findRows) Close() error {
r.Delete("docs") // Free up memory used by any remaining rows
return nil
}
func (r *findRows) Next(row *driver.Row) (err error) {
defer bindings.RecoverError(&err)
if r.Get("docs") == js.Undefined || r.Get("docs").Length() == 0 {
return io.EOF
}
next := r.Get("docs").Call("shift")
row.Doc = strings.NewReader(jsJSON.Call("stringify", next).String())
return nil
}
type queryPlan struct {
DBName string `json:"dbname"`
Index map[string]interface{} `json:"index"`
Selector map[string]interface{} `json:"selector"`
Options map[string]interface{} `json:"opts"`
Limit int64 `json:"limit"`
Skip int64 `json:"skip"`
Fields fields `json:"fields"`
Range map[string]interface{} `json:"range"`
}
type fields []interface{}
func (f *fields) UnmarshalJSON(data []byte) error {
if string(data) == `"all_fields"` {
return nil
}
var i []interface{}
if err := json.Unmarshal(data, &i); err != nil {
return err
}
newFields := make([]interface{}, len(i))
copy(newFields, i)
*f = newFields
return nil
}
func (d *db) Explain(ctx context.Context, query interface{}, _ map[string]interface{}) (*driver.QueryPlan, error) {
result, err := d.db.Explain(ctx, query)
if err != nil {
return nil, err
}
planJSON := js.Global.Get("JSON").Call("stringify", result).String()
var plan queryPlan
if err := json.Unmarshal([]byte(planJSON), &plan); err != nil {
return nil, err
}
return &driver.QueryPlan{
DBName: plan.DBName,
Index: plan.Index,
Selector: plan.Selector,
Options: plan.Options,
Limit: plan.Limit,
Skip: plan.Skip,
Fields: plan.Fields,
Range: plan.Range,
}, nil
}