Esempio n. 1
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();
        }
Esempio n. 2
0
        public void CloseTest()
        {
            int num = 10;

            for (int i = 0; i < num; i++)
            {
                BsonDocument insertor = new BsonDocument();
                insertor.Add("num", i);
                coll.Insert(insertor);
            }

            BsonDocument dummy  = new BsonDocument();
            DBCursor     cursor = coll.Query(dummy, dummy, dummy, dummy);
            BsonDocument obj    = new BsonDocument();

            obj = cursor.Current();
            Assert.IsNotNull(obj);
            obj = cursor.Next();
            Assert.IsNotNull(obj);
            // DO:
            cursor.Close();
            // close
            try
            {
                cursor.Close();
            }catch (BaseException)
            {
                Assert.Fail();
                //Console.WriteLine("After close cursor, call Close() get errno " + e.ErrorCode );
                //Assert.IsTrue(e.ErrorType.Equals("SDB_DMS_CONTEXT_IS_CLOSE"));
            }
            // current
            try
            {
                cursor.Current();
            }
            catch (BaseException e)
            {
                Console.WriteLine("After close cursor, call Current() get errno " + e.ErrorCode);
                Assert.IsTrue(e.ErrorType.Equals("SDB_DMS_CONTEXT_IS_CLOSE"));
            }
            // next
            try
            {
                cursor.Next();
            }
            catch (BaseException e)
            {
                Console.WriteLine("After close cursor, call Next() get errno " + e.ErrorCode);
                Assert.IsTrue(e.ErrorType.Equals("SDB_DMS_CONTEXT_IS_CLOSE"));
            }
        }
Esempio n. 3
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++;
            }
        }
Esempio n. 4
0
        public void CurrentTest()
        {
            BsonDocument insertor1 = new BsonDocument();

            insertor1.Add("Last Name", "怪");
            insertor1.Add("First Name", "啊");
            insertor1.Add("Address", "SYSU");
            BsonDocument sInsertor1 = new BsonDocument();

            sInsertor1.Add("Phone", "10000");
            sInsertor1.Add("EMail", "*****@*****.**");
            insertor1.Add("Contact", sInsertor1);
            coll.Insert(insertor1);

            BsonDocument dummy    = new BsonDocument();
            BsonDocument updater  = new BsonDocument();
            BsonDocument matcher  = new BsonDocument();
            BsonDocument modifier = new BsonDocument();

            updater.Add("Age", 25);
            modifier.Add("$group", updater);
            matcher.Add("First Name", "啊");
            DBCursor cursor = coll.Query(matcher, dummy, dummy, dummy);

            Assert.IsNotNull(cursor);
            BsonDocument obj = new BsonDocument();

            obj = cursor.Current();
            Assert.IsNotNull(obj);

            Assert.IsTrue(obj["First Name"].AsString.Equals("啊"));
            Assert.IsTrue(obj["Last Name"].AsString.Equals("怪"));
            Assert.IsTrue(obj["Address"].AsString.Equals("SYSU"));
        }
Esempio n. 5
0
        public void QueryOneTest()
        {
            for (int i = 0; i < 10; ++i)
            {
                string       date     = DateTime.Now.ToString();
                BsonDocument insertor = new BsonDocument();
                insertor.Add("operation", "Query");
                insertor.Add("date", date);
                coll.Insert(insertor);
            }
            BsonDocument matcher = new BsonDocument();
            DBQuery      query   = new DBQuery();

            matcher.Add("operation", "Query");
            query.Matcher         = matcher;
            query.ReturnRowsCount = 5;
            query.SkipRowsCount   = 5;
            query.Flag            = DBQuery.FLG_QUERY_WITH_RETURNDATA;
            DBCursor cursor = coll.Query(query);

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

            while (cursor.Next() != null)
            {
                ++count;
                BsonDocument bson = cursor.Current();
                Assert.IsNotNull(bson);
            }
            Assert.IsTrue(count == 5);
        }
