forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_definition.go
120 lines (101 loc) · 3.52 KB
/
index_definition.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
package ravendb
type IndexDefinition struct {
Name string `json:"Name"`
Priority IndexPriority `json:"Priority,omitempty"`
LockMode IndexLockMode `json:"LockMode,omitempty"`
AdditionalSources map[string]string `json:"AdditionalSources"`
Maps []string `json:"Maps"`
Reduce *string `json:"Reduce"`
Fields map[string]*IndexFieldOptions `json:"Fields"`
Configuration IndexConfiguration `json:"Configuration"`
IndexType IndexType `json:"Type"`
//TBD 4.1 bool testIndex;
OutputReduceToCollection *string `json:"OutputReduceToCollection"`
PatternReferencesCollectionName *string `json:"PatternReferencesCollectionName"`
PatternForOutputReduceToCollectionReferences *string `json:"PatternForOutputReduceToCollectionReferences"`
}
func toStrPtr(s string) *string {
if s == "" {
return nil
}
return &s
}
func NewIndexDefinition() *IndexDefinition {
res := &IndexDefinition{
Configuration: NewIndexConfiguration(),
// Note: initializing those is possibly wasteful but
// it's needed to serialize them like Java, as {} and not null
Fields: make(map[string]*IndexFieldOptions),
AdditionalSources: make(map[string]string),
}
return res
}
func (d *IndexDefinition) GetAdditionalSources() map[string]string {
if d.AdditionalSources == nil {
d.AdditionalSources = make(map[string]string)
}
return d.AdditionalSources
}
func (d *IndexDefinition) SetAdditionalSources(additionalSources map[string]string) {
// preserve additionalSources being always non-nil
// so that JSON serializes it as {} and not 'null'
if additionalSources == nil {
if len(d.AdditionalSources) == 0 {
return
}
additionalSources = make(map[string]string)
}
d.AdditionalSources = additionalSources
}
func (d *IndexDefinition) String() string {
return d.Name
}
func (d *IndexDefinition) GetFields() map[string]*IndexFieldOptions {
if d.Fields == nil {
d.Fields = make(map[string]*IndexFieldOptions)
}
return d.Fields
}
func (d *IndexDefinition) GetConfiguration() IndexConfiguration {
if d.Configuration == nil {
d.Configuration = NewIndexConfiguration()
}
return d.Configuration
}
func (d *IndexDefinition) SetConfiguration(configuration IndexConfiguration) {
d.Configuration = configuration
}
// Note: this must be called after finishing building index definition to set IndexType
// In Java it's calculated on demand via getType
func (d *IndexDefinition) updateIndexTypeAndMaps() {
if d.IndexType == "" || d.IndexType == IndexTypeNone {
d.IndexType = d.detectStaticIndexType()
}
d.Maps = stringArrayRemoveDuplicates(d.Maps)
}
func (d *IndexDefinition) GetType() IndexType {
if d.IndexType == "" || d.IndexType == IndexTypeNone {
d.IndexType = d.detectStaticIndexType()
}
return d.IndexType
}
func (d *IndexDefinition) SetType(indexType IndexType) {
if indexType == "" {
indexType = IndexTypeNone
}
d.IndexType = indexType
}
func (d *IndexDefinition) detectStaticIndexType() IndexType {
if d.Reduce == nil || stringIsBlank(*d.Reduce) {
return IndexTypeMap
}
return IndexTypeMapReduce
}
//TBD 4.1 bool isTestIndex()
//TBD 4.1 setTestIndex(bool testIndex)
func (d *IndexDefinition) GetOutputReduceToCollection() *string {
return d.OutputReduceToCollection
}
func (d *IndexDefinition) SetOutputReduceToCollection(outputReduceToCollection string) {
d.OutputReduceToCollection = toStrPtr(outputReduceToCollection)
}