#59 #72
authoradam <adamansky@gmail.com>
Thu, 18 Jul 2013 11:44:31 +0000 (18:44 +0700)
committeradam <adamansky@gmail.com>
Thu, 18 Jul 2013 11:44:31 +0000 (18:44 +0700)
nejdb/Ejdb.DB/EJDB.cs
nejdb/Ejdb.Tests/TestEJDB.cs
tcejdb/debian/libtcejdb9.symbols
tcejdb/ejdb.c
tcejdb/ejdb.h

index ed173e0..659a511 100644 (file)
@@ -226,6 +226,9 @@ namespace Ejdb.DB {
                                UnixMarshal.FreeHeap(cptr);
                        }
                }
+               //EJDB_EXPORT bson* ejdbcommand(EJDB *jb, bson *cmd);
+               [DllImport(EJDB_LIB_NAME, EntryPoint="ejdbcommand2")]
+               internal static extern IntPtr _ejdbcommand([In] IntPtr db, [In] byte[] cmd);
                //EJDB_EXPORT bool ejdbsavebson3(EJCOLL *jcoll, void *bsdata, bson_oid_t *oid, bool merge);
                [DllImport(EJDB_LIB_NAME, EntryPoint="ejdbsavebson3")]
                internal static extern bool _ejdbsavebson([In] IntPtr coll, [In] byte[] bsdata, [Out] byte[] oid, [In] bool merge);
@@ -799,6 +802,60 @@ namespace Ejdb.DB {
                }
 
                /// <summary>
+               /// Executes EJDB command.
+               /// </summary>
+               /// <remarks>
+               /// Supported commands:
+               ///
+               /// 1) Exports database collections data. See ejdbexport() method.
+               /// 
+               ///     "export" : {
+               ///     "path" : string,                    //Exports database collections data
+               ///     "cnames" : [string array]|null,     //List of collection names to export
+               ///     "mode" : int|null                   //Values: null|`JBJSONEXPORT` See ejdbexport() method
+               /// }
+               /// 
+               /// Command response:
+               /// {
+               ///     "log" : string,        //Diagnostic log about executing this command
+               ///     "error" : string|null, //ejdb error message
+               ///     "errorCode" : int|0,   //ejdb error code
+               /// }
+               /// 
+               /// 2) Imports previously exported collections data into ejdb.
+               /// 
+               ///     "import" : {
+               ///     "path" : string                     //The directory path in which data resides
+               ///             "cnames" : [string array]|null,     //List of collection names to import
+               ///             "mode" : int|null                //Values: null|`JBIMPORTUPDATE`|`JBIMPORTREPLACE` See ejdbimport() method
+               /// }
+               /// 
+               /// Command response:
+               /// {
+               ///     "log" : string,        //Diagnostic log about executing this command
+               ///     "error" : string|null, //ejdb error message
+               ///     "errorCode" : int|0,   //ejdb error code
+               /// }
+               /// </remarks>
+               /// <param name="cmd">Command object</param>
+               /// <returns>Command response.</returns>
+               public BSONDocument Command(BSONDocument cmd)  {
+                       CheckDisposed();
+                       byte[] cmdata = cmd.ToByteArray();
+                       //internal static extern IntPtr _ejdbcommand([In] IntPtr db, [In] byte[] cmd);
+                       IntPtr cmdret = _ejdbcommand(_db, cmdata);
+                       if (cmdret == IntPtr.Zero) {
+                               return null;
+                       }
+                       byte[] bsdata = BsonPtrIntoByteArray(cmdret);
+                       if (bsdata.Length == 0) {
+                               return null;
+                       }
+                       BSONIterator it = new BSONIterator(bsdata);
+                       return it.ToBSONDocument();
+               }
+
+               /// <summary>
                /// Save the BSON document doc into the collection.
                /// </summary>
                /// <param name="cname">Name of collection.</param>
@@ -854,6 +911,7 @@ namespace Ejdb.DB {
                        }
                        return true;
                }
+       
 
                bool Save(IntPtr cptr, BSONDocument doc, bool merge) {
                        bool rv;
index c921fa3..b8b916b 100644 (file)
@@ -182,6 +182,14 @@ namespace Ejdb.Tests {
                        //[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();
+
+                       //Test command execution
+                       BSONDocument cmret = jb.Command(BSONDocument.ValueOf(new {
+                               ping = ""
+                       }));
+                       Assert.IsNotNull(cmret);
+                       Assert.AreEqual("pong", cmret["log"]);
+
                        jb.Dispose();
                }
        }
index 63a468f..b4587a0 100644 (file)
@@ -162,6 +162,7 @@ libtcejdb.so.9 libtcejdb9 #MINVER#
  ejcollockmethod@Base 1.1.8
  ejcollunlockmethod@Base 1.1.8
  ejdbclose@Base 1.1.8
+ ejdbcommand2@Base 1.1.14
  ejdbcommand@Base 1.1.14
  ejdbcreatecoll@Base 1.1.8
  ejdbcreatequery2@Base 1.1.13
index d54d1c9..adafc36 100644 (file)
@@ -843,6 +843,13 @@ finish:
     return !err;
 }
 
+bson* ejdbcommand2(EJDB *jb, void *cmdbsondata) {
+    bson cmd;
+    bson_init_with_data(&cmd, cmdbsondata);
+    bson *bret = ejdbcommand(jb, &cmd);
+    return bret;
+}
+
 bson* ejdbcommand(EJDB *jb, bson *cmd) {
     bson *ret = bson_create();
     int ecode = 0;
index d527f14..a21c79d 100644 (file)
@@ -596,7 +596,8 @@ EJDB_EXPORT bool ejdbimport(EJDB *jb, const char *path, TCLIST *cnames, int flag
  * @param cmd   BSON command spec.
  * @return Allocated BSON command response object. Caller shoud call `bson_del()` on it.
  */
-EJDB_EXPORT bson* ejdbcommand(EJDB *jb, bson *cmd);
+EJDB_EXPORT bson* ejdbcommand(EJDB *jb, bson *cmdbson);
+EJDB_EXPORT bson* ejdbcommand2(EJDB *jb, void *cmdbsondata);
 
 EJDB_EXTERN_C_END