Esempio n. 6
0
        public static void FindRecord(DBCollection dbc, BsonDocument query, BsonDocument selector, BsonDocument orderBy,
                                      BsonDocument hint, long skip, long numReturn)
        {
            try
            {
                // find specific records from collection with rules
                DBCursor cursor = dbc.Query(query, selector, orderBy, hint, skip, numReturn);

                Console.WriteLine("Record read:");
                while (cursor.Next() != null)
                {
                    Console.WriteLine(cursor.Current().ToString());
                }
            }
            catch (BaseException e)
            {
                Console.WriteLine("Failed to find records from collection, ErrorType = {0}", e.ErrorType);
                return;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Environment.Exit(0);
            }
        }
        public void MyTestInitialize()
        {
            // check whether it is in the cluster environment or not
            if (!Constants.isClusterEnv(sdb))
            {
                Console.WriteLine("removeRG is for cluster environment only.");
                return;
            }
            // argument
            groupName = config.conf.Groups[0].GroupName;
            hostName  = config.conf.Groups[0].Nodes[0].HostName;
            port      = config.conf.Groups[0].Nodes[0].Port;
            dbpath    = config.conf.Groups[0].Nodes[0].DBPath;
            // drop the exist group
            group = sdb.GetReplicaGroup(groupName);
            if (group != null)
            {
                // drop all the cs in current group, and then remove this group
                int nodeNum = group.GetNodeNum(SDBConst.NodeStatus.SDB_NODE_ALL);
                if (nodeNum > 0)
                {
                    var       nd = group.GetMaster();
                    Sequoiadb db = new Sequoiadb(nd.HostName, nd.Port);
                    Assert.IsTrue(nd.Start());
                    db.Connect(config.conf.UserName, config.conf.Password);
                    DBCursor cursor = db.ListCollectionSpaces();
                    while (cursor.Next() != null)
                    {
                        BsonDocument obj  = cursor.Current();
                        string       temp = null;
                        if (obj.Contains("Name"))
                        {
                            temp = obj["Name"].AsString;
                        }
                        sdb.DropCollectionSpace(temp);
                    }
                    db.Disconnect();
                }
                try
                {
                    sdb.RemoveReplicaGroup(group.GroupName);
                }
                catch (BaseException e)
                {
                    string errInfo = e.Message;
                    Console.WriteLine("Error code is: " + errInfo);
                }
            }
            // create a new group
            group = sdb.CreateReplicaGroup(groupName);
            Assert.IsTrue(groupName.Equals(group.GroupName));
            // create a node
            Dictionary <string, string> map = new Dictionary <string, string>();

            map.Add("diaglevel", config.conf.Groups[0].Nodes[0].DiagLevel);
            node = group.CreateNode(hostName, port, dbpath, map);
            Assert.IsNotNull(node);
            group.Start();
        }
Esempio n. 8
0
        public void CloseAllCursorsTest()
        {
            int num = 10000;

            for (int i = 0; i < num; i++)
            {
                BsonDocument insertor = new BsonDocument();
                insertor.Add("num", i);
                coll.Insert(insertor);
            }

            BsonDocument dummy   = new BsonDocument();
            DBCursor     cursor  = coll.Query(dummy, dummy, dummy, dummy);
            DBCursor     cursor1 = coll.Query(dummy, dummy, dummy, dummy);
            DBCursor     cursor2 = coll.Query(dummy, dummy, dummy, dummy);
            BsonDocument obj     = new BsonDocument();

            obj = cursor1.Next();
            obj = cursor2.Next();
            // DO:
            sdb.CloseAllCursors();
            // cursor
            try
            {
                while (null != cursor.Next())
                {
                }
            }
            catch (BaseException e)
            {
                int eno = e.ErrorCode;
                Assert.IsTrue(e.ErrorType.Equals("SDB_RTN_CONTEXT_NOTEXIST"));
            }
            // cursor1
            try
            {
                while (null != cursor1.Next())
                {
                }
            }
            catch (BaseException e)
            {
                int eno = e.ErrorCode;
                Assert.IsTrue(e.ErrorType.Equals("SDB_RTN_CONTEXT_NOTEXIST"));
            }
            // curosr2
            try
            {
                obj = cursor2.Current();
                cursor2.Close();
            }
            catch (BaseException)
            {
                Assert.Fail();
                //int eno = e.ErrorCode;
                //Assert.IsTrue(e.ErrorType.Equals("SDB_RTN_CONTEXT_NOTEXIST"));
            }
        }
Esempio n. 9
0
        public void AggregateTest()
        {
            String[] command = new String[2];
            command[0] = "{$match:{status:\"A\"}}";
            command[1] = "{$group:{_id:\"$cust_id\",total:{$sum:\"$amount\"}}}";
            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++;
            }
            Assert.IsTrue(2 == count);
        }
