forked from couchbaselabs/cbft
/
remote.go
217 lines (182 loc) · 5.53 KB
/
remote.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
// Copyright (c) 2014 Couchbase, Inc.
// 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 cbft
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/document"
"github.com/blevesearch/bleve/index"
"github.com/blevesearch/bleve/index/store"
)
var httpPost = http.Post // Overridable for unit-testability.
var httpGet = http.Get // Overridable for unit-testability.
var indexClientUnimplementedErr = errors.New("unimplemented")
// IndexClient implements the Search() and DocCount() subset of the
// bleve.Index interface by accessing a remote cbft server via REST
// protocol. This allows callers to add a IndexClient as a target of
// a bleve.IndexAlias, and implements cbft protocol features like
// query consistency and auth.
//
// TODO: Implement propagating auth info in IndexClient.
type IndexClient struct {
QueryURL string
CountURL string
Consistency *ConsistencyParams
}
func (r *IndexClient) Index(id string, data interface{}) error {
return indexClientUnimplementedErr
}
func (r *IndexClient) Delete(id string) error {
return indexClientUnimplementedErr
}
func (r *IndexClient) Batch(b *bleve.Batch) error {
return indexClientUnimplementedErr
}
func (r *IndexClient) Document(id string) (*document.Document, error) {
return nil, indexClientUnimplementedErr
}
func (r *IndexClient) DocCount() (uint64, error) {
if r.CountURL == "" {
return 0, fmt.Errorf("remote: no CountURL provided")
}
resp, err := httpGet(r.CountURL)
if err != nil {
return 0, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return 0, fmt.Errorf("remote: count got status code: %d,"+
" docCountURL: %s, resp: %#v", resp.StatusCode, r.CountURL, resp)
}
respBuf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, fmt.Errorf("remote: count error reading resp.Body,"+
" docCountURL: %s, resp: %#v", r.CountURL, resp)
}
rv := struct {
Status string `json:"status"`
Count uint64 `json:"count"`
}{}
err = json.Unmarshal(respBuf, &rv)
if err != nil {
return 0, fmt.Errorf("remote: count error parsing respBuf: %s,"+
" docCountURL: %s, resp: %#v", respBuf, r.CountURL, resp)
}
return rv.Count, nil
}
func (r *IndexClient) Search(req *bleve.SearchRequest) (
*bleve.SearchResult, error) {
if req == nil {
return nil, fmt.Errorf("remote: no req provided")
}
if r.QueryURL == "" {
return nil, fmt.Errorf("remote: no QueryURL provided")
}
queryCtlParams := &QueryCtlParams{
Ctl: QueryCtl{
Consistency: r.Consistency,
},
}
buf, err := json.Marshal(struct {
*QueryCtlParams
*bleve.SearchRequest
}{
queryCtlParams,
req,
})
if err != nil {
return nil, err
}
respBuf, err := r.Query(buf)
if err != nil {
return nil, err
}
rv := &bleve.SearchResult{}
err = json.Unmarshal(respBuf, rv)
if err != nil {
return nil, fmt.Errorf("remote: search error parsing respBuf: %s,"+
" queryURL: %s", respBuf, r.QueryURL)
}
return rv, nil
}
func (r *IndexClient) Fields() ([]string, error) {
return nil, indexClientUnimplementedErr
}
func (r *IndexClient) FieldDict(field string) (index.FieldDict, error) {
return nil, indexClientUnimplementedErr
}
func (r *IndexClient) FieldDictRange(field string,
startTerm []byte, endTerm []byte) (index.FieldDict, error) {
return nil, indexClientUnimplementedErr
}
func (r *IndexClient) FieldDictPrefix(field string,
termPrefix []byte) (index.FieldDict, error) {
return nil, indexClientUnimplementedErr
}
func (r *IndexClient) DumpAll() chan interface{} {
return nil
}
func (r *IndexClient) DumpDoc(id string) chan interface{} {
return nil
}
func (r *IndexClient) DumpFields() chan interface{} {
return nil
}
func (r *IndexClient) Close() error {
return indexClientUnimplementedErr
}
func (r *IndexClient) Mapping() *bleve.IndexMapping {
return nil
}
func (r *IndexClient) NewBatch() *bleve.Batch {
return nil
}
func (r *IndexClient) Stats() *bleve.IndexStat {
return nil
}
func (r *IndexClient) GetInternal(key []byte) ([]byte, error) {
return nil, indexClientUnimplementedErr
}
func (r *IndexClient) SetInternal(key, val []byte) error {
return indexClientUnimplementedErr
}
func (r *IndexClient) DeleteInternal(key []byte) error {
return indexClientUnimplementedErr
}
// -----------------------------------------------------
func (r *IndexClient) Count() (uint64, error) {
return r.DocCount()
}
func (r *IndexClient) Query(buf []byte) ([]byte, error) {
resp, err := httpPost(r.QueryURL, "application/json", bytes.NewBuffer(buf))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("remote: query got status code: %d,"+
" queryURL: %s, buf: %s, resp: %#v",
resp.StatusCode, r.QueryURL, buf, resp)
}
respBuf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("remote: query error reading resp.Body,"+
" queryURL: %s, buf: %s, resp: %#v",
r.QueryURL, buf, resp)
}
return respBuf, err
}
func (r *IndexClient) Advanced() (index.Index, store.KVStore, error) {
return nil, nil, indexClientUnimplementedErr
}