|
| 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 | +} |
0 commit comments