diff options
author | adam <adamansky@gmail.com> | 2013-06-06 09:29:15 +0700 |
---|---|---|
committer | adam <adamansky@gmail.com> | 2013-06-06 09:29:15 +0700 |
commit | 10a2f9ba08427ed0665a0f76f1482a3937bfbcb3 (patch) | |
tree | bda8196dc0793c5051b47676ddc41c907eee1170 | |
parent | e6e3ee2f8d9c346761c0e1b55886ca26a5f69534 (diff) | |
download | ejdb-10a2f9ba08427ed0665a0f76f1482a3937bfbcb3.tar.gz ejdb-10a2f9ba08427ed0665a0f76f1482a3937bfbcb3.tar.bz2 ejdb-10a2f9ba08427ed0665a0f76f1482a3937bfbcb3.zip |
#24
-rw-r--r-- | nejdb/Ejdb.SON/BSONIterator.cs | 102 | ||||
-rw-r--r-- | nejdb/Ejdb.SON/BSONOid.cs | 68 | ||||
-rw-r--r-- | nejdb/Ejdb.SON/BSONType.cs | 39 | ||||
-rw-r--r-- | nejdb/Ejdb.SON/BSONUtils.cs | 25 | ||||
-rw-r--r-- | nejdb/nejdb.csproj | 2 | ||||
-rw-r--r-- | nejdb/nejdb.userprefs | 12 |
6 files changed, 202 insertions, 46 deletions
diff --git a/nejdb/Ejdb.SON/BSONIterator.cs b/nejdb/Ejdb.SON/BSONIterator.cs index c340c43..d4f7ac6 100644 --- a/nejdb/Ejdb.SON/BSONIterator.cs +++ b/nejdb/Ejdb.SON/BSONIterator.cs @@ -17,15 +17,17 @@ using System; using System.IO; using System.Collections.Generic; using System.Collections; +using System.Text; namespace Ejdb.SON { - public class BSONIterator : IDisposable, IEnumerable<BSONType>, IEnumerator<BSONType> { + public class BSONIterator : IDisposable { - private readonly BinaryReader _input; - private BSONType _ctype; + private BinaryReader _input; private int _doclen; - private bool _first = true; + private BSONType _ctype = BSONType.UNKNOWN; + private string _entryKey; + private int _entryLen; public int DocumentLength { get { return this._doclen; } @@ -36,44 +38,98 @@ namespace Ejdb.SON { } public BSONIterator(Stream input) { - this._input = new BinaryReader(input); + if (!input.CanRead) { + Dispose(); + throw new IOException("Input stream must be readable"); + } + if (!input.CanSeek) { + Dispose(); + throw new IOException("Input stream must be seekable"); + } + this._input = new BinaryReader(input, Encoding.UTF8); + this._ctype = BSONType.EOO; this._doclen = _input.ReadInt32(); - byte bv = _input.ReadByte(); - if (!Enum.IsDefined(typeof(BSONType), (object) bv)) { - throw new InvalidBSONDataException("Unknown bson type: " + bv); + if (this._doclen < 5) { + Dispose(); + throw new InvalidBSONDataException(); } - this._ctype = (BSONType) bv; } ~BSONIterator() { Dispose(); } - public BSONType Current { - get { - return this._ctype; + public void Dispose() { + if (_input != null) { + _input.Close(); + _input = null; } } - BSONType IEnumerator.Current { - get { - return this._ctype; + public BSONType Next() { + if (_ctype != BSONType.UNKNOWN) { + if (_ctype == BSONType.EOO) { + return BSONType.EOO; + } + SkipData(); } + byte bv = _input.ReadByte(); + if (!Enum.IsDefined(typeof(BSONType), (object) bv)) { + throw new InvalidBSONDataException("Unknown bson type: " + bv); + } + _ctype = (BSONType) bv; + _entryKey = null; + _entryLen = 0; + if (_ctype != BSONType.EOO) { + ReadKey(); + } + switch (_ctype) { + case BSONType.EOO: + return BSONType.EOO; + case BSONType.UNDEFINED: + case BSONType.NULL: + break; + case BSONType.BOOL: + _entryLen = 1; + break; + case BSONType.INT: + _entryLen = 4; + break; + case BSONType.LONG: + case BSONType.DOUBLE: + case BSONType.TIMESTAMP: + case BSONType.DATE: + _entryLen = 8; + break; + case BSONType.OID: + _entryLen = 12; + break; + case BSONType.STRING: + case BSONType.CODE: + case BSONType.SYMBOL: + break; + } + return BSONType.EOO; } - IEnumerator IEnumerable.GetEnumerator() { - return GetEnumerator(); + private void SkipData() { + if (_entryLen > 0) { + var len = _input.BaseStream.Seek(_entryLen, SeekOrigin.Current); + _entryLen = 0; + } } - public IEnumerator<BSONType> GetEnumerator() { - return this; + private string ReadKey() { + //todo + return string.Empty; } - public void Dispose() { - if (_input != null) { - _input.Close(); - } + private string ReadCString() { + //todo + return string.Empty; } + + } } diff --git a/nejdb/Ejdb.SON/BSONOid.cs b/nejdb/Ejdb.SON/BSONOid.cs new file mode 100644 index 0000000..06b22a6 --- /dev/null +++ b/nejdb/Ejdb.SON/BSONOid.cs @@ -0,0 +1,68 @@ +// ============================================================================================ +// .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.SON { + + public sealed class BSONOid { + + private byte[] _bytes; + + private BSONOid() { + } + + public BSONOid(string val) { + if (val == null) { + throw new ArgumentNullException("val"); + } + ParseOIDString(val); + } + + public BSONOid(byte[] val) { + if (val == null) { + throw new ArgumentNullException("val"); + } + _bytes = new byte[12]; + Array.Copy(val, _bytes, 12); + } + + private bool IsValidOid(string oid) { + var i = 0; + for (; i < oid.Length && + ((oid[i] >= 0x30 && oid[i] <= 0x39) || (oid[i] >= 0x61 && oid[i] <= 0x66)); + ++i) { + } + return (i == 24); + } + + private void ParseOIDString(string val) { + if (!IsValidOid(val)) { + throw new ArgumentException("Invalid oid string"); + } + var vlen = val.Length; + _bytes = new byte[vlen / 2]; + for (var i = 0; i < vlen; i += 2) { + try { + _bytes[i / 2] = Convert.ToByte(val.Substring(i, 2), 16); + } catch { + //failed to convert these 2 chars, they may contain illegal charracters + _bytes[i / 2] = 0; + } + } + } + } +} + diff --git a/nejdb/Ejdb.SON/BSONType.cs b/nejdb/Ejdb.SON/BSONType.cs index f0592b5..d36b68b 100644 --- a/nejdb/Ejdb.SON/BSONType.cs +++ b/nejdb/Ejdb.SON/BSONType.cs @@ -22,25 +22,26 @@ namespace Ejdb.SON { /** <summary> BSON types according to the bsonspec (http://bsonspec.org/)</summary> */ public enum BSONType : byte { - BSON_EOO = 0, - BSON_DOUBLE = 1, - BSON_STRING = 2, - BSON_OBJECT = 3, - BSON_ARRAY = 4, - BSON_BINDATA = 5, - BSON_UNDEFINED = 6, - BSON_OID = 7, - BSON_BOOL = 8, - BSON_DATE = 9, - BSON_NULL = 10, - BSON_REGEX = 11, - BSON_DBREF = 12, /**< Deprecated. */ - BSON_CODE = 13, - BSON_SYMBOL = 14, - BSON_CODEWSCOPE = 15, - BSON_INT = 16, - BSON_TIMESTAMP = 17, - BSON_LONG = 18 + UNKNOWN = 0xfe, + EOO = 0, + DOUBLE = 1, + STRING = 2, + OBJECT = 3, + ARRAY = 4, + BINDATA = 5, + UNDEFINED = 6, + OID = 7, + BOOL = 8, + DATE = 9, + NULL = 10, + REGEX = 11, + DBREF = 12, /**< Deprecated. */ + CODE = 13, + SYMBOL = 14, + CODEWSCOPE = 15, + INT = 16, + TIMESTAMP = 17, + LONG = 18 } } diff --git a/nejdb/Ejdb.SON/BSONUtils.cs b/nejdb/Ejdb.SON/BSONUtils.cs new file mode 100644 index 0000000..844c6e0 --- /dev/null +++ b/nejdb/Ejdb.SON/BSONUtils.cs @@ -0,0 +1,25 @@ +// ============================================================================================ +// .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.SON { + + public static class BSONUtils { + static BSONUtils() { + } + } +} + diff --git a/nejdb/nejdb.csproj b/nejdb/nejdb.csproj index ea04393..1a68961 100644 --- a/nejdb/nejdb.csproj +++ b/nejdb/nejdb.csproj @@ -44,6 +44,8 @@ <Compile Include="Ejdb.SON\BSONIterator.cs" /> <Compile Include="Ejdb.SON\BSONType.cs" /> <Compile Include="Ejdb.SON\InvalidBSONDataException.cs" /> + <Compile Include="Ejdb.SON\BSONUtils.cs" /> + <Compile Include="Ejdb.SON\BSONOid.cs" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <ItemGroup> diff --git a/nejdb/nejdb.userprefs b/nejdb/nejdb.userprefs index 1215bde..b39729e 100644 --- a/nejdb/nejdb.userprefs +++ b/nejdb/nejdb.userprefs @@ -1,18 +1,22 @@ <Properties> <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" /> - <MonoDevelop.Ide.Workbench ActiveDocument="Ejdb.SON/BSONIterator.cs"> + <MonoDevelop.Ide.Workbench ActiveDocument="Ejdb.SON/BSONOid.cs"> <Files> <File FileName="Ejdb.SON/BSONMarshaller.cs" Line="1" Column="1" /> - <File FileName="Ejdb.SON/BSONIterator.cs" Line="29" Column="1" /> + <File FileName="Ejdb.SON/BSONIterator.cs" Line="93" Column="5" /> + <File FileName="Ejdb.SON/BSONType.cs" Line="31" Column="15" /> + <File FileName="Ejdb.SON/BSONOid.cs" Line="24" Column="1" /> </Files> <Pads> <Pad Id="ProjectPad"> - <State expanded="True" selected="True"> + <State expanded="True"> <Node name="Solution Items" expanded="True" /> <Node name="nejdb" expanded="True"> <Node name="References" expanded="True" /> <Node name="Ejdb.DB" expanded="True" /> - <Node name="Ejdb.SON" expanded="True" /> + <Node name="Ejdb.SON" expanded="True"> + <Node name="BSONOid.cs" selected="True" /> + </Node> <Node name="Ejdb.Tests" expanded="True" /> </Node> </State> |