-
Notifications
You must be signed in to change notification settings - Fork 15
/
find.go
161 lines (149 loc) · 4.45 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
// 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 couchdb
import (
"context"
"encoding/json"
"fmt"
"net/http"
"path"
"github.com/go-kivik/couchdb/v4/chttp"
"github.com/go-kivik/kivik/v4/driver"
)
const (
pathIndex = "_index"
)
func (d *db) CreateIndex(ctx context.Context, ddoc, name string, index interface{}, opts map[string]interface{}) error {
reqPath := pathIndex
if part, ok := opts[OptionPartition].(string); ok {
delete(opts, OptionPartition)
reqPath = path.Join("_partition", part, reqPath)
}
indexObj, err := deJSONify(index)
if err != nil {
return err
}
parameters := struct {
Index interface{} `json:"index"`
Ddoc string `json:"ddoc,omitempty"`
Name string `json:"name,omitempty"`
}{
Index: indexObj,
Ddoc: ddoc,
Name: name,
}
options := &chttp.Options{
Body: chttp.EncodeBody(parameters),
}
_, err = d.Client.DoError(ctx, http.MethodPost, d.path(reqPath), options)
return err
}
func (d *db) GetIndexes(ctx context.Context, opts map[string]interface{}) ([]driver.Index, error) {
reqPath := pathIndex
if part, ok := opts[OptionPartition].(string); ok {
delete(opts, OptionPartition)
reqPath = path.Join("_partition", part, reqPath)
}
var result struct {
Indexes []driver.Index `json:"indexes"`
}
err := d.Client.DoJSON(ctx, http.MethodGet, d.path(reqPath), nil, &result)
return result.Indexes, err
}
func (d *db) DeleteIndex(ctx context.Context, ddoc, name string, opts map[string]interface{}) error {
if ddoc == "" {
return missingArg("ddoc")
}
if name == "" {
return missingArg("name")
}
reqPath := pathIndex
if part, ok := opts[OptionPartition].(string); ok {
delete(opts, OptionPartition)
reqPath = path.Join("_partition", part, reqPath)
}
path := fmt.Sprintf("%s/%s/json/%s", reqPath, ddoc, name)
_, err := d.Client.DoError(ctx, http.MethodDelete, d.path(path), nil)
return err
}
func (d *db) Find(ctx context.Context, query interface{}, opts map[string]interface{}) (driver.Rows, error) {
reqPath := "_find"
if part, ok := opts[OptionPartition].(string); ok {
delete(opts, OptionPartition)
reqPath = path.Join("_partition", part, reqPath)
}
options := &chttp.Options{
GetBody: chttp.BodyEncoder(query),
Header: http.Header{
chttp.HeaderIdempotencyKey: []string{},
},
}
resp, err := d.Client.DoReq(ctx, http.MethodPost, d.path(reqPath), options)
if err != nil {
return nil, err
}
if err = chttp.ResponseError(resp); err != nil {
return nil, err
}
return newFindRows(ctx, resp.Body), 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{}, opts map[string]interface{}) (*driver.QueryPlan, error) {
reqPath := "_explain"
if part, ok := opts[OptionPartition].(string); ok {
delete(opts, OptionPartition)
reqPath = path.Join("_partition", part, reqPath)
}
options := &chttp.Options{
GetBody: chttp.BodyEncoder(query),
Header: http.Header{
chttp.HeaderIdempotencyKey: []string{},
},
}
var plan queryPlan
if err := d.Client.DoJSON(ctx, http.MethodPost, d.path(reqPath), options, &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
}