diff options
author | adam <adamansky@gmail.com> | 2013-06-12 00:58:54 +0700 |
---|---|---|
committer | adam <adamansky@gmail.com> | 2013-06-12 00:58:54 +0700 |
commit | dc188a158ba5d6e53b0508ffe7841c548a321fab (patch) | |
tree | e42db531ba6ecc0b21c31748fbd25cdf09b76a48 | |
parent | c4d3d94944a14010c17da1b2603ceceebcd06848 (diff) | |
download | ejdb-dc188a158ba5d6e53b0508ffe7841c548a321fab.tar.gz ejdb-dc188a158ba5d6e53b0508ffe7841c548a321fab.tar.bz2 ejdb-dc188a158ba5d6e53b0508ffe7841c548a321fab.zip |
#24
-rw-r--r-- | nejdb/Ejdb.SON/BSONArray.cs | 1 | ||||
-rw-r--r-- | nejdb/Ejdb.SON/BSONCodeWScope.cs | 24 | ||||
-rw-r--r-- | nejdb/Ejdb.SON/BSONDocument.cs | 53 | ||||
-rw-r--r-- | nejdb/Ejdb.SON/BSONOid.cs | 22 | ||||
-rw-r--r-- | nejdb/Ejdb.SON/BSONRegexp.cs | 19 | ||||
-rw-r--r-- | nejdb/Ejdb.SON/BSONTimestamp.cs | 23 | ||||
-rw-r--r-- | nejdb/Ejdb.Tests/TestBSON.cs | 5 | ||||
-rw-r--r-- | nejdb/Ejdb.Tests/TestUtils.cs | 26 | ||||
-rw-r--r-- | nejdb/nejdb.csproj | 1 | ||||
-rw-r--r-- | nejdb/nejdb.userprefs | 20 |
10 files changed, 119 insertions, 75 deletions
diff --git a/nejdb/Ejdb.SON/BSONArray.cs b/nejdb/Ejdb.SON/BSONArray.cs index b05db90..9025a5a 100644 --- a/nejdb/Ejdb.SON/BSONArray.cs +++ b/nejdb/Ejdb.SON/BSONArray.cs @@ -17,6 +17,7 @@ using System; namespace Ejdb.SON { + [Serializable] public class BSONArray : BSONDocument { public override BSONType BSONType { diff --git a/nejdb/Ejdb.SON/BSONCodeWScope.cs b/nejdb/Ejdb.SON/BSONCodeWScope.cs index e798eec..a5ebd4c 100644 --- a/nejdb/Ejdb.SON/BSONCodeWScope.cs +++ b/nejdb/Ejdb.SON/BSONCodeWScope.cs @@ -17,7 +17,8 @@ using System; namespace Ejdb.SON { - public class BSONCodeWScope : BSONDocument { + [Serializable] + public sealed class BSONCodeWScope : BSONDocument { readonly string _code; @@ -42,6 +43,27 @@ namespace Ejdb.SON { public BSONCodeWScope(string code) { this._code = code; } + + public override bool Equals(object obj) { + if (obj == null) { + return false; + } + if (ReferenceEquals(this, obj)) { + return true; + } + if (!(obj is BSONCodeWScope)) { + return false; + } + BSONCodeWScope cw = (BSONCodeWScope) obj; + if (_code != cw._code) { + return false; + } + return base.Equals(obj); + } + + public override int GetHashCode() { + return (_code != null ? _code.GetHashCode() : 0) ^ base.GetHashCode(); + } } } diff --git a/nejdb/Ejdb.SON/BSONDocument.cs b/nejdb/Ejdb.SON/BSONDocument.cs index fd4c657..91e94dc 100644 --- a/nejdb/Ejdb.SON/BSONDocument.cs +++ b/nejdb/Ejdb.SON/BSONDocument.cs @@ -27,9 +27,33 @@ namespace Ejdb.SON { /// BSON document deserialized data wrapper. /// </summary> [Serializable] - public class BSONDocument : IBSONValue, IEnumerable<BSONValue> { - Dictionary<string, BSONValue> _fields; + public class BSONDocument : IBSONValue, IEnumerable<BSONValue>, ICloneable { + static Dictionary<Type, Action<BSONDocument, string, object>> TYPE_SETTERS = + new Dictionary<Type, Action<BSONDocument, string, object>> { + {typeof(bool), (d, k, v) => d.SetBool(k, (bool) v)}, + {typeof(byte), (d, k, v) => d.SetNumber(k, (int) v)}, + {typeof(sbyte), (d, k, v) => d.SetNumber(k, (int) v)}, + {typeof(ushort), (d, k, v) => d.SetNumber(k, (int) v)}, + {typeof(short), (d, k, v) => d.SetNumber(k, (int) v)}, + {typeof(uint), (d, k, v) => d.SetNumber(k, (int) v)}, + {typeof(int), (d, k, v) => d.SetNumber(k, (int) v)}, + {typeof(ulong), (d, k, v) => d.SetNumber(k, (long) v)}, + {typeof(long), (d, k, v) => d.SetNumber(k, (long) v)}, + {typeof(float), (d, k, v) => d.SetNumber(k, (float) v)}, + {typeof(double), (d, k, v) => d.SetNumber(k, (double) v)}, + {typeof(char), (d, k, v) => d.SetString(k, v.ToString())}, + {typeof(string), (d, k, v) => d.SetString(k, (string) v)}, + {typeof(BSONOid), (d, k, v) => d.SetOID(k, (BSONOid) v)}, + {typeof(BSONRegexp), (d, k, v) => d.SetRegexp(k, (BSONRegexp) v)}, + {typeof(BSONValue), (d, k, v) => d.SetBSONValue((BSONValue) v)}, + {typeof(BSONTimestamp), (d, k, v) => d.SetTimestamp(k, (BSONTimestamp) v)}, + {typeof(BSONCodeWScope), (d, k, v) => d.SetCodeWScope(k, (BSONCodeWScope) v)}, + {typeof(BSONBinData), (d, k, v) => d.SetBinData(k, (BSONBinData) v)}, + }; readonly List<BSONValue> _fieldslist; + [NonSerializedAttribute] + Dictionary<string, BSONValue> _fields; + [NonSerializedAttribute] int? _cachedhash; /// <summary> @@ -161,6 +185,19 @@ namespace Ejdb.SON { get { return GetObjectValue(key); } + set { + object v = value; + if (v == null) { + SetNull(key); + return; + } + Action<BSONDocument, string, object> setter; + TYPE_SETTERS.TryGetValue(v.GetType(), out setter); + if (setter == null) { + throw new Exception(string.Format("Unsupported value type: {0} for doc[key] assign operation", v.GetType())); + } + setter(this, key, v); + } } public BSONDocument SetNull(string key) { @@ -347,6 +384,14 @@ namespace Ejdb.SON { public static bool operator !=(BSONDocument d1, BSONDocument d2) { return !(d1 == d2); } + + public object Clone() { + return new BSONDocument(this); + } + + public override string ToString() { + return string.Format("[{0}: {1}]", GetType().Name, _fieldslist); + } //.////////////////////////////////////////////////////////////////// // Private staff //.////////////////////////////////////////////////////////////////// @@ -392,8 +437,8 @@ namespace Ejdb.SON { { WriteTypeAndKey(bv, bw); BSONOid oid = (BSONOid) bv.Value; - Debug.Assert(oid.Bytes.Length == 12); - bw.Write(oid.Bytes); + Debug.Assert(oid._bytes.Length == 12); + bw.Write(oid._bytes); break; } case BSONType.STRING: diff --git a/nejdb/Ejdb.SON/BSONOid.cs b/nejdb/Ejdb.SON/BSONOid.cs index 9059792..61410b7 100644 --- a/nejdb/Ejdb.SON/BSONOid.cs +++ b/nejdb/Ejdb.SON/BSONOid.cs @@ -22,7 +22,7 @@ namespace Ejdb.SON { [Serializable] public sealed class BSONOid : IComparable<BSONOid>, IBSONValue { - internal byte[] Bytes; + internal byte[] _bytes; string _cachedString; public BSONType BSONType { @@ -39,12 +39,12 @@ namespace Ejdb.SON { } public BSONOid(byte[] val) { - Bytes = new byte[12]; - Array.Copy(val, Bytes, 12); + _bytes = new byte[12]; + Array.Copy(val, _bytes, 12); } public BSONOid(BinaryReader reader) { - Bytes = reader.ReadBytes(12); + _bytes = reader.ReadBytes(12); } bool IsValidOid(string oid) { @@ -61,9 +61,9 @@ namespace Ejdb.SON { throw new ArgumentException("Invalid oid string"); } var vlen = val.Length; - Bytes = new byte[vlen / 2]; + _bytes = new byte[vlen / 2]; for (var i = 0; i < vlen; i += 2) { - Bytes[i / 2] = Convert.ToByte(val.Substring(i, 2), 16); + _bytes[i / 2] = Convert.ToByte(val.Substring(i, 2), 16); } } @@ -71,12 +71,12 @@ namespace Ejdb.SON { if (ReferenceEquals(other, null)) { return 1; } - var obytes = other.Bytes; - for (var x = 0; x < Bytes.Length; x++) { - if (Bytes[x] < obytes[x]) { + var obytes = other._bytes; + for (var x = 0; x < _bytes.Length; x++) { + if (_bytes[x] < obytes[x]) { return -1; } - if (Bytes[x] > obytes[x]) { + if (_bytes[x] > obytes[x]) { return 1; } } @@ -85,7 +85,7 @@ namespace Ejdb.SON { public override string ToString() { if (_cachedString == null) { - _cachedString = BitConverter.ToString(Bytes).Replace("-", "").ToLower(); + _cachedString = BitConverter.ToString(_bytes).Replace("-", "").ToLower(); } return _cachedString; } diff --git a/nejdb/Ejdb.SON/BSONRegexp.cs b/nejdb/Ejdb.SON/BSONRegexp.cs index 516e505..23ee8e4 100644 --- a/nejdb/Ejdb.SON/BSONRegexp.cs +++ b/nejdb/Ejdb.SON/BSONRegexp.cs @@ -52,19 +52,18 @@ namespace Ejdb.SON { this._opts = opts; } - public override string ToString() { - return string.Format("[BSONRegexp: re={0}, opts={1}]", _re, _opts); - } - public override bool Equals(object obj) { - if (obj == null) + if (obj == null) { return false; - if (ReferenceEquals(this, obj)) + } + if (ReferenceEquals(this, obj)) { return true; - if (obj.GetType() != typeof(BSONRegexp)) + } + if (!(obj is BSONRegexp)) { return false; + } BSONRegexp other = (BSONRegexp) obj; - return _re == other._re && _opts == other._opts; + return (_re == other._re && _opts == other._opts); } public override int GetHashCode() { @@ -72,6 +71,10 @@ namespace Ejdb.SON { return (_re != null ? _re.GetHashCode() : 0) ^ (_opts != null ? _opts.GetHashCode() : 0); } } + + public override string ToString() { + return string.Format("[BSONRegexp: re={0}, opts={1}]", _re, _opts); + } } } diff --git a/nejdb/Ejdb.SON/BSONTimestamp.cs b/nejdb/Ejdb.SON/BSONTimestamp.cs index 6b38315..763acf8 100644 --- a/nejdb/Ejdb.SON/BSONTimestamp.cs +++ b/nejdb/Ejdb.SON/BSONTimestamp.cs @@ -21,7 +21,7 @@ namespace Ejdb.SON { /// BSON Timestamp complex value. /// </summary> [Serializable] - public class BSONTimestamp : IBSONValue { + public sealed class BSONTimestamp : IBSONValue { readonly int _inc; readonly int _ts; @@ -52,26 +52,29 @@ namespace Ejdb.SON { } } - public override string ToString() { - return string.Format("[BSONTimestamp: inc={0}, ts={1}]", _inc, _ts); - } - public override bool Equals(object obj) { - if (obj == null) + if (obj == null) { return false; - if (ReferenceEquals(this, obj)) + } + if (ReferenceEquals(this, obj)) { return true; - if (obj.GetType() != typeof(BSONTimestamp)) + } + if (!(obj is BSONTimestamp)) { return false; + } BSONTimestamp other = (BSONTimestamp) obj; - return _inc == other._inc && _ts == other._ts; + return (_inc == other._inc && _ts == other._ts); } public override int GetHashCode() { unchecked { - return _inc.GetHashCode() ^ _ts.GetHashCode(); + return (_inc.GetHashCode() ^ _ts.GetHashCode()); } } + + public override string ToString() { + return string.Format("[BSONTimestamp: inc={0}, ts={1}]", _inc, _ts); + } } } diff --git a/nejdb/Ejdb.Tests/TestBSON.cs b/nejdb/Ejdb.Tests/TestBSON.cs index f9ac5a8..de48950 100644 --- a/nejdb/Ejdb.Tests/TestBSON.cs +++ b/nejdb/Ejdb.Tests/TestBSON.cs @@ -78,6 +78,11 @@ namespace Ejdb.Tests { doc2 = new BSONDocument(doc2.ToByteArray()); Assert.AreEqual("13-00-00-00-10-30-00-02-00-00-00-10-31-00-FF-FF-FF-7F-00", doc2.ToDebugDataString()); + + + doc = new BSONDocument(); + doc["a"] = 1; + Assert.AreEqual("0C-00-00-00-10-61-00-01-00-00-00-00", doc.ToDebugDataString()); } } } diff --git a/nejdb/Ejdb.Tests/TestUtils.cs b/nejdb/Ejdb.Tests/TestUtils.cs deleted file mode 100644 index 23cbe00..0000000 --- a/nejdb/Ejdb.Tests/TestUtils.cs +++ /dev/null @@ -1,26 +0,0 @@ -// ============================================================================================ -// .NET API for EJDB database library http://ejdb.org -// Copyright (C) 2012-2013 Softmotions Ltd <info@softmotions.com> -// -// 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; - -namespace Ejdb.Tests { - - 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 f14018d..cb4bc9d 100644 --- a/nejdb/nejdb.csproj +++ b/nejdb/nejdb.csproj @@ -57,7 +57,6 @@ <Compile Include="Ejdb.IO\ExtBinaryWriter.cs" /> <Compile Include="Ejdb.SON\BSONConstants.cs" /> <Compile Include="Ejdb.Tests\TestBSON.cs" /> - <Compile Include="Ejdb.Tests\TestUtils.cs" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <ItemGroup> diff --git a/nejdb/nejdb.userprefs b/nejdb/nejdb.userprefs index b090246..823972a 100644 --- a/nejdb/nejdb.userprefs +++ b/nejdb/nejdb.userprefs @@ -1,15 +1,9 @@ <Properties> <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" /> - <MonoDevelop.Ide.Workbench ActiveDocument="Ejdb.SON/BSONValue.cs"> + <MonoDevelop.Ide.Workbench ActiveDocument="Ejdb.SON/BSONDocument.cs"> <Files> - <File FileName="Ejdb.Tests/TestBSON.cs" Line="56" Column="23" /> - <File FileName="Ejdb.SON/BSONDocument.cs" Line="321" Column="12" /> - <File FileName="Ejdb.SON/BSONValue.cs" Line="84" Column="3" /> - <File FileName="Ejdb.SON/BSONOid.cs" Line="107" Column="36" /> - <File FileName="Ejdb.SON/BSONIterator.cs" Line="160" Column="1" /> - <File FileName="Ejdb.SON/BSONType.cs" Line="26" Column="8" /> - <File FileName="Ejdb.Tests/TestUtils.cs" Line="22" Column="36" /> - <File FileName="Ejdb.SON/BSONArray.cs" Line="30" Column="5" /> + <File FileName="Ejdb.SON/BSONDocument.cs" Line="71" Column="3" /> + <File FileName="Ejdb.Tests/TestBSON.cs" Line="46" Column="4" /> </Files> <Pads> <Pad Id="ProjectPad"> @@ -20,7 +14,7 @@ <Node name="Ejdb.DB" expanded="True" /> <Node name="Ejdb.IO" expanded="True" /> <Node name="Ejdb.SON" expanded="True"> - <Node name="BSONValue.cs" selected="True" /> + <Node name="BSONDocument.cs" selected="True" /> </Node> <Node name="Ejdb.Tests" expanded="True" /> </Node> @@ -38,13 +32,11 @@ <State selected="True" /> </Pad> <Pad Id="MonoDevelop.NUnit.TestPad"> - <State expanded="True"> + <State expanded="True" selected="True"> <Node name="nejdb" expanded="True"> <Node name="Ejdb" expanded="True"> <Node name="Tests" expanded="True"> - <Node name="TestBSON" expanded="True"> - <Node name="TestSerialize1" selected="True" /> - </Node> + <Node name="TestBSON" expanded="True" /> </Node> </Node> </Node> |