File: QueryProcessor.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (1122 lines) | stat: -rw-r--r-- 29,049 bytes parent folder | download
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Dispatcher
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Runtime;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Diagnostics;
    using System.Threading;

    class ProcessingContext
    {
        internal ProcessingContext next; // for chaining together in free lists
        int nodeCount;
        QueryProcessor processor;
        EvalStack sequenceStack;
        EvalStack valueStack;

        internal ProcessingContext()
        {
            this.valueStack = new EvalStack(2, 4);
            this.sequenceStack = new EvalStack(1, 2);
            this.nodeCount = -1;
        }
#if NO
        internal int FrameCount
        {
            get
            {
                return this.valueStack.FrameCount;
            }
        }

        internal int FramePtr
        {
            get
            {
                return this.valueStack.FramePtr;
            }
        }
#endif
        internal StackFrame this[int frameIndex]
        {
            get
            {
                return this.valueStack[frameIndex];
            }
        }

        internal int IterationCount
        {
            get
            {
                if (-1 == this.nodeCount)
                {
                    this.nodeCount = this.sequenceStack.CalculateNodecount();
                    if (this.nodeCount == 0 && !this.sequenceStack.InUse)
                    {
                        this.nodeCount = 1;
                    }
                }

                return this.nodeCount;
            }
        }

        internal int NodeCount
        {
            get
            {
                return this.nodeCount;
            }
            set
            {
                this.nodeCount = value;
            }
        }

        internal ProcessingContext Next
        {
            get
            {
                return this.next;
            }
            set
            {
                this.next = value;
            }
        }

        internal QueryProcessor Processor
        {
            get
            {
                return this.processor;
            }
            set
            {
                this.processor = value;
            }
        }

        internal StackFrame SecondArg
        {
            get
            {
                return this.valueStack.SecondArg;
            }
        }

        internal Value[] Sequences
        {
            get
            {
                return this.sequenceStack.Buffer;
            }
        }

        internal bool SequenceStackInUse
        {
            get
            {
                return this.sequenceStack.InUse;
            }
        }

        internal bool StacksInUse
        {
            get
            {
                return (this.valueStack.frames.Count > 0 || this.sequenceStack.frames.Count > 0);
            }
        }

        internal StackFrame TopArg
        {
            get
            {
                return this.valueStack.TopArg;
            }
        }

        internal StackFrame TopSequenceArg
        {
            get
            {
                return this.sequenceStack.TopArg;
            }
        }

        internal Value[] Values
        {
            get
            {
                return this.valueStack.Buffer;
            }
        }

        internal ProcessingContext Clone()
        {
            return this.processor.CloneContext(this);
        }

        internal void ClearContext()
        {
            this.sequenceStack.Clear();
            this.valueStack.Clear();
            this.nodeCount = -1;
        }

        internal void CopyFrom(ProcessingContext context)
        {
            Fx.Assert(null != context, "");

            this.processor = context.processor;
            if (context.sequenceStack.frames.Count > 0)
            {
                this.sequenceStack.CopyFrom(ref context.sequenceStack);
            }
            else
            {
                this.sequenceStack.Clear();
            }
            if (context.valueStack.frames.Count > 0)
            {
                this.valueStack.CopyFrom(ref context.valueStack);
            }
            else
            {
                this.valueStack.Clear();
            }
            this.nodeCount = context.nodeCount;
        }

        internal NodeSequence CreateSequence()
        {
            NodeSequence sequence = this.processor.PopSequence();
            if (null == sequence)
            {
                sequence = new NodeSequence();
            }
            sequence.OwnerContext = this;
            sequence.refCount++;

            return sequence;
        }

        internal bool LoadVariable(int var)
        {
            return this.Processor.LoadVariable(this, var);
        }

        internal void EvalCodeBlock(Opcode block)
        {
            this.processor.Eval(block, this);
        }

        internal bool PeekBoolean(int index)
        {
            return this.valueStack.PeekBoolean(index);
        }

        internal double PeekDouble(int index)
        {
            return this.valueStack.PeekDouble(index);
        }
#if NO
        internal int PeekInteger(int index)
        {
            return (int) this.valueStack.PeekInteger(index);
        }
#endif
        internal NodeSequence PeekSequence(int index)
        {
            return this.valueStack.PeekSequence(index);
        }

        internal string PeekString(int index)
        {
            return this.valueStack.PeekString(index);
        }

        internal void PopFrame()
        {
            this.valueStack.PopFrame(this);
        }

        internal void PopSequenceFrame()
        {
            this.sequenceStack.PopFrame(this);
            this.nodeCount = -1;
        }

        internal void PopContextSequenceFrame()
        {
            PopSequenceFrame();
            if (!this.sequenceStack.InUse)
                this.sequenceStack.contextOnTopOfStack = false;
        }

        internal void Push(bool boolVal)
        {
            this.valueStack.Push(boolVal);
        }

        internal void Push(bool boolVal, int addCount)
        {
            this.valueStack.Push(boolVal, addCount);
        }

#if NO
        internal void Push(double doubleVal)
        {
            this.valueStack.Push(doubleVal);
        }
#endif
        internal void Push(double doubleVal, int addCount)
        {
            this.valueStack.Push(doubleVal, addCount);
        }

        internal void Push(NodeSequence sequence)
        {
            this.valueStack.Push(sequence);
        }

        internal void Push(NodeSequence sequence, int addCount)
        {
            this.valueStack.Push(sequence, addCount);
        }

        internal void Push(string stringVal)
        {
            this.valueStack.Push(stringVal);
        }

        internal void Push(string stringVal, int addCount)
        {
            this.valueStack.Push(stringVal, addCount);
        }

        internal void PushFrame()
        {
            this.valueStack.PushFrame();
        }

        internal void PopSequenceFrameToValueStack()
        {
            this.sequenceStack.PopSequenceFrameTo(ref this.valueStack);
            this.nodeCount = -1;
        }

        internal void PushSequence(NodeSequence seq)
        {
            this.sequenceStack.Push(seq);
            this.nodeCount = -1;
        }

        internal void PushSequenceFrame()
        {
            this.sequenceStack.PushFrame();
            this.nodeCount = -1;
        }

        internal void PushContextSequenceFrame()
        {
            if (!this.sequenceStack.InUse)
                this.sequenceStack.contextOnTopOfStack = true;
            PushSequenceFrame();
        }

        internal void PushSequenceFrameFromValueStack()
        {
            this.valueStack.PopSequenceFrameTo(ref this.sequenceStack);
            this.nodeCount = -1;
        }

        internal void ReleaseSequence(NodeSequence sequence)
        {
            Fx.Assert(null != sequence, "");
            if (this == sequence.OwnerContext)
            {
                sequence.refCount--;
                Fx.Assert(sequence.refCount >= 0, "");
                if (0 == sequence.refCount)
                {
                    this.processor.ReleaseSequenceToPool(sequence);
                }
            }
        }

        internal void Release()
        {
            this.processor.ReleaseContext(this);
        }

        internal void ReplaceSequenceAt(int index, NodeSequence sequence)
        {
            this.sequenceStack.ReplaceAt(index, sequence);
            this.nodeCount = -1;
        }

        internal void SaveVariable(int var, int count)
        {
            this.Processor.SaveVariable(this, var, count);
        }

        internal void SetValue(ProcessingContext context, int index, bool val)
        {
            this.valueStack.SetValue(this, index, val);
        }

        internal void SetValue(ProcessingContext context, int index, double val)
        {
            this.valueStack.SetValue(this, index, val);
        }

        internal void SetValue(ProcessingContext context, int index, string val)
        {
            this.valueStack.SetValue(this, index, val);
        }

        internal void SetValue(ProcessingContext context, int index, NodeSequence val)
        {
            this.valueStack.SetValue(this, index, val);
        }

        internal void TransferSequenceSize()
        {
            this.sequenceStack.TransferSequenceSizeTo(ref this.valueStack);
        }

        internal void TransferSequencePositions()
        {
            this.sequenceStack.TransferPositionsTo(ref this.valueStack);
        }

        #region IQueryBufferPool Members
#if NO
        public virtual void Reset()
        {
            this.valueStack.Clear();
            this.valueStack.Trim();
            this.sequenceStack.Clear();
            this.sequenceStack.Trim();
        }

        public virtual void Trim()
        {
            this.valueStack.Trim();
            this.sequenceStack.Trim();
        }
#endif
        #endregion
    }

    internal enum QueryProcessingFlags : byte
    {
        None = 0x00,
        Match = 0x01,
        Message = 0x02,
        //Select      = 0x04,
    }

    internal class QueryProcessor : ProcessingContext
    {
        SeekableXPathNavigator contextNode; // original context node off which everything started
        ProcessingContext contextPool;
        INodeCounter counter;
        QueryProcessingFlags flags;
        QueryMatcher matcher;
        Message message;
        bool matchMessageBody;
        int refCount;
        bool result; // for singleton matches...
        XPathResult queryResult; // for singleton queries...
        QueryBranchResultSet resultPool;
        Collection<MessageFilter> matchList;
        ICollection<MessageFilter> matchSet; // for inverse queries that produce multiple matches
        ICollection<KeyValuePair<MessageQuery, XPathResult>> resultSet;  // for inverse queries that produce multiple query results
        NodeSequence sequencePool;
        //string selectResults;
        SubExprVariable[] subExprVars;
        string messageAction;
        //string messageAddress;
        //string messageVia;
        string messageId;
        string messageSoapUri;
        string messageTo;

        internal QueryProcessor(QueryMatcher matcher)
            : base()
        {
            this.Processor = this;
            this.matcher = matcher;
            this.flags = QueryProcessingFlags.Match;

            // PERF, [....], see if we can just let these to their default init
            this.messageAction = null;
            //this.messageAddress = null;
            //this.messageVia = null;
            this.messageId = null;
            this.messageSoapUri = null;
            this.messageTo = null;

            if (matcher.SubExprVarCount > 0)
            {
                this.subExprVars = new SubExprVariable[matcher.SubExprVarCount];
            }
        }


        internal string Action
        {
            get
            {
                return this.messageAction;
            }
            set
            {
                this.messageAction = value;
            }
        }
#if NO 
        internal string Address
        {
            get
            {
                return this.messageAddress;
            }
            set
            {
                this.messageAddress = value;
            }
        }
#endif
        // IMPORTANT: Either ContextNode.get or CounterMarker.get MUST be called before this.counter
        //            can be considered valid.
        internal SeekableXPathNavigator ContextNode
        {
            get
            {
                if (null == this.contextNode)
                {
                    if (null != this.message)
                    {
                        this.contextNode = this.matcher.CreateMessageNavigator(this.message, this.matchMessageBody);
                    }
                    else
                    {
#pragma warning suppress 56503 // [....], property is more readable for this
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperCritical(new QueryProcessingException(QueryProcessingError.Unexpected));
                    }
                    this.counter = this.contextNode as INodeCounter;
                    if (null == this.counter)
                    {
                        this.counter = DummyNodeCounter.Dummy;
                    }
                }
                return this.contextNode;
            }
            set
            {
                this.contextNode = value;
                this.counter = value as INodeCounter;
            }
        }

        internal Message ContextMessage
        {
            get
            {
                return this.message;
            }
            set
            {
                this.message = value;
                if (null != value)
                {
                    this.flags |= QueryProcessingFlags.Message;
                }
                else
                {
                    this.flags &= ~QueryProcessingFlags.Message;
                }
            }
        }

        // IMPORTANT: Either ContextNode.get or CounterMarker.get MUST be called before this.counter
        //            can be considered valid.
        internal int CounterMarker
        {
            get
            {
                if (this.counter == null)
                {
                    this.counter = this.ContextNode as INodeCounter;
                    if (this.counter == null)
                        this.counter = DummyNodeCounter.Dummy;
                }
                return this.counter.CounterMarker;
            }
            set
            {
                this.counter.CounterMarker = value;
            }
        }

#if NO
        internal QueryProcessingFlags Flags
        {
            get
            {
                return this.flags;
            }
        }
        
        internal bool HasContextNode
        {
            get
            {
                return (null != this.contextNode);
            }
        }
#endif

        internal bool MatchBody
        {
#if NO
            get
            {
                return this.matchMessageBody;
            }
#endif
            set
            {
                this.matchMessageBody = value;
            }
        }

        internal QueryMatcher Matcher
        {
            get
            {
                return this.matcher;
            }
        }

        internal ICollection<KeyValuePair<MessageQuery, XPathResult>> ResultSet
        {
            get
            {
                return this.resultSet;
            }
            set
            {
                this.resultSet = value;
            }
        }

        internal string MessageId
        {
            get
            {
                return this.messageId;
            }
            set
            {
                this.messageId = value;
            }
        }

        internal bool Result
        {
            get
            {
                return this.result;
            }
            set
            {
                this.result = value;
            }
        }

        internal XPathResult QueryResult
        {
            get
            {
                return this.queryResult;
            }
            set
            {
                this.queryResult = value;
            }
        }

        internal Collection<MessageFilter> MatchList
        {
            get
            {
                return this.matchList;
            }
        }

        internal ICollection<MessageFilter> MatchSet
        {
            get
            {
                return this.matchSet;
            }
            set
            {
                this.matchSet = value;
            }
        }

        internal string SoapUri
        {
            get
            {
                return this.messageSoapUri;
            }
            set
            {
                this.messageSoapUri = value;
            }
        }

        internal string ToHeader
        {
            get
            {
                return this.messageTo;
            }
            set
            {
                this.messageTo = value;
            }
        }
#if NO 
        internal string Via
        {
            get
            {
                return this.messageVia;
            }
            set
            {
                this.messageVia = value;
            }
        }
#endif
        internal void AddRef()
        {
            Interlocked.Increment(ref this.refCount);
        }

        internal void ClearProcessor()
        {
            base.ClearContext();

            this.flags = QueryProcessingFlags.Match;

            this.messageAction = null;
            //this.messageAddress = null;
            //this.messageVia = null;
            this.messageId = null;
            this.messageSoapUri = null;
            this.messageTo = null;

            int exprCount = this.matcher.SubExprVarCount;
            if (exprCount == 0)
            {
                // No vars. Recycle entire subexpression cache
                this.subExprVars = null;
                return;
            }

            SubExprVariable[] vars = this.subExprVars; // save locally for speed
            if (vars == null)
            {
                // Allocate space for sub-expressions
                this.subExprVars = new SubExprVariable[exprCount];
                return;
            }

            int varCount = vars.Length;
            // The # of subexpressions changed since this processor was last used.
            if (varCount != exprCount)
            {
                this.subExprVars = new SubExprVariable[exprCount];
                return;
            }

            if (varCount == 1)
            {
                NodeSequence seq = vars[0].seq;
                if (seq != null)
                {
                    this.ReleaseSequenceToPool(seq);
                }
                return;
            }

            // We can reuse the sub-expression cache
            // Clear out the sub-expression results from an earlier run, and return sequences to pool
            for (int i = 0; i < varCount; ++i)
            {
                NodeSequence seq = vars[i].seq;
                if (seq != null && seq.refCount > 0)
                {
                    this.ReleaseSequenceToPool(seq);
                }
            }
            Array.Clear(vars, 0, vars.Length);
        }

        internal ProcessingContext CloneContext(ProcessingContext srcContext)
        {
            ProcessingContext context = this.PopContext();
            if (null == context)
            {
                context = new ProcessingContext();
            }
            context.CopyFrom(srcContext);

            return context;
        }

        internal QueryBranchResultSet CreateResultSet()
        {
            QueryBranchResultSet resultSet = this.PopResultSet();
            if (null == resultSet)
            {
                resultSet = new QueryBranchResultSet();
            }
            else
            {
                resultSet.Clear();
            }
            return resultSet;
        }

        internal int ElapsedCount(int marker)
        {
            return this.counter.ElapsedCount(marker);
        }

        internal void EnsureFilterCollection()
        {
            this.resultSet = null;

            if (null == this.matchSet)
            {
                if (null == this.matchList)
                {
                    this.matchList = new Collection<MessageFilter>();
                }
                else
                {
                    this.matchList.Clear();
                }
                this.matchSet = this.matchList;
            }
        }

        internal void Eval(Opcode block)
        {
            Opcode op = block;
            try
            {
                // Walk over and evaulate the entire trace
                while (null != op)
                {
                    op = op.Eval(this);
                }
            }
            catch (XPathNavigatorException e)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(e.Process(op));
            }
            catch (NavigatorInvalidBodyAccessException e)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(e.Process(op));
            }
        }

        internal void Eval(Opcode block, ProcessingContext context)
        {
            Opcode op = block;
            try
            {
                // Walk over and evaulate the entire trace
                while (null != op)
                {
                    op = op.Eval(context);
                }
            }
            catch (XPathNavigatorException e)
            {
                throw TraceUtility.ThrowHelperError(e.Process(op), this.message);
            }
            catch (NavigatorInvalidBodyAccessException e)
            {
                throw TraceUtility.ThrowHelperError(e.Process(op), this.message);
            }
        }

        //
        // Set up the query processor to match messages
        //
        internal void Eval(Opcode block, Message message, bool matchBody)
        {
            this.result = false;
            this.ContextNode = null;
            this.ContextMessage = message;
            this.MatchBody = matchBody;
            this.Eval(block);
            this.message = null;
            this.contextNode = null;
        }

        internal void Eval(Opcode block, SeekableXPathNavigator navigator)
        {
            this.result = false;
            this.ContextNode = navigator;
            this.ContextMessage = null;
            this.Eval(block);
        }

        internal bool LoadVariable(ProcessingContext context, int var)
        {
            if (this.subExprVars[var].seq == null)
            {
                return false;
            }

            int iter = context.IterationCount;
            this.counter.IncreaseBy(iter * this.subExprVars[var].count);

            NodeSequence seq = this.subExprVars[var].seq;
            context.PushSequenceFrame();
            for (int i = 0; i < iter; ++i)
            {
                seq.refCount++;
                context.PushSequence(seq);
            }

            return true;
        }

        internal ProcessingContext PopContext()
        {
            ProcessingContext context = this.contextPool;
            if (null != context)
            {
                this.contextPool = context.Next;
                context.Next = null;
            }
            return context;
        }

        internal NodeSequence PopSequence()
        {
            NodeSequence sequence = this.sequencePool;
            if (null != sequence)
            {
                this.sequencePool = sequence.Next;
                sequence.Next = null;
            }
            return sequence;
        }

        internal QueryBranchResultSet PopResultSet()
        {
            QueryBranchResultSet resultSet = this.resultPool;
            if (null != resultSet)
            {
                this.resultPool = resultSet.Next;
                resultSet.Next = null;
            }
            return resultSet;
        }

        internal void PushContext(ProcessingContext context)
        {
            Fx.Assert(null != context, "");

            context.Next = this.contextPool;
            this.contextPool = context;
        }

        internal void PushResultSet(QueryBranchResultSet resultSet)
        {
            Fx.Assert(null != resultSet, "");

            resultSet.Next = this.resultPool;
            this.resultPool = resultSet;
        }

        internal bool ReleaseRef()
        {
            return (Interlocked.Decrement(ref this.refCount) == 0);
        }

        internal void ReleaseContext(ProcessingContext context)
        {
            Fx.Assert(null != context, "");
            this.PushContext(context);
        }

        internal void ReleaseResults(QueryBranchResultSet resultSet)
        {
            Fx.Assert(null != resultSet, "");
            this.PushResultSet(resultSet);
        }

        internal void ReleaseSequenceToPool(NodeSequence sequence)
        {
            if (NodeSequence.Empty != sequence)
            {
                sequence.Reset(this.sequencePool);
                this.sequencePool = sequence;
            }
        }

        internal void SaveVariable(ProcessingContext context, int var, int count)
        {
            NodeSequence seq = context.Sequences[context.TopSequenceArg.basePtr].Sequence;
            if (seq == null)
                seq = CreateSequence();
            seq.OwnerContext = null;
            this.subExprVars[var].seq = seq;
            this.subExprVars[var].count = count;
        }

        #region IQueryBufferPool Members
