@@ -39,17 +39,17 @@ public static ExecutableQuery<TDocument, TOutput, TResult> Create<TDocument, TOu
39
39
AstPipeline unoptimizedPipeline ,
40
40
IExecutableQueryFinalizer < TOutput , TResult > finalizer )
41
41
{
42
- var pipeline = AstPipelineOptimizer . Optimize ( unoptimizedPipeline ) ;
42
+ var optimizedPipeline = AstPipelineOptimizer . Optimize ( unoptimizedPipeline ) ;
43
43
return provider . Collection == null ?
44
- new ExecutableQuery < TDocument , TOutput , TResult > ( provider . Database , provider . Options , unoptimizedPipeline , pipeline , finalizer ) :
45
- new ExecutableQuery < TDocument , TOutput , TResult > ( provider . Collection , provider . Options , unoptimizedPipeline , pipeline , finalizer ) ;
44
+ new ExecutableQuery < TDocument , TOutput , TResult > ( provider . Database , provider . Options , optimizedPipeline , finalizer ) :
45
+ new ExecutableQuery < TDocument , TOutput , TResult > ( provider . Collection , provider . Options , optimizedPipeline , finalizer ) ;
46
46
}
47
47
}
48
48
49
49
internal abstract class ExecutableQuery < TDocument >
50
50
{
51
+ public abstract BsonDocument [ ] LoggedStages { get ; }
51
52
public abstract AstPipeline Pipeline { get ; }
52
- public abstract AstPipeline UnoptimizedPipeline { get ; }
53
53
}
54
54
55
55
internal abstract class ExecutableQuery < TDocument , TResult > : ExecutableQuery < TDocument >
@@ -64,71 +64,69 @@ internal class ExecutableQuery<TDocument, TOutput, TResult> : ExecutableQuery<TD
64
64
private readonly IMongoCollection < TDocument > _collection ;
65
65
private readonly IMongoDatabase _database ;
66
66
private readonly IExecutableQueryFinalizer < TOutput , TResult > _finalizer ;
67
+ private BsonDocument [ ] _loggedStages = null ;
67
68
private readonly AggregateOptions _options ;
68
69
private readonly AstPipeline _pipeline ;
69
- private readonly AstPipeline _unoptimizedPipeline ;
70
70
71
71
// constructors
72
72
public ExecutableQuery (
73
73
IMongoCollection < TDocument > collection ,
74
74
AggregateOptions options ,
75
- AstPipeline unoptimizedPipeline ,
76
75
AstPipeline pipeline ,
77
76
IExecutableQueryFinalizer < TOutput , TResult > finalizer )
78
- : this ( options , unoptimizedPipeline , pipeline , finalizer )
77
+ : this ( options , pipeline , finalizer )
79
78
{
80
79
_collection = Ensure . IsNotNull ( collection , nameof ( collection ) ) ;
81
80
}
82
81
83
82
public ExecutableQuery (
84
83
IMongoDatabase database ,
85
84
AggregateOptions options ,
86
- AstPipeline unoptimizedPipeline ,
87
85
AstPipeline pipeline ,
88
86
IExecutableQueryFinalizer < TOutput , TResult > finalizer )
89
- : this ( options , unoptimizedPipeline , pipeline , finalizer )
87
+ : this ( options , pipeline , finalizer )
90
88
{
91
89
_database = Ensure . IsNotNull ( database , nameof ( database ) ) ;
92
90
}
93
91
94
92
private ExecutableQuery (
95
93
AggregateOptions options ,
96
- AstPipeline unoptimizedPipeline ,
97
94
AstPipeline pipeline ,
98
95
IExecutableQueryFinalizer < TOutput , TResult > finalizer )
99
96
{
100
97
_options = options ;
101
- _unoptimizedPipeline = unoptimizedPipeline ;
102
98
_pipeline = pipeline ;
103
99
_finalizer = finalizer ;
104
100
}
105
101
106
102
// public properties
103
+ public override BsonDocument [ ] LoggedStages => _loggedStages ;
107
104
public override AstPipeline Pipeline => _pipeline ;
108
- public override AstPipeline UnoptimizedPipeline => _unoptimizedPipeline ;
109
105
110
106
// public methods
111
107
public override TResult Execute ( IClientSessionHandle session , CancellationToken cancellationToken )
112
108
{
109
+ _loggedStages = RenderPipeline ( ) ;
113
110
var cursor = ( _collection , session ) switch
114
111
{
115
- ( null , null ) => _database . Aggregate ( CreateDatabasePipelineDefinition ( ) , _options , cancellationToken ) ,
116
- ( null , _ ) => _database . Aggregate ( session , CreateDatabasePipelineDefinition ( ) , _options , cancellationToken ) ,
117
- ( _, null ) => _collection . Aggregate ( CreateCollectionPipelineDefinition ( ) , _options , cancellationToken ) ,
118
- ( _, _) => _collection . Aggregate ( session , CreateCollectionPipelineDefinition ( ) , _options , cancellationToken )
112
+ ( null , null ) => _database . Aggregate ( CreateDatabasePipelineDefinition ( _loggedStages ) , _options , cancellationToken ) ,
113
+ ( null , _ ) => _database . Aggregate ( session , CreateDatabasePipelineDefinition ( _loggedStages ) , _options , cancellationToken ) ,
114
+ ( _, null ) => _collection . Aggregate ( CreateCollectionPipelineDefinition ( _loggedStages ) , _options , cancellationToken ) ,
115
+ ( _, _) => _collection . Aggregate ( session , CreateCollectionPipelineDefinition ( _loggedStages ) , _options , cancellationToken )
119
116
} ;
120
117
121
118
return _finalizer . Finalize ( cursor , cancellationToken ) ;
122
119
}
123
120
124
121
public override async Task < TResult > ExecuteAsync ( IClientSessionHandle session , CancellationToken cancellationToken )
125
122
{
123
+ _loggedStages = RenderPipeline ( ) ;
126
124
var cursor = ( _collection , session ) switch
127
125
{
128
- ( null , null ) => await _database . AggregateAsync ( CreateDatabasePipelineDefinition ( ) , _options , cancellationToken ) . ConfigureAwait ( false ) ,
129
- ( null , _ ) => await _database . AggregateAsync ( session , CreateDatabasePipelineDefinition ( ) , _options , cancellationToken ) . ConfigureAwait ( false ) ,
130
- ( _, null ) => await _collection . AggregateAsync ( CreateCollectionPipelineDefinition ( ) , _options , cancellationToken ) . ConfigureAwait ( false ) ,
131
- ( _, _) => await _collection . AggregateAsync ( session , CreateCollectionPipelineDefinition ( ) , _options , cancellationToken ) . ConfigureAwait ( false )
126
+ ( null , null ) => await _database . AggregateAsync ( CreateDatabasePipelineDefinition ( _loggedStages ) , _options , cancellationToken ) . ConfigureAwait ( false ) ,
127
+ ( null , _ ) => await _database . AggregateAsync ( session , CreateDatabasePipelineDefinition ( _loggedStages ) , _options , cancellationToken ) . ConfigureAwait ( false ) ,
128
+ ( _, null ) => await _collection . AggregateAsync ( CreateCollectionPipelineDefinition ( _loggedStages ) , _options , cancellationToken ) . ConfigureAwait ( false ) ,
129
+ ( _, _) => await _collection . AggregateAsync ( session , CreateCollectionPipelineDefinition ( _loggedStages ) , _options , cancellationToken ) . ConfigureAwait ( false )
132
130
} ;
133
131
134
132
return await _finalizer . FinalizeAsync ( cursor , cancellationToken ) . ConfigureAwait ( false ) ;
@@ -141,16 +139,19 @@ public override string ToString()
141
139
}
142
140
143
141
// private methods
144
- private BsonDocumentStagePipelineDefinition < TDocument , TOutput > CreateCollectionPipelineDefinition ( )
142
+ private BsonDocumentStagePipelineDefinition < TDocument , TOutput > CreateCollectionPipelineDefinition ( BsonDocument [ ] stages )
145
143
{
146
- var stages = _pipeline . Stages . Select ( s => ( BsonDocument ) s . Render ( ) ) ;
147
144
return new BsonDocumentStagePipelineDefinition < TDocument , TOutput > ( stages , ( IBsonSerializer < TOutput > ) _pipeline . OutputSerializer ) ;
148
145
}
149
146
150
- private BsonDocumentStagePipelineDefinition < NoPipelineInput , TOutput > CreateDatabasePipelineDefinition ( )
147
+ private BsonDocumentStagePipelineDefinition < NoPipelineInput , TOutput > CreateDatabasePipelineDefinition ( BsonDocument [ ] stages )
151
148
{
152
- var stages = _pipeline . Stages . Select ( s => ( BsonDocument ) s . Render ( ) ) ;
153
149
return new BsonDocumentStagePipelineDefinition < NoPipelineInput , TOutput > ( stages , ( IBsonSerializer < TOutput > ) _pipeline . OutputSerializer ) ;
154
150
}
151
+
152
+ private BsonDocument [ ] RenderPipeline ( )
153
+ {
154
+ return _pipeline . Render ( ) . AsBsonArray . Cast < BsonDocument > ( ) . ToArray ( ) ;
155
+ }
155
156
}
156
157
}
0 commit comments