-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathMongoCollection+Indexes.swift
208 lines (199 loc) · 8.82 KB
/
MongoCollection+Indexes.swift
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
import MongoSwift
/// An extension of `MongoCollection` encapsulating index management capabilities.
extension MongoCollection {
/**
* Creates an index over the collection for the provided keys with the provided options.
*
* - Parameters:
* - keys: a `Document` specifing the keys for the index
* - indexOptions: Optional `IndexOptions` to use for the index
* - options: Optional `CreateIndexOptions` to use for the command
* - session: Optional `ClientSession` to use when executing this command
*
* - Returns: The name of the created index.
*
* - Throws:
* - `MongoError.WriteError` if an error occurs while performing the write.
* - `MongoError.CommandError` if an error occurs that prevents the command from executing.
* - `MongoError.InvalidArgumentError` if the options passed in form an invalid combination.
* - `MongoError.LogicError` if the provided session is inactive.
* - `EncodingError` if an error occurs while encoding the index specification or options.
*/
@discardableResult
public func createIndex(
_ keys: BSONDocument,
indexOptions: IndexOptions? = nil,
options: CreateIndexOptions? = nil,
session: ClientSession? = nil
) throws -> String {
try self.asyncColl.createIndex(
keys,
indexOptions: indexOptions,
options: options,
session: session?.asyncSession
)
.wait()
}
/**
* Creates an index over the collection for the provided keys with the provided options.
*
* - Parameters:
* - model: An `IndexModel` representing the keys and options for the index
* - options: Optional `CreateIndexOptions` to use for the command
* - session: Optional `ClientSession` to use when executing this command
*
* - Returns: The name of the created index.
*
* - Throws:
* - `MongoError.WriteError` if an error occurs while performing the write.
* - `MongoError.CommandError` if an error occurs that prevents the command from executing.
* - `MongoError.InvalidArgumentError` if the options passed in form an invalid combination.
* - `MongoError.LogicError` if the provided session is inactive.
* - `EncodingError` if an error occurs while encoding the index specification or options.
*/
@discardableResult
public func createIndex(
_ model: IndexModel,
options: CreateIndexOptions? = nil,
session: ClientSession? = nil
) throws -> String {
try self.asyncColl.createIndex(model, options: options, session: session?.asyncSession).wait()
}
/**
* Creates multiple indexes in the collection.
*
* - Parameters:
* - models: An `[IndexModel]` specifying the indexes to create
* - options: Optional `CreateIndexOptions` to use for the command
* - session: Optional `ClientSession` to use when executing this command
*
* - Returns: An `[String]` containing the names of all the indexes that were created.
*
* - Throws:
* - `MongoError.WriteError` if an error occurs while performing the write.
* - `MongoError.CommandError` if an error occurs that prevents the command from executing.
* - `MongoError.InvalidArgumentError` if the options passed in form an invalid combination.
* - `MongoError.LogicError` if the provided session is inactive.
* - `EncodingError` if an error occurs while encoding the index specifications or options.
*/
@discardableResult
public func createIndexes(
_ models: [IndexModel],
options: CreateIndexOptions? = nil,
session: ClientSession? = nil
) throws -> [String] {
try self.asyncColl.createIndexes(models, options: options, session: session?.asyncSession).wait()
}
/**
* Drops a single index from the collection by the index name.
*
* - Parameters:
* - name: The name of the index to drop
* - options: Optional `DropIndexOptions` to use for the command
* - session: Optional `ClientSession` to use when executing this command
*
* - Throws:
* - `MongoError.WriteError` if an error occurs while performing the command.
* - `MongoError.CommandError` if an error occurs that prevents the command from executing.
* - `MongoError.InvalidArgumentError` if the options passed in form an invalid combination.
* - `EncodingError` if an error occurs while encoding the options.
*/
public func dropIndex(
_ name: String,
options: DropIndexOptions? = nil,
session: ClientSession? = nil
) throws {
try self.asyncColl.dropIndex(name, options: options, session: session?.asyncSession).wait()
}
/**
* Attempts to drop a single index from the collection given the keys describing it.
*
* - Parameters:
* - keys: a `Document` containing the keys for the index to drop
* - options: Optional `DropIndexOptions` to use for the command
* - session: Optional `ClientSession` to use when executing this command
*
* - Throws:
* - `MongoError.WriteError` if an error occurs while performing the command.
* - `MongoError.CommandError` if an error occurs that prevents the command from executing.
* - `MongoError.InvalidArgumentError` if the options passed in form an invalid combination.
* - `MongoError.LogicError` if the provided session is inactive.
* - `EncodingError` if an error occurs while encoding the options.
*/
public func dropIndex(
_ keys: BSONDocument,
options: DropIndexOptions? = nil,
session: ClientSession? = nil
) throws {
try self.asyncColl.dropIndex(keys, options: options, session: session?.asyncSession).wait()
}
/**
* Attempts to drop a single index from the collection given an `IndexModel` describing it.
*
* - Parameters:
* - model: An `IndexModel` describing the index to drop
* - options: Optional `DropIndexOptions` to use for the command
* - session: Optional `ClientSession` to use when executing this command
*
* - Throws:
* - `MongoError.WriteError` if an error occurs while performing the command.
* - `MongoError.CommandError` if an error occurs that prevents the command from executing.
* - `MongoError.InvalidArgumentError` if the options passed in form an invalid combination.
* - `MongoError.LogicError` if the provided session is inactive.
* - `EncodingError` if an error occurs while encoding the options.
*/
public func dropIndex(
_ model: IndexModel,
options: DropIndexOptions? = nil,
session: ClientSession? = nil
) throws {
try self.asyncColl.dropIndex(model, options: options, session: session?.asyncSession).wait()
}
/**
* Drops all indexes in the collection.
*
* - Parameters:
* - options: Optional `DropIndexOptions` to use for the command
* - session: Optional `ClientSession` to use when executing this command
*
* - Throws:
* - `MongoError.WriteError` if an error occurs while performing the command.
* - `MongoError.CommandError` if an error occurs that prevents the command from executing.
* - `MongoError.InvalidArgumentError` if the options passed in form an invalid combination.
* - `MongoError.LogicError` if the provided session is inactive.
* - `EncodingError` if an error occurs while encoding the options.
*/
public func dropIndexes(
options: DropIndexOptions? = nil,
session: ClientSession? = nil
) throws {
try self.asyncColl.dropIndexes(options: options, session: session?.asyncSession).wait()
}
/**
* Retrieves a list of the indexes currently on this collection.
*
* - Parameters:
* - session: Optional `ClientSession` to use when executing this command
*
* - Returns: A `MongoCursor` over the `IndexModel`s.
*
* - Throws: `MongoError.LogicError` if the provided session is inactive.
*/
public func listIndexes(session: ClientSession? = nil) throws -> MongoCursor<IndexModel> {
let asyncCursor = try self.asyncColl.listIndexes(session: session?.asyncSession).wait()
return MongoCursor(wrapping: asyncCursor, client: self.client)
}
/**
* Retrieves a list of names of the indexes currently on this collection.
*
* - Parameters:
* - session: Optional `ClientSession` to use when executing this command
*
* - Returns: A `MongoCursor` over the index names.
*
* - Throws: `MongoError.LogicError` if the provided session is inactive.
*/
public func listIndexNames(session: ClientSession? = nil) throws -> [String] {
try self.asyncColl.listIndexNames(session: session?.asyncSession).wait()
}
}