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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! Read operations for query executor
//!
//! This module contains all read operations including:
//! - find, findOne
//! - count, estimatedDocumentCount
//! - distinct
use futures::stream::TryStreamExt;
use mongodb::Collection;
use mongodb::bson::{Bson, Document};
use tracing::{debug, info};
use crate::error::{ExecutionError, Result};
use crate::parser::{FindOptions, QueryMode};
use super::super::export::streaming::FindStreamingQuery;
use super::super::killable::run_killable_command;
use super::super::result::{ExecutionResult, ExecutionStats, ResultData};
/// Read operations implementation
impl super::QueryExecutor {
/// Execute findOne command
///
/// # Arguments
/// * `collection` - Collection name
/// * `filter` - Query filter
/// * `options` - Find options
///
/// # Returns
/// * `Result<ExecutionResult>` - Query result with single document
pub(super) async fn execute_find_one(
&self,
collection: String,
filter: Document,
options: FindOptions,
) -> Result<ExecutionResult> {
debug!(
"Executing findOne on collection '{}' with filter: {:?}",
collection, filter
);
let db = self.context.get_database().await?;
let coll: Collection<Document> = db.collection(&collection);
// Build find options
let mut find_options = mongodb::options::FindOptions::default();
find_options.projection = options.projection;
find_options.sort = options.sort;
find_options.limit = Some(1); // FindOne always limits to 1
// Execute find query
let mut cursor = coll.find(filter).with_options(find_options).await?;
// Get first document
let doc = cursor.try_next().await?;
match doc {
Some(document) => Ok(ExecutionResult {
success: true,
data: ResultData::Document(document),
stats: ExecutionStats {
execution_time_ms: 0,
documents_returned: 1,
documents_affected: None,
},
error: None,
}),
None => Ok(ExecutionResult {
success: true,
data: ResultData::None,
stats: ExecutionStats {
execution_time_ms: 0,
documents_returned: 0,
documents_affected: None,
},
error: None,
}),
}
}
/// Execute find command
pub(super) async fn execute_find(
&self,
collection: String,
filter: Document,
options: FindOptions,
mode: QueryMode,
) -> Result<ExecutionResult> {
match mode {
QueryMode::Interactive { batch_size } => {
self.execute_find_interactive(collection, filter, options, batch_size).await
}
QueryMode::Streaming { batch_size } => {
self.execute_find_streaming(collection, filter, options, batch_size).await
}
}
}
/// Execute find in streaming mode for export
pub(super) async fn execute_find_streaming(
&self,
collection: String,
filter: Document,
options: FindOptions,
batch_size: u32,
) -> Result<ExecutionResult> {
info!(
"Executing find (streaming) on collection '{}' with filter: {:?}",
collection, filter
);
let client = self.context.get_client().await?;
let client_id = self.context.get_client_id();
let cancel_token = self.context.get_cancel_token();
let db_name = self.context.get_current_database().await;
// Execute find with killOp support
let cursor = run_killable_command(
client,
client_id,
cancel_token,
|client, handle| {
let db_name = db_name.clone();
let collection = collection.clone();
let filter = filter.clone();
let options = options.clone();
Box::pin(async move {
let coll: Collection<Document> = client
.database(&db_name)
.collection(&collection);
// Build MongoDB find options
let mut find_opts = mongodb::options::FindOptions::default();
// CRITICAL: Set comment for killOp support
find_opts.comment = Some(Bson::String(handle.comment().to_string()));
// Apply user-specified limit if any
if let Some(limit) = options.limit {
find_opts.limit = Some(limit);
debug!("Applied limit: {}", limit);
}
// Apply skip if specified
if let Some(skip) = options.skip {
find_opts.skip = Some(skip);
debug!("Applied skip: {}", skip);
}
if let Some(ref sort) = options.sort {
find_opts.sort = Some(sort.clone());
debug!("Applied sort");
}
if let Some(ref projection) = options.projection {
find_opts.projection = Some(projection.clone());
debug!("Applied projection");
}
// Execute query and create cursor
let cursor = coll
.find(filter)
.with_options(find_opts)
.await
.map_err(|e| ExecutionError::QueryFailed(e.to_string()))?;
Ok(cursor)
})
},
)
.await?;
// Create streaming query wrapper
let streaming_query = FindStreamingQuery::new_find(cursor, batch_size);
Ok(ExecutionResult {
success: true,
data: ResultData::Stream(Box::new(streaming_query)),
stats: ExecutionStats {
execution_time_ms: 0,
documents_returned: 0,
documents_affected: None,
},
error: None,
})
}
/// Execute find in interactive mode with pagination
pub(super) async fn execute_find_interactive(
&self,
collection: String,
filter: Document,
options: FindOptions,
batch_size: u32,
) -> Result<ExecutionResult> {
info!(
"Executing find on collection '{}' with filter: {:?}",
collection, filter
);
// Clear any previous cursor state - this is a new query
self.context.shared_state.clear_cursor().await;
let client = self.context.get_client().await?;
let client_id = self.context.get_client_id();
let cancel_token = self.context.get_cancel_token();
let db_name = self.context.get_current_database().await;
// Execute find with killOp support
let mut cursor = run_killable_command(
client,
client_id,
cancel_token,
|client, handle| {
let db_name = db_name.clone();
let collection = collection.clone();
let filter = filter.clone();
let options = options.clone();
Box::pin(async move {
let coll: Collection<Document> = client
.database(&db_name)
.collection(&collection);
// Build MongoDB find options
let mut find_opts = mongodb::options::FindOptions::default();
// CRITICAL: Set comment for killOp support
find_opts.comment = Some(Bson::String(handle.comment().to_string()));
// Use provided batch size
find_opts.batch_size = Some(batch_size);
debug!("Applied batch_size: {}", batch_size);
// Apply user-specified limit if any
if let Some(limit) = options.limit {
find_opts.limit = Some(limit);
debug!("Applied limit: {}", limit);
}
// Apply skip if specified
if let Some(skip) = options.skip {
find_opts.skip = Some(skip);
debug!("Applied skip: {}", skip);
}
if let Some(ref sort) = options.sort {
find_opts.sort = Some(sort.clone());
debug!("Applied sort");
}
if let Some(ref projection) = options.projection {
find_opts.projection = Some(projection.clone());
debug!("Applied projection");
}
// Execute query and create cursor
let cursor = coll
.find(filter)
.with_options(find_opts)
.await
.map_err(|e| ExecutionError::QueryFailed(e.to_string()))?;
Ok(cursor)
})
},
)
.await?;
// Fetch first batch of documents
let mut documents = Vec::new();
let mut count = 0;
while count < batch_size as usize {
match cursor
.try_next()
.await
.map_err(|e| ExecutionError::CursorError(e.to_string()))?
{
Some(doc) => {
documents.push(doc);
count += 1;
}
None => break, // No more documents
}
}
info!("Retrieved {} documents in first batch", count);
// Check if there might be more documents
// If we got a full batch, there's likely more
let has_more = count == batch_size as usize;
// If there are more documents, save the live cursor for pagination
if has_more {
let mut cursor_state = crate::repl::CursorState::new(
collection.clone(),
cursor, // Store the LIVE cursor
batch_size,
);
cursor_state.update_retrieved(count);
self.context.shared_state.set_cursor(cursor_state).await;
debug!(
"Saved live cursor for pagination with {} documents retrieved",
count
);
}
// Create result with pagination info
let result_data = if has_more {
ResultData::DocumentsWithPagination {
documents,
has_more: true,
displayed: count,
}
} else {
ResultData::Documents(documents)
};
Ok(ExecutionResult {
success: true,
data: result_data,
stats: ExecutionStats {
execution_time_ms: 0, // Will be set by caller
documents_returned: count,
documents_affected: None,
},
error: None,
})
}
/// Execute count operation
///
/// # Arguments
/// * `collection` - Collection name
/// * `filter` - Optional query filter
///
/// # Returns
/// * `Result<ExecutionResult>` - Count result or error
pub(super) async fn execute_count(
&self,
collection: String,
filter: Option<Document>,
) -> Result<ExecutionResult> {
info!("Executing count on collection '{}'", collection);
let client = self.context.get_client().await?;
let client_id = self.context.get_client_id();
let cancel_token = self.context.get_cancel_token();
let db_name = self.context.get_current_database().await;
let count = run_killable_command(
client,
client_id,
cancel_token,
|client, handle| {
let db_name = db_name.clone();
let collection = collection.clone();
let filter = filter.clone();
Box::pin(async move {
let coll: Collection<Document> = client
.database(&db_name)
.collection(&collection);
let count = if let Some(f) = filter {
use mongodb::options::CountOptions;
let mut options = CountOptions::default();
// CRITICAL: Set comment for killOp support
options.comment = Some(Bson::String(handle.comment().to_string()));
coll.count_documents(f)
.with_options(options)
.await
.map_err(|e| ExecutionError::QueryFailed(e.to_string()))?
} else {
use mongodb::options::EstimatedDocumentCountOptions;
let mut options = EstimatedDocumentCountOptions::default();
// CRITICAL: Set comment for killOp support
options.comment = Some(Bson::String(handle.comment().to_string()));
coll.estimated_document_count()
.with_options(options)
.await
.map_err(|e| ExecutionError::QueryFailed(e.to_string()))?
};
info!("Count result: {}", count);
Ok(count)
})
},
)
.await?;
Ok(ExecutionResult {
success: true,
data: ResultData::Count(count),
stats: ExecutionStats {
execution_time_ms: 0,
documents_returned: 0,
documents_affected: Some(count),
},
error: None,
})
}
/// Execute estimatedDocumentCount command
///
/// # Arguments
/// * `collection` - Collection name
///
/// # Returns
/// * `Result<ExecutionResult>` - Count result or error
pub(super) async fn execute_estimated_document_count(
&self,
collection: String,
) -> Result<ExecutionResult> {
debug!(
"Executing estimatedDocumentCount on collection '{}'",
collection
);
let db = self.context.get_database().await?;
let coll: Collection<Document> = db.collection(&collection);
let count = coll
.estimated_document_count()
.await
.map_err(|e| ExecutionError::QueryFailed(e.to_string()))?;
info!("Estimated document count: {}", count);
Ok(ExecutionResult {
success: true,
data: ResultData::Count(count),
stats: ExecutionStats {
execution_time_ms: 0,
documents_returned: 0,
documents_affected: Some(count),
},
error: None,
})
}
/// Execute distinct command
///
/// # Arguments
/// * `collection` - Collection name
/// * `field` - Field to get distinct values for
/// * `filter` - Optional query filter
///
/// # Returns
/// * `Result<ExecutionResult>` - Distinct values result or error
pub(super) async fn execute_distinct(
&self,
collection: String,
field: String,
filter: Option<Document>,
) -> Result<ExecutionResult> {
debug!(
"Executing distinct on collection '{}' for field '{}'",
collection, field
);
let client = self.context.get_client().await?;
let client_id = self.context.get_client_id();
let cancel_token = self.context.get_cancel_token();
let db_name = self.context.get_current_database().await;
let values = run_killable_command(
client,
client_id,
cancel_token,
|client, handle| {
let db_name = db_name.clone();
let collection = collection.clone();
let field = field.clone();
let filter = filter.clone();
Box::pin(async move {
let coll: Collection<Document> = client
.database(&db_name)
.collection(&collection);
use mongodb::options::DistinctOptions;
let mut options = DistinctOptions::default();
// CRITICAL: Set comment for killOp support
options.comment = Some(Bson::String(handle.comment().to_string()));
let filter_doc = filter.unwrap_or_else(|| Document::new());
let values = coll
.distinct(&field, filter_doc)
.with_options(options)
.await
.map_err(|e| ExecutionError::QueryFailed(e.to_string()))?;
Ok(values)
})
},
)
.await?;
// Convert Bson values to Documents for display
let count = values.len();
let docs: Vec<Document> = values
.into_iter()
.map(|v| {
let mut doc = Document::new();
doc.insert("value", v);
doc
})
.collect();
info!("Distinct returned {} unique values", count);
Ok(ExecutionResult {
success: true,
data: ResultData::Documents(docs),
stats: ExecutionStats {
execution_time_ms: 0,
documents_returned: count,
documents_affected: None,
},
error: None,
})
}
}