예제 #1
0
파일: EJDB.cs 프로젝트: sujeetpillai/ejdb
        public bool Save(string cname, params object[] docs)
        {
            CheckDisposed();
            IntPtr cptr = _ejdbcreatecoll(_db, cname, null);

            if (cptr == IntPtr.Zero)
            {
                if (_throwonfail)
                {
                    throw new EJDBException(this);
                }
                else
                {
                    return(false);
                }
            }
            foreach (var doc in docs)
            {
                if (!Save(cptr, BSONDocument.ValueOf(doc), false))
                {
                    if (_throwonfail)
                    {
                        throw new EJDBException(this);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
예제 #2
0
        public void TestFilteredDoc()
        {
            var doc = new BSONDocument();

            doc["c"]    = "d";
            doc["aaa"]  = 11;
            doc["ndoc"] = BSONDocument.ValueOf(new {
                aaaa = "nv1",
                d    = "nv2",
                nnd  = BSONDocument.ValueOf(new {
                    nnv = true,
                    nns = "s"
                })
            });
            doc["ndoc2"] = BSONDocument.ValueOf(new {
                n = "v"
            });
            doc["f"] = "f";
            BSONIterator it   = new BSONIterator(doc);
            BSONDocument doc2 = it.ToBSONDocument("c", "ndoc.d", "ndoc.nnd.nns", "f");

            Assert.AreEqual(3, doc2.KeysCount);
            Assert.AreEqual("d", doc2["c"]);
            Assert.AreEqual(2, ((BSONDocument)doc2["ndoc"]).KeysCount);
            Assert.AreEqual("nv2", ((BSONDocument)doc2["ndoc"])["d"]);
            Assert.AreEqual("s", ((BSONDocument)((BSONDocument)doc2["ndoc"])["nnd"])["nns"]);
            Assert.AreEqual("s", doc2["ndoc.nnd.nns"]);
            Assert.AreEqual("f", "f");
            //Console.WriteLine("doc2=" + doc2);
        }
예제 #3
0
        /// <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);
        }
예제 #4
0
        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();
        }
예제 #5
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();
        }
예제 #6
0
        public void TestAnonTypes()
        {
            BSONDocument doc = BSONDocument.ValueOf(new { a = "b", c = 1 });

            //15-00-00-00
            //02-61-00
            //02-00-00-00
            //62-00
            //10-63-00-01-00-00-00-00
            Assert.AreEqual("15-00-00-00-02-61-00-02-00-00-00-62-00-10-63-00-01-00-00-00-00",
                            doc.ToDebugDataString());
            doc["d"] = new{ e = new BSONRegexp("r1", "o2") };         //subdocument
            //26-00-00-00-02-61-00-02-00-00-00-62-00-10-63-00-01-00-00-00-
            //03
            //64-00
            //0E-00-00-00
            //0B
            //65-00
            //72-31-00-6F-32-00-00-00
            Assert.AreEqual("26-00-00-00-02-61-00-02-00-00-00-62-00-10-63-00-01-00-00-00-" +
                            "03-64-00-0E-00-00-00-0B-65-00-72-31-00-6F-32-00-00-00",
                            doc.ToDebugDataString());
        }
예제 #7
0
        public void TestIterateRE()
        {
            var doc = new BSONDocument();

            doc["a"] = new BSONRegexp("b", "c");
            doc["d"] = 1;
            doc["e"] = BSONDocument.ValueOf(new { f = new BSONRegexp("g", "") });
            doc["h"] = 2;
            //28-00-00-00
            //0B-61-00-62-00-63-00
            //10-64-00-01-00-00-00
            //03-65-00-0B-00-00-00
            //0B-66-00-67-00-00-00
            //10-68-00-02-00-00-00-00
            var cs = "";

            foreach (var bt in new BSONIterator(doc))
            {
                cs += bt.ToString();
            }
            Assert.AreEqual("REGEXINTOBJECTINT", cs);
            cs = "";
            foreach (var bv in new BSONIterator(doc).Values())
            {
                if (bv.Key == "a")
                {
                    cs += ((BSONRegexp)bv.Value).Re;
                    cs += ((BSONRegexp)bv.Value).Opts;
                }
                else
                {
                    cs += bv.Value;
                }
            }
            Assert.AreEqual("bc1[BSONDocument: [BSONValue: BSONType=REGEX, Key=f, Value=[BSONRegexp: re=g, opts=]]]2", cs);
        }
예제 #8
0
        public void TestIterate2()
        {
            var doc = new BSONDocument();

            doc["a"] = "av";
            doc["b"] = BSONDocument.ValueOf(new{ cc = 1 });
            doc["d"] = new BSONOid("51b9f3af98195c4600000000");
            Assert.AreEqual(3, doc.KeysCount);
            //Console.WriteLine(doc.KeysCount);
            //Console.WriteLine(doc.ToDebugDataString());
            //2E-00-00-00					    +4
            //02-61-00-03-00-00-00-61-76-00		+10 (14)
            //03-62-00							+3  (17) "d" =
            //0D-00-00-00						+4  (21) doc len = 13
            //10-63-63-00-01-00-00-00 -00		+9  (30)
            //07-64-00							+3  (33)
            //51-B9-F3-AF-98-19-5C-46-00-00-00-00	 +12 (45)
            //00									+1 (46)
            Assert.AreEqual("2E-00-00-00-" +
                            "02-61-00-03-00-00-00-61-76-00-" +
                            "03-62-00-" +
                            "0D-00-00-00-" +
                            "10-63-63-00-01-00-00-00-00-" +
                            "07-64-00-" +
                            "51-B9-F3-AF-98-19-5C-46-00-00-00-00-" +
                            "00", doc.ToDebugDataString());
            BSONIterator it = new BSONIterator(doc);
            int          c  = 0;

            foreach (var bt in it)
            {
                if (c == 0)
                {
                    Assert.IsTrue(bt == BSONType.STRING);
                }
                if (c == 1)
                {
                    Assert.IsTrue(bt == BSONType.OBJECT);
                }
                if (c == 2)
                {
                    Assert.IsTrue(bt == BSONType.OID);
                }
                ++c;
            }
            bool thrown = false;

            Assert.IsTrue(it.Disposed);
            try {
                it.Next();
            } catch (ObjectDisposedException) {
                thrown = true;
            }
            Assert.IsTrue(thrown);

            c  = 0;
            it = new BSONIterator(doc);
            foreach (var bv in it.Values())
            {
                if (c == 0)
                {
                    Assert.AreEqual("a", bv.Key);
                    Assert.AreEqual("av", bv.Value);
                }
                if (c == 1)
                {
                    Assert.AreEqual("b", bv.Key);
                    BSONDocument sdoc = bv.Value as BSONDocument;
                    Assert.IsNotNull(sdoc);
                    foreach (var bv2 in new BSONIterator(sdoc).Values())
                    {
                        Assert.AreEqual("cc", bv2.Key);
                        Assert.AreEqual(1, bv2.Value);
                        Assert.AreEqual(BSONType.INT, bv2.BSONType);
                    }
                }
                if (c == 2)
                {
                    Assert.AreEqual(BSONType.OID, bv.BSONType);
                    Assert.IsInstanceOf(typeof(BSONOid), bv.Value);
                    var oid = bv.Value as BSONOid;
                    Assert.AreEqual("51b9f3af98195c4600000000", oid.ToString());
                }
                c++;
            }
        }
예제 #9
0
        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
파일: EJDB.cs 프로젝트: sujeetpillai/ejdb
 /// <summary>
 /// Creates the query.
 /// </summary>
 /// <returns>The query object.</returns>
 /// <param name="qdoc">BSON query spec.</param>
 /// <param name="defaultcollection">Name of the collection used by default.</param>
 public EJDBQuery CreateQuery(object qv = null, string defaultcollection = null)
 {
     CheckDisposed();
     return(new EJDBQuery(this, BSONDocument.ValueOf(qv), defaultcollection));
 }