-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
107 lines (91 loc) · 2.25 KB
/
index.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
package arangox
import (
"context"
"encoding/json"
"fmt"
"strings"
arangoDriver "github.com/arangodb/go-driver"
"github.com/clinia/x/errorx"
)
type IndexType int
const (
IndexTypePersistent IndexType = iota
IndexTypeGeo
IndexTypeHash
IndexTypeInverted
IndexTypeTTL
IndexTypeZKD
IndexTypeSkipList
)
var (
IndexType_name = map[int]string{
0: "PERSISTENT",
1: "GEO",
2: "HASH",
3: "INVERTED",
4: "TIME_TO_LIVE",
5: "ZKD",
6: "SKIP_LIST",
}
IndexType_value = map[string]int{
"PERSISTENT": 0,
"GEO": 1,
"HASH": 2,
"INVERTED": 3,
"TIME_TO_LIVE": 4,
"ZKD": 5,
"SKIP_LIST": 6,
}
)
func (u IndexType) String() string {
return IndexType_name[int(u)]
}
func ParseIndexType(s string) (IndexType, error) {
s = strings.TrimSpace(strings.ToUpper(s))
value, ok := IndexType_value[s]
if !ok {
return IndexType(0), errorx.NewInternalError(fmt.Errorf("%q is not a valid index type", s))
}
return IndexType(value), nil
}
func (u IndexType) MarshalJSON() ([]byte, error) {
return json.Marshal(u.String())
}
func (u *IndexType) UnmarshalJSON(data []byte) (err error) {
var indexType string
if err := json.Unmarshal(data, &indexType); err != nil {
return err
}
if *u, err = ParseIndexType(indexType); err != nil {
return err
}
return nil
}
func MigrateIndexes(ctx context.Context, col arangoDriver.Collection, indexes []IndexParam) error {
if len(indexes) == 0 {
return nil
}
for _, idx := range indexes {
var err error
switch idx.Type {
case IndexTypePersistent:
_, _, err = col.EnsurePersistentIndex(ctx, idx.Fields, idx.PersistentIndexOptions)
case IndexTypeGeo:
_, _, err = col.EnsureGeoIndex(ctx, idx.Fields, idx.GeoIndexOptions)
case IndexTypeHash:
_, _, err = col.EnsureHashIndex(ctx, idx.Fields, idx.HashIndexOptions)
case IndexTypeInverted:
_, _, err = col.EnsureInvertedIndex(ctx, idx.InvertedIndexOptions)
case IndexTypeSkipList:
_, _, err = col.EnsureSkipListIndex(ctx, idx.Fields, idx.SkipListIndexOptions)
case IndexTypeTTL:
_, _, err = col.EnsureTTLIndex(ctx, idx.Fields[0], idx.ExpireAfter, idx.TTLIndexOptions)
case IndexTypeZKD:
_, _, err = col.EnsureZKDIndex(ctx, idx.Fields, idx.ZKDIndexOptions)
}
if err != nil {
return err
}
}
return nil
}