summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoradam <adamansky@gmail.com>2013-06-21 00:53:57 +0700
committeradam <adamansky@gmail.com>2013-06-21 00:53:57 +0700
commit17b79d3bcb9dcc4edb381a0fdf6de5ad26b75b0d (patch)
tree25bb393e9fa5bb3c7322d3dfa12aed6e675e26d4
parent87ac7052452177a2cb71823671b44db89ea890e9 (diff)
downloadejdb-17b79d3bcb9dcc4edb381a0fdf6de5ad26b75b0d.tar.gz
ejdb-17b79d3bcb9dcc4edb381a0fdf6de5ad26b75b0d.tar.bz2
ejdb-17b79d3bcb9dcc4edb381a0fdf6de5ad26b75b0d.zip
#24
-rw-r--r--nejdb/Ejdb.BSON/BSONIterator.cs12
-rw-r--r--nejdb/Ejdb.DB/EJDB.cs93
-rw-r--r--nejdb/Ejdb.Tests/TestEJDB.cs8
-rw-r--r--nejdb/nejdb.userprefs21
-rw-r--r--tcejdb/ejdb.c25
-rw-r--r--tcejdb/testejdb/t2.c15
6 files changed, 145 insertions, 29 deletions
diff --git a/nejdb/Ejdb.BSON/BSONIterator.cs b/nejdb/Ejdb.BSON/BSONIterator.cs
index d7c15c3..261f4b6 100644
--- a/nejdb/Ejdb.BSON/BSONIterator.cs
+++ b/nejdb/Ejdb.BSON/BSONIterator.cs
@@ -24,13 +24,21 @@ namespace Ejdb.BSON {
public sealed class BSONIterator : IDisposable, IEnumerable<BSONType> {
ExtBinaryReader _input;
+
bool _closeOnDispose = true;
+
bool _disposed;
+
int _doclen;
+
BSONType _ctype = BSONType.UNKNOWN;
+
string _entryKey;
+
int _entryLen;
+
bool _entryDataSkipped;
+
BSONValue _entryDataValue;
public bool Disposed {
@@ -304,7 +312,9 @@ namespace Ejdb.BSON {
}
return _entryDataValue;
}
-
+ //.//////////////////////////////////////////////////////////////////
+ // Private staff
+ //.//////////////////////////////////////////////////////////////////
void SkipData(bool force = false) {
if (_entryDataSkipped && !force) {
return;
diff --git a/nejdb/Ejdb.DB/EJDB.cs b/nejdb/Ejdb.DB/EJDB.cs
index 5bacc00..63bb1f1 100644
--- a/nejdb/Ejdb.DB/EJDB.cs
+++ b/nejdb/Ejdb.DB/EJDB.cs
@@ -230,6 +230,18 @@ namespace Ejdb.DB {
//EJDB_EXPORT bson* ejdbmeta(EJDB *jb)
[DllImport(EJDB_LIB_NAME, EntryPoint="ejdbmeta")]
internal static extern IntPtr _ejdbmeta([In] IntPtr db);
+ //EJDB_EXPORT bool ejdbtranbegin(EJCOLL *coll);
+ [DllImport(EJDB_LIB_NAME, EntryPoint="ejdbtranbegin")]
+ internal static extern bool _ejdbtranbegin([In] IntPtr coll);
+ //EJDB_EXPORT bool ejdbtrancommit(EJCOLL *coll);
+ [DllImport(EJDB_LIB_NAME, EntryPoint="ejdbtrancommit")]
+ internal static extern bool _ejdbtrancommit([In] IntPtr coll);
+ //EJDB_EXPORT bool ejdbtranabort(EJCOLL *coll);
+ [DllImport(EJDB_LIB_NAME, EntryPoint="ejdbtranabort")]
+ internal static extern bool _ejdbtranabort([In] IntPtr coll);
+ //EJDB_EXPORT bool ejdbtranstatus(EJCOLL *jcoll, bool *txactive);
+ [DllImport(EJDB_LIB_NAME, EntryPoint="ejdbtranstatus")]
+ internal static extern bool _ejdbtranstatus([In] IntPtr coll, out bool txactive);
internal static bool _ejdbsetindex(IntPtr coll, string ipath, int flags) {
IntPtr ipathptr = UnixMarshal.StringToHeap(ipath, Encoding.UTF8);
@@ -274,18 +286,26 @@ namespace Ejdb.DB {
}
/// <summary>
- /// Gets description of EJDB database and its collections.
+ /// Gets info of EJDB database itself and its collections.
/// </summary>
/// <value>The DB meta.</value>
public BSONDocument DBMeta {
get {
CheckDisposed(true);
- //internal static extern IntPtr ejdbmeta([In] IntPtr db);
- //IntPtr bsptr =
-
-
- BSONDocument meta = new BSONDocument();
- return meta;
+ //internal static extern IntPtr _ejdbmeta([In] IntPtr db);
+ IntPtr bsptr = _ejdbmeta(_db);
+ if (bsptr == IntPtr.Zero) {
+ throw new EJDBException(this);
+ }
+ try {
+ int size;
+ IntPtr bsdataptr = _bson_data2(bsptr, out size);
+ byte[] bsdata = new byte[size];
+ Marshal.Copy(bsdataptr, bsdata, 0, bsdata.Length);
+ return new BSONDocument(bsdata);
+ } finally {
+ _bson_del(bsptr);
+ }
}
}
@@ -522,6 +542,65 @@ namespace Ejdb.DB {
}
/// <summary>
+ /// Begin transaction for EJDB collection.
+ /// </summary>
+ /// <returns><c>true</c>, if begin was transactioned, <c>false</c> otherwise.</returns>
+ /// <param name="cname">Cname.</param>
+ public bool TransactionBegin(string cname) {
+ CheckDisposed();
+ IntPtr cptr = _ejdbgetcoll(_db, cname);
+ if (cptr == IntPtr.Zero) {
+ return true;
+ }
+ //internal static extern bool _ejdbtranbegin([In] IntPtr coll);
+ return _ejdbtranbegin(cptr);
+ }
+
+ /// <summary>
+ /// Commit the transaction.
+ /// </summary>
+ /// <returns><c>false</c>, if error occurred.</returns>
+ public bool TransactionCommit(string cname) {
+ CheckDisposed();
+ IntPtr cptr = _ejdbgetcoll(_db, cname);
+ if (cptr == IntPtr.Zero) {
+ return true;
+ }
+ //internal static extern bool _ejdbtrancommit([In] IntPtr coll);
+ return _ejdbtrancommit(cptr);
+ }
+
+ /// <summary>
+ /// Abort the transaction.
+ /// </summary>
+ /// <returns><c>false</c>, if error occurred.</returns>
+ public bool AbortTransaction(string cname) {
+ CheckDisposed();
+ IntPtr cptr = _ejdbgetcoll(_db, cname);
+ if (cptr == IntPtr.Zero) {
+ return true;
+ }
+ //internal static extern bool _ejdbtranabort([In] IntPtr coll);
+ return _ejdbtranabort(cptr);
+ }
+
+ /// <summary>
+ /// Get the transaction status.
+ /// </summary>
+ /// <returns><c>false</c>, if error occurred.</returns>
+ /// <param name="cname">Name of collection.</param>
+ /// <param name="active">Out parameter. It <c>true</c> transaction is active.</param>
+ public bool TransactionStatus(string cname, out bool active) {
+ CheckDisposed();
+ IntPtr cptr = _ejdbgetcoll(_db, cname);
+ if (cptr == IntPtr.Zero) {
+ active = false;
+ return false;
+ }
+ return _ejdbtranstatus(cptr, out active);
+ }
+
+ /// <summary>
/// Save the BSON document doc into the collection.
/// </summary>
/// <param name="cname">Name of collection.</param>
diff --git a/nejdb/Ejdb.Tests/TestEJDB.cs b/nejdb/Ejdb.Tests/TestEJDB.cs
index 192302a..652baf0 100644
--- a/nejdb/Ejdb.Tests/TestEJDB.cs
+++ b/nejdb/Ejdb.Tests/TestEJDB.cs
@@ -172,6 +172,14 @@ namespace Ejdb.Tests {
name = "Bounty"
});
Assert.AreEqual(2, q.Count());
+ q.AddOR(new{
+ name = "Bounty2"
+ });
+ Assert.AreEqual(2, q.Count());
+
+ Console.WriteLine(jb.DBMeta);
+ //[BSONDocument: [BSONValue: BSONType=STRING, Key=file, Value=testdb1],
+ //[BSONValue: BSONType=ARRAY, Key=collections, Value=[BSONArray: [BSONValue: BSONType=OBJECT, Key=0, Value=[BSONDocument: [BSONValue: BSONType=STRING, Key=name, Value=parrots], [BSONValue: BSONType=STRING, Key=file, Value=testdb1_parrots], [BSONValue: BSONType=LONG, Key=records, Value=2], [BSONValue: BSONType=OBJECT, Key=options, Value=[BSONDocument: [BSONValue: BSONType=LONG, Key=buckets, Value=131071], [BSONValue: BSONType=LONG, Key=cachedrecords, Value=0], [BSONValue: BSONType=BOOL, Key=large, Value=False], [BSONValue: BSONType=BOOL, Key=compressed, Value=False]]], [BSONValue: BSONType=ARRAY, Key=indexes, Value=[BSONArray: ]]]]]]]
q.Dispose();
jb.Dispose();
diff --git a/nejdb/nejdb.userprefs b/nejdb/nejdb.userprefs
index 36c0d1a..beed88f 100644
--- a/nejdb/nejdb.userprefs
+++ b/nejdb/nejdb.userprefs
@@ -2,15 +2,12 @@
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench ActiveDocument="Ejdb.DB/EJDB.cs">
<Files>
- <File FileName="Ejdb.DB/EJDB.cs" Line="278" Column="17" />
+ <File FileName="Ejdb.DB/EJDB.cs" Line="599" Column="5" />
<File FileName="Ejdb.DB/EJDBQuery.cs" Line="124" Column="5" />
- <File FileName="Ejdb.DB/EJDBQCursor.cs" Line="1" Column="1" />
- <File FileName="Ejdb.BSON/BSONDocument.cs" Line="1" Column="1" />
- <File FileName="Ejdb.Tests/TestEJDB.cs" Line="77" Column="29" />
- <File FileName="Ejdb.BSON/BSONRegexp.cs" Line="1" Column="1" />
- <File FileName="Ejdb.BSON/BSONIterator.cs" Line="1" Column="1" />
- <File FileName="Ejdb.JSON/JSONElement.cs" Line="15" Column="96" />
- <File FileName="Ejdb.BSON/BSONType.cs" Line="20" Column="29" />
+ <File FileName="Ejdb.BSON/BSONDocument.cs" Line="334" Column="60" />
+ <File FileName="Ejdb.Tests/TestEJDB.cs" Line="176" Column="13" />
+ <File FileName="Ejdb.BSON/BSONType.cs" Line="24" Column="17" />
+ <File FileName="Ejdb.BSON/BSONIterator.cs" Line="78" Column="29" />
</Files>
<Pads>
<Pad Id="ProjectPad">
@@ -19,7 +16,9 @@
<Node name="nejdb" expanded="True">
<Node name="References" expanded="True" />
<Node name="Ejdb.BSON" expanded="True" />
- <Node name="Ejdb.DB" expanded="True" />
+ <Node name="Ejdb.DB" expanded="True">
+ <Node name="EJDB.cs" selected="True" />
+ </Node>
<Node name="Ejdb.IO" expanded="True" />
<Node name="Ejdb.Tests" expanded="True" />
<Node name="Ejdb.Utils" expanded="True" />
@@ -43,7 +42,9 @@
<Node name="Ejdb" expanded="True">
<Node name="Tests" expanded="True">
<Node name="TestBSON" expanded="True" />
- <Node name="TestEJDB" expanded="True" selected="True" />
+ <Node name="TestEJDB" expanded="True">
+ <Node name="Test4Q2" selected="True" />
+ </Node>
</Node>
</Node>
</Node>
diff --git a/tcejdb/ejdb.c b/tcejdb/ejdb.c
index c569ef7..9f12d80 100644
--- a/tcejdb/ejdb.c
+++ b/tcejdb/ejdb.c
@@ -869,22 +869,21 @@ bool ejdbtranstatus(EJCOLL *jcoll, bool *txactive) {
}
bson* ejdbmeta(EJDB *jb) {
- if (!JBISOPEN(jb)) {
- _ejdbsetecode(jb, TCEINVALID, __FILE__, __LINE__, __func__);
- return NULL;
- }
+ JBENSUREOPENLOCK(jb, false, NULL);
char nbuff[TCNUMBUFSIZ];
bson *bs = bson_create();
bson_init(bs);
bson_append_string(bs, "file", jb->metadb->hdb->path);
- bson_append_start_array(bs, "collections");
+ bson_append_start_array(bs, "collections"); //collections
TCLIST *cols = ejdbgetcolls(jb);
for (int i = 0; i < TCLISTNUM(cols); ++i) {
- if (!JBISOPEN(jb)) {
- break;
- }
EJCOLL *coll = (EJCOLL*) TCLISTVALPTR(cols, i);
- if (!coll || !_ejcollockmethod(coll, false)) continue;
+ if (!JBCLOCKMETHOD(coll, false)) {
+ tclistdel(cols);
+ bson_del(bs);
+ JBUNLOCKMETHOD(jb);
+ return NULL;
+ }
bson_numstrn(nbuff, TCNUMBUFSIZ, i);
bson_append_start_object(bs, nbuff); //coll obj
bson_append_string_n(bs, "name", coll->cname, coll->cnamesz);
@@ -930,11 +929,17 @@ bson* ejdbmeta(EJDB *jb) {
}
bson_append_finish_array(bs); //eof coll.indexes[]
bson_append_finish_object(bs); //eof coll
- _ejcollunlockmethod(coll);
+ JBCUNLOCKMETHOD(coll);
}
bson_append_finish_array(bs); //eof collections
bson_finish(bs);
tclistdel(cols);
+ JBUNLOCKMETHOD(jb);
+ if (bs->err) {
+ _ejdbsetecode(jb, JBEINVALIDBSON, __FILE__, __LINE__, __func__);
+ bson_del(bs);
+ bs = NULL;
+ }
return bs;
}
diff --git a/tcejdb/testejdb/t2.c b/tcejdb/testejdb/t2.c
index 7d9df54..e6c3e3f 100644
--- a/tcejdb/testejdb/t2.c
+++ b/tcejdb/testejdb/t2.c
@@ -4229,6 +4229,18 @@ void testTicket54() {
CU_ASSERT_TRUE(ejdbsetindex(coll, "value", JBIDXNUM));
}
+void testMetaInfo() {
+ bson *meta = ejdbmeta(jb);
+ CU_ASSERT_PTR_NOT_NULL_FATAL(meta);
+ const char *metabsdata = bson_data(meta);
+ CU_ASSERT_FALSE(bson_compare_string("dbt2", metabsdata, "file"));
+ CU_ASSERT_FALSE(bson_compare_string("contacts", metabsdata, "collections.0.name"));
+ CU_ASSERT_FALSE(bson_compare_string("dbt2_contacts", metabsdata, "collections.0.file"));
+ CU_ASSERT_FALSE(bson_compare_long(131071, metabsdata, "collections.0.options.buckets"));
+ CU_ASSERT_FALSE(bson_compare_long(8, metabsdata, "collections.0.records"));
+ bson_del(meta);
+}
+
int main() {
setlocale(LC_ALL, "en_US.UTF-8");
@@ -4298,7 +4310,8 @@ int main() {
(NULL == CU_add_test(pSuite, "testTicket28", testTicket28)) ||
(NULL == CU_add_test(pSuite, "testTicket38", testTicket38)) ||
(NULL == CU_add_test(pSuite, "testTicket43", testTicket43)) ||
- (NULL == CU_add_test(pSuite, "testTicket54", testTicket54))
+ (NULL == CU_add_test(pSuite, "testTicket54", testTicket54)) ||
+ (NULL == CU_add_test(pSuite, "testMetaInfo", testMetaInfo))
) {
CU_cleanup_registry();
return CU_get_error();