summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--nejdb/Ejdb.BSON/BSONDocument.cs3
-rw-r--r--nejdb/Ejdb.DB/EJDB.cs74
-rw-r--r--nejdb/Ejdb.Tests/TestEJDB.cs41
-rw-r--r--nejdb/nejdb.csproj1
-rw-r--r--nejdb/nejdb.userprefs22
-rw-r--r--tcejdb/bson.c7
-rw-r--r--tcejdb/bson.h1
-rw-r--r--tcejdb/ejdb.c6
-rw-r--r--tcejdb/ejdb.h2
9 files changed, 140 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>
diff --git a/tcejdb/bson.c b/tcejdb/bson.c
index 7e6a705..8d1e3c8 100644
--- a/tcejdb/bson.c
+++ b/tcejdb/bson.c
@@ -1677,6 +1677,13 @@ bson* bson_create_from_buffer2(bson *rv, const void* buf, int bufsz) {
return rv;
}
+void bson_init_with_data(bson *bs, const void *bsdata) {
+ memset(bs, 0, sizeof(*bs));
+ bs->data = (char*) bsdata;
+ bson_little_endian32(&bs->dataSize, bsdata);
+ bs->finished = true;
+}
+
bool bson_find_merged_array_sets(const void *mbuf, const void *inbuf, bool expandall) {
assert(mbuf && inbuf);
bool found = false;
diff --git a/tcejdb/bson.h b/tcejdb/bson.h
index bf43a40..b98bd89 100644
--- a/tcejdb/bson.h
+++ b/tcejdb/bson.h
@@ -1126,6 +1126,7 @@ EJDB_EXPORT bson* bson_dup(const bson *src);
EJDB_EXPORT bson* bson_create_from_iterator(bson_iterator *from);
EJDB_EXPORT bson* bson_create_from_buffer(const void *buf, int bufsz);
EJDB_EXPORT bson* bson_create_from_buffer2(bson *bs, const void *buf, int bufsz);
+EJDB_EXPORT void bson_init_with_data(bson *bs, const void *bsdata);
EJDB_EXPORT bool bson_find_unmerged_array_sets(const void *mbuf, const void *inbuf);
EJDB_EXPORT bool bson_find_merged_array_sets(const void *mbuf, const void *inbuf, bool expandall);
diff --git a/tcejdb/ejdb.c b/tcejdb/ejdb.c
index acbb117..63a505c 100644
--- a/tcejdb/ejdb.c
+++ b/tcejdb/ejdb.c
@@ -384,6 +384,12 @@ bool ejdbsavebson2(EJCOLL *jcoll, bson *bs, bson_oid_t *oid, bool merge) {
return rv;
}
+bool ejdbsavebson3(EJCOLL *jcoll, void *bsdata, bson_oid_t *oid, bool merge) {
+ bson bs;
+ bson_init_with_data(&bs, bsdata);
+ return ejdbsavebson2(jcoll, &bs, oid, merge);
+}
+
bool ejdbrmbson(EJCOLL *jcoll, bson_oid_t *oid) {
assert(jcoll && oid);
if (!JBISOPEN(jcoll->jb)) {
diff --git a/tcejdb/ejdb.h b/tcejdb/ejdb.h
index f06ef99..427c292 100644
--- a/tcejdb/ejdb.h
+++ b/tcejdb/ejdb.h
@@ -218,6 +218,8 @@ EJDB_EXPORT bool ejdbsavebson(EJCOLL *coll, bson *bs, bson_oid_t *oid);
*/
EJDB_EXPORT bool ejdbsavebson2(EJCOLL *jcoll, bson *bs, bson_oid_t *oid, bool merge);
+EJDB_EXPORT bool ejdbsavebson3(EJCOLL *jcoll, void *bsdata, bson_oid_t *oid, bool merge);
+
/**
* Remove BSON object from collection.
* The `oid` argument should points the primary key (_id)