-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindexes.go
241 lines (197 loc) · 5.22 KB
/
indexes.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
package lungo
import (
"context"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/256dpi/lungo/bsonkit"
"github.com/256dpi/lungo/mongokit"
)
var _ IIndexView = &IndexView{}
// IndexView wraps an Engine to be mongo compatible.
type IndexView struct {
engine *Engine
handle Handle
}
// CreateMany implements the IIndexView.CreateMany method.
func (v *IndexView) CreateMany(ctx context.Context, indexes []mongo.IndexModel, opts ...*options.CreateIndexesOptions) ([]string, error) {
// merge options
opt := options.MergeCreateIndexesOptions(opts...)
// assert supported options
assertOptions(opt, map[string]string{
"MaxTime": ignored,
})
// check filer
if len(indexes) == 0 {
panic("lungo: missing indexes")
}
// created indexes separately
var names []string
for _, index := range indexes {
name, err := v.CreateOne(ctx, index, opts...)
if err != nil {
return names, err
}
names = append(names, name)
}
return names, nil
}
// CreateOne implements the IIndexView.CreateOne method.
func (v *IndexView) CreateOne(ctx context.Context, index mongo.IndexModel, opts ...*options.CreateIndexesOptions) (string, error) {
// merge options
opt := options.MergeCreateIndexesOptions(opts...)
// assert supported options
assertOptions(opt, map[string]string{
"MaxTime": ignored,
})
// assert supported index options
if index.Options != nil {
assertOptions(index.Options, map[string]string{
"Background": ignored,
"ExpireAfterSeconds": supported,
"Name": supported,
"Unique": supported,
"Version": ignored,
"PartialFilterExpression": supported,
})
}
// transform key
key, err := bsonkit.Transform(index.Keys)
if err != nil {
return "", err
}
// get expiry
var expiry time.Duration
if index.Options != nil && index.Options.ExpireAfterSeconds != nil {
if *index.Options.ExpireAfterSeconds == 0 {
expiry = time.Nanosecond
} else {
expiry = time.Duration(*index.Options.ExpireAfterSeconds) * time.Second
}
}
// get name
var name string
if index.Options != nil && index.Options.Name != nil {
name = *index.Options.Name
}
// get unique
var unique bool
if index.Options != nil && index.Options.Unique != nil {
unique = *index.Options.Unique
}
// get partial
var partial bsonkit.Doc
if index.Options != nil && index.Options.PartialFilterExpression != nil {
partial, err = bsonkit.Transform(index.Options.PartialFilterExpression)
if err != nil {
return "", err
}
}
// begin transaction
txn, err := v.engine.Begin(ctx, true)
if err != nil {
return "", err
}
// ensure abortion
defer v.engine.Abort(txn)
// create index
name, err = txn.CreateIndex(v.handle, name, mongokit.IndexConfig{
Key: key,
Unique: unique,
Partial: partial,
Expiry: expiry,
})
if err != nil {
return "", err
}
// commit transaction
err = v.engine.Commit(txn)
if err != nil {
return "", err
}
return name, nil
}
// DropAll implements the IIndexView.DropAll method.
func (v *IndexView) DropAll(ctx context.Context, opts ...*options.DropIndexesOptions) (bson.Raw, error) {
// merge options
opt := options.MergeDropIndexesOptions(opts...)
// assert supported options
assertOptions(opt, map[string]string{
"MaxTime": ignored,
})
// begin transaction
txn, err := v.engine.Begin(ctx, true)
if err != nil {
return nil, err
}
// ensure abortion
defer v.engine.Abort(txn)
// drop all indexes
err = txn.DropIndex(v.handle, "")
if err != nil {
return nil, err
}
// commit transaction
err = v.engine.Commit(txn)
if err != nil {
return nil, err
}
return nil, nil
}
// DropOne implements the IIndexView.DropOne method.
func (v *IndexView) DropOne(ctx context.Context, name string, opts ...*options.DropIndexesOptions) (bson.Raw, error) {
// merge options
opt := options.MergeDropIndexesOptions(opts...)
// assert supported options
assertOptions(opt, map[string]string{
"MaxTime": ignored,
})
// check name
if name == "" || name == "*" {
panic("lungo: invalid index name")
}
// begin transaction
txn, err := v.engine.Begin(ctx, true)
if err != nil {
return nil, err
}
// ensure abortion
defer v.engine.Abort(txn)
// drop all indexes
err = txn.DropIndex(v.handle, name)
if err != nil {
return nil, err
}
// commit transaction
err = v.engine.Commit(txn)
if err != nil {
return nil, err
}
return nil, nil
}
// List implements the IIndexView.List method.
func (v *IndexView) List(ctx context.Context, opts ...*options.ListIndexesOptions) (ICursor, error) {
// merge options
opt := options.MergeListIndexesOptions(opts...)
// assert supported options
assertOptions(opt, map[string]string{
"BatchSize": ignored,
"MaxTime": ignored,
})
// begin transaction
txn, err := v.engine.Begin(ctx, false)
if err != nil {
return nil, err
}
// list indexes
list, err := txn.ListIndexes(v.handle)
if err != nil {
return nil, err
}
return &Cursor{list: list}, nil
}
// ListSpecifications implements the IIndexView.ListSpecifications method.
func (v *IndexView) ListSpecifications(context.Context, ...*options.ListIndexesOptions) ([]*mongo.IndexSpecification, error) {
panic("lungo: not implemented")
}