Skip to content

Commit 065dd45

Browse files
author
Hugo Bonacci
committed
Minor adjustments to names - and updates to Oid generation
1 parent 5ee9432 commit 065dd45

File tree

6 files changed

+62
-21
lines changed

6 files changed

+62
-21
lines changed

MongoCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private void _PerformUpdates() {
208208
//handles deleting records that need to be removed
209209
private void _PerformDeletes() {
210210
IEnumerable<MongoOid> ids = this._Deletes.Select(item => item.Id);
211-
this.Database.Find(this.Name).In("_id", ids).Delete();
211+
this.Database.From(this.Name).In("_id", ids).Delete();
212212
}
213213

214214
#endregion

MongoConnection.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ public bool Connected {
111111
/// </summary>
112112
public void Open() {
113113
if (this.Connected) { return; }
114-
Console.WriteLine("opening...");
115114

116115
//notify any event handlers this is opening
117116
if (this.BeforeConnectionOpened != null) { this.BeforeConnectionOpened(this); }
@@ -131,7 +130,6 @@ public void Open() {
131130
/// Handles disconnecting from the client
132131
/// </summary>
133132
public void Close() {
134-
Console.WriteLine("closing...");
135133

136134
//notify any event handlers
137135
if (this.BeforeConnectionClosed != null) { this.BeforeConnectionClosed(this); }

MongoDatabase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,14 @@ public MongoCollection this[string collection] {
171171
/// <summary>
172172
/// Starts a new query for a collection
173173
/// </summary>
174-
public MongoQuery Find(string collection) {
174+
public MongoQuery From(string collection) {
175175
return this.GetCollection(collection).Find();
176176
}
177177

178178
/// <summary>
179179
/// Starts a new query for a collection
180180
/// </summary>
181-
public TQueryProvider Find<TQueryProvider>(string collection) where TQueryProvider : MongoQueryBase {
181+
public TQueryProvider From<TQueryProvider>(string collection) where TQueryProvider : MongoQueryBase {
182182
return this.GetCollection(collection).Find<TQueryProvider>();
183183
}
184184

MongoDocument.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ public MongoDocument(bool generateId, object source)
9292
/// Creates an Oid for this document - MongoDB will
9393
/// automatically create an Oid when needed
9494
/// </summary>
95-
public void GenerateId() {
95+
public MongoOid GenerateId() {
9696
this.Id = new MongoOid();
97+
return this.Id;
9798
}
9899

99100
#endregion
@@ -104,7 +105,7 @@ public void GenerateId() {
104105
/// The Oid for a document (if any)
105106
/// </summary>
106107
public MongoOid Id {
107-
get { return this.Get<MongoOid>(Mongo.DocumentIdKey); }
108+
get { return this.Get<MongoOid>(Mongo.DocumentIdKey, new MongoOid()); }
108109
set {
109110
if (value != null && !(value is MongoOid)) {
110111
throw new ArgumentException("You can only assign a MongoOid as the Id for a MongoDocument");
@@ -126,18 +127,16 @@ protected override IEnumerable<BsonFieldDetail> OnBeforeFinishBsonRender(IEnumer
126127
List<BsonFieldDetail> list = fields.ToList();
127128

128129
//get all of the ids to use
129-
IEnumerable<BsonFieldDetail> ids = list.Where(item => item.Type is MongoOidType);
130+
IEnumerable<BsonFieldDetail> ids = fields.Where(item => item.Type is MongoOidType);
130131
list.RemoveAll(item => item.Type is MongoOidType);
131132

132-
//reorder the document now so the Ids are in the front
133-
list.OrderBy(item => item.Length);
134-
135133
//because the items are shared in the same list
136134
//we have to insert these one at the time since
137135
//if we use AddRange an exception will be thrown
138136
//since the list is modified while enumerating
139137
//through the values. We're also going backwards
140138
//to make sure they retain their original order
139+
list = list.OrderBy(item => item.Length).ToList();
141140
for (int index = ids.Count(); index-- > 0; ) {
142141
list.Insert(0, ids.ElementAt(index));
143142
}

MongoOid.cs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
using CSMongo.Exceptions;
77
using System.Collections.Generic;
88
using System.Globalization;
9+
using System.Linq;
10+
using System.Security.Cryptography;
11+
using System.Net;
12+
using System.Diagnostics;
913

1014
namespace CSMongo {
1115

@@ -29,15 +33,14 @@ public class MongoOid {
2933
/// Creates a new Oid and generates an Oid automatically
3034
/// </summary>
3135
public MongoOid() {
36+
this.Value = MongoOid.Generate();
37+
}
3238

33-
//generate a unique id
34-
DynamicStream stream = new DynamicStream();
35-
stream.Append(BitConverter.GetBytes(DateTime.Now.Ticks));
36-
stream.Append(BitConverter.GetBytes(MongoOid._GetIdentifier()));
37-
38-
//build the new Oid
39-
this.Value = stream.ToArray();
40-
39+
/// <summary>
40+
/// Creates an ID using a string version of the Oid
41+
/// </summary>
42+
public MongoOid(string id) {
43+
this.SetId(id);
4144
}
4245

4346
/// <summary>
@@ -95,8 +98,8 @@ public void SetId(string value) {
9598

9699
//parse every pair of bytes
97100
List<byte> bytes = new List<byte>();
98-
for (int i = 0; i < value.Length / 2; i++) {
99-
string pair = value.Substring((i * 2), 2);
101+
for (int i = 0; i < value.Length; i += 2) {
102+
string pair = value.Substring(i, 2);
100103
byte parsed = byte.Parse(pair, NumberStyles.HexNumber);
101104
bytes.Add(parsed);
102105
}
@@ -128,6 +131,28 @@ public override string ToString() {
128131

129132
#endregion
130133

134+
#region Static Creation
135+
136+
/// <summary>
137+
/// Generates the bytes for a new MongoOid
138+
/// </summary>
139+
public static byte[] Generate() {
140+
List<byte> bytes = new List<byte>();
141+
142+
//generate the correct prefix (Suggestion from Sam Corder)
143+
double span = (DateTime.UtcNow - Mongo.Epoch).TotalSeconds;
144+
int floor = Convert.ToInt32(Math.Floor(span));
145+
bytes.AddRange(BitConverter.GetBytes(floor).Reverse());
146+
147+
//use a semi-unique value - Not sure if this
148+
//is supposed to be any paticular format
149+
bytes.AddRange(Guid.NewGuid().ToByteArray().Skip(8));
150+
return bytes.ToArray();
151+
152+
}
153+
154+
#endregion
155+
131156
}
132157

133158
}

Query/MongoQuery.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,30 @@ public MongoQuery EqualTo(string field, object value) {
131131

132132
}
133133

134+
/// <summary>
135+
/// Finds a record based on the Oid value
136+
/// </summary>
137+
public MongoQuery FindById(string id) {
138+
return this.FindById(new MongoOid(id));
139+
}
140+
141+
/// <summary>
142+
/// Finds a record based on the Oid value
143+
/// </summary>
144+
public MongoQuery FindById(byte[] id) {
145+
return this.FindById(new MongoOid(id));
146+
}
147+
134148
/// <summary>
135149
/// Finds a record based on the Oid value
136150
/// </summary>
137151
public MongoQuery FindById(MongoOid id) {
152+
153+
//use 'in' to find the id - There is an
154+
//actual option to use for this which
155+
//will be converted to later on
138156
return this.In(Mongo.DocumentIdKey, new MongoOid[] { id });
157+
139158
}
140159

141160
/// <summary>

0 commit comments

Comments
 (0)