diff options
author | adam <adamansky@gmail.com> | 2013-06-18 09:04:59 +0700 |
---|---|---|
committer | adam <adamansky@gmail.com> | 2013-06-18 09:04:59 +0700 |
commit | 2a40dfcb1e9a1cff29c72bea186af630658c146c (patch) | |
tree | a96cae2c5e3fa777e0b5f17129159ca9ce36a9a5 /nejdb | |
parent | 5530443606234178360e3289ffc0da5992bd58bc (diff) | |
download | ejdb-2a40dfcb1e9a1cff29c72bea186af630658c146c.tar.gz ejdb-2a40dfcb1e9a1cff29c72bea186af630658c146c.tar.bz2 ejdb-2a40dfcb1e9a1cff29c72bea186af630658c146c.zip |
#24
Diffstat (limited to 'nejdb')
-rw-r--r-- | nejdb/Ejdb.BSON/BSONDocument.cs | 3 | ||||
-rw-r--r-- | nejdb/Ejdb.DB/EJDB.cs | 74 | ||||
-rw-r--r-- | nejdb/Ejdb.Tests/TestEJDB.cs | 41 | ||||
-rw-r--r-- | nejdb/nejdb.csproj | 1 | ||||
-rw-r--r-- | nejdb/nejdb.userprefs | 22 |
5 files changed, 124 insertions, 17 deletions
diff --git a/nejdb/Ejdb.BSON/BSONDocument.cs b/nejdb/Ejdb.BSON/BSONDocument.cs index 58821a3..964faac 100644 --- a/nejdb/Ejdb.BSON/BSONDocument.cs +++ b/nejdb/Ejdb.BSON/BSONDocument.cs @@ -449,6 +449,9 @@ namespace Ejdb.BSON { internal BSONDocument SetBSONValue(BSONValue val) { _cachedhash = null; CheckFields(); + if (val.BSONType == BSONType.STRING && val.Key == "_id") { + val = new BSONValue(BSONType.OID, val.Key, new BSONOid((string) val.Value)); + } BSONValue ov; if (_fields.TryGetValue(val.Key, out ov)) { ov.Key = val.Key; diff --git a/nejdb/Ejdb.DB/EJDB.cs b/nejdb/Ejdb.DB/EJDB.cs index 1ff5442..824b3a2 100644 --- a/nejdb/Ejdb.DB/EJDB.cs +++ b/nejdb/Ejdb.DB/EJDB.cs @@ -15,6 +15,9 @@ // ============================================================================================ using System; using System.Runtime.InteropServices; +using Mono.Unix; +using System.Text; +using Ejdb.BSON; namespace Ejdb.DB { @@ -29,7 +32,7 @@ namespace Ejdb.DB { public const int JBOTSYNC = 1 << 6; public const int DEFAULT_OPEN_MODE = (JBOWRITER | JBOCREAT); public const string EJDB_LIB_NAME = "tcejdb"; - IntPtr _db = (IntPtr) 0x0; + IntPtr _db = IntPtr.Zero; #region Functions [DllImport(EJDB_LIB_NAME, EntryPoint="ejdbnew")] static extern IntPtr _ejdbnew(); @@ -37,8 +40,17 @@ namespace Ejdb.DB { [DllImport(EJDB_LIB_NAME, EntryPoint="ejdbdel")] static extern IntPtr _ejdbdel(IntPtr db); - [DllImport(EJDB_LIB_NAME, EntryPoint="ejdbopen", CharSet=CharSet.Auto)] - static extern bool _ejdbopen(IntPtr db, string path, int mode); + [DllImport(EJDB_LIB_NAME, EntryPoint="ejdbopen")] + static extern bool _ejdbopen(IntPtr db, IntPtr path, int mode); + + static bool _ejdbopen(IntPtr db, string path, int mode) { + IntPtr pptr = UnixMarshal.StringToHeap(path, Encoding.UTF8); + try { + return _ejdbopen(db, pptr, mode); + } finally { + UnixMarshal.FreeHeap(pptr); + } + } [DllImport(EJDB_LIB_NAME, EntryPoint="ejdbclose")] static extern bool _ejdbclose(IntPtr db); @@ -50,7 +62,22 @@ namespace Ejdb.DB { static extern int _ejdbecode(IntPtr db); [DllImport(EJDB_LIB_NAME, EntryPoint="ejdberrmsg")] - static extern string _ejdberrmsg(int ecode); + static extern IntPtr _ejdberrmsg(int ecode); + + [DllImport(EJDB_LIB_NAME, EntryPoint="ejdbgetcoll")] + static extern IntPtr _ejdbgetcoll(IntPtr db, IntPtr cname); + + static IntPtr _ejdbgetcoll(IntPtr db, string cname) { + IntPtr cptr = UnixMarshal.StringToHeap(cname, Encoding.UTF8); + try { + return _ejdbgetcoll(db, cptr); + } finally { + UnixMarshal.FreeHeap(cptr); + } + } + //EJDB_EXPORT bool ejdbsavebson3(EJCOLL *jcoll, void *bsdata, bson_oid_t *oid, bool merge); + [DllImport(EJDB_LIB_NAME, EntryPoint="ejdbsavebson3")] + static extern IntPtr _ejdbsavebson(IntPtr coll, [In] byte[] bsdata, [Out] byte[] oid, bool merge); #endregion /// <summary> /// Gets the last DB error code or <c>null</c> if underlying native database object does not exist. @@ -58,7 +85,7 @@ namespace Ejdb.DB { /// <value>The last DB error code.</value> public int? LastDBErrorCode { get { - return (_db.ToInt32() != 0x0) ? (int?) _ejdbecode(_db) : null; + return (_db != IntPtr.Zero) ? (int?) _ejdbecode(_db) : null; } } @@ -68,7 +95,10 @@ namespace Ejdb.DB { public string LastDBErrorMsg { get { int? ecode = LastDBErrorCode; - return ecode != null ? _ejdberrmsg((int) ecode) : null; + if (ecode == null) { + return null; + } + return UnixMarshal.PtrToString(_ejdberrmsg((int) ecode), Encoding.UTF8); } } @@ -87,10 +117,10 @@ namespace Ejdb.DB { /// </summary> /// <param name="path">The main database file path.</param> /// <param name="omode">Open mode.</param> - public EJDB(string path, int omode) { + public EJDB(string path, int omode=DEFAULT_OPEN_MODE) { bool rv; _db = _ejdbnew(); - if (_db.ToInt32() == 0x0) { + if (_db == IntPtr.Zero) { throw new EJDBException("Unable to create ejdb instance"); } try { @@ -109,14 +139,36 @@ namespace Ejdb.DB { } public void Dispose() { - if (_db.ToInt32() != 0x0) { + if (_db != IntPtr.Zero) { IntPtr db = _db; - _db = (IntPtr) 0x0; - if (db.ToInt32() != 0x0) { + _db = IntPtr.Zero; + if (_db != IntPtr.Zero) { _ejdbdel(db); } } } + + /// <summary> + /// Save the BSON document doc into the collection cname. + /// </summary> + /// <param name="cname">Name of collection.</param> + /// <param name="doc">BSON document to save.</param> + /// <param name="merge">If set to <c>true</c> + /// If true the merge will be performend with old and new objects. + /// Otherwise old object will be replaced.</param> + /// <returns>True on success.</returns> + public bool Save(string cname, BSONDocument doc, bool merge) { + bool rv = false; + IntPtr cptr = _ejdbgetcoll(_db, cname); + if (cptr == IntPtr.Zero) { + return false; + } + BSONValue bv = doc.GetBSONValue("_id"); + byte[] bdoc = doc.ToByteArray(); + //todo + + return rv; + } } } diff --git a/nejdb/Ejdb.Tests/TestEJDB.cs b/nejdb/Ejdb.Tests/TestEJDB.cs new file mode 100644 index 0000000..44ac7d5 --- /dev/null +++ b/nejdb/Ejdb.Tests/TestEJDB.cs @@ -0,0 +1,41 @@ +// ============================================================================================ +// .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.DB; + +namespace Ejdb.Tests { + + [TestFixture] + public class TestEJDB { + + [Test] + public void TestOpenClose() { + EJDB jb = new EJDB("testdb1", EJDB.DEFAULT_OPEN_MODE | EJDB.JBOTRUNC); + Assert.IsTrue(jb.IsOpen); + Assert.AreEqual(0, jb.LastDBErrorCode); + Assert.AreEqual("success", jb.LastDBErrorMsg); + jb.Dispose(); + Assert.IsFalse(jb.IsOpen); + jb.Dispose(); //double dispose + } + + + + + } +} + diff --git a/nejdb/nejdb.csproj b/nejdb/nejdb.csproj index 266852e..eb07c23 100644 --- a/nejdb/nejdb.csproj +++ b/nejdb/nejdb.csproj @@ -63,6 +63,7 @@ <Compile Include="Ejdb.JSON\JSONReader.cs" /> <Compile Include="Ejdb.JSON\JSONElement.cs" /> <Compile Include="Ejdb.DB\EJDBException.cs" /> + <Compile Include="Ejdb.Tests\TestEJDB.cs" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <ItemGroup> diff --git a/nejdb/nejdb.userprefs b/nejdb/nejdb.userprefs index 2120258..4b8db9d 100644 --- a/nejdb/nejdb.userprefs +++ b/nejdb/nejdb.userprefs @@ -2,11 +2,12 @@ <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" /> <MonoDevelop.Ide.Workbench ActiveDocument="Ejdb.DB/EJDB.cs"> <Files> - <File FileName="Ejdb.Tests/TestBSON.cs" Line="27" Column="37" /> - <File FileName="Ejdb.DB/EJDB.cs" Line="26" Column="38" /> - <File FileName="Ejdb.DB/EJDBException.cs" Line="25" Column="4" /> - <File FileName="Ejdb.BSON/InvalidBSONDataException.cs" Line="22" Column="36" /> - <File FileName="Ejdb.BSON/BSONType.cs" Line="20" Column="15" /> + <File FileName="Ejdb.Tests/TestBSON.cs" Line="27" Column="21" /> + <File FileName="Ejdb.DB/EJDB.cs" Line="80" Column="74" /> + <File FileName="Ejdb.BSON/BSONType.cs" Line="1" Column="1" /> + <File FileName="Ejdb.Tests/TestEJDB.cs" Line="36" Column="3" /> + <File FileName="Ejdb.BSON/BSONDocument.cs" Line="191" Column="24" /> + <File FileName="Ejdb.BSON/BSONOid.cs" Line="133" Column="4" /> </Files> <Pads> <Pad Id="ProjectPad"> @@ -42,7 +43,16 @@ <State selected="True" /> </Pad> <Pad Id="MonoDevelop.NUnit.TestPad"> - <State expanded="True" selected="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="TestEJDB" expanded="True" /> + </Node> + </Node> + </Node> + </State> </Pad> </Pads> </MonoDevelop.Ide.Workbench> |