Exemplo n.º 1
0
        public void Test4Q1()
        {
            EJDB jb = new EJDB("testdb1", EJDB.DEFAULT_OPEN_MODE | EJDB.JBOTRUNC);

            Assert.IsTrue(jb.IsOpen);
            BSONDocument doc = new BSONDocument().SetNumber("age", 33);

            Assert.IsNull(doc["_id"]);
            bool rv = jb.Save("mycoll", doc);

            Assert.IsTrue(rv);
            Assert.IsNotNull(doc["_id"]);
            EJDBQuery q = jb.CreateQuery(BSONDocument.ValueOf(new{ age = 33 }), "mycoll");

            Assert.IsNotNull(q);
            using (EJDBQCursor cursor = q.Find()) {
                Assert.IsNotNull(cursor);
                Assert.AreEqual(1, cursor.Length);
                int c = 0;
                foreach (BSONIterator oit in cursor)
                {
                    c++;
                    Assert.IsNotNull(oit);
                    BSONDocument rdoc = oit.ToBSONDocument();
                    Assert.IsTrue(rdoc.HasKey("_id"));
                    Assert.AreEqual(33, rdoc["age"]);
                }
                Assert.AreEqual(1, c);
            }
            using (EJDBQCursor cursor = q.Find(null, EJDBQuery.EXPLAIN_FLAG)) {
                Assert.IsNotNull(cursor);
                Assert.AreEqual(1, cursor.Length);
                Assert.IsTrue(cursor.Log.IndexOf("MAX: 4294967295") != -1);
                Assert.IsTrue(cursor.Log.IndexOf("SKIP: 0") != -1);
                Assert.IsTrue(cursor.Log.IndexOf("RS SIZE: 1") != -1);
            }
            q.Max(10);
            using (EJDBQCursor cursor = q.Find(null, EJDBQuery.EXPLAIN_FLAG)) {
                Assert.IsTrue(cursor.Log.IndexOf("MAX: 10") != -1);
            }

            q.Dispose();
            jb.Dispose();
        }
Exemplo n.º 2
0
 public EJDBQCursor Find(string cname = null, int qflags = 0)
 {
     CheckDisposed();
     if (cname == null) {
         cname = _defaultcollection;
     }
     if (cname == null) {
         throw new ArgumentException("Collection name must be provided");
     }
     IntPtr cptr = EJDB._ejdbgetcoll(_jb.DBPtr, cname);
     if (cptr == IntPtr.Zero) {
         return new EJDBQCursor(IntPtr.Zero, 0);
     }
     if (_dutyhints) {
         SetHints(_hints);
         _dutyhints = false;
     }
     int count;
     IntPtr logsptr = IntPtr.Zero;
     if ((qflags & EXPLAIN_FLAG) != 0) {
         //static extern IntPtr _tcxstrnew();
         logsptr = _tcxstrnew(); //Create dynamic query execution log buffer
     }
     EJDBQCursor cur = null;
     try {
         //static extern IntPtr _ejdbqryexecute([In] IntPtr jcoll, [In] IntPtr q, out int count, [In] int qflags, [In] IntPtr logxstr);
         IntPtr qresptr = _ejdbqryexecute(cptr, _qptr, out count, qflags, logsptr);
         cur = new EJDBQCursor(qresptr, count);
     } finally {
         if (logsptr != IntPtr.Zero) {
             try {
                 if (cur != null) {
                     //static extern IntPtr _tcxstrptr([In] IntPtr strptr);
                     IntPtr sbptr = _tcxstrptr(logsptr);
                     cur.Log = Native.StringFromNativeUtf8(sbptr); //UnixMarshal.PtrToString(sbptr, Encoding.UTF8);
                 }
             } finally {
                 //static extern IntPtr _tcxstrdel([In] IntPtr strptr);
                 _tcxstrdel(logsptr);
             }
         }
     }
     int ecode = _jb.LastDBErrorCode ?? 0;
     if (ecode != 0) {
         cur.Dispose();
         throw new EJDBException(_jb);
     }
     return cur;
 }