/
index.go
244 lines (219 loc) · 6.65 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
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
package mgo
import (
"context"
"errors"
"github.com/yaziming/mgo/bson"
"sort"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
ErrIndexNotFound = errors.New("index not found")
)
type Index struct {
Key []string // Index key fields; prefix name with dash (-) for descending order
Unique bool // Prevent two documents from having the same index key
//DropDups bool // deprecated since version 2.7.5 DropCollection documents with the same index key as a previously indexed one
Background bool // Build index in background and return immediately
Sparse bool // Only index documents containing the Key fields
PartialFilter bson.M // Partial index filter expression
// If ExpireAfter is defined the server will periodically delete
// documents with indexed time.Time older than the provided delta.
ExpireAfter time.Duration
// Name holds the stored index name. On creation if this field is unset it is
// computed by EnsureIndex based on the index key.
Name string
// Properties for spatial indexes.
//
// Min and Max were improperly typed as int when they should have been
// floats. To preserve backwards compatibility they are still typed as
// int and the following two fields enable reading and writing the same
// fields as float numbers. In mgo.v3, these fields will be dropped and
// Min/Max will become floats.
Minf, Maxf float64
BucketSize float64
Bits int
// Properties for text indexes.
DefaultLanguage string
LanguageOverride string
// Weights defines the significance of provided fields relative to other
// fields in a text index. The score for a given word in a document is derived
// from the weighted sum of the frequency for each of the indexed fields in
// that document. The default field weight is 1.
Weights map[string]int
// Collation defines the collation to use for the index.
Collation *Collation
}
func (i *Index) ToIndexModels() (model mongo.IndexModel, err error) {
indexOpts := options.Index().
SetBackground(i.Background)
if i.Bits > 0 {
indexOpts.SetBits(int32(i.Bits))
}
if i.Sparse {
indexOpts.SetSparse(i.Sparse)
}
if i.BucketSize > 0 {
indexOpts.SetBucketSize(int32(i.BucketSize))
}
if len(i.PartialFilter) > 0 {
indexOpts.SetPartialFilterExpression(i.PartialFilter)
}
if i.Maxf != i.Minf {
indexOpts.SetMax(i.Maxf)
indexOpts.SetMin(i.Minf)
}
if i.Unique {
indexOpts.SetUnique(i.Unique)
}
if i.Minf > 0 {
indexOpts.SetMin(i.Minf)
}
if i.ExpireAfter.Seconds() > 0 {
indexOpts.SetExpireAfterSeconds(int32(i.ExpireAfter.Seconds()))
}
if i.LanguageOverride != "" {
indexOpts.SetLanguageOverride(i.LanguageOverride)
}
if i.DefaultLanguage != "" {
indexOpts.SetDefaultLanguage(i.DefaultLanguage)
}
info, err := parseIndexKey(i.Key)
if err != nil {
return
}
weights := map[string]interface{}{}
if len(info.weights) > 0 {
for _, v := range info.weights {
weights[v.Key] = v.Value
}
}
if len(i.Weights) > 0 {
for k, v := range i.Weights {
weights[k] = v
}
}
if len(weights) > 0 {
indexOpts.SetWeights(weights)
}
if i.Name != "" {
indexOpts.SetName(i.Name)
} else {
indexOpts.SetName(info.name)
}
return mongo.IndexModel{
Keys: info.key,
Options: indexOpts,
}, nil
}
func simpleIndexKey(realKey bson.D) (key []string) {
for i := range realKey {
var vi int
field := realKey[i].Key
switch realKey[i].Value.(type) {
case int64:
vf, _ := realKey[i].Value.(int64)
vi = int(vf)
case int32:
vf, _ := realKey[i].Value.(int32)
vi = int(vf)
case float64:
vf, _ := realKey[i].Value.(float64)
vi = int(vf)
case float32:
vf, _ := realKey[i].Value.(float32)
vi = int(vf)
case string:
if vs, ok := realKey[i].Value.(string); ok {
key = append(key, "$"+vs+":"+field)
continue
}
case int:
vi = realKey[i].Value.(int)
}
if vi == 1 {
key = append(key, field)
continue
}
if vi == -1 {
key = append(key, "-"+field)
continue
}
panic("Got unknown index key type for field " + field)
}
return
}
type indexSpec struct {
Name, NS string
Key bson.D
Unique bool `bson:",omitempty"`
DropDups bool `bson:"dropDups,omitempty"`
Background bool `bson:",omitempty"`
Sparse bool `bson:",omitempty"`
Bits int `bson:",omitempty"`
Min float64 `bson:",omitempty"`
Max float64 `bson:",omitempty"`
BucketSize float64 `bson:"bucketSize,omitempty"`
ExpireAfter int `bson:"expireAfterSeconds,omitempty"`
Weights bson.D `bson:",omitempty"`
DefaultLanguage string `bson:"default_language,omitempty"`
LanguageOverride string `bson:"language_override,omitempty"`
TextIndexVersion int `bson:"textIndexVersion,omitempty"`
PartialFilterExpression bson.M `bson:"partialFilterExpression,omitempty"`
Collation *Collation `bson:"collation,omitempty"`
}
func (c *Collection) Indexes() (indexes []Index, err error) {
ctx := context.Background()
cursor, err := c.collection.Indexes().List(ctx)
if err != nil {
return
}
if err = cursor.Err(); err != nil {
return
}
for cursor.Next(ctx) {
var current indexSpec
_ = cursor.Decode(¤t)
indexes = append(indexes, indexFromSpec(current))
}
sort.Sort(indexSlice(indexes))
return
}
func indexFromSpec(spec indexSpec) Index {
index := Index{
Name: spec.Name,
Key: simpleIndexKey(spec.Key),
Unique: spec.Unique,
Background: spec.Background,
Sparse: spec.Sparse,
Minf: spec.Min,
Maxf: spec.Max,
Bits: spec.Bits,
BucketSize: spec.BucketSize,
DefaultLanguage: spec.DefaultLanguage,
LanguageOverride: spec.LanguageOverride,
ExpireAfter: time.Duration(spec.ExpireAfter) * time.Second,
Collation: spec.Collation,
PartialFilter: spec.PartialFilterExpression,
}
//if float64(int(spec.Min)) == spec.Min && float64(int(spec.Max)) == spec.Max {
// index.Min = int(spec.Min)
// index.Max = int(spec.Max)
//}
if spec.TextIndexVersion > 0 {
index.Key = make([]string, len(spec.Weights))
index.Weights = make(map[string]int)
for i, elem := range spec.Weights {
index.Key[i] = "$text:" + elem.Key
if w, ok := elem.Value.(int32); ok {
index.Weights[elem.Key] = int(w)
}
}
}
return index
}
type indexSlice []Index
func (idxs indexSlice) Len() int { return len(idxs) }
func (idxs indexSlice) Less(i, j int) bool { return idxs[i].Name < idxs[j].Name }
func (idxs indexSlice) Swap(i, j int) { idxs[i], idxs[j] = idxs[j], idxs[i] }