-
Notifications
You must be signed in to change notification settings - Fork 19
/
impls_view.go
211 lines (199 loc) · 6.7 KB
/
impls_view.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
package arangomigo
import (
"context"
"log"
"reflect"
"github.com/arangodb/go-driver"
"github.com/pkg/errors"
)
func (view SearchAliasView) Migrate(ctx context.Context, db driver.Database, extras map[string]interface{}) error {
switch view.Action {
case CREATE:
options := &driver.ArangoSearchAliasViewProperties{
Indexes: view.ArangoSearchAliasIndexes(),
}
_, err := db.CreateArangoSearchAliasView(ctx, view.Name, options)
if !e(err) {
log.Printf("Created view '%s'\n", view.Name)
}
return errors.Wrapf(err, "Couldn't create view '%s'", view.Name)
case DELETE:
dbView, err := db.View(ctx, view.Name)
if e(err) {
return errors.Wrapf(err, "Couldn't find view '%s' to delete", view.Name)
}
err = dbView.Remove(ctx)
if !e(err) {
log.Printf("Deleted view '%s'\n", view.Name)
}
return errors.Wrapf(err, "Couldn't delete view '%s'", view.Name)
}
return nil
}
func (view SearchAliasView) ArangoSearchAliasIndexes() []driver.ArangoSearchAliasIndex {
result := []driver.ArangoSearchAliasIndex{}
for _, searchIndex := range view.Indexes {
result = append(result, driver.ArangoSearchAliasIndex{Collection: searchIndex.Collection, Index: searchIndex.Index})
}
return result
}
// The impls_view contains all the implementation code for create, modifying and deleting an Arango
// Search View.
func (searchView SearchView) Migrate(ctx context.Context, db driver.Database, extras map[string]interface{}) error {
switch searchView.Action {
case CREATE:
viewProperties := buildViewProperties(searchView)
_, err := db.CreateArangoSearchView(ctx, searchView.Name, &viewProperties)
if !e(err) {
log.Printf("Created view '%s'\n", searchView.Name)
}
return errors.Wrapf(err, "Couldn't create view '%s'", searchView.Name)
case DELETE:
view, err := db.View(ctx, searchView.Name)
if e(err) {
return errors.Wrapf(err, "Couldn't find view '%s' to delete", searchView.Name)
}
err = view.Remove(ctx)
if !e(err) {
log.Printf("Deleted view '%s'\n", searchView.Name)
}
return errors.Wrapf(err, "Couldn't delete view '%s'", searchView.Name)
case MODIFY:
view, err := db.View(ctx, searchView.Name)
if e(err) {
return errors.Wrapf(err, "Couldn't find view '%s' to update", searchView.Name)
}
aView, err := view.ArangoSearchView()
if e(err) {
return errors.Wrapf(err, "Couldn't get ArangoSearchView '%s' to update", searchView.Name)
}
viewProperties := buildViewProperties(searchView)
err = aView.SetProperties(ctx, viewProperties)
if !e(err) {
log.Printf("Updated view '%s'\n", searchView.Name)
}
return errors.Wrapf(err, "Couldn't update SearchView '%s'", searchView.Name)
}
return nil
}
func buildViewProperties(searchView SearchView) driver.ArangoSearchViewProperties {
viewProperties := driver.ArangoSearchViewProperties{}
if searchView.CleanupIntervalStep != nil {
viewProperties.CleanupIntervalStep = searchView.CleanupIntervalStep
}
if searchView.CommitIntervalMsec != nil {
viewProperties.CommitInterval = searchView.CommitIntervalMsec
}
if searchView.ConsolidationIntervalMsec != nil {
viewProperties.ConsolidationInterval = searchView.ConsolidationIntervalMsec
}
if searchView.ConsolidationPolicy != nil {
policy := buildSearchConsolidationPolicy(searchView.ConsolidationPolicy)
viewProperties.ConsolidationPolicy = &policy
}
if len(searchView.SortFields) > 0 {
for _, field := range searchView.SortFields {
sortField := buildSortField(field)
viewProperties.PrimarySort = append(
viewProperties.PrimarySort,
sortField)
}
}
if len(searchView.Links) > 0 {
viewProperties.Links = driver.ArangoSearchLinks{}
for _, link := range searchView.Links {
viewProperties.Links[link.Name] = buildFields(link)
}
}
return viewProperties
}
func buildSortField(field SortField) driver.ArangoSearchPrimarySortEntry {
sortEntry := driver.ArangoSearchPrimarySortEntry{Field: field.Field}
if field.Ascending != nil {
direction := driver.ArangoSearchSortDirectionDesc
if *field.Ascending {
direction = driver.ArangoSearchSortDirectionAsc
}
sortEntry.Ascending = field.Ascending
sortEntry.Direction = &direction
}
return sortEntry
}
func buildFields(properties SearchElementProperties) driver.ArangoSearchElementProperties {
props := driver.ArangoSearchElementProperties{}
if properties.IncludeAllFields != nil {
props.IncludeAllFields = properties.IncludeAllFields
}
if properties.TrackListPositions != nil {
props.TrackListPositions = properties.TrackListPositions
}
if properties.StoreValues != nil {
if string(driver.ArangoSearchStoreValuesNone) == *properties.StoreValues {
props.StoreValues = driver.ArangoSearchStoreValuesNone
}
if string(driver.ArangoSearchStoreValuesID) == *properties.StoreValues {
props.StoreValues = driver.ArangoSearchStoreValuesID
}
}
if len(properties.Analyzers) > 0 {
props.Analyzers = properties.Analyzers
}
if len(properties.Fields) > 0 {
fields := driver.ArangoSearchFields{}
for _, field := range properties.Fields {
toUse := buildFields(field)
fields[field.Name] = toUse
}
props.Fields = fields
}
return props
}
func buildSearchConsolidationPolicy(consolidationPolicy *ConsolidationPolicy) driver.ArangoSearchConsolidationPolicy {
policy := driver.ArangoSearchConsolidationPolicy{}
switch consolidationPolicy.Type {
case string(driver.ArangoSearchConsolidationPolicyTypeTier):
policy.Type = driver.ArangoSearchConsolidationPolicyTypeTier
if val, ok := consolidationPolicy.Options["lookahead"]; ok {
lookahead := getInt(val)
policy.Lookahead = &lookahead
}
if val, ok := consolidationPolicy.Options["maxSegments"]; ok {
maxSegments := getInt(val)
policy.MaxSegments = &maxSegments
}
if val, ok := consolidationPolicy.Options["minSegments"]; ok {
minSegments := getInt(val)
policy.MinSegments = &minSegments
}
if val, ok := consolidationPolicy.Options["segmentsBytesFloor"]; ok {
segmentsBytesFloor := getInt(val)
policy.SegmentsBytesFloor = &segmentsBytesFloor
}
if val, ok := consolidationPolicy.Options["segmentsBytesMax"]; ok {
segmentsBytesMax := getInt(val)
policy.SegmentsBytesMax = &segmentsBytesMax
}
break
case string(driver.ArangoSearchConsolidationPolicyTypeBytesAccum):
policy.Type = driver.ArangoSearchConsolidationPolicyTypeBytesAccum
if val, ok := consolidationPolicy.Options["threshold"]; ok {
threshold := getFloat(val)
policy.Threshold = &threshold
}
}
return policy
}
var floatType = reflect.TypeOf(float64(0))
var intType = reflect.TypeOf(int64(0))
func getFloat(unk interface{}) float64 {
v := reflect.ValueOf(unk)
v = reflect.Indirect(v)
fv := v.Convert(floatType)
return fv.Float()
}
func getInt(unk interface{}) int64 {
v := reflect.ValueOf(unk)
v = reflect.Indirect(v)
fv := v.Convert(intType)
return fv.Int()
}