public void TestSerialize1() { byte[] bdata; BSONDocument doc = new BSONDocument(); doc.SetNumber("0", 1); //0C-00-00-00 len //10 type //30-00 key //01-00-00-00 int val //00 zero term bdata = doc.ToByteArray(); Assert.AreEqual("0C-00-00-00-10-30-00-01-00-00-00-00", doc.ToDebugDataString()); Assert.AreEqual(bdata.Length, (int)Convert.ToByte(doc.ToDebugDataString().Substring(0, 2), 16)); BSONDocument doc2 = new BSONDocument(doc.ToByteArray()); Assert.AreEqual(1, doc2.KeysCount); int c = 0; foreach (BSONValue bv in doc2) { c++; Assert.IsNotNull(bv); Assert.AreEqual(BSONType.INT, bv.BSONType); Assert.AreEqual("0", bv.Key); Assert.AreEqual(1, bv.Value); } Assert.That(c > 0); doc2.SetNumber("0", 2); Assert.AreEqual(1, doc2.KeysCount); object ival = doc2["0"]; Assert.IsInstanceOf(typeof(int), ival); Assert.AreEqual(2, ival); doc2.SetNumber("1", Int32.MaxValue); //13-00-00-00 //10 //30-00 //02-00-00-00 //10-31-00 //FF-FF-FF-7F //00 Assert.AreEqual("13-00-00-00-10-30-00-02-00-00-00-10-31-00-FF-FF-FF-7F-00", doc2.ToDebugDataString()); doc2 = new BSONDocument(doc2); Assert.AreEqual("13-00-00-00-10-30-00-02-00-00-00-10-31-00-FF-FF-FF-7F-00", doc2.ToDebugDataString()); doc2 = new BSONDocument(doc2.ToByteArray()); Assert.AreEqual("13-00-00-00-10-30-00-02-00-00-00-10-31-00-FF-FF-FF-7F-00", doc2.ToDebugDataString()); doc = new BSONDocument(); doc["a"] = 1; Assert.AreEqual("0C-00-00-00-10-61-00-01-00-00-00-00", doc.ToDebugDataString()); }
internal EJDBQuery(EJDB jb, BSONDocument qdoc, string defaultcollection = null) { _qptr = _ejdbcreatequery(jb.DBPtr, qdoc.ToByteArray()); if (_qptr == IntPtr.Zero) { throw new EJDBQueryException(jb); } _jb = jb; _defaultcollection = defaultcollection; }
/// <summary> /// Append OR joined restriction to this query. /// </summary> /// <returns>This query object.</returns> /// <param name="doc">Query document.</param> public EJDBQuery AddOR(object docobj) { CheckDisposed(); BSONDocument doc = BSONDocument.ValueOf(docobj); //static extern IntPtr _ejdbqueryaddor([In] IntPtr jb, [In] IntPtr qptr, [In] byte[] bsdata); IntPtr qptr = _ejdbqueryaddor(_jb.DBPtr, _qptr, doc.ToByteArray()); if (qptr == IntPtr.Zero) { throw new EJDBQueryException(_jb); } return(this); }
/// <summary> /// Sets the query hints. /// </summary> /// <remarks> /// Replaces previous hints associated with this query. /// </remarks> /// <returns>This query object.</returns> /// <param name="hints">Hints document.</param> public EJDBQuery SetHints(BSONDocument hints) { CheckDisposed(); //0F-00-00-00-10-24-6D-61-78-00-0A-00-00-00-00 //static extern IntPtr _ejdbqueryhints([In] IntPtr jb, [In] IntPtr qptr, [In] byte[] bsdata); IntPtr qptr = _ejdbqueryhints(_jb.DBPtr, _qptr, hints.ToByteArray()); if (qptr == IntPtr.Zero) { throw new EJDBQueryException(_jb); } _hints = hints; return(this); }
public void TestIterate1() { var doc = new BSONDocument(); doc["a"] = "av"; doc["bb"] = 24; //doc["ccc"] = BSONDocument.ValueOf(new{na1 = 1, nb = "2"}); //doc["d"] = new BSONOid("51b9f3af98195c4600000000"); //17-00-00-00 +4 //02-61-00-03-00-00-00-61-76-00 +10 //10-62-62-00-18-00-00-00 +8 //00 +1 Assert.AreEqual("17-00-00-00-02-61-00-03-00-00-00-61-76-00-10-62-62-00-18-00-00-00-00", doc.ToDebugDataString()); BSONIterator it = new BSONIterator(doc); Assert.AreEqual(doc.ToByteArray().Length, it.DocumentLength); var c = ""; while (it.Next() != BSONType.EOO) { c += it.CurrentKey; } Assert.AreEqual("abb", c); it.Dispose(); it = new BSONIterator(doc); var cnt = 0; while (it.Next() != BSONType.EOO) { BSONValue bv = it.FetchCurrentValue(); Assert.IsNotNull(bv); if (cnt == 0) { Assert.IsTrue(bv.BSONType == BSONType.STRING); Assert.IsTrue(bv.Key == "a"); Assert.AreEqual("av", bv.Value); } if (cnt == 1) { Assert.IsTrue(bv.BSONType == BSONType.INT); Assert.IsTrue(bv.Key == "bb"); Assert.AreEqual(24, bv.Value); } cnt++; } }
bool Save(IntPtr cptr, BSONDocument doc, bool merge) { bool rv; BSONValue bv = doc.GetBSONValue("_id"); byte[] bsdata = doc.ToByteArray(); byte[] oiddata = new byte[12]; //static extern bool _ejdbsavebson([In] IntPtr coll, [In] byte[] bsdata, [Out] byte[] oid, bool merge); rv = _ejdbsavebson(cptr, bsdata, oiddata, merge); if (rv && bv == null) { doc.SetOID("_id", new BSONOid(oiddata)); } if (_throwonfail && !rv) { throw new EJDBException(this); } return(rv); }
/// <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 ejdb.h#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 ejdb.h#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()); }
bool Save(IntPtr cptr, BSONDocument doc, bool merge) { bool rv; BSONValue bv = doc.GetBSONValue("_id"); byte[] bsdata = doc.ToByteArray(); byte[] oiddata = new byte[12]; //static extern bool _ejdbsavebson([In] IntPtr coll, [In] byte[] bsdata, [Out] byte[] oid, bool merge); rv = _ejdbsavebson(cptr, bsdata, oiddata, merge); if (rv && bv == null) { doc.SetOID("_id", new BSONOid(oiddata)); } if (_throwonfail && !rv) { throw new EJDBException(this); } return rv; }
/// <summary> /// Sets the query hints. /// </summary> /// <remarks> /// Replaces previous hints associated with this query. /// </remarks> /// <returns>This query object.</returns> /// <param name="hints">Hints document.</param> public EJDBQuery SetHints(BSONDocument hints) { CheckDisposed(); //0F-00-00-00-10-24-6D-61-78-00-0A-00-00-00-00 //static extern IntPtr _ejdbqueryhints([In] IntPtr jb, [In] IntPtr qptr, [In] byte[] bsdata); IntPtr qptr = _ejdbqueryhints(_jb.DBPtr, _qptr, hints.ToByteArray()); if (qptr == IntPtr.Zero) { throw new EJDBQueryException(_jb); } _hints = hints; return this; }
/// <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 ejdb.h#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 ejdb.h#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(); }
public void TestSerialize1() { byte[] bdata; BSONDocument doc = new BSONDocument(); doc.SetNumber("0", 1); //0C-00-00-00 len //10 type //30-00 key //01-00-00-00 int val //00 zero term bdata = doc.ToByteArray(); Assert.AreEqual("0C-00-00-00-10-30-00-01-00-00-00-00", doc.ToDebugDataString()); Assert.AreEqual(bdata.Length, (int) Convert.ToByte(doc.ToDebugDataString().Substring(0, 2), 16)); BSONDocument doc2 = new BSONDocument(doc.ToByteArray()); Assert.AreEqual(1, doc2.KeysCount); int c = 0; foreach (BSONValue bv in doc2) { c++; Assert.IsNotNull(bv); Assert.AreEqual(BSONType.INT, bv.BSONType); Assert.AreEqual("0", bv.Key); Assert.AreEqual(1, bv.Value); } Assert.That(c > 0); doc2.SetNumber("0", 2); Assert.AreEqual(1, doc2.KeysCount); object ival = doc2["0"]; Assert.IsInstanceOf(typeof(int), ival); Assert.AreEqual(2, ival); doc2.SetNumber("1", Int32.MaxValue); //13-00-00-00 //10 //30-00 //02-00-00-00 //10-31-00 //FF-FF-FF-7F //00 Assert.AreEqual("13-00-00-00-10-30-00-02-00-00-00-10-31-00-FF-FF-FF-7F-00", doc2.ToDebugDataString()); doc2 = new BSONDocument(doc2); Assert.AreEqual("13-00-00-00-10-30-00-02-00-00-00-10-31-00-FF-FF-FF-7F-00", doc2.ToDebugDataString()); doc2 = new BSONDocument(doc2.ToByteArray()); Assert.AreEqual("13-00-00-00-10-30-00-02-00-00-00-10-31-00-FF-FF-FF-7F-00", doc2.ToDebugDataString()); doc = new BSONDocument(); doc["a"] = 1; Assert.AreEqual("0C-00-00-00-10-61-00-01-00-00-00-00", doc.ToDebugDataString()); }