Пример #1
0
        /// <summary>
        /// 简单测试。
        /// </summary>
        /// <param name="sdb"></param>
        static void Test1(Sequoiadb sdb)
        {
            if (sdb.IsCollectionSpaceExist("dbo"))
            {
                sdb.DropCollectionSpace("dbo");
            }

            var cs   = sdb.CreateCollectionSpace("dbo");
            var coll = cs.CreateCollection("foo");

            // insert
            BsonDocument insertor = new BsonDocument();
            string       date     = DateTime.Now.ToString();

            insertor.Add("operation", "Insert");
            insertor.Add("date", date);
            Object id = (ObjectId)coll.Insert(insertor);

            BsonDocument matcher = new BsonDocument();
            DBQuery      query   = new DBQuery();

            matcher.Add("date", date);
            query.Matcher = matcher;
            DBCursor     cursor = coll.Query(query);
            BsonDocument bson   = cursor.Next();

            System.Console.WriteLine(bson);
            System.Console.ReadLine();
        }
Пример #2
0
        // create collection space, if the collection space exists then return
        public static CollectionSpace CreateCollecitonSpace(Sequoiadb sdb, string csName, int pageSize)
        {
            CollectionSpace cs = null;

            try
            {
                cs = sdb.CreateCollectionSpace(csName, pageSize);
            }
            catch (BaseException e)
            {
                // verify whether the collection space exists
                if ("SDB_DMS_CS_EXIST" == e.ErrorType)
                {
                    cs = GetCollecitonSpace(sdb, csName);
                }
                // invalid page size argument
                else if ("SDB_INVALIDARG" == e.ErrorType)
                {
                    Console.WriteLine("Failed to create collection space {0}, invalid page size {1}", csName, pageSize);
                    Environment.Exit(0);
                }
                else
                {
                    Console.WriteLine("Failed to create collection space {0},ErrorType = {1}", csName, e.ErrorType);
                    Environment.Exit(0);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Environment.Exit(0);
            }

            return(cs);
        }
Пример #3
0
        public void ConnectWithSSLTest()
        {
            ConfigOptions   cfgOpt = null;
            CollectionSpace cs2    = null;
            DBCollection    coll2  = null;
            Sequoiadb       sdb2   = new Sequoiadb(config.conf.Coord.Address);

            System.Console.WriteLine(config.conf.Coord.Address.ToString());

            // set connect using ssl
            cfgOpt        = new ConfigOptions();
            cfgOpt.UseSSL = true;

            // connect to database
            sdb2.Connect("", "", cfgOpt);
            if (true == sdb2.IsCollectionSpaceExist("testSSL"))
            {
                cs2 = sdb2.GetCollecitonSpace("testSSL");
            }
            else
            {
                cs2 = sdb2.CreateCollectionSpace("testSSL");
            }
            if (true == cs2.IsCollectionExist("testSSL"))
            {
                coll2 = cs2.GetCollection("testSSL");
            }
            else
            {
                coll2 = cs2.CreateCollection("testSSL");
            }

            sdb2.DropCollectionSpace("testSSL");
        }
Пример #4
0
        public void MyTestInitialize()
        {
            csArr[0]  = csName1;
            csArr[1]  = csName2;
            csArr[2]  = csName3;
            clArr1[0] = clName1_1;
            clArr1[1] = clName1_2;
            clArr1[2] = clName1_3;
            clArr2[0] = clName2_1;
            clArr2[1] = clName2_2;
            clArr2[2] = clName2_3;
            clArr3[0] = clName3_1;
            clArr3[1] = clName3_2;
            clArr3[2] = clName3_3;
            sdb       = new Sequoiadb(config.conf.Coord.Address);
            sdb.Connect(config.conf.UserName, config.conf.Password);
            BsonDocument options = new BsonDocument();

            options.Add("ReplSize", 0);
            if (sdb.IsCollectionSpaceExist(csName))
            {
                sdb.DropCollectionSpace(csName);
            }
            cs   = sdb.CreateCollectionSpace(csName);
            coll = cs.CreateCollection(cName, options);
        }
Пример #5
0
        public void CollectionTest()
        {
            string          csName = "testCS1";
            string          clName = "testCL1";
            CollectionSpace cs     = null;
            Sequoiadb       sdb    = new Sequoiadb(config.conf.Coord.Address);

            sdb.Connect(config.conf.UserName, config.conf.Password);
            if (sdb.IsCollectionSpaceExist(csName))
            {
                cs = sdb.GetCollecitonSpace(csName);
            }
            else
            {
                cs = sdb.CreateCollectionSpace(csName);
            }
            if (!cs.IsCollectionExist(clName))
            {
                cs.CreateCollection(clName);
            }
            cs.DropCollection(clName);
            Assert.IsFalse(cs.IsCollectionExist(clName));
            sdb.DropCollectionSpace(csName);
            sdb.Disconnect();
        }
Пример #6
0
 public void MyTestInitialize()
 {
     sdb = new Sequoiadb(config.conf.Coord.Address);
     sdb.Connect(config.conf.UserName, config.conf.Password);
     if (sdb.IsCollectionSpaceExist(csName))
         sdb.DropCollectionSpace(csName);
     cs = sdb.CreateCollectionSpace(csName);
     coll = cs.CreateCollection(cName);
 }
Пример #7
0
 public void MyTestInitialize()
 {
     sdb = new Sequoiadb(config.conf.Coord.Address);
     sdb.Connect(config.conf.UserName, config.conf.Password);
     if (sdb.IsCollectionSpaceExist(csName))
     {
         sdb.DropCollectionSpace(csName);
     }
     cs   = sdb.CreateCollectionSpace(csName);
     coll = cs.CreateCollection(cName);
 }
Пример #8
0
        public void MyTestInitialize()
        {
            sdb = new Sequoiadb(config.conf.Coord.Address);
            sdb.Connect(config.conf.UserName, config.conf.Password);
            BsonDocument options = new BsonDocument();

            options.Add("ReplSize", 0);
            if (sdb.IsCollectionSpaceExist(csName))
            {
                sdb.DropCollectionSpace(csName);
            }
            cs   = sdb.CreateCollectionSpace(csName);
            coll = cs.CreateCollection(cName, options);
        }
Пример #9
0
 public void MyTestInitialize()
 {
     try
     {
         sdb = new Sequoiadb(config.conf.Coord.Address);
         sdb.Connect(config.conf.UserName, config.conf.Password);
         if (sdb.IsCollectionSpaceExist(csName))
             sdb.DropCollectionSpace(csName);
         cs = sdb.CreateCollectionSpace(csName);
         cl = cs.CreateCollection(cName);
     }
     catch (BaseException e)
     {
         Console.WriteLine("Failed to Initialize in DBLobTest, ErrorType = {0}", e.ErrorType);
         Environment.Exit(0);
     }
 }
 public void CollectionTest()
 {
     string csName = "testCS1";
     string clName = "testCL1";
     CollectionSpace cs = null;
     Sequoiadb sdb = new Sequoiadb(config.conf.Coord.Address);
     sdb.Connect(config.conf.UserName, config.conf.Password);
     if(sdb.IsCollectionSpaceExist(csName))
         cs = sdb.GetCollecitonSpace(csName);
     else
         cs = sdb.CreateCollectionSpace(csName);
     if (!cs.IsCollectionExist(clName))
         cs.CreateCollection(clName);
     cs.DropCollection(clName);
     Assert.IsFalse(cs.IsCollectionExist(clName));
     sdb.DropCollectionSpace(csName);
     sdb.Disconnect();
 }
Пример #11
0
 public void MyTestInitialize()
 {
     try
     {
         sdb = new Sequoiadb(config.conf.Coord.Address);
         sdb.Connect(config.conf.UserName, config.conf.Password);
         if (sdb.IsCollectionSpaceExist(csName))
         {
             sdb.DropCollectionSpace(csName);
         }
         cs   = sdb.CreateCollectionSpace(csName);
         coll = cs.CreateCollection(cName);
     }
     catch (BaseException e)
     {
         Console.WriteLine("Failed to Initialize in DBCollectionTest, ErrorType = {0}", e.ErrorType);
         Environment.Exit(0);
     }
 }
Пример #12
0
        public void CollectionSpaceTest()
        {
            string       csName  = "Test";
            string       csName2 = "Test2";
            BsonDocument option  = new BsonDocument();

            option.Add(SequoiadbConstants.FIELD_PAGESIZE, 4096);

            if (!sdb.IsCollectionSpaceExist(csName))
            {
                // default and with page size
                sdb.CreateCollectionSpace(csName);
            }
            Assert.IsTrue(sdb.IsCollectionSpaceExist(csName));
            sdb.DropCollectionSpace(csName);
            Assert.IsFalse(sdb.IsCollectionSpaceExist(csName));
            // with options
            sdb.CreateCollectionSpace(csName2, option);
            Assert.IsTrue(sdb.IsCollectionSpaceExist(csName2));
            sdb.DropCollectionSpace(csName2);
            Assert.IsFalse(sdb.IsCollectionSpaceExist(csName2));
        }
Пример #13
0
        public void CheckDisableCacheLogic()
        {
            // test1: test default value
            bool          boolValue = false;
            long          longValue = 300 * 1000;
            ClientOptions opt       = new ClientOptions();

            opt.CacheInterval = longValue;
            opt.EnableCache   = boolValue;
            Sequoiadb db = new Sequoiadb(config.conf.Coord.Address);

            db.Connect(config.conf.UserName, config.conf.Password);
            Sequoiadb.InitClient(opt);
            // check
            Type         t                    = db.GetType();
            BindingFlags flag                 = BindingFlags.NonPublic | BindingFlags.Instance;
            FieldInfo    f_nameCache          = t.GetField("nameCache", flag);
            Dictionary <String, DateTime> dic = f_nameCache.GetValue(db) as Dictionary <String, DateTime>;

            Assert.AreEqual(0, dic.Count);

            // create cs
            CollectionSpace[] csObjArr = new CollectionSpace[3];
            for (int i = 0; i < csArr.Length; i++)
            {
                try
                {
                    db.DropCollectionSpace(csArr[i]);
                }
                catch (BaseException e)
                {
                }
            }
            for (int i = 0; i < csArr.Length; i++)
            {
                csObjArr[i] = db.CreateCollectionSpace(csArr[i]);
            }
            // test1: 检测db对象中cs的缓存情况
            Console.Out.WriteLine("point 1: after creating cs, nameCache.Count is: " + dic.Count);
            Assert.AreEqual(0, dic.Count);
            // drop one cs
            db.DropCollectionSpace(csArr[0]);
            // test2: 删除一个cs之后,检测db对象中cs的缓存情况
            Assert.AreEqual(0, dic.Count);
            // create the drop cs
            csObjArr[0] = db.CreateCollectionSpace(csArr[0]);
            Assert.AreEqual(0, dic.Count);
            // create cl
            BsonDocument conf = new BsonDocument();

            conf.Add("ReplSize", 0);
            for (int i = 0; i < clArr1.Length; i++)
            {
                //Console.Out.WriteLine("csObjArr[0] is: " + csObjArr[0].getName() + ", clArr1[x] is: " + clArr1[i]);
                csObjArr[0].CreateCollection(clArr1[i], conf);
            }
            // test3: 检测db对象中cs,cl的缓存情况
            Assert.AreEqual(0, dic.Count);
            // drop one cl
            csObjArr[0].DropCollection(clArr1[0]);
            // test4: 检测删除cl之后,cs,cl的缓存情况
            Assert.AreEqual(0, dic.Count);
            // drop one cs
            db.DropCollectionSpace(csArr[0]);
            // test5: 检测删除cs之后,cs,cl的缓存情况
            Assert.AreEqual(0, dic.Count);
            db.Disconnect();
        }
Пример #14
0
        // create collection space, if the collection space exists then return
        public static CollectionSpace CreateCollecitonSpace(Sequoiadb sdb, string csName, int pageSize)
        {
            CollectionSpace cs = null;
            try
            {
                cs = sdb.CreateCollectionSpace(csName, pageSize);
            }
            catch (BaseException e)
            {
                // verify whether the collection space exists
                if ("SDB_DMS_CS_EXIST" == e.ErrorType)
                    cs = GetCollecitonSpace(sdb, csName);
                // invalid page size argument
                else if ("SDB_INVALIDARG" == e.ErrorType)
                {
                    Console.WriteLine("Failed to create collection space {0}, invalid page size {1}", csName, pageSize);
                    Environment.Exit(0);
                }
                else
                {
                    Console.WriteLine("Failed to create collection space {0},ErrorType = {1}", csName, e.ErrorType);
                    Environment.Exit(0);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Environment.Exit(0);
            }

            return cs;
        }
Пример #15
0
        static void TestAggregate5(Sequoiadb sdb)
        {
            // The collection space name
            string csName = "sample";
            // The collection name
            string cName = "sample";

            // connect
            CollectionSpace cs;

            if (sdb.IsCollectionSpaceExist(csName))
            {
                cs = sdb.GetCollecitonSpace(csName);
            }
            else
            {
                cs = sdb.CreateCollectionSpace(csName);
            }

            DBCollection coll = null;

            if (cs.IsCollectionExist(cName))
            {
                coll = cs.GetCollection(cName);
            }
            else
            {
                coll = cs.CreateCollection(cName);
            }

            // delete all records from the collection
            BsonDocument bson = new BsonDocument();

            coll.Delete(bson);

            String[] command = new String[2];
            command[0] = "{$match:{status:\"A\"}}";
            command[1] = "{$group:{_id:\"$cust_id\",amount:{\"$sum\":\"$amount\"},cust_id:{\"$first\":\"$cust_id\"}}}";
            String[] record = new String[4];
            record[0] = "{cust_id:\"A123\",amount:500,status:\"A\"}";
            record[1] = "{cust_id:\"A123\",amount:250,status:\"A\"}";
            record[2] = "{cust_id:\"B212\",amount:200,status:\"A\"}";
            record[3] = "{cust_id:\"A123\",amount:300,status:\"D\"}";
            // insert record into database
            for (int i = 0; i < record.Length; i++)
            {
                BsonDocument obj = new BsonDocument();
                obj = BsonDocument.Parse(record[i]);
                Console.WriteLine("Record is: " + obj.ToString());
                coll.Insert(obj);
            }
            List <BsonDocument> list = new List <BsonDocument>();

            for (int i = 0; i < command.Length; i++)
            {
                BsonDocument obj = new BsonDocument();
                obj = BsonDocument.Parse(command[i]);
                list.Add(obj);
            }

            DBCursor cursor = coll.Aggregate(list);
            int      count  = 0;

            while (null != cursor.Next())
            {
                Console.WriteLine("Result is: " + cursor.Current().ToString());
                String str = cursor.Current().ToString();
                count++;
            }

            System.Console.ReadLine();
        }
Пример #16
0
        /// <summary>
        /// Update测试。
        /// </summary>
        /// <param name="sdb"></param>
        static void TestUpdate(Sequoiadb sdb)
        {
            // The collection space name
            string csName = "sample";
            // The collection name
            string cName = "sample";

            // connect
            CollectionSpace cs;

            if (sdb.IsCollectionSpaceExist(csName))
            {
                cs = sdb.GetCollecitonSpace(csName);
            }
            else
            {
                cs = sdb.CreateCollectionSpace(csName);
            }

            DBCollection coll = null;

            if (cs.IsCollectionExist(cName))
            {
                coll = cs.GetCollection(cName);
            }
            else
            {
                coll = cs.CreateCollection(cName);
            }

            // delete all records from the collection
            BsonDocument bson = new BsonDocument();

            coll.Delete(bson);

            String[] record = new String[4];
            record[0] = "{cust_id:\"A123\",amount:500,status:\"A\"}";
            record[1] = "{cust_id:\"A123\",amount:250,status:\"A\"}";
            record[2] = "{cust_id:\"B212\",amount:200,status:\"A\"}";
            record[3] = "{cust_id:\"A123\",amount:300,status:\"D\"}";
            // insert record into database
            for (int i = 0; i < record.Length; i++)
            {
                BsonDocument obj = new BsonDocument();
                obj = BsonDocument.Parse(record[i]);
                Console.WriteLine("Record is: " + obj.ToString());
                coll.Insert(obj);
            }

            //准备update
            BsonDocument updater  = new BsonDocument();
            BsonDocument matcher  = new BsonDocument();
            BsonDocument modifier = new BsonDocument();
            BsonDocument hint     = new BsonDocument();

            //条件
            matcher.Add("cust_id", new BsonDocument("$et", "A123"));
            //更新。
            updater.Add("amount", "1000");
            updater.Add("status", "C");
            modifier.Add("$set", updater);
            //update
            coll.Update(matcher, modifier, hint);

            System.Console.ReadLine();
        }
Пример #17
0
        public void DomainGlobalTest()
        {
            string       dmName    = "testDomainInCS";
            string       csName    = "domainCS";
            string       clName    = "domainCL";
            string       groupName = config.conf.Groups[0].GroupName;
            BsonDocument options   = new BsonDocument();
            BsonArray    arr       = new BsonArray();

            arr.Add(groupName);
            options.Add(SequoiadbConstants.FIELD_GROUPS, arr);
            Domain       dm     = null;
            DBCursor     cur    = null;
            BsonDocument record = null;

            /// IsDomainExist
            bool flag = sdb.IsDomainExist(dmName);

            Assert.IsFalse(flag);

            /// getDomain
            try
            {
                dm = sdb.GetDomain(dmName);
            }
            catch (BaseException e)
            {
                Assert.IsTrue(e.ErrorType.Equals("SDB_CAT_DOMAIN_NOT_EXIST"));
            }

            /// createDomain
            dm = null;
            dm = sdb.CreateDomain(dmName, options);
            Assert.IsNotNull(dm);

            /// IsDomainExist
            flag = false;
            flag = sdb.IsDomainExist(dmName);
            Assert.IsTrue(flag);

            /// getDomain
            dm = null;
            dm = sdb.GetDomain(dmName);
            Assert.IsNotNull(dm);

            /// listDomains
            cur = sdb.ListDomains(null, null, null, null);
            Assert.IsNotNull(cur);
            record = cur.Next();
            Assert.IsNotNull(record);

            /// getName
            string name = dm.Name;

            Assert.IsTrue(name.Equals(dmName));

            // create cs
            BsonDocument opts1 = new BsonDocument();

            opts1.Add("Domain", dmName);
            CollectionSpace cs = sdb.CreateCollectionSpace(csName, opts1);
            // create cl
            BsonDocument opts2 = new BsonDocument();

            //BsonDocument key = new BsonDocument();
            //key.Add("a", 1);
            opts2.Add("ShardingKey", new BsonDocument("a", 1));
            opts2.Add("ShardingType", "hash");
            opts2.Add("AutoSplit", true);
            DBCollection cl = cs.CreateCollection(clName, opts2);

            /// listCS
            cur = dm.ListCS();
            Assert.IsNotNull(cur);
            record = cur.Next();
            Assert.IsNotNull(record);

            /// listCL
            cur = dm.ListCL();
            Assert.IsNotNull(cur);
            record = cur.Next();
            Assert.IsNotNull(record);

            // dropCS
            sdb.DropCollectionSpace(csName);

            /// alter
            /// TODO: alter should be verified
            BsonDocument opts3 = new BsonDocument();
            BsonArray    arr2  = new BsonArray();

            opts3.Add("Groups", arr2);
            opts3.Add("AutoSplit", false);
            dm.Alter(opts3);

            /// listDomains
            cur = sdb.ListDomains(null, null, null, null);
            Assert.IsNotNull(cur);
            record = cur.Next();
            Assert.IsNotNull(record);

            /// dropDomain
            sdb.DropDomain(dmName);

            /// listDomains
            cur = sdb.ListDomains(null, null, null, null);
            Assert.IsNotNull(cur);
            record = cur.Next();
            Assert.IsNull(record);
        }
Пример #18
0
        public void CheckInvalidSituaction()
        {
            bool          boolValue = true;
            long          longValue = 300 * 1000;
            ClientOptions opt       = new ClientOptions();

            opt.CacheInterval = longValue;
            opt.EnableCache   = boolValue;
            Sequoiadb db = new Sequoiadb(config.conf.Coord.Address);

            db.Connect(config.conf.UserName, config.conf.Password);
            Sequoiadb.InitClient(opt);
            // check
            Type         t                    = db.GetType();
            BindingFlags flag                 = BindingFlags.NonPublic | BindingFlags.Instance;
            FieldInfo    f_nameCache          = t.GetField("nameCache", flag);
            Dictionary <String, DateTime> dic = f_nameCache.GetValue(db) as Dictionary <String, DateTime>;

            Assert.AreEqual(0, dic.Count);

            String csName = "foo_csharp.";
            String clName = "bar_csharp.";

            try {
                db.DropCollectionSpace(csName);
            } catch (BaseException e) {
            }
            try {
                db.CreateCollectionSpace(csName);
                Assert.Fail();
            } catch (BaseException e) {
                csName = "foo_csharp";
            }
            try {
                db.DropCollectionSpace(csName);
            } catch (BaseException e) {
            }
            // check
            Assert.AreEqual(0, dic.Count);
            CollectionSpace cs = db.CreateCollectionSpace(csName);

            // check
            Assert.AreEqual(1, dic.Count);
            Assert.IsTrue(dic.ContainsKey(csName));
            try {
                cs.CreateCollection(clName);
                Assert.Fail();
            } catch (BaseException e) {
                clName = "bar_csharp";
            }
            // check
            Assert.AreEqual(1, dic.Count);
            Assert.IsTrue(dic.ContainsKey(csName));
            cs.CreateCollection(clName);
            // check
            Assert.AreEqual(2, dic.Count);
            Assert.IsTrue(dic.ContainsKey(csName + "." + clName));
            Assert.IsTrue(dic.ContainsKey(csName));

            db.DropCollectionSpace(csName);
            // check
            Assert.AreEqual(0, dic.Count);

            db.Disconnect();
        }