summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoradam <adamansky@gmail.com>2013-06-11 01:45:37 +0700
committeradam <adamansky@gmail.com>2013-06-11 01:45:37 +0700
commit4263cb2b085497d8a7158c8a8a24769244e846c3 (patch)
treef3e98f2e0c5e7ed940445db9e963fb57f7db279a
parente6654b87a9680ad2e486b7f653821ea16e145991 (diff)
downloadejdb-4263cb2b085497d8a7158c8a8a24769244e846c3.tar.gz
ejdb-4263cb2b085497d8a7158c8a8a24769244e846c3.tar.bz2
ejdb-4263cb2b085497d8a7158c8a8a24769244e846c3.zip
#24
-rw-r--r--nejdb/Ejdb.SON/BSONArray.cs91
-rw-r--r--nejdb/Ejdb.SON/BSONBinData.cs9
-rw-r--r--nejdb/Ejdb.SON/BSONConstants.cs (renamed from nejdb/Ejdb.SON/BSON.cs)0
-rw-r--r--nejdb/Ejdb.SON/BSONDocument.cs149
-rw-r--r--nejdb/Ejdb.SON/BSONOid.cs13
-rw-r--r--nejdb/Ejdb.SON/BSONRegexp.cs23
-rw-r--r--nejdb/Ejdb.SON/BSONValue.cs4
-rw-r--r--nejdb/Ejdb.Tests/TestBSON.cs48
-rw-r--r--nejdb/Ejdb.Tests/TestUtils.cs (renamed from nejdb/Ejdb.Tests/TestOne.cs)16
-rw-r--r--nejdb/nejdb.csproj5
10 files changed, 290 insertions, 68 deletions
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
index daf47b5..daf47b5 100644
--- a/nejdb/Ejdb.SON/BSON.cs
+++ b/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.
/// </summary>
[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 <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;
+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
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 @@
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
<Compile Include="Ejdb.DB\EJDB.cs" />
- <Compile Include="Ejdb.Tests\TestOne.cs" />
<Compile Include="Ejdb.SON\BSONType.cs" />
<Compile Include="Ejdb.SON\InvalidBSONDataException.cs" />
<Compile Include="Ejdb.SON\BSONOid.cs" />
- <Compile Include="Ejdb.SON\BSON.cs" />
<Compile Include="Ejdb.SON\BSONTimestamp.cs" />
<Compile Include="Ejdb.SON\BSONRegexp.cs" />
<Compile Include="Ejdb.SON\IBSONValue.cs" />
@@ -57,6 +55,9 @@
<Compile Include="Ejdb.SON\BSONDocument.cs" />
<Compile Include="Ejdb.IO\ExtBinaryReader.cs" />
<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>