Esempio n. 1
0
        public void IndexesTest()
        {
            // Insert
            BsonDocument insertor = new BsonDocument();

            insertor.Add("Last Name", "Lin");
            insertor.Add("First Name", "Hetiu");
            insertor.Add("Address", "SYSU");
            BsonDocument sInsertor = new BsonDocument();

            sInsertor.Add("Phone", "10086");
            sInsertor.Add("EMail", "*****@*****.**");
            insertor.Add("Contact", sInsertor);
            ObjectId insertID = (ObjectId)coll.Insert(insertor);

            Assert.IsNotNull(insertID);

            // Create Index
            BsonDocument key = new BsonDocument();

            key.Add("Last Name", 1);
            key.Add("First Name", 1);
            string name = "index name";

            coll.CreateIndex(name, key, false, false);

            // Get Indexes
            DBCursor cursor = coll.GetIndex(name);

            Assert.IsNotNull(cursor);
            BsonDocument index = cursor.Next();

            Assert.IsNotNull(index);
            Assert.IsTrue(index["IndexDef"].AsBsonDocument["name"].AsString.Equals("index name"));

            // Drop Index
            coll.DropIndex(name);
            cursor = coll.GetIndex(name);
            Assert.IsNotNull(cursor);
            index = cursor.Next();
            Assert.IsNull(index);
        }
Esempio n. 2
0
 // create index on a given collection
 public static void CreateIndex(DBCollection dbc, string indexName,
                                BsonDocument key, bool isUnique, bool isEnforced)
 {
     try
     {
         dbc.CreateIndex(indexName, key, isUnique, isEnforced);
     }
     catch (BaseException e)
     {
         Console.WriteLine("Failed to create index, ErrorType = {1}", e.ErrorType);
         Environment.Exit(0);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         Environment.Exit(0);
     }
 }
Esempio n. 3
0
        public void GetQueryMetaTest()
        {
            try{
                // create cl
                DBCollection coll2  = null;
                string       cName2 = "testbar2";
                if (cs.IsCollectionExist(cName2))
                {
                    cs.DropCollection(cName);
                }
                coll2 = cs.CreateCollection(cName2);
                // create index
                coll2.CreateIndex("ageIndex", new BsonDocument("age", -1), false, false);
                // prepare record
                Random ro                    = new Random();
                int    recordNum             = 10000;
                List <BsonDocument> insertor = new List <BsonDocument>();
                for (int i = 0; i < recordNum; i++)
                {
                    BsonDocument obj = new BsonDocument();
                    obj.Add("Id", i);
                    obj.Add("age", ro.Next(0, 100));
                    obj.Add("date", DateTime.Now.ToString());
                    insertor.Add(obj);
                }
                coll2.BulkInsert(insertor, 0);

                // TODO:

                // query
                BsonDocument subobj = new BsonDocument();
                BsonDocument query  = new BsonDocument();
                query.Add("age", subobj);
                subobj.Add("$gt", 1);
                subobj.Add("$lt", 99);
                // hint
                BsonDocument hint = new BsonDocument();
                hint.Add("", "ageIndex");
                // orderBy
                BsonDocument orderBy = new BsonDocument();
                orderBy.Add("Indexblocks", 1);
                // execute getQueryMeta
                DBCursor cursor     = coll2.GetQueryMeta(query, orderBy, null, 0, -1);
                DBCursor datacursor = null;
                long     count      = 0;
                while (cursor.Next() != null)
                {
                    BsonDocument temp = new BsonDocument();
                    temp = cursor.Current();
                    BsonDocument h = new BsonDocument();
                    if (temp.Contains("Indexblocks") && temp["Indexblocks"].IsBsonArray)
                    {
                        h.Add("Indexblocks", temp["Indexblocks"].AsBsonArray);
                    }
                    datacursor = coll2.Query(null, null, null, h, 0, -1);
                    while (datacursor.Next() != null)
                    {
                        count++;
                    }
                }
                Assert.IsTrue(recordNum == count);
            }catch (BaseException e)
            {
                Console.WriteLine(e.ErrorType);
                return;
            }
        }
Esempio n. 4
0
        public void IndexesTest()
        {
            // Insert
            BsonDocument insertor = new BsonDocument();

            insertor.Add("Last Name", "Lin");
            insertor.Add("First Name", "Hetiu");
            insertor.Add("Address", "SYSU");
            BsonDocument sInsertor = new BsonDocument();

            sInsertor.Add("Phone", "10086");
            sInsertor.Add("EMail", "*****@*****.**");
            insertor.Add("Contact", sInsertor);
            ObjectId insertID = (ObjectId)coll.Insert(insertor);

            Assert.IsNotNull(insertID);

            // Create Index with default sort buffer size
            BsonDocument key = new BsonDocument();

            key.Add("Last Name", 1);
            key.Add("First Name", 1);
            string name = "index_name_default";

            coll.CreateIndex(name, key, false, false);

            // Create Index without sort buffer
            BsonDocument key2 = new BsonDocument();

            key2.Add("Last Name2", 1);
            key2.Add("First Name2", 1);
            string name2 = "index_name_without_buffer";

            coll.CreateIndex(name2, key2, true, true, 0);

            // Create Index with user-defined sort buffer
            BsonDocument key3 = new BsonDocument();

            key3.Add("Last Name3", 1);
            key3.Add("First Name3", 1);
            string name3 = "index_name_with_user-defined_buffer";

            coll.CreateIndex(name3, key3, true, true, 128);

            // Create Index, expect -6 exception
            BsonDocument key4 = new BsonDocument();

            key3.Add("Last Name4", 1);
            key3.Add("First Name4", 1);
            string name4 = "index_name_with_error";

            try
            {
                coll.CreateIndex(name4, key4, true, true, -1);
                Assert.IsTrue(false);
            }
            catch (BaseException e)
            {
                Assert.IsTrue(e.ErrorCode == new BaseException("SDB_INVALIDARG").ErrorCode);
            }

            // Get Indexes
            DBCursor cursor = coll.GetIndex(name);

            Assert.IsNotNull(cursor);
            BsonDocument index = cursor.Next();

            Assert.IsNotNull(index);
            Assert.IsTrue(index["IndexDef"].AsBsonDocument["name"].AsString.Equals(name));

            // Get Indexes
            DBCursor cursor2 = coll.GetIndex(name2);

            Assert.IsNotNull(cursor2);
            BsonDocument index2 = cursor2.Next();

            Assert.IsNotNull(index2);
            Assert.IsTrue(index2["IndexDef"].AsBsonDocument["name"].AsString.Equals(name2));

            // Get Indexes
            DBCursor cursor3 = coll.GetIndex(name3);

            Assert.IsNotNull(cursor3);
            BsonDocument index3 = cursor3.Next();

            Assert.IsNotNull(index3);
            Assert.IsTrue(index3["IndexDef"].AsBsonDocument["name"].AsString.Equals(name3));

            // Drop Index
            coll.DropIndex(name);
            cursor = coll.GetIndex(name);
            Assert.IsNotNull(cursor);
            index = cursor.Next();
            Assert.IsNull(index);

            coll.DropIndex(name2);
            cursor = coll.GetIndex(name2);
            Assert.IsNotNull(cursor);
            index = cursor.Next();
            Assert.IsNull(index);

            coll.DropIndex(name3);
            cursor = coll.GetIndex(name3);
            Assert.IsNotNull(cursor);
            index = cursor.Next();
            Assert.IsNull(index);
        }