예제 #1
0
파일: TestEJDB.cs 프로젝트: JulianLiu/ejdb
        public void Test3SaveLoad()
        {
            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"]);
            Assert.IsInstanceOf(typeof(BSONOid), doc["_id"]);
            rv = jb.Save("mycoll", doc);
            Assert.IsTrue(rv);

            BSONIterator it = jb.Load("mycoll", doc["_id"] as BSONOid);
            Assert.IsNotNull(it);

            BSONDocument doc2 = it.ToBSONDocument();
            Assert.AreEqual(doc.ToDebugDataString(), doc2.ToDebugDataString());
            Assert.IsTrue(doc == doc2);

            Assert.AreEqual(1, jb.CreateQueryFor("mycoll").Count());
            Assert.IsTrue(jb.Remove("mycoll", doc["_id"] as BSONOid));
            Assert.AreEqual(0, jb.CreateQueryFor("mycoll").Count());

            jb.Save("mycoll", doc);
            Assert.AreEqual(1, jb.CreateQueryFor("mycoll").Count());
            Assert.IsTrue(jb.DropCollection("mycoll"));
            Assert.AreEqual(0, jb.CreateQueryFor("mycoll").Count());

            Assert.IsTrue(jb.Sync());
            jb.Dispose();
        }
예제 #2
0
파일: EJDBQuery.cs 프로젝트: JulianLiu/ejdb
 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;
 }
예제 #3
0
파일: TestEJDB.cs 프로젝트: JulianLiu/ejdb
 public void Test2EnsureCollection()
 {
     EJDB jb = new EJDB("testdb1", EJDB.DEFAULT_OPEN_MODE | EJDB.JBOTRUNC);
     EJDBCollectionOptionsN co = new EJDBCollectionOptionsN();
     co.large = true;
     co.compressed = false;
     co.records = 50000;
     Assert.IsTrue(jb.EnsureCollection("mycoll2", co));
     jb.Dispose();
 }
예제 #4
0
파일: TestEJDB.cs 프로젝트: JulianLiu/ejdb
 public void Test1OpenClose()
 {
     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
 }
예제 #5
0
 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;
 }
예제 #6
0
 public void Dispose()
 {
     if (_qptr != IntPtr.Zero)
     {
         //static extern void _ejdbquerydel([In] IntPtr qptr);
         _ejdbquerydel(_qptr);
         _qptr = IntPtr.Zero;
     }
     if (_jb != null)
     {
         _jb = null;
     }
     _hints = null;
 }
예제 #7
0
파일: Program.cs 프로젝트: JulianLiu/ejdb
        public static void Main(string[] args)
        {
            var jb = new EJDB("zoo", EJDB.DEFAULT_OPEN_MODE | EJDB.JBOTRUNC);
            jb.ThrowExceptionOnFail = true;

            var parrot1 = BSONDocument.ValueOf(new {
                name = "Grenny",
                type = "African Grey",
                male = true,
                age = 1,
                birthdate = DateTime.Now,
                likes = new string[] { "green color", "night", "toys" },
                extra = BSONull.VALUE
            });

            var parrot2 = BSONDocument.ValueOf(new {
                name = "Bounty",
                type = "Cockatoo",
                male = false,
                age = 15,
                birthdate = DateTime.Now,
                likes = new string[] { "sugar cane" }
            });

            jb.Save("parrots", parrot1, parrot2);

            Console.WriteLine("Grenny OID: " + parrot1["_id"]);
            Console.WriteLine("Bounty OID: " + parrot2["_id"]);

            var q = jb.CreateQuery(new {
                likes = "toys"
            }, "parrots").OrderBy("name");

            using (var cur = q.Find()) {
                Console.WriteLine("Found " + cur.Length + " parrots");
                foreach (var e in cur) {
                    //fetch  the `name` and the first element of likes array from the current BSON iterator.
                    //alternatively you can fetch whole document from the iterator: `e.ToBSONDocument()`
                    BSONDocument rdoc = e.ToBSONDocument("name", "likes.0");
                    Console.WriteLine(string.Format("{0} likes the '{1}'", rdoc["name"], rdoc["likes.0"]));
                }
            }
            q.Dispose();
            jb.Dispose();
            Console.ReadKey();
        }
예제 #8
0
파일: TestEJDB.cs 프로젝트: JulianLiu/ejdb
        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();
        }
예제 #9
0
파일: TestEJDB.cs 프로젝트: JulianLiu/ejdb
        public void Test4Q2()
        {
            EJDB jb = new EJDB("testdb1", EJDB.DEFAULT_OPEN_MODE | EJDB.JBOTRUNC);
            Assert.IsTrue(jb.IsOpen);

            var parrot1 = BSONDocument.ValueOf(new{
                name = "Grenny",
                type = "African Grey",
                male = true,
                age = 1,
                birthdate = DateTime.Now,
                likes = new string[] { "green color", "night", "toys" },
                extra1 = BSONull.VALUE
            });

            var parrot2 = BSONDocument.ValueOf(new{
                name = "Bounty",
                type = "Cockatoo",
                male = false,
                age = 15,
                birthdate = DateTime.Now,
                likes = new string[] { "sugar cane" },
                extra1 = BSONull.VALUE
            });
            Assert.IsTrue(jb.Save("parrots", parrot1, parrot2));
            Assert.AreEqual(2, jb.CreateQueryFor("parrots").Count());

            var q = jb.CreateQuery(new{
                    name = new BSONRegexp("(grenny|bounty)", "i")
            }).SetDefaultCollection("parrots").OrderBy("name");

            using (var cur = q.Find()) {
                Assert.AreEqual(2, cur.Length);

                var doc = cur[0].ToBSONDocument();
                Assert.AreEqual("Bounty", doc["name"]);
                Assert.AreEqual(15, doc["age"]);

                doc = cur[1].ToBSONDocument();
                Assert.AreEqual("Grenny", doc["name"]);
                Assert.AreEqual(1, doc["age"]);
            }
            q.Dispose();

            q = jb.CreateQueryFor("parrots");
            Assert.AreEqual(2, q.Count());
            q.AddOR(new{
                name = "Grenny"
            });
            Assert.AreEqual(1, q.Count());
            q.AddOR(new{
                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();

            //Test command execution
            BSONDocument cmret = jb.Command(BSONDocument.ValueOf(new {
                ping = ""
            }));
            Assert.IsNotNull(cmret);
            Assert.AreEqual("pong", cmret["log"]);

            jb.Dispose();
        }
예제 #10
0
파일: EJDBQuery.cs 프로젝트: JulianLiu/ejdb
 public void Dispose()
 {
     if (_qptr != IntPtr.Zero) {
         //static extern void _ejdbquerydel([In] IntPtr qptr);
         _ejdbquerydel(_qptr);
         _qptr = IntPtr.Zero;
     }
     if (_jb != null) {
         _jb = null;
     }
     _hints = null;
 }
 public EJDBQueryException(EJDB db) : base(db)
 {
 }
예제 #12
0
 public EJDBQueryException(EJDB db)
     : base(db)
 {
 }
예제 #13
0
 public EJDBException(EJDB db)
     : base(db.LastDBErrorMsg)
 {
     this.Code = db.LastDBErrorCode;
 }
예제 #14
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);
        }
예제 #15
0
 public EJDBException(EJDB db) : base(db.LastDBErrorMsg)
 {
     this.Code = db.LastDBErrorCode;
 }