Skip to content

Commit 4a25e87

Browse files
Lars Maierneunhoef
Lars Maier
authored andcommitted
Feature/custom analyzer api (#217)
* Added test for views in backup. * Added internal support for custom analzyers. * Implemented analyzers API. * Updated test for views and analyzers. * Added test for eventual view recreation. * Removed wrong test. * Fixing test.
1 parent 430fe7a commit 4a25e87

9 files changed

+536
-19
lines changed

client_admin_backup_impl.go

+8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func (c *clientBackup) Create(ctx context.Context, opt *BackupCreateOptions) (Ba
4343
if err != nil {
4444
return "", BackupCreateResponse{}, WithStack(err)
4545
}
46+
applyContextSettings(ctx, req)
4647
if opt != nil {
4748
body := struct {
4849
Label string `json:"label,omitempty"`
@@ -91,6 +92,7 @@ func (c *clientBackup) Delete(ctx context.Context, id BackupID) error {
9192
if err != nil {
9293
return WithStack(err)
9394
}
95+
applyContextSettings(ctx, req)
9496
body := struct {
9597
ID BackupID `json:"id,omitempty"`
9698
}{
@@ -116,6 +118,7 @@ func (c *clientBackup) Restore(ctx context.Context, id BackupID, opt *BackupRest
116118
if err != nil {
117119
return WithStack(err)
118120
}
121+
applyContextSettings(ctx, req)
119122
body := struct {
120123
ID BackupID `json:"id,omitempty"`
121124
IgnoreVersion bool `json:"ignoreVersion,omitempty"`
@@ -146,6 +149,7 @@ func (c *clientBackup) List(ctx context.Context, opt *BackupListOptions) (map[Ba
146149
if err != nil {
147150
return nil, WithStack(err)
148151
}
152+
applyContextSettings(ctx, req)
149153
if opt != nil {
150154
req, err = req.SetBody(opt)
151155
if err != nil {
@@ -175,6 +179,7 @@ func (c *clientBackup) Upload(ctx context.Context, id BackupID, remoteRepository
175179
if err != nil {
176180
return "", WithStack(err)
177181
}
182+
applyContextSettings(ctx, req)
178183
body := struct {
179184
ID BackupID `json:"id,omitempty"`
180185
RemoteRepo string `json:"remoteRepository,omitempty"`
@@ -211,6 +216,7 @@ func (c *clientBackup) Download(ctx context.Context, id BackupID, remoteReposito
211216
if err != nil {
212217
return "", WithStack(err)
213218
}
219+
applyContextSettings(ctx, req)
214220
body := struct {
215221
ID BackupID `json:"id,omitempty"`
216222
RemoteRepo string `json:"remoteRepository,omitempty"`
@@ -247,6 +253,7 @@ func (c *clientBackup) Progress(ctx context.Context, job BackupTransferJobID) (r
247253
if err != nil {
248254
return BackupTransferProgressReport{}, WithStack(err)
249255
}
256+
applyContextSettings(ctx, req)
250257
body := struct {
251258
ID BackupTransferJobID `json:"uploadId,omitempty"`
252259
}{
@@ -275,6 +282,7 @@ func (c *clientBackup) Abort(ctx context.Context, job BackupTransferJobID) error
275282
if err != nil {
276283
return WithStack(err)
277284
}
285+
applyContextSettings(ctx, req)
278286
body := struct {
279287
ID BackupTransferJobID `json:"uploadId,omitempty"`
280288
Abort bool `json:"abort,omitempty"`

database.go

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ type Database interface {
5555
// Streaming Transactions functions
5656
DatabaseStreamingTransactions
5757

58+
// ArangoSearch Analyzers API
59+
DatabaseArangoSearchAnalyzers
60+
5861
// Query performs an AQL query, returning a cursor used to iterate over the returned documents.
5962
// Note that the returned Cursor must always be closed to avoid holding on to resources in the server while they are no longer needed.
6063
Query(ctx context.Context, query string, bindVars map[string]interface{}) (Cursor, error)

database_arangosearch_analyzers.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Lars Maier
21+
//
22+
23+
package driver
24+
25+
import "context"
26+
27+
type ArangoSearchAnalyzer interface {
28+
// Name returns the analyzer name
29+
Name() string
30+
31+
// Type returns the analyzer type
32+
Type() ArangoSearchAnalyzerType
33+
34+
// UniqueName returns the unique name: <database>::<analyzer-name>
35+
UniqueName() string
36+
37+
// Definition returns the analyzer definition
38+
Definition() ArangoSearchAnalyzerDefinition
39+
40+
// Properties returns the analyzer properties
41+
Properties() ArangoSearchAnalyzerProperties
42+
43+
// Database returns the database of this analyzer
44+
Database() Database
45+
46+
// Removes the analyzers
47+
Remove(ctx context.Context, force bool) error
48+
}
49+
50+
type DatabaseArangoSearchAnalyzers interface {
51+
52+
// Ensure ensures that the given analyzer exists. If it does not exist it is created.
53+
// The function returns whether the analyzer already existed or an error.
54+
EnsureAnalyzer(ctx context.Context, analyzer ArangoSearchAnalyzerDefinition) (bool, ArangoSearchAnalyzer, error)
55+
56+
// Get returns the analyzer definition for the given analyzer or returns an error
57+
Analyzer(ctx context.Context, name string) (ArangoSearchAnalyzer, error)
58+
59+
// List returns a list of all analyzers
60+
Analyzers(ctx context.Context) ([]ArangoSearchAnalyzer, error)
61+
}
+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Lars Maier
21+
//
22+
23+
package driver
24+
25+
import (
26+
"context"
27+
"path"
28+
"strings"
29+
)
30+
31+
type analyzer struct {
32+
definition ArangoSearchAnalyzerDefinition
33+
db *database
34+
}
35+
36+
// Name returns the analyzer name
37+
func (a *analyzer) Name() string {
38+
split := strings.Split(a.definition.Name, "::")
39+
return split[len(split)-1]
40+
}
41+
42+
// UniqueName returns the unique name: <database>::<analyzer-name>
43+
func (a *analyzer) UniqueName() string {
44+
return a.definition.Name
45+
}
46+
47+
// Type returns the analyzer type
48+
func (a *analyzer) Type() ArangoSearchAnalyzerType {
49+
return a.definition.Type
50+
}
51+
52+
// Definition returns the analyzer definition
53+
func (a *analyzer) Definition() ArangoSearchAnalyzerDefinition {
54+
return a.definition
55+
}
56+
57+
// Properties returns the analyzer properties
58+
func (a *analyzer) Properties() ArangoSearchAnalyzerProperties {
59+
return a.definition.Properties
60+
}
61+
62+
// Removes the analyzers
63+
func (a *analyzer) Remove(ctx context.Context, force bool) error {
64+
req, err := a.db.conn.NewRequest("DELETE", path.Join(a.db.relPath(), "_api/analyzer/", a.Name()))
65+
if err != nil {
66+
return WithStack(err)
67+
}
68+
applyContextSettings(ctx, req)
69+
payload := struct {
70+
Force bool `json:"force,omitempty"`
71+
}{
72+
Force: force,
73+
}
74+
req, err = req.SetBody(payload)
75+
if err != nil {
76+
return WithStack(err)
77+
}
78+
resp, err := a.db.conn.Do(ctx, req)
79+
if err != nil {
80+
return WithStack(err)
81+
}
82+
if err := resp.CheckStatus(200); err != nil {
83+
return WithStack(err)
84+
}
85+
var actualDef ArangoSearchAnalyzerDefinition
86+
if err := resp.ParseBody("", &actualDef); err != nil {
87+
return WithStack(err)
88+
}
89+
return nil
90+
}
91+
92+
// Database returns the database of this analyzer
93+
func (a *analyzer) Database() Database {
94+
return a.db
95+
}
96+
97+
// Ensure ensures that the given analyzer exists. If it does not exist it is created.
98+
// The function returns whether the analyzer already existed or an error.
99+
func (d *database) EnsureAnalyzer(ctx context.Context, definition ArangoSearchAnalyzerDefinition) (bool, ArangoSearchAnalyzer, error) {
100+
req, err := d.conn.NewRequest("POST", path.Join(d.relPath(), "_api/analyzer"))
101+
if err != nil {
102+
return false, nil, WithStack(err)
103+
}
104+
applyContextSettings(ctx, req)
105+
req, err = req.SetBody(definition)
106+
if err != nil {
107+
return false, nil, WithStack(err)
108+
}
109+
resp, err := d.conn.Do(ctx, req)
110+
if err != nil {
111+
return false, nil, WithStack(err)
112+
}
113+
if err := resp.CheckStatus(201, 200); err != nil {
114+
return false, nil, WithStack(err)
115+
}
116+
found := resp.StatusCode() == 200
117+
var actualDef ArangoSearchAnalyzerDefinition
118+
if err := resp.ParseBody("", &actualDef); err != nil {
119+
return false, nil, WithStack(err)
120+
}
121+
return found, &analyzer{
122+
db: d,
123+
definition: actualDef,
124+
}, nil
125+
}
126+
127+
// Get returns the analyzer definition for the given analyzer or returns an error
128+
func (d *database) Analyzer(ctx context.Context, name string) (ArangoSearchAnalyzer, error) {
129+
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/analyzer/", name))
130+
if err != nil {
131+
return nil, WithStack(err)
132+
}
133+
applyContextSettings(ctx, req)
134+
resp, err := d.conn.Do(ctx, req)
135+
if err != nil {
136+
return nil, WithStack(err)
137+
}
138+
if err := resp.CheckStatus(200); err != nil {
139+
return nil, WithStack(err)
140+
}
141+
var actualDef ArangoSearchAnalyzerDefinition
142+
if err := resp.ParseBody("", &actualDef); err != nil {
143+
return nil, WithStack(err)
144+
}
145+
return &analyzer{
146+
db: d,
147+
definition: actualDef,
148+
}, nil
149+
}
150+
151+
type analyzerListResponse struct {
152+
Analyzer []ArangoSearchAnalyzerDefinition `json:"result,omitempty"`
153+
}
154+
155+
// List returns a list of all analyzers
156+
func (d *database) Analyzers(ctx context.Context) ([]ArangoSearchAnalyzer, error) {
157+
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/analyzer"))
158+
if err != nil {
159+
return nil, WithStack(err)
160+
}
161+
applyContextSettings(ctx, req)
162+
resp, err := d.conn.Do(ctx, req)
163+
if err != nil {
164+
return nil, WithStack(err)
165+
}
166+
if err := resp.CheckStatus(200); err != nil {
167+
return nil, WithStack(err)
168+
}
169+
var response analyzerListResponse
170+
if err := resp.ParseBody("", &response); err != nil {
171+
return nil, WithStack(err)
172+
}
173+
174+
result := make([]ArangoSearchAnalyzer, 0, len(response.Analyzer))
175+
for _, a := range response.Analyzer {
176+
result = append(result, &analyzer{
177+
db: d,
178+
definition: a,
179+
})
180+
}
181+
182+
return result, nil
183+
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ require (
88
github.com/dgrijalva/jwt-go v3.2.0+incompatible
99
github.com/stretchr/testify v1.2.2
1010
github.com/pkg/errors v0.8.1
11+
github.com/stretchr/testify v1.2.2
1112
)

0 commit comments

Comments
 (0)