Esempio n. 10
0
 public static void SequoiadbCleamUp()
 {
     // check whether it is in the cluster environment or not
     if (!Constants.isClusterEnv(sdb))
     {
         return;
     }
     group = sdb.GetReplicaGroup(groupName);
     if (null != group)
     {
         // drop all the cs in current group, and then remove this group
         int nodeNum = group.GetNodeNum(SDBConst.NodeStatus.SDB_NODE_ALL);
         if (nodeNum > 0)
         {
             SequoiaDB.Node nd = group.GetMaster();
             Sequoiadb      db = new Sequoiadb(nd.HostName, nd.Port);
             Assert.IsTrue(nd.Start());
             db.Connect(config.conf.UserName, config.conf.Password);
             DBCursor cursor = db.ListCollectionSpaces();
             while (cursor.Next() != null)
             {
                 BsonDocument obj  = cursor.Current();
                 string       temp = null;
                 if (obj.Contains("Name"))
                 {
                     temp = obj["Name"].AsString;
                 }
                 sdb.DropCollectionSpace(temp);
             }
             db.Disconnect();
         }
         Assert.IsTrue(group.Stop());
         // remove group
         try
         {
             sdb.RemoveReplicaGroup(group.GroupName);
         }
         catch (BaseException e)
         {
             string errInfo = e.Message;
             Console.WriteLine("Error code is: " + errInfo);
         }
     }
     create_node_flag  = false;
     create_node2_flag = false;
     // disconnect
     sdb.Disconnect();
 }
Esempio n. 11
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();
        }
Esempio n. 12
0
        public void NextTest()
        {
            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);
            coll.Insert(insertor);

            BsonDocument insertor1 = new BsonDocument();

            insertor1.Add("Last Name", "怪");
            insertor1.Add("First Name", "啊");
            insertor1.Add("Address", "SYSU");
            BsonDocument sInsertor1 = new BsonDocument();

            sInsertor1.Add("Phone", "10000");
            sInsertor1.Add("EMail", "*****@*****.**");
            insertor1.Add("Contact", sInsertor);
            coll.Insert(insertor1);

            BsonDocument dummy  = new BsonDocument();
            DBCursor     cursor = coll.Query(dummy, dummy, dummy, dummy);
            BsonDocument obj1   = new BsonDocument();
            BsonDocument obj2   = new BsonDocument();

            obj1 = cursor.Current();
            Assert.IsNotNull(obj1);
            obj2 = cursor.Next();
            Assert.IsNotNull(obj2);

            Assert.IsTrue(obj1["Address"].AsString.Equals("SYSU"));
            Assert.IsTrue(obj2["Address"].AsString.Equals("SYSU"));
        }
Esempio n. 13
0
        public static void FindRecord(DBCollection dbc, DBQuery query)
        {
            try
            {
                // find specific records from collection with DBQuery
                DBCursor cursor = dbc.Query(query);

                Console.WriteLine("Record read:");
                while (cursor.Next() != null)
                {
                    Console.WriteLine(cursor.Current().ToString());
                }
            }
            catch (BaseException e)
            {
                Console.WriteLine("Failed to find records from collection, ErrorType = {0}", e.ErrorType);
                return;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Environment.Exit(0);
            }
        }
Esempio n. 14
0
        public void Transaction_Begin_Rollback_update_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);
            // 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);
            // transction begin
            sdb.TransactionBegin();
            // update
            BsonDocument matcher  = new BsonDocument();
            BsonDocument modifier = new BsonDocument();

            matcher.Add("name", "sam");
            modifier.Add("$set", new BsonDocument("age", 50));
            cl.Update(matcher, modifier, null);
            // rollback
            sdb.TransactionRollback();
            // check up
            BsonDocument matcher1 = new BsonDocument("age", 27);
            DBCursor     cursor   = cl.Query(matcher1, null, null, null);

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

            while (cursor.Next() != null)
            {
                ++count;
                BsonDocument bson = cursor.Current();
                Assert.IsNotNull(bson);
            }
            Assert.IsTrue(count == 1);
            // check up
            cursor = cl.Query();
            Assert.IsNotNull(cursor);
            count = 0;
            while (cursor.Next() != null)
            {
                ++count;
                BsonDocument bson = cursor.Current();
                Assert.IsNotNull(bson);
            }
            Assert.IsTrue(count == 2);
        }
