Exemplo n.º 1
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");
        }
Exemplo n.º 2
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();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Linq测试。
        /// </summary>
        /// <param name="sdb"></param>
        static void TestLinq(Sequoiadb sdb)
        {
            var cs   = sdb.GetCollecitonSpace("dbo");
            var coll = cs.GetCollection <HFareDetail>();

            var vList = coll.AsQueryable <HFareDetail>()
                        .Where(p => p.CreateTime > DateTime.Now.Date.AddMonths(-12))
                        .Skip(10).Take(1000)
                        .ToList();

            System.Console.WriteLine(string.Format("query {0} records", vList.Count));

            var vList2 = coll.AsQueryable <HFareDetail>()
                         .Where(p => p.PNumber == 624)
                         .ToList();

            System.Console.WriteLine(string.Format("query {0} records", vList2.Count));

            int count = coll.AsQueryable <HFareDetail>()
                        .Where(p => p.PNumber == 624)
                        .Count();

            System.Console.WriteLine(string.Format("Count:{0}", count));

            System.Console.ReadLine();
        }
Exemplo n.º 4
0
        // get collection space, if the collection space does not exist it will try to create one
        public static CollectionSpace GetCollecitonSpace(Sequoiadb sdb, string csName)
        {
            CollectionSpace cs = null;

            try
            {
                cs = sdb.GetCollecitonSpace(csName);
            }
            catch (BaseException e)
            {
                // verify whether the collection space exists
                if ("SDB_DMS_CS_NOTEXIST" == e.ErrorType)
                {
                    // if the collection space does not exist, we are going to create one
                    cs = CreateCollecitonSpace(sdb, csName);
                }
                else
                {
                    Console.WriteLine("Failed to get collection space {0},ErrorType = {1}", csName, e.ErrorType);
                    Environment.Exit(0);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Environment.Exit(0);
            }

            return(cs);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Linq测试。
        /// </summary>
        /// <param name="sdb"></param>
        static void TestAggregate(Sequoiadb sdb)
        {
            var cs   = sdb.GetCollecitonSpace("dbo");
            var coll = cs.GetCollection <HFareDetail>();

            String[] command = new String[2];
            //command[0] = "{$match:{PNumber:\"3\"}}";
            command[0] = string.Empty;
            command[1] = "{$group:{_id:\"$EDeptID\",total:{$sum:\"$Cash\"}}}";
            List <BsonDocument> list = new List <BsonDocument>();

            for (int i = 0; i < command.Length; i++)
            {
                BsonDocument obj = new BsonDocument();
                if (!string.IsNullOrEmpty(command[i]))
                {
                    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();
        }
Exemplo n.º 6
0
        static void TestAggregate2(Sequoiadb sdb)
        {
            var cs = sdb.GetCollecitonSpace("dbo");

            DBCollection coll = null;

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

            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++;
            }
        }
 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();
 }
Exemplo n.º 8
0
        /// <summary>
        /// 大量插入数据。
        /// </summary>
        /// <param name="sdb"></param>
        static void Test4(Sequoiadb sdb)
        {
            using (AgileHIS.Entities.DbEntities db = new AgileHIS.Entities.DbEntities())
            {
                var vList = db.HFareDetails.ToList();

                var cs   = sdb.GetCollecitonSpace("dbo");
                var coll = cs.GetCollection <HFareDetail>();

                //插入。
                foreach (var item in vList)
                {
                    coll.Insert(item);
                }

                System.Console.WriteLine(string.Format("insert {0} records", vList.Count));
                System.Console.ReadLine();
            }
        }
Exemplo n.º 9
0
        public void Transaction_Begin_Commit_Insert_Test()
        {
            // create cs, cl
            string csName = "testfoo";
            string cName  = "testbar";

            if (sdb.IsCollectionSpaceExist(csName))
            {
                sdb.DropCollectionSpace(csName);
            }
            sdb.CreateCollectionSpace(csName);
            CollectionSpace cs = sdb.GetCollecitonSpace(csName);
            DBCollection    cl = cs.CreateCollection(cName);

            // transction begin
            sdb.TransactionBegin();
            // insert record
            BsonDocument insertor1 = new BsonDocument();

            insertor1.Add("name", "tom");
            insertor1.Add("age", 25);
            insertor1.Add("addr", "guangzhou");
            BsonDocument insertor2 = new BsonDocument();

            insertor2.Add("name", "sam");
            insertor2.Add("age", 27);
            insertor2.Add("addr", "shanghai");
            cl.Insert(insertor1);
            cl.Insert(insertor2);
            // commit
            sdb.TransactionCommit();
            // check up
            DBCursor cursor = cl.Query();

            Assert.IsNotNull(cursor);
            int count = 0;

            while (cursor.Next() != null)
            {
                ++count;
                BsonDocument bson = cursor.Current();
                Assert.IsNotNull(bson);
            }
            Assert.IsTrue(count == 2);
            //sdb.TransactionRollback();
        }
Exemplo n.º 10
0
        public void CheckCacheWorksOrNotTest()
        {
            bool          boolValue = true;
            long          longValue = 6 * 1000;
            ClientOptions opt       = new ClientOptions();

            opt.CacheInterval = longValue;
            opt.EnableCache   = boolValue;
            Sequoiadb.InitClient(opt);
            Thread.Sleep(6000);
            CollectionSpace cs = sdb.GetCollecitonSpace(csName);

            Console.Out.WriteLine("begin, expect get cl from db");
            DBCollection cl = cs.GetCollection(cName);

            Thread.Sleep(3000);
            Console.Out.WriteLine("begin, expect get cl from cache");
            cl = cs.GetCollection(cName);
            Thread.Sleep(3500);
            Console.Out.WriteLine("begin, expect get cl from db");
            cl = cs.GetCollection(cName);
            Console.Out.WriteLine("haha");
        }
Exemplo n.º 11
0
        static void Test2(Sequoiadb sdb)
        {
            using (AgileHIS.Entities.DbEntities db = new AgileHIS.Entities.DbEntities())
            {
                var          v   = db.GBCodes.FirstOrDefault();
                BsonDocument doc = new BsonDocument();
                using (SequoiaDB.Bson.IO.BsonWriter w = SequoiaDB.Bson.IO.BsonWriter.Create(doc))
                {
                    SequoiaDB.Bson.Serialization.BsonSerializer.Serialize <GBCode>(w, v);
                }

                var          cs   = sdb.GetCollecitonSpace("dbo");
                DBCollection coll = null;
                if (!cs.IsCollectionExist("GBCode"))
                {
                    coll = cs.CreateCollection("GBCode");
                }
                else
                {
                    coll = cs.GetCollection("GBCode");
                }

                //插入。
                var vValue = coll.Insert(doc);
                System.Console.WriteLine(doc);

                //
                BsonDocument matcher = new BsonDocument();
                DBQuery      query   = new DBQuery();
                query.Matcher = matcher;
                DBCursor     cursor = coll.Query(query);
                BsonDocument bson   = cursor.Next();
                var          v2     = SequoiaDB.Bson.Serialization.BsonSerializer.Deserialize <GBCode>(bson);
                System.Console.WriteLine(bson);
                System.Console.ReadLine();
            }
        }
Exemplo n.º 12
0
        // get collection space, if the collection space does not exist it will try to create one
        public static CollectionSpace GetCollecitonSpace(Sequoiadb sdb, string csName)
        {
            CollectionSpace cs = null;
            try
            {
                cs = sdb.GetCollecitonSpace(csName);
            }
            catch (BaseException e)
            {
                // verify whether the collection space exists
                if ("SDB_DMS_CS_NOTEXIST" == e.ErrorType)
                {
                    // if the collection space does not exist, we are going to create one
                    cs = CreateCollecitonSpace(sdb, csName);
                }
                else
                {
                    Console.WriteLine("Failed to get collection space {0},ErrorType = {1}", csName, e.ErrorType);
                    Environment.Exit(0);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Environment.Exit(0);
            }

            return cs;
        }
Exemplo n.º 13
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();
        }
Exemplo n.º 14
0
        /// <summary>
        /// Linq测试。
        /// </summary>
        /// <param name="sdb"></param>
        static void TestLinq2(Sequoiadb sdb)
        {
            ////create Sequoiadb
            //Sequoiadb sdb = new Sequoiadb("192.168.23.57:50000");
            //sdb.Connect("", "");

            //GetCollecitonSpace。
            var cs = sdb.GetCollecitonSpace("dbo");

            //GetCollection。
            var coll = cs.GetCollection <HFareDetail>();

            //insert from rdb。
            List <HFareDetail> vList = null;

            using (AgileHIS.Entities.DbEntities db = new AgileHIS.Entities.DbEntities())
            {
                vList = db.HFareDetails.ToList();
                //insert。
                foreach (var item in vList)
                {
                    coll.Insert(item);
                }
                System.Console.WriteLine(string.Format("insert {0} records", vList.Count));
                System.Console.ReadLine();
            }

            //update docuemnt by linq。
            var v1 = vList.FirstOrDefault();

            v1.Name = string.Empty;
            v1.Cash = decimal.Zero;
            coll.Update(v1, p => p.ID == v1.ID);
            //update docuemnt by linq。
            coll.Update(p => new HFareDetail {
                Cash = decimal.Zero, Name = string.Empty, Price = decimal.Zero
            }, p => p.ChargeTime > DateTime.Now.AddDays(-1));
            //delete by linq
            coll.Delete(p => p.ChargeTime > DateTime.Now.AddDays(-1));

            //get Count
            int count = coll.AsQueryable <HFareDetail>()
                        .Where(p => p.SourceID == 0)
                        .Count();

            //Linq query Take\Skip。
            var vList2 = coll.AsQueryable <HFareDetail>()
                         .Where(p => p.CreateTime > DateTime.Now.Date.AddMonths(-12))
                         .Skip(10).Take(1000)
                         .ToList();

            System.Console.WriteLine(string.Format("query {0} records", vList.Count));

            //Linq FirstOrDefault。
            var vFare = coll.AsQueryable <HFareDetail>()
                        .Where(p => p.CreateTime > DateTime.Now.Date.AddMonths(-12))
                        .FirstOrDefault();

            System.Console.WriteLine(vFare);

            //Linq aggregate,Not Implemented
            var sum = coll.AsQueryable <HFareDetail>()
                      .Where(p => p.CreateTime > DateTime.Now.Date.AddMonths(-12))
                      .Sum(p => p.Cash);

            System.Console.ReadLine();
        }
Exemplo n.º 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();
        }