From 4263cb2b085497d8a7158c8a8a24769244e846c3 Mon Sep 17 00:00:00 2001 From: adam Date: Tue, 11 Jun 2013 01:45:37 +0700 Subject: [PATCH] #24 --- nejdb/Ejdb.SON/BSONArray.cs | 91 ++++++++++++++++ nejdb/Ejdb.SON/BSONBinData.cs | 9 +- nejdb/Ejdb.SON/{BSON.cs => BSONConstants.cs} | 0 nejdb/Ejdb.SON/BSONDocument.cs | 149 ++++++++++++++++++++------ nejdb/Ejdb.SON/BSONOid.cs | 13 ++- nejdb/Ejdb.SON/BSONRegexp.cs | 23 ++-- nejdb/Ejdb.SON/BSONValue.cs | 4 + nejdb/Ejdb.Tests/TestBSON.cs | 48 +++++++++ nejdb/Ejdb.Tests/{TestOne.cs => TestUtils.cs} | 16 +-- nejdb/nejdb.csproj | 5 +- 10 files changed, 290 insertions(+), 68 deletions(-) rename nejdb/Ejdb.SON/{BSON.cs => BSONConstants.cs} (100%) create mode 100644 nejdb/Ejdb.Tests/TestBSON.cs rename nejdb/Ejdb.Tests/{TestOne.cs => TestUtils.cs} (81%) diff --git a/nejdb/Ejdb.SON/BSONArray.cs b/nejdb/Ejdb.SON/BSONArray.cs index 6d1ae90..6d66215 100644 --- a/nejdb/Ejdb.SON/BSONArray.cs +++ b/nejdb/Ejdb.SON/BSONArray.cs @@ -24,6 +24,97 @@ namespace Ejdb.SON { return BSONType.ARRAY; } } + + public BSONDocument SetNull(int idx) { + return base.SetNull(idx.ToString()); + } + + public BSONDocument SetUndefined(int idx) { + return base.SetUndefined(idx.ToString()); + } + + public BSONDocument SetMaxKey(int idx) { + return base.SetMaxKey(idx.ToString()); + } + + public BSONDocument SetMinKey(int idx) { + return base.SetMinKey(idx.ToString()); + } + + public BSONDocument SetOID(int idx, string oid) { + return base.SetOID(idx.ToString(), oid); + } + + public BSONDocument SetOID(int idx, BSONOid oid) { + return base.SetOID(idx.ToString(), oid); + } + + public BSONDocument SetBool(int idx, bool val) { + return base.SetBool(idx.ToString(), val); + } + + public BSONDocument SetNumber(int idx, int val) { + return base.SetNumber(idx.ToString(), val); + } + + public BSONDocument SetNumber(int idx, long val) { + return base.SetNumber(idx.ToString(), val); + } + + public BSONDocument SetNumber(int idx, double val) { + return base.SetNumber(idx.ToString(), val); + } + + public BSONDocument SetNumber(int idx, float val) { + return base.SetNumber(idx.ToString(), val); + } + + public BSONDocument SetString(int idx, string val) { + return base.SetString(idx.ToString(), val); + } + + public BSONDocument SetCode(int idx, string val) { + return base.SetCode(idx.ToString(), val); + } + + public BSONDocument SetSymbol(int idx, string val) { + return base.SetSymbol(idx.ToString(), val); + } + + public BSONDocument SetDate(int idx, DateTime val) { + return base.SetDate(idx.ToString(), val); + } + + public BSONDocument SetRegexp(int idx, BSONRegexp val) { + return base.SetRegexp(idx.ToString(), val); + } + + public BSONDocument SetBinData(int idx, BSONBinData val) { + return base.SetBinData(idx.ToString(), val); + } + + public BSONDocument SetObject(int idx, BSONDocument val) { + return base.SetObject(idx.ToString(), val); + } + + public BSONDocument SetArray(int idx, BSONArray val) { + return base.SetArray(idx.ToString(), val); + } + + public BSONDocument SetTimestamp(int idx, BSONTimestamp val) { + return base.SetTimestamp(idx.ToString(), val); + } + + public BSONDocument SetCodeWScope(int idx, BSONCodeWScope val) { + return base.SetCodeWScope(idx.ToString(), val); + } + + protected override void CheckKey(string key) { + int idx; + if (key == null || !int.TryParse(key, out idx) || idx < 0) { + throw new InvalidBSONDataException(string.Format("Invalid array key: {0}", key)); + } + } } } diff --git a/nejdb/Ejdb.SON/BSONBinData.cs b/nejdb/Ejdb.SON/BSONBinData.cs index b5bbc6f..f47b2b4 100644 --- a/nejdb/Ejdb.SON/BSONBinData.cs +++ b/nejdb/Ejdb.SON/BSONBinData.cs @@ -18,7 +18,8 @@ using System.IO; namespace Ejdb.SON { - public class BSONBinData { + [Serializable] + public sealed class BSONBinData { readonly byte _subtype; readonly byte[] _data; @@ -34,6 +35,12 @@ namespace Ejdb.SON { } } + public BSONBinData(byte subtype, byte[] data) { + _subtype = subtype; + _data = new byte[data.Length]; + Array.Copy(data, _data, data.Length); + } + internal BSONBinData(byte subtype, int len, BinaryReader input) { _subtype = subtype; _data = input.ReadBytes(len); diff --git a/nejdb/Ejdb.SON/BSON.cs b/nejdb/Ejdb.SON/BSONConstants.cs similarity index 100% rename from nejdb/Ejdb.SON/BSON.cs rename to nejdb/Ejdb.SON/BSONConstants.cs diff --git a/nejdb/Ejdb.SON/BSONDocument.cs b/nejdb/Ejdb.SON/BSONDocument.cs index 41d38d3..2784288 100644 --- a/nejdb/Ejdb.SON/BSONDocument.cs +++ b/nejdb/Ejdb.SON/BSONDocument.cs @@ -19,6 +19,7 @@ using System.IO; using System.Text; using System.Diagnostics; using Ejdb.IO; +using Ejdb.SON; namespace Ejdb.SON { @@ -63,49 +64,88 @@ namespace Ejdb.SON { } } - public BSONDocument Add(BSONValue bv) { - _fieldslist.Add(bv); - if (_fields != null) { - _fields.Add(bv.Key, bv); - } - return this; + public BSONDocument SetNull(string key) { + return SetBSONValue(new BSONValue(BSONType.NULL, key)); } - public BSONValue this[string key] { - get { - return GetBSONValue(key); - } - set { - SetBSONValue(key, value); - } + public BSONDocument SetUndefined(string key) { + return SetBSONValue(new BSONValue(BSONType.UNKNOWN, key)); } - public BSONValue GetBSONValue(string key) { - CheckFields(); - return _fields[key]; + public BSONDocument SetMaxKey(string key) { + return SetBSONValue(new BSONValue(BSONType.MAXKEY, key)); } - public void SetBSONValue(string key, BSONValue val) { - CheckFields(); - var ov = _fields[key]; - if (ov != null) { - ov.Key = val.Key; - ov.BSONType = val.BSONType; - ov.Value = val.Value; - } else { - _fieldslist.Add(val); - _fields[key] = val; - } + public BSONDocument SetMinKey(string key) { + return SetBSONValue(new BSONValue(BSONType.MINKEY, key)); } - public object GetObjectValue(string key) { - CheckFields(); - var bv = _fields[key]; - return bv != null ? bv.Value : null; + public BSONDocument SetOID(string key, string oid) { + return SetBSONValue(new BSONValue(BSONType.OID, key, new BSONOid(oid))); } - public BSONValue SetObjectValue(string key, object value) { - throw new NotImplementedException(); + public BSONDocument SetOID(string key, BSONOid oid) { + return SetBSONValue(new BSONValue(BSONType.OID, key, oid)); + } + + public BSONDocument SetBool(string key, bool val) { + return SetBSONValue(new BSONValue(BSONType.BOOL, key, val)); + } + + public BSONDocument SetNumber(string key, int val) { + return SetBSONValue(new BSONValue(BSONType.INT, key, val)); + } + + public BSONDocument SetNumber(string key, long val) { + return SetBSONValue(new BSONValue(BSONType.LONG, key, val)); + } + + public BSONDocument SetNumber(string key, double val) { + return SetBSONValue(new BSONValue(BSONType.DOUBLE, key, val)); + } + + public BSONDocument SetNumber(string key, float val) { + return SetBSONValue(new BSONValue(BSONType.DOUBLE, key, val)); + } + + public BSONDocument SetString(string key, string val) { + return SetBSONValue(new BSONValue(BSONType.STRING, key, val)); + } + + public BSONDocument SetCode(string key, string val) { + return SetBSONValue(new BSONValue(BSONType.CODE, key, val)); + } + + public BSONDocument SetSymbol(string key, string val) { + return SetBSONValue(new BSONValue(BSONType.SYMBOL, key, val)); + } + + public BSONDocument SetDate(string key, DateTime val) { + return SetBSONValue(new BSONValue(BSONType.DATE, key, val)); + } + + public BSONDocument SetRegexp(string key, BSONRegexp val) { + return SetBSONValue(new BSONValue(BSONType.REGEX, key, val)); + } + + public BSONDocument SetBinData(string key, BSONBinData val) { + return SetBSONValue(new BSONValue(BSONType.BINDATA, key, val)); + } + + public BSONDocument SetObject(string key, BSONDocument val) { + return SetBSONValue(new BSONValue(BSONType.OBJECT, key, val)); + } + + public BSONDocument SetArray(string key, BSONArray val) { + return SetBSONValue(new BSONValue(BSONType.ARRAY, key, val)); + } + + public BSONDocument SetTimestamp(string key, BSONTimestamp val) { + return SetBSONValue(new BSONValue(BSONType.TIMESTAMP, key, val)); + } + + public BSONDocument SetCodeWScope(string key, BSONCodeWScope val) { + return SetBSONValue(new BSONValue(BSONType.CODEWSCOPE, key, val)); } public BSONValue DropValue(string key) { @@ -147,10 +187,53 @@ namespace Ejdb.SON { bw.Write((byte) 0x00); } } + os.Flush(); } //.////////////////////////////////////////////////////////////////// // Private staff //.////////////////////////////////////////////////////////////////// + internal BSONValue this[string key] { + get { + return GetBSONValue(key); + } + } + + internal BSONDocument Add(BSONValue bv) { + _fieldslist.Add(bv); + if (_fields != null) { + _fields[bv.Key] = bv; + } + return this; + } + + internal BSONValue GetBSONValue(string key) { + CheckFields(); + return _fields[key]; + } + + internal BSONDocument SetBSONValue(BSONValue val) { + CheckFields(); + var ov = _fields[val.Key]; + if (ov != null) { + ov.Key = val.Key; + ov.BSONType = val.BSONType; + ov.Value = val.Value; + } else { + _fieldslist.Add(val); + _fields[val.Key] = val; + } + return this; + } + + internal object GetObjectValue(string key) { + CheckFields(); + var bv = _fields[key]; + return bv != null ? bv.Value : null; + } + + protected virtual void CheckKey(string key) { + } + protected void WriteBSONValue(BSONValue bv, ExtBinaryWriter bw) { BSONType bt = bv.BSONType; switch (bt) { diff --git a/nejdb/Ejdb.SON/BSONOid.cs b/nejdb/Ejdb.SON/BSONOid.cs index a85595d..192d3da 100644 --- a/nejdb/Ejdb.SON/BSONOid.cs +++ b/nejdb/Ejdb.SON/BSONOid.cs @@ -25,6 +25,12 @@ namespace Ejdb.SON { internal byte[] Bytes; string _cachedString; + public BSONType BSONType { + get { + return BSONType.OID; + } + } + BSONOid() { } @@ -40,13 +46,6 @@ namespace Ejdb.SON { public BSONOid(BinaryReader reader) { Bytes = reader.ReadBytes(12); } - - - public BSONType BSONType { - get { - return BSONType.OID; - } - } bool IsValidOid(string oid) { var i = 0; diff --git a/nejdb/Ejdb.SON/BSONRegexp.cs b/nejdb/Ejdb.SON/BSONRegexp.cs index 5298844..516e505 100644 --- a/nejdb/Ejdb.SON/BSONRegexp.cs +++ b/nejdb/Ejdb.SON/BSONRegexp.cs @@ -21,20 +21,11 @@ namespace Ejdb.SON { /// BSON Regexp complex value. /// [Serializable] - public class BSONRegexp : IBSONValue { + public sealed class BSONRegexp : IBSONValue { readonly string _re; - readonly string _opts; - BSONRegexp() { - } - - public BSONRegexp(string re, string opts) { - this._re = re; - this._opts = opts; - } - public BSONType BSONType { get { return BSONType.REGEX; @@ -52,7 +43,15 @@ namespace Ejdb.SON { return this._opts; } } - + + BSONRegexp() { + } + + public BSONRegexp(string re, string opts) { + this._re = re; + this._opts = opts; + } + public override string ToString() { return string.Format("[BSONRegexp: re={0}, opts={1}]", _re, _opts); } @@ -72,7 +71,7 @@ namespace Ejdb.SON { unchecked { return (_re != null ? _re.GetHashCode() : 0) ^ (_opts != null ? _opts.GetHashCode() : 0); } - } + } } } diff --git a/nejdb/Ejdb.SON/BSONValue.cs b/nejdb/Ejdb.SON/BSONValue.cs index 6f93542..f73c62a 100644 --- a/nejdb/Ejdb.SON/BSONValue.cs +++ b/nejdb/Ejdb.SON/BSONValue.cs @@ -46,6 +46,10 @@ namespace Ejdb.SON { public BSONValue(BSONType type, string key) : this(type, key, null) { } + + public override string ToString() { + return string.Format("[BSONValue: BSONType={0}, Key={1}, Value={2}]", BSONType, Key, Value); + } } } diff --git a/nejdb/Ejdb.Tests/TestBSON.cs b/nejdb/Ejdb.Tests/TestBSON.cs new file mode 100644 index 0000000..1cb9422 --- /dev/null +++ b/nejdb/Ejdb.Tests/TestBSON.cs @@ -0,0 +1,48 @@ +// ============================================================================================ +// .NET API for EJDB database library http://ejdb.org +// Copyright (C) 2012-2013 Softmotions Ltd +// +// This file is part of EJDB. +// EJDB is free software; you can redistribute it and/or modify it under the terms of +// the GNU Lesser General Public License as published by the Free Software Foundation; either +// version 2.1 of the License or any later version. EJDB is distributed in the hope +// that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +// License for more details. +// You should have received a copy of the GNU Lesser General Public License along with EJDB; +// if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, +// Boston, MA 02111-1307 USA. +// ============================================================================================ +using System; +using NUnit.Framework; +using Ejdb.SON; +using System.IO; + +namespace Ejdb.Tests { + + [TestFixture] + public class TestBSON { + + [Test] + public void TestSerializeEmpty() { + MemoryStream ms = new MemoryStream(); + BSONDocument doc = new BSONDocument(); + doc.Serialize(ms); + string hv = TestUtils.ToHexString(ms.ToArray()); + ms.Close(); + Assert.AreEqual("05-00-00-00-00", hv); + } + + [Test] + public void TestSerializeSimple() { + MemoryStream ms = new MemoryStream(); + BSONDocument doc = new BSONDocument(); + doc.Serialize(ms); + string hv = TestUtils.ToHexString(ms.ToArray()); + ms.Close(); + Console.WriteLine("HV=" + hv); + } + + } +} + diff --git a/nejdb/Ejdb.Tests/TestOne.cs b/nejdb/Ejdb.Tests/TestUtils.cs similarity index 81% rename from nejdb/Ejdb.Tests/TestOne.cs rename to nejdb/Ejdb.Tests/TestUtils.cs index 2f6a625..23cbe00 100644 --- a/nejdb/Ejdb.Tests/TestOne.cs +++ b/nejdb/Ejdb.Tests/TestUtils.cs @@ -13,23 +13,13 @@ // if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // Boston, MA 02111-1307 USA. // ============================================================================================ - using System; -using NUnit.Framework; -using Ejdb.SON; namespace Ejdb.Tests { - [TestFixture()] - public class TestOne { - [Test()] - public void TestCase() { - //BSONType t; - Console.WriteLine("isdef=" + Enum.IsDefined(typeof(BSONType), (byte)1)); - } - - [Test] - public void TestCase2() { + public static class TestUtils { + internal static string ToHexString(byte[] bv) { + return BitConverter.ToString(bv); } } } diff --git a/nejdb/nejdb.csproj b/nejdb/nejdb.csproj index 1c0dd6c..f14018d 100644 --- a/nejdb/nejdb.csproj +++ b/nejdb/nejdb.csproj @@ -41,11 +41,9 @@ - - @@ -57,6 +55,9 @@ + + + -- 2.7.4