From b88dae6f68c4ba605a8c32d9357701fdd84e3c28 Mon Sep 17 00:00:00 2001 From: adam Date: Sat, 15 Jun 2013 01:06:48 +0700 Subject: [PATCH] #24 --- nejdb/Ejdb.BSON/BSONDocument.cs | 4 ++- nejdb/Ejdb.BSON/BSONIterator.cs | 15 ++++++++-- nejdb/Ejdb.IO/ExtBinaryWriter.cs | 4 ++- nejdb/Ejdb.Tests/TestBSON.cs | 65 ++++++++++++++++++++++++++++++++++++++-- nejdb/nejdb.userprefs | 23 +++++++------- 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 { 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 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 @@ - - - - - - + + + + + + + @@ -41,9 +42,12 @@ _input.BaseStream.Position + + + - - + + @@ -52,9 +56,6 @@ - - - -- 2.7.4