-
Notifications
You must be signed in to change notification settings - Fork 81
/
collection_indexes_impl.go
436 lines (412 loc) · 14.8 KB
/
collection_indexes_impl.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//
// DISCLAIMER
//
// Copyright 2017-2024 ArangoDB GmbH, Cologne, Germany
//
// 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.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
package driver
import (
"context"
"encoding/json"
"path"
)
type indexData struct {
ID string `json:"id,omitempty"`
Type string `json:"type"`
Fields []string `json:"fields,omitempty"`
Unique *bool `json:"unique,omitempty"`
Deduplicate *bool `json:"deduplicate,omitempty"`
Sparse *bool `json:"sparse,omitempty"`
GeoJSON *bool `json:"geoJson,omitempty"`
InBackground *bool `json:"inBackground,omitempty"`
Estimates *bool `json:"estimates,omitempty"`
MaxNumCoverCells int `json:"maxNumCoverCells,omitempty"`
MinLength int `json:"minLength,omitempty"`
ExpireAfter int `json:"expireAfter"`
Name string `json:"name,omitempty"`
FieldValueTypes string `json:"fieldValueTypes,omitempty"`
IsNewlyCreated *bool `json:"isNewlyCreated,omitempty"`
SelectivityEstimate float64 `json:"selectivityEstimate,omitempty"`
BestIndexedLevel int `json:"bestIndexedLevel,omitempty"`
WorstIndexedLevel int `json:"worstIndexedLevel,omitempty"`
LegacyPolygons *bool `json:"legacyPolygons,omitempty"`
CacheEnabled *bool `json:"cacheEnabled,omitempty"`
StoredValues []string `json:"storedValues,omitempty"`
PrefixFields []string `json:"prefixFields,omitempty"`
ArangoError `json:",inline"`
}
type indexListResponse struct {
Indexes []json.RawMessage `json:"indexes,omitempty"`
ArangoError
}
// Index opens a connection to an existing index within the collection.
// If no index with given name exists, an NotFoundError is returned.
func (c *collection) Index(ctx context.Context, name string) (Index, error) {
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("index"), name))
if err != nil {
return nil, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var data map[string]interface{}
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
rawResponse, err := json.Marshal(data)
if err != nil {
return nil, WithStack(err)
}
idx, err := newIndexFromMap(rawResponse, c)
if err != nil {
return nil, WithStack(err)
}
return idx, nil
}
// IndexExists returns true if an index with given name exists within the collection.
func (c *collection) IndexExists(ctx context.Context, name string) (bool, error) {
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("index"), name))
if err != nil {
return false, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return false, WithStack(err)
}
if err := resp.CheckStatus(200); err == nil {
return true, nil
} else if IsNotFound(err) {
return false, nil
} else {
return false, WithStack(err)
}
}
// Indexes returns a list of all indexes in the collection.
func (c *collection) Indexes(ctx context.Context) ([]Index, error) {
req, err := c.conn.NewRequest("GET", path.Join(c.db.relPath(), "_api", "index"))
if err != nil {
return nil, WithStack(err)
}
req.SetQuery("collection", c.name)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var data indexListResponse
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
result := make([]Index, 0, len(data.Indexes))
for _, x := range data.Indexes {
idx, err := newIndexFromMap(x, c)
if err != nil {
return nil, WithStack(err)
}
result = append(result, idx)
}
return result, nil
}
// Deprecated: since 3.10 version. Use ArangoSearch view instead.
// EnsureFullTextIndex creates a fulltext index in the collection, if it does not already exist.
//
// Fields is a slice of attribute names. Currently, the slice is limited to exactly one attribute.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *collection) EnsureFullTextIndex(ctx context.Context, fields []string, options *EnsureFullTextIndexOptions) (Index, bool, error) {
input := indexData{
Type: string(FullTextIndex),
Fields: fields,
}
if options != nil {
input.InBackground = &options.InBackground
input.Name = options.Name
input.MinLength = options.MinLength
input.Estimates = options.Estimates
}
idx, created, err := c.ensureIndex(ctx, input)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}
// EnsureGeoIndex creates a hash index in the collection, if it does not already exist.
//
// Fields is a slice with one or two attribute paths. If it is a slice with one attribute path location,
// then a geo-spatial index on all documents is created using location as path to the coordinates.
// The value of the attribute must be a slice with at least two double values. The slice must contain the latitude (first value)
// and the longitude (second value). All documents, which do not have the attribute path or with value that are not suitable, are ignored.
// If it is a slice with two attribute paths latitude and longitude, then a geo-spatial index on all documents is created
// using latitude and longitude as paths the latitude and the longitude. The value of the attribute latitude and of the
// attribute longitude must a double. All documents, which do not have the attribute paths or which values are not suitable, are ignored.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *collection) EnsureGeoIndex(ctx context.Context, fields []string, options *EnsureGeoIndexOptions) (Index, bool, error) {
input := indexData{
Type: string(GeoIndex),
Fields: fields,
}
if options != nil {
input.InBackground = &options.InBackground
input.Name = options.Name
input.GeoJSON = &options.GeoJSON
input.Estimates = options.Estimates
if options.LegacyPolygons {
input.LegacyPolygons = &options.LegacyPolygons
}
}
idx, created, err := c.ensureIndex(ctx, input)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}
// EnsureHashIndex creates a hash index in the collection, if it does not already exist.
// Fields is a slice of attribute paths.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *collection) EnsureHashIndex(ctx context.Context, fields []string, options *EnsureHashIndexOptions) (Index, bool, error) {
input := indexData{
Type: string(HashIndex),
Fields: fields,
}
off := false
if options != nil {
input.InBackground = &options.InBackground
input.Name = options.Name
input.Unique = &options.Unique
input.Sparse = &options.Sparse
input.Estimates = options.Estimates
if options.NoDeduplicate {
input.Deduplicate = &off
}
}
idx, created, err := c.ensureIndex(ctx, input)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}
// EnsurePersistentIndex creates a persistent index in the collection, if it does not already exist.
// Fields is a slice of attribute paths.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *collection) EnsurePersistentIndex(ctx context.Context, fields []string, options *EnsurePersistentIndexOptions) (Index, bool, error) {
input := indexData{
Type: string(PersistentIndex),
Fields: fields,
}
off := false
on := true
if options != nil {
input.InBackground = &options.InBackground
input.Name = options.Name
input.Unique = &options.Unique
input.Sparse = &options.Sparse
input.Estimates = options.Estimates
if options.NoDeduplicate {
input.Deduplicate = &off
}
if options.CacheEnabled {
input.CacheEnabled = &on
}
if options.StoredValues != nil {
input.StoredValues = options.StoredValues
}
}
idx, created, err := c.ensureIndex(ctx, input)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}
// EnsureSkipListIndex creates a skiplist index in the collection, if it does not already exist.
// Fields is a slice of attribute paths.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *collection) EnsureSkipListIndex(ctx context.Context, fields []string, options *EnsureSkipListIndexOptions) (Index, bool, error) {
input := indexData{
Type: string(SkipListIndex),
Fields: fields,
}
off := false
if options != nil {
input.InBackground = &options.InBackground
input.Name = options.Name
input.Unique = &options.Unique
input.Sparse = &options.Sparse
input.Estimates = options.Estimates
if options.NoDeduplicate {
input.Deduplicate = &off
}
}
idx, created, err := c.ensureIndex(ctx, input)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}
// EnsureTTLIndex creates a TLL collection, if it does not already exist.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *collection) EnsureTTLIndex(ctx context.Context, field string, expireAfter int, options *EnsureTTLIndexOptions) (Index, bool, error) {
input := indexData{
Type: string(TTLIndex),
Fields: []string{field},
ExpireAfter: expireAfter,
}
if options != nil {
input.InBackground = &options.InBackground
input.Name = options.Name
input.Estimates = options.Estimates
}
idx, created, err := c.ensureIndex(ctx, input)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}
// EnsureZKDIndex creates a ZKD index in the collection, if it does not already exist.
// Fields is a slice of attribute paths.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *collection) EnsureZKDIndex(ctx context.Context, fields []string, options *EnsureZKDIndexOptions) (Index, bool, error) {
input := indexData{
Type: string(ZKDIndex),
Fields: fields,
// fieldValueTypes is required and the only allowed value is "double". Future extensions of the index will allow other types.
FieldValueTypes: "double",
}
if options != nil {
input.InBackground = &options.InBackground
input.Name = options.Name
input.Unique = &options.Unique
//input.Sparse = &options.Sparse
}
idx, created, err := c.ensureIndex(ctx, input)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}
func (c *collection) EnsureMDIIndex(ctx context.Context, fields []string, options *EnsureMDIIndexOptions) (Index, bool, error) {
input := indexData{
Type: string(MDIIndex),
Fields: fields,
// fieldValueTypes is required and the only allowed value is "double". Future extensions of the index will allow other types.
FieldValueTypes: "double",
}
if options != nil {
input.InBackground = &options.InBackground
input.Name = options.Name
input.Unique = &options.Unique
input.Sparse = &options.Sparse
input.StoredValues = options.StoredValues
}
idx, created, err := c.ensureIndex(ctx, input)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}
func (c *collection) EnsureMDIPrefixedIndex(ctx context.Context, fields []string, options *EnsureMDIPrefixedIndexOptions) (Index, bool, error) {
input := indexData{
Type: string(MDIPrefixedIndex),
Fields: fields,
// fieldValueTypes is required and the only allowed value is "double". Future extensions of the index will allow other types.
FieldValueTypes: "double",
}
if options != nil {
input.InBackground = &options.InBackground
input.Name = options.Name
input.Unique = &options.Unique
input.Sparse = &options.Sparse
input.StoredValues = options.StoredValues
input.PrefixFields = options.PrefixFields
}
idx, created, err := c.ensureIndex(ctx, input)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}
type invertedIndexData struct {
InvertedIndexOptions
Type string `json:"type"`
ID string `json:"id,omitempty"`
ArangoError `json:",inline"`
}
// EnsureInvertedIndex creates an inverted index in the collection, if it does not already exist.
// Available in ArangoDB 3.10 and later.
func (c *collection) EnsureInvertedIndex(ctx context.Context, options *InvertedIndexOptions) (Index, bool, error) {
req, err := c.conn.NewRequest("POST", path.Join(c.db.relPath(), "_api/index"))
if err != nil {
return nil, false, WithStack(err)
}
if options == nil {
options = &InvertedIndexOptions{}
}
req.SetQuery("collection", c.name)
if _, err := req.SetBody(invertedIndexData{InvertedIndexOptions: *options, Type: string(InvertedIndex)}); err != nil {
return nil, false, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, false, WithStack(err)
}
if err := resp.CheckStatus(200, 201); err != nil {
return nil, false, WithStack(err)
}
created := resp.StatusCode() == 201
var data invertedIndexData
if err := resp.ParseBody("", &data); err != nil {
return nil, false, WithStack(err)
}
idx, err := newInvertedIndex(data, c)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}
// ensureIndex creates a persistent index in the collection, if it does not already exist.
// Fields is a slice of attribute paths.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *collection) ensureIndex(ctx context.Context, options indexData) (Index, bool, error) {
req, err := c.conn.NewRequest("POST", path.Join(c.db.relPath(), "_api/index"))
if err != nil {
return nil, false, WithStack(err)
}
req.SetQuery("collection", c.name)
if _, err := req.SetBody(options); err != nil {
return nil, false, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, false, WithStack(err)
}
if err := resp.CheckStatus(200, 201); err != nil {
return nil, false, WithStack(err)
}
created := resp.StatusCode() == 201
var data indexData
if err := resp.ParseBody("", &data); err != nil {
return nil, false, WithStack(err)
}
idx, err := newIndex(data, c)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}