This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
/
Cursor.cs
145 lines (126 loc) · 4.83 KB
/
Cursor.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using MongoDB.Driver.Bson;
using MongoDB.Driver.IO;
namespace MongoDB.Driver
{
public class Cursor : ICursor {
private Connection connection;
private long id = -1;
public long Id{
get {return id;}
}
private String fullCollectionName;
public string FullCollectionName {
get {return fullCollectionName;}
set {fullCollectionName = value;}
}
private String collName;
public string CollName {
get {return collName;}
set {collName = value;}
}
private Document spec;
public Document Spec{
get {return spec;}
set {spec = value;}
}
private int limit;
public int Limit{
get {return limit;}
set {limit = value;}
}
private int skip;
public int Skip{
get {return skip;}
set {skip = value;}
}
private Document fields;
public Document Fields{
get {return fields;}
set {fields = value;}
}
private bool modifiable = true;
public bool Modifiable{
get {return modifiable;}
}
private ReplyMessage reply;
public Cursor(Connection conn, String fullCollectionName, Document spec, int limit, int skip, Document fields){
this.connection = conn;
this.FullCollectionName = fullCollectionName;
if(spec == null)spec = new Document();
this.Spec = spec;
this.Limit = limit;
this.Skip = skip;
this.Fields = fields;
}
public IEnumerable<Document> Documents{
get{
if(this.reply == null){
RetrieveData();
}
int docsReturned = 0;
BsonDocument[] bdocs = this.reply.Documents;
Boolean shouldBreak = false;
while(!shouldBreak){
foreach(BsonDocument bdoc in bdocs){
if((this.Limit == 0) || (this.Limit != 0 && docsReturned < this.Limit)){
docsReturned++;
yield return (Document)bdoc.ToNative();
}else{
shouldBreak = true;
yield break;
}
}
if(this.Id != 0 && shouldBreak == false){
RetrieveMoreData();
bdocs = this.reply.Documents;
if(bdocs == null){
shouldBreak = true;
}
}else{
shouldBreak = true;
}
}
}
}
private void RetrieveData(){
QueryMessage query = new QueryMessage();
query.FullCollectionName = this.FullCollectionName;
query.Query = BsonConvert.From(this.Spec);
query.NumberToReturn = this.Limit;
query.NumberToSkip = this.Skip;
if(this.Fields != null){
query.ReturnFieldSelector = BsonConvert.From(this.Fields);
}
try{
this.reply = connection.SendTwoWayMessage(query);
this.id = this.reply.CursorID;
if(this.Limit < 0)this.Limit = this.Limit * -1;
}catch(IOException ioe){
throw new MongoCommException("Could not read data, communication failure", this.connection,ioe);
}
}
private void RetrieveMoreData(){
GetMoreMessage gmm = new GetMoreMessage(this.FullCollectionName, this.Id, this.Limit);
try{
this.reply = connection.SendTwoWayMessage(gmm);
this.id = this.reply.CursorID;
}catch(IOException ioe){
this.id = 0;
throw new MongoCommException("Could not read data, communication failure", this.connection,ioe);
}
}
public void Dispose(){
if(this.Id == 0) return; //All server side resources disposed of.
KillCursorsMessage kcm = new KillCursorsMessage(this.Id);
try{
this.id = 0;
connection.SendMessage(kcm);
}catch(IOException ioe){
throw new MongoCommException("Could not read data, communication failure", this.connection,ioe);
}
}
}
}