Esempio n. 15
0
        public void ExecTest()
        {
            // insert English
            string sqlInsert = "INSERT INTO " + csName + "." + cName +
                               " ( c, d, e, f ) values( 6.1, \"8.1\", \"aaa\", \"bbb\")";

            try
            {
                sdb.ExecUpdate(sqlInsert);
            }
            catch (BaseException e)
            {
                string errInfo = e.Message;
                Console.WriteLine("The error info is: " + errInfo);
                Assert.IsFalse(1 == 1);
            }
            // insert Chinese
            string sqlInsert1 = "INSERT INTO " + csName + "." + cName +
                                " ( 城市1, 城市2 ) values( \"广州\",\"上海\")";

            //string str = "INSERT into testfoo.testbar ( 城市1, 城市2 )  values( \"广州\",\"上海\" )";
            try
            {
                sdb.ExecUpdate(sqlInsert1);
            }
            catch (BaseException e)
            {
                string errInfo = e.Message;
                Console.WriteLine("The error info is: " + errInfo);
                Assert.IsFalse(1 == 1);
            }
            // select some
            string   sqlSelect = "SELECT 城市1 FROM " + csName + "." + cName;
            DBCursor cursor    = sdb.Exec(sqlSelect);

            Assert.IsNotNull(cursor);
            while (cursor.Next() != null)
            {
                BsonDocument bson = cursor.Current();
                string       temp = bson.ToString();
                Assert.IsNotNull(bson);
            }
            // select all
            string sqlSelect1 = "SELECT FROM " + csName + "." + cName;

            cursor = sdb.Exec(sqlSelect);
            Assert.IsNotNull(cursor);
            int count = 0;

            while (cursor.Next() != null)
            {
                count++;
                BsonDocument bson = cursor.Current();
                string       temp = bson.ToString();
                Assert.IsNotNull(bson);
            }
            Assert.AreEqual(count, 2);
            // remove all the record
            string sqlDel = "DELETE FROM " + csName + "." + cName;

            try
            {
                sdb.ExecUpdate(sqlDel);
            }
            catch (BaseException e)
            {
                string errInfo = e.Message;
                Console.WriteLine("The error info is: " + errInfo);
                Assert.IsFalse(1 == 1);
            }
        }
Esempio n. 16
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. 17
0
        public static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Please give the database server address <IP:Port>");
                Environment.Exit(0);
            }

            // The database server address
            string sdbIP = args[0];
            // The collection space name
            string csName = "sample";
            // The collection name
            string cName = "sample";

            Sequoiadb sdb = new Sequoiadb(sdbIP);

            // connect
            Common.Connect(sdb);
            CollectionSpace cs   = Common.GetCollecitonSpace(sdb, csName);
            DBCollection    coll = Common.GetColleciton(cs, cName);

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

            Common.DeleteRecords(coll, bson, 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++;
            }

            Common.Disconnect(sdb);
        }
Esempio n. 18
0
        public static void SequoiadbInitialize(TestContext testContext)
        {
            if (config == null)
            {
                config = new Config();
            }
            sdb = new Sequoiadb(config.conf.Coord.Address);
            sdb.Connect(config.conf.UserName, config.conf.Password);
            // check whether it is in the cluster environment or not
            if (!Constants.isClusterEnv(sdb))
            {
                return;
            }
            // argument
            groupName = config.conf.Groups[3].GroupName;
            hostName  = config.conf.Groups[3].Nodes[0].HostName;
            port      = config.conf.Groups[3].Nodes[0].Port;
            dbpath    = config.conf.Groups[3].Nodes[0].DBPath;
            // drop the exist group
            group = sdb.GetReplicaGroup(groupName);
            if (group != null)
            {
                // drop all the cs in current group, and then remove this group
                int nodeNum = group.GetNodeNum(SDBConst.NodeStatus.SDB_NODE_ALL);
                if (nodeNum > 0)
                {
                    SequoiaDB.Node nd = group.GetMaster();
                    Sequoiadb      db = new Sequoiadb(nd.HostName, nd.Port);
                    Assert.IsTrue(nd.Start());
                    db.Connect(config.conf.UserName, config.conf.Password);
                    DBCursor cursor = db.ListCollectionSpaces();
                    while (cursor.Next() != null)
                    {
                        BsonDocument obj  = cursor.Current();
                        string       temp = null;
                        if (obj.Contains("Name"))
                        {
                            temp = obj["Name"].AsString;
                        }
                        sdb.DropCollectionSpace(temp);
                    }
                    db.Disconnect();
                }
                try
                {
                    sdb.RemoveReplicaGroup(group.GroupName);
                }
                catch (BaseException e)
                {
                    string errInfo = e.Message;
                    Console.WriteLine("Error code is: " + errInfo);
                }
            }
            // create a new group
            group = sdb.CreateReplicaGroup(groupName);
            Assert.IsTrue(groupName.Equals(group.GroupName));
            create_rg_flag = true;
            // create a node
            BsonDocument options = new BsonDocument();

            options.Add("logfilenum", 1);
            node = group.CreateNode(hostName, port, dbpath, options);
            Assert.IsNotNull(node);
            node.Start();
            create_node_flag = true;
        }
Esempio n. 19
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();
        }