-
Notifications
You must be signed in to change notification settings - Fork 5
/
admmgmt.nim
244 lines (224 loc) · 9.13 KB
/
admmgmt.nim
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
import std/[strformat, sequtils]
from std/sugar import `=>`
import ../core/[types, bson, wire, utils]
import multisock
## Administration Commands
## ***********************
##
## The actual APIs documentation can be referred to `Mongo command`_
## documentation for better understanding of each API and its return
## value or BsonDocument. Any read commands will return either
## ``seq[BsonBase]`` or ``seq[BsonDocument]`` for fine tune handling
## query result.
##
## For any write/update/modify/delete commands, it will usually return
## tuple of bool success and string reason or bool of success and int
## n of affected documents.
##
## All APIs are async.
##
## .. _Mongo command: https://docs.mongodb.com/manual/reference/command/nav-administration/
proc create*(db: Database[AsyncSocket], name: string, capsizemax = (false, 0, 0),
storageEngine = bsonNull(),
validator = bsonNull(), validationLevel = "strict", validationAction = "error",
indexOptionDefaults = bsonNull(), viewOn = "",
pipeline = bsonArray(), collation = bsonNull(), writeConcern = bsonNull(),
expireAfterSeconds = 0, timeseries = bsonNull()):
Future[WriteResult] {.multisock.} =
var q = bson({
create: name,
})
if capsizemax[0]:
q["capped"] = true
q["size"] = capsizemax[1]
q["max"] = capsizemax[2]
q.addOptional("timeseries", timeseries)
if expireAfterSeconds > 0: q["expireAfterSeconds"] = expireAfterSeconds
q.addOptional("storageEngine", storageEngine)
q.addOptional("validator", validator)
q["validationLevel"] = validationLevel
q["validationAction"] = validationAction
q.addOptional("indexOptionDefaults", indexOptionDefaults)
if viewOn != "":
q["viewOn"] = viewOn
if pipeline.ofArray.len != 0:
q["pipeline"] = pipeline
q.addOptional("collation", collation)
q.addWriteConcern(db, writeConcern)
result = await db.proceed(q, cmd = ckWrite)
proc createIndexes*(db: Database[AsyncSocket], coll: string, indexes: BsonBase,
writeConcern = bsonNull(), commitQuorum = bsonNull(), comment = bsonNull()):
Future[WriteResult]{.multisock.} =
var q = bson({
createIndexes: coll,
indexes: indexes,
})
q.addWriteConcern(db, writeConcern)
q.addOptional("commitQuorum", commitQuorum)
q.addOptional("comment", comment)
result = await db.proceed(q, cmd = ckWrite)
proc dropCollection*(db: Database[AsyncSocket], coll: string, wt = bsonNull(),
comment = bsonNull()): Future[WriteResult]{.multisock.} =
var q = bson({ drop: coll })
q.addWriteConcern(db, wt)
q.addOptional("comment", comment)
result = await db.proceed(q, cmd = ckWrite)
proc dropDatabase*(db: Database[AsyncSocket], wt = bsonNull(), comment = bsonNull()):
Future[WriteResult]{.multisock.} =
var q = bson({ dropDatabase: 1 })
q.addWriteConcern(db, wt)
q.addOptional("comment", comment)
result = await db.proceed(q, cmd = ckWrite)
proc dropIndexes*(db: Database[AsyncSocket], coll: string, indexes: BsonBase,
wt = bsonNull(), comment = bsonNull()): Future[WriteResult] {.multisock.} =
var q = bson({
dropIndexes: coll,
index: indexes,
})
q.addWriteConcern(db, wt)
result = await db.proceed(q, cmd = ckWrite)
proc listCollections*(db: Database[AsyncSocket], dbname = "", filter = bsonNull(),
nameonly = false, authorizedCollections = false, comment = bsonNull()):
Future[seq[BsonBase]] {.multisock.} =
var q = bson({ listCollections: 1})
if not filter.isNil:
q["filter"] = filter
q.addConditional("nameOnly", nameonly)
q.addConditional("authorizedCollections", authorizedCollections)
q.addOptional("comment", comment)
let compression = if db.db.compressions.len > 0: db.db.compressions[0]
else: cidNoop
let reply = await sendops(q, db, dbname, cmd = ckRead, compression = compression)
let (success, reason) = check reply
if not success:
echo reason
return
let res = reply.documents[0]
if res.ok:
result = res["cursor"]["firstBatch"].ofArray
proc listCollectionNames*(db: Database[AsyncSocket], dbname = ""):
Future[seq[string]] {.multisock.} =
for b in await db.listCollections(dbname):
var name: string = b["name"]
result.add name.move
# proc listDatabases*(db: Mongo | Database, filter = bsonNull(), nameonly = false,
proc listDatabases*(db: Database[AsyncSocket], filter = bsonNull(), nameonly = false,
authorizedCollections = false, comment = bsonNull()): Future[seq[BsonBase]] {.multisock.} =
var q = bson({ listDatabases: 1 })
q.addOptional("filter", filter)
q.addConditional("nameOnly", nameonly)
q.addConditional("authorizedCollections", authorizedCollections)
q.addOptional("comment", comment)
# when db is Mongo:
# let dbm = db["admin"]
# else:
# let dbm = db
let dbm = db
let reply = await sendops(q, dbm, "admin", cmd = ckRead)
let (success, reason) = check reply
if not success:
echo reason
return
let res = reply.documents[0]
if res.ok:
when not defined(release): echo "All database size: ", res["totalSize"]
result = res["databases"]
else:
echo res.errmsg
# proc listDatabaseNames*(db: Mongo | Database): Future[seq[string]] {.multisock.} =
proc listDatabaseNames*(db: Database[AsyncSocket]): Future[seq[string]] {.multisock.} =
for d in await listDatabases(db):
result.add d["name"]
proc listIndexes*(db: Database[AsyncSocket], coll: string, comment = bsonNull()):
Future[seq[BsonBase]]{.multisock.} =
var q = bson({ listIndexes: coll })
q.addOptional("comment", comment)
let compression = if db.db.compressions.len > 0: db.db.compressions[0]
else: cidNoop
let reply = await sendops(q, db, cmd = ckRead, compression = compression)
let (success, reason) = check reply
if not success:
echo reason
return
let res = reply.documents[0]
if res.ok:
result = res["cursor"]["firstBatch"]
proc renameCollection*(db: Database[AsyncSocket], `from`, to: string, wt = bsonNull(),
comment = bsonNull()):
Future[WriteResult] {.multisock.} =
let source = &"{db.name}.{`from`}"
let dest = &"{db.name}.{to}"
var q = bson({
renameCollection: source,
to: dest,
dropTarget: false,
})
q.addWriteConcern(db, wt)
q.addOptional("comment", comment)
result = await db.proceed(q, "admin", cmd = ckWrite)
# proc shutdown*(db: Mongo | Database, force = false, timeout = 10,
proc shutdown*(db: Database[AsyncSocket], force = false, timeout = 10,
comment = bsonNull()): Future[WriteResult] {.multisock.} =
var q = bson({ shutdown: 1, force: force, timeoutSecs: timeout })
q.addOptional("comment", comment)
let mdb = db
try:
result = await mdb.proceed(q, "admin", cmd = ckWrite)
except IOError:
result = WriteResult(
success: true,
reason: getCurrentExceptionMsg(),
kind: wkSingle
)
proc shutdown*(m: Mongo[AsyncSocket], force = false, timeout = 10,
comment = bsonNull()): Future[WriteResult] {.multisock.} =
let db = m["admin"]
result = await db.shutdown(force, timeout, comment)
proc currentOp*(db: Database[AsyncSocket], opt = bson()): Future[BsonDocument]{.multisock.} =
var q = bson({ currentOp: 1})
for k, v in opt:
q[k] = v
let compression = if db.db.compressions.len > 0: db.db.compressions[0]
else: cidNoop
let reply = await sendops(q, db, "admin", cmd = ckRead, compression = compression)
let (success, reason) = check reply
if not success:
echo reason
return
result = reply.documents[0]
proc killOp*(db: Database[AsyncSocket], opid: int32, comment = bsonNull()):
Future[WriteResult] {.multisock.} =
var q = bson({ killerOp: 1, op: opid })
q.addConditional("comment", comment)
result = await db.proceed(q, "admin", cmd = ckWrite)
# template sendEpilogue(db: Database[AsyncSocket], q: BsonDocument, mode: CommandKind): untyped =
proc sendEpilogue(db: Database[AsyncSocket], q: BsonDocument, mode: CommandKind): Future[BsonDocument] {.multisock.} =
let compression = if db.db.compressions.len > 0: db.db.compressions[0]
else: cidNoop
let reply = await sendops(q, db, "admin", cmd = mode, compression = compression)
let (success, reason) = check reply
if not success:
echo reason
result = bsonNull()
return
result = reply.documents[0]
proc killCursors*(db: Database[AsyncSocket], collname: string, cursorIds: seq[int64]):
Future[BsonDocument] {.multisock.} =
let q = bson({ killCursors: collname, cursors: cursorIds.map toBson })
result = await sendEpilogue(db, q, ckWrite)
proc setDefaultRWConcern*(db: Database[AsyncSocket], defaultReadConcern = bsonNull(),
defaultWriteConcern = bsonNull(), wt = bsonNull(), comment = bsonNull()):
Future[BsonDocument]{.multisock.} =
if all([defaultReadConcern, defaultWriteConcern].map(isNil), (x) => x ):
result = bsonNull()
return
var q = bson { setDefaultRWConcern: 1 }
q.addOptional("defaultReadConcern", defaultReadConcern)
q.addOptional("defaultWriteConcern", defaultWriteConcern)
q.addOptional("writeConcern", wt)
q.addOptional("comment", comment)
result = await sendEpilogue(db, q, ckWrite)
proc getDefaultReadConcern*(db: Database[AsyncSocket], inMemory = false, comment = bsonNull()):
Future[BsonDocument]{.multisock.} =
let q = bson { getDefaultReadConcern: 1, inMemory: inMemory, comment: comment}
result = await sendEpilogue(db, q, ckRead)