summaryrefslogtreecommitdiff
path: root/nejdb
diff options
context:
space:
mode:
Diffstat (limited to 'nejdb')
-rw-r--r--nejdb/Ejdb.BSON/BSONDocument.cs4
-rw-r--r--nejdb/Ejdb.BSON/BSONIterator.cs15
-rw-r--r--nejdb/Ejdb.IO/ExtBinaryWriter.cs4
-rw-r--r--nejdb/Ejdb.Tests/TestBSON.cs65
-rw-r--r--nejdb/nejdb.userprefs23
5 files changed, 93 insertions, 18 deletions
diff --git a/nejdb/Ejdb.BSON/BSONDocument.cs b/nejdb/Ejdb.BSON/BSONDocument.cs
index 55e46f7..58821a3 100644
--- a/nejdb/Ejdb.BSON/BSONDocument.cs
+++ b/nejdb/Ejdb.BSON/BSONDocument.cs
@@ -22,6 +22,7 @@ using Ejdb.IO;
using Ejdb.BSON;
using Ejdb.Utils;
using System.Reflection;
+using System.Linq;
namespace Ejdb.BSON {
@@ -430,7 +431,8 @@ namespace Ejdb.BSON {
}
public override string ToString() {
- return string.Format("[{0}: {1}]", GetType().Name, _fieldslist);
+ return string.Format("[{0}: {1}]", GetType().Name,
+ string.Join(", ", from bv in _fieldslist select bv.ToString()));
}
//.//////////////////////////////////////////////////////////////////
// Private staff
diff --git a/nejdb/Ejdb.BSON/BSONIterator.cs b/nejdb/Ejdb.BSON/BSONIterator.cs
index b6895a5..8afc052 100644
--- a/nejdb/Ejdb.BSON/BSONIterator.cs
+++ b/nejdb/Ejdb.BSON/BSONIterator.cs
@@ -25,6 +25,8 @@ namespace Ejdb.BSON {
public class BSONIterator : IDisposable, IEnumerable<BSONType> {
ExtBinaryReader _input;
+ bool _closeOnDispose = true;
+ bool _disposed;
int _doclen;
BSONType _ctype = BSONType.UNKNOWN;
string _entryKey;
@@ -34,7 +36,7 @@ namespace Ejdb.BSON {
public bool Disposed {
get {
- return (_input == null);
+ return _disposed;
}
}
@@ -85,7 +87,8 @@ namespace Ejdb.BSON {
}
public void Dispose() {
- if (_input != null) {
+ _disposed = true;
+ if (_closeOnDispose && _input != null) {
_input.Close();
_input = null;
}
@@ -107,6 +110,13 @@ namespace Ejdb.BSON {
return GetEnumerator();
}
+ public IEnumerable<BSONValue> Values() {
+ while (Next() != BSONType.EOO) {
+ yield return FetchCurrentValue();
+ }
+
+ }
+
public BSONType Next() {
CheckDisposed();
if (_ctype == BSONType.EOO) {
@@ -218,6 +228,7 @@ namespace Ejdb.BSON {
{
BSONDocument doc = (_ctype == BSONType.OBJECT ? new BSONDocument() : new BSONArray());
BSONIterator sit = new BSONIterator(this._input, _entryLen + 4);
+ sit._closeOnDispose = false;
while (sit.Next() != BSONType.EOO) {
doc.Add(sit.FetchCurrentValue());
}
diff --git a/nejdb/Ejdb.IO/ExtBinaryWriter.cs b/nejdb/Ejdb.IO/ExtBinaryWriter.cs
index 88219b7..a62b838 100644
--- a/nejdb/Ejdb.IO/ExtBinaryWriter.cs
+++ b/nejdb/Ejdb.IO/ExtBinaryWriter.cs
@@ -49,7 +49,9 @@ namespace Ejdb.IO {
}
public void WriteCString(string val) {
- Write(_encoding.GetBytes(val));
+ if (val.Length > 0) {
+ Write(_encoding.GetBytes(val));
+ }
Write((byte) 0x00);
}
}
diff --git a/nejdb/Ejdb.Tests/TestBSON.cs b/nejdb/Ejdb.Tests/TestBSON.cs
index 6dfb8bf..f6c83d1 100644
--- a/nejdb/Ejdb.Tests/TestBSON.cs
+++ b/nejdb/Ejdb.Tests/TestBSON.cs
@@ -150,7 +150,7 @@ namespace Ejdb.Tests {
}
[Test]
- public void testIterate2() {
+ public void TestIterate2() {
var doc = new BSONDocument();
doc["a"] = "av";
doc["b"] = BSONDocument.ValueOf(new{cc = 1});
@@ -188,11 +188,70 @@ namespace Ejdb.Tests {
}
++c;
}
+ bool thrown = false;
Assert.IsTrue(it.Disposed);
+ try {
+ it.Next();
+ } catch (ObjectDisposedException) {
+ thrown = true;
+ }
+ Assert.IsTrue(thrown);
+ c = 0;
+ it = new BSONIterator(doc);
+ foreach (var bv in it.Values()) {
+ if (c == 0) {
+ Assert.AreEqual("a", bv.Key);
+ Assert.AreEqual("av", bv.Value);
+ }
+ if (c == 1) {
+ Assert.AreEqual("b", bv.Key);
+ BSONDocument sdoc = bv.Value as BSONDocument;
+ Assert.IsNotNull(sdoc);
+ foreach (var bv2 in new BSONIterator(sdoc).Values()) {
+ Assert.AreEqual("cc", bv2.Key);
+ Assert.AreEqual(1, bv2.Value);
+ Assert.AreEqual(BSONType.INT, bv2.BSONType);
+ }
+ }
+ if (c == 2) {
+ Assert.AreEqual(BSONType.OID, bv.BSONType);
+ Assert.IsInstanceOfType(typeof(BSONOid), bv.Value);
+ var oid = bv.Value as BSONOid;
+ Assert.AreEqual("51b9f3af98195c4600000000", oid.ToString());
+ }
+ c++;
+ }
+ }
-
-
+ [Test]
+ public void TestIterateRE() {
+ var doc = new BSONDocument();
+ doc["a"] = new BSONRegexp("b", "c");
+ doc["d"] = 1;
+ doc["e"] = BSONDocument.ValueOf(new {f = new BSONRegexp("g", "")});
+ doc["h"] = 2;
+ //28-00-00-00
+ //0B-61-00-62-00-63-00
+ //10-64-00-01-00-00-00
+ //03-65-00-0B-00-00-00
+ //0B-66-00-67-00-00-00
+ //10-68-00-02-00-00-00-00
+ var cs = "";
+ foreach (var bt in new BSONIterator(doc)) {
+ cs += bt.ToString();
+ }
+ Assert.AreEqual("REGEXINTOBJECTINT", cs);
+ cs = "";
+ foreach (var bv in new BSONIterator(doc).Values()) {
+ if (bv.Key == "a") {
+ cs += ((BSONRegexp) bv.Value).Re;
+ cs += ((BSONRegexp) bv.Value).Opts;
+ } else {
+ cs += bv.Value;
+ }
+ }
+ Assert.AreEqual("bc1[BSONDocument: [BSONValue: BSONType=REGEX, Key=f, Value=[BSONRegexp: re=g, opts=]]]2", cs);
}
}
}
diff --git a/nejdb/nejdb.userprefs b/nejdb/nejdb.userprefs
index afae674..e2f2f45 100644
--- a/nejdb/nejdb.userprefs
+++ b/nejdb/nejdb.userprefs
@@ -3,13 +3,14 @@
<MonoDevelop.Ide.Workbench ActiveDocument="Ejdb.Tests/TestBSON.cs">
<Files>
<File FileName="Ejdb.BSON/BSONArray.cs" Line="12" Column="61" />
- <File FileName="Ejdb.BSON/InvalidBSONDataException.cs" Line="30" Column="1" />
<File FileName="Ejdb.JSON/JSONReader.cs" Line="33" Column="4" />
- <File FileName="Ejdb.JSON/JSONElement.cs" Line="26" Column="1" />
- <File FileName="Ejdb.BSON/BSONDocument.cs" Line="141" Column="15" />
- <File FileName="Ejdb.Tests/TestBSON.cs" Line="183" Column="18" />
- <File FileName="Ejdb.BSON/BSONIterator.cs" Line="234" Column="9" />
- <File FileName="Ejdb.BSON/BSONType.cs" Line="29" Column="14" />
+ <File FileName="Ejdb.JSON/JSONElement.cs" Line="1" Column="1" />
+ <File FileName="Ejdb.BSON/BSONDocument.cs" Line="322" Column="1" />
+ <File FileName="Ejdb.Tests/TestBSON.cs" Line="133" Column="31" />
+ <File FileName="Ejdb.BSON/BSONIterator.cs" Line="308" Column="37" />
+ <File FileName="Ejdb.BSON/BSONType.cs" Line="39" Column="20" />
+ <File FileName="Ejdb.BSON/BSONOid.cs" Line="1" Column="1" />
+ <File FileName="Ejdb.IO/ExtBinaryWriter.cs" Line="54" Column="5" />
</Files>
<Pads>
<Pad Id="ProjectPad">
@@ -41,9 +42,12 @@
<Value>_input.BaseStream.Position</Value>
</State>
</Pad>
+ <Pad Id="ConnectionManagerPad">
+ <State selected="True" />
+ </Pad>
<Pad Id="MonoDevelop.NUnit.TestPad">
- <State expanded="True" selected="True">
- <Node name="nejdb" expanded="True">
+ <State expanded="True">
+ <Node name="nejdb" expanded="True" selected="True">
<Node name="Ejdb" expanded="True">
<Node name="Tests" expanded="True">
<Node name="TestBSON" expanded="True" />
@@ -52,9 +56,6 @@
</Node>
</State>
</Pad>
- <Pad Id="ConnectionManagerPad">
- <State selected="True" />
- </Pad>
</Pads>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>