#if NO
        public override void Reset()
        {
            base.Release();            
            // Trim local pools by releasing all references
            while (null != this.PopResultSet());            
            this.resultPool = null;
            while (null != this.PopSequence());
            this.sequencePool = null;
            while (null != this.PopContext());
            this.contextPool = null;
        }

        public override void Trim()
        {
            // Trim stacks
            base.Trim();   
            // Trim local pools individually         
            QueryBranchResultSet result = this.resultPool;
            while (null != result)
            {
                result.Trim();
                result = result.Next;
            }
            NodeSequence sequence = this.sequencePool;
            while (null != sequencePool)
            {
                sequencePool.Trim();
                sequence = sequence.Next;
            }
            ProcessingContext context = this.contextPool;
            while (null != context)
            {
                context.Trim();
                context = context.Next;
            }
        }
#endif
        #endregion

        struct SubExprVariable
        {
            internal NodeSequence seq;
            internal int count;
        }
    }


#if NO 
    internal struct QueryStatistics
    {
        int backupCapacity;

        int nodeCapacity;

        int seqStackCapacity;

        int seqFrameCapacity;

        int valFrameCapacity;

        int valStackCapacity;

        internal QueryStatistics()
        {
            this.backupCapacity = 8;
            this.nodeCapacity = 8;
            this.seqFrameCapacity = 2;
            this.seqStackCapacity = 4;
            this.valFrameCapacity = 2;
            this.valStackCapacity = 4;
        }

        internal int BackupCapacity
        {
            get
            {
                return this.backupCapacity;
            }
        }

        internal int NodeCapacity
        {
            get
            {
                return this.nodeCapacity;
            }
        }

        internal int SeqFrameCapacity
        {
            get
            {
                return this.seqFrameCapacity;
            }
        }

        internal int SeqStackCapacity
        {
            get
            {
                return this.seqStackCapacity;
            }
        }

        internal int ValFrameCapacity
        {
            get
            {
                return this.valFrameCapacity;
            }
        }

        internal int ValStackCapacity
        {
            get
            {
                return this.valStackCapacity;
            }
        }
    }
#endif
}