Exemple #1
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);
            }
        }
Exemple #2
0
        public void GetListTest()
        {
            BsonDocument dummy  = new BsonDocument();
            BsonDocument bson   = null;
            DBCursor     cursor = null;

            // list cs
            cursor = sdb.GetList(SDBConst.SDB_LIST_COLLECTIONSPACES, dummy, dummy, dummy);
            Assert.IsNotNull(cursor);
            bson = cursor.Next();
            Assert.IsNotNull(bson);

            // list cl
            cursor = sdb.GetList(SDBConst.SDB_LIST_COLLECTIONS, dummy, dummy, dummy);
            Assert.IsNotNull(cursor);
            bson = cursor.Next();
            Assert.IsNotNull(bson);

            BsonDocument obj = new BsonDocument("test", "test");

            coll.Insert(obj);

            // list groups
            // check whether it is in the cluster environment or not
            if (Constants.isClusterEnv(sdb))
            {
                cursor = sdb.GetList(SDBConst.SDB_LIST_GROUPS, dummy, dummy, dummy);
                Assert.IsNotNull(cursor);
                bson = cursor.Next();
                Assert.IsNotNull(bson);
            }

            // list task
            cursor = sdb.GetList(SDBConst.SDB_LIST_TASKS, dummy, dummy, dummy);
            Assert.IsNotNull(cursor);

            // list domains
            if (Constants.isClusterEnv(sdb))
            {
                string dmName = "testListDomain";
                Domain dm     = sdb.CreateDomain(dmName, null);
                cursor = null;
                cursor = sdb.ListDomains(null, null, null, null);
                Assert.IsNotNull(cursor);
                Assert.IsNotNull(cursor.Next());
                sdb.DropDomain(dmName);
            }

            // list stored procedure
            cursor = sdb.GetList(SDBConst.SDB_LIST_STOREPROCEDURES, dummy, dummy, dummy);
            Assert.IsNotNull(cursor);

            // list all the contexts
            if (Constants.isClusterEnv(sdb))
            {
                sdb.Disconnect();
                sdb = new Sequoiadb(config.conf.Data.Address);
                sdb.Connect(config.conf.UserName, config.conf.Password);
            }
            cursor = sdb.GetList(SDBConst.SDB_LIST_CONTEXTS, dummy, dummy, dummy);
            Assert.IsNotNull(cursor);
            bson = cursor.Next();
            Assert.IsNotNull(bson);

            // list current context
            cursor = sdb.GetList(SDBConst.SDB_LIST_CONTEXTS_CURRENT, dummy, dummy, dummy);
            Assert.IsNotNull(cursor);
            bson = cursor.Next();
            Assert.IsNotNull(bson);

            // list all the sessions
            cursor = sdb.GetList(SDBConst.SDB_LIST_SESSIONS, dummy, dummy, dummy);
            Assert.IsNotNull(cursor);
            bson = cursor.Next();
            Assert.IsNotNull(bson);

            // list current session
            cursor = sdb.GetList(SDBConst.SDB_LIST_SESSIONS_CURRENT, dummy, dummy, dummy);
            Assert.IsNotNull(cursor);
            bson = cursor.Next();
            Assert.IsNotNull(bson);

            // list storge units
            cursor = sdb.GetList(SDBConst.SDB_LIST_STORAGEUNITS, dummy, dummy, dummy);
            Assert.IsNotNull(cursor);
            bson = cursor.Next();
            Assert.IsNotNull(bson);
        }
        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;
            }
        }
 public void AttachAndDetachCollectionTest()
 {
     try
     {
         // create main cl
         DBCollection mainCL     = null;
         string       mainCLName = "maincl";
         if (cs.IsCollectionExist(mainCLName))
         {
             cs.DropCollection(mainCLName);
         }
         BsonDocument conf = new BsonDocument
         {
             { "IsMainCL", true },
             { "ShardingKey", new BsonDocument
               {
                   { "id", 1 }
               } },
             { "ReplSize", 0 }
         };
         mainCL = cs.CreateCollection(mainCLName, conf);
         // create sub cl
         DBCollection subCL     = null;
         string       subCLName = "subcl";
         if (cs.IsCollectionExist(subCLName))
         {
             cs.DropCollection(subCLName);
         }
         BsonDocument conf1 = new BsonDocument
         {
             { "ReplSize", 0 }
         };
         subCL = cs.CreateCollection(subCLName, conf1);
         // case 1: test attachCollection
         // TODO:
         int num = 10;
         // mainCL attach subCL
         BsonDocument options = new BsonDocument
         {
             { "LowBound", new BsonDocument {
                   { "id", 0 }
               } },
             { "UpBound", new BsonDocument {
                   { "id", num }
               } }
         };
         string subCLFullName = cs.Name + "." + subCLName;
         mainCL.AttachCollection(subCLFullName, options);
         // insert some records
         List <BsonDocument> insertor = new List <BsonDocument>();
         for (int i = 0; i < num + 10; i++)
         {
             BsonDocument obj = new BsonDocument();
             obj.Add("id", i);
             //insertor.Add(obj);
             try
             {
                 mainCL.Insert(obj);
             }
             catch (BaseException e)
             {
                 int errno = e.ErrorCode;
                 Console.WriteLine(e.ErrorType);
             }
         }
         try
         {
             //mainCL.BulkInsert(insertor, 0);
         }
         catch (BaseException e)
         {
             Assert.IsTrue(e.ErrorType.Equals("SDB_CAT_NO_MATCH_CATALOG"));
         }
         // check
         BsonDocument subobj  = new BsonDocument();
         BsonDocument matcher = new BsonDocument();
         matcher.Add("id", subobj);
         subobj.Add("$gte", 0);
         subobj.Add("$lt", num);
         DBCursor cursor = mainCL.Query(matcher, null, null, null, 0, -1);
         long     count  = 0;
         while (cursor.Next() != null)
         {
             count++;
         }
         Assert.IsTrue(num == count);
         BsonDocument subobj1  = new BsonDocument();
         BsonDocument matcher1 = new BsonDocument();
         matcher1.Add("id", subobj1);
         subobj1.Add("$gte", num);
         DBCursor cursor1 = mainCL.Query(matcher1, null, null, null, 0, -1);
         count = 0;
         while (cursor1.Next() != null)
         {
             count++;
         }
         Assert.IsTrue(0 == count);
         // case 2: test detachCollection
         // TODO:
         mainCL.DetachCollection(subCLFullName);
         // check
         try
         {
             BsonDocument obj = new BsonDocument("test", "test");
             mainCL.Insert(obj);
         }
         catch (BaseException e)
         {
             Assert.IsTrue(e.ErrorType.Equals("SDB_CAT_NO_MATCH_CATALOG"));
             Console.WriteLine(e.ErrorType);
         }
     }
     catch (BaseException e)
     {
         Console.WriteLine(e.ErrorType);
         return;
     }
 }
        public void setSessionAttrTest()
        {
            // create another node
            string       host      = "192.168.20.42";
            int          port      = 55555;
            string       dataPath  = "/opt/sequoiadb/database/data/55555";
            string       groupName = "group1";
            ReplicaGroup rg        = null;

            try
            {
                // get the exist group
                rg = sdb.GetReplicaGroup(groupName);
                // remove the node we going to use
                var node = rg.GetNode(host, port);
                if (node != null)
                {
                    rg.RemoveNode(host, port, new BsonDocument());
                }
                // create node
                Dictionary <string, string> opt = new Dictionary <string, string>();
                rg.CreateNode(host, port, dataPath, opt);
                rg.Start();
                // insert some records first
                int num = 10;
                List <BsonDocument> insertor = new List <BsonDocument>();
                for (int i = 0; i < num; i++)
                {
                    BsonDocument obj = new BsonDocument();
                    obj.Add("id", i);
                    insertor.Add(obj);
                }
                coll.BulkInsert(insertor, 0);
                // begin a new session
                Sequoiadb sdb2 = new Sequoiadb(config.conf.Coord.Address);
                sdb2.Connect(config.conf.UserName, config.conf.Password);
                Assert.IsNotNull(sdb2.Connection);
                // TODO:
                BsonDocument conf = new BsonDocument("PreferedInstance", "m");
                sdb2.SetSessionAttr(conf);
                // check
                // record the slave note "TotalDataRead" before query
                var sddb = new Sequoiadb(host, port);
                sddb.Connect(config.conf.UserName, config.conf.Password);
                DBCursor     cur1    = sddb.GetSnapshot(6, null, null, null);
                BsonDocument status1 = cur1.Next();
                long         count1  = status1.GetValue("TotalDataRead").AsInt64;
                // query
                DBCursor     cursor = coll.Query(null, null, null, null, 0, -1);
                BsonDocument o      = new BsonDocument();
                long         count  = 0;
                while ((o = cursor.Next()) != null)
                {
                    count++;
                }
                // record the slave note "TotalRead" after query
                DBCursor     cur2    = sddb.GetSnapshot(6, null, null, null);
                BsonDocument status2 = cur2.Next();
                long         count2  = status2.GetValue("TotalDataRead").AsInt64;
                //Assert.IsTrue(num == count2 - count1);
                long temp = count2 - count1;
                Console.WriteLine("count2 is " + count2 + ", count1 is " + count1);
                DBCursor     cur3    = sddb.GetSnapshot(6, null, null, null);
                BsonDocument status3 = cur3.Next();
                long         count3  = status3.GetValue("TotalRead").AsInt64;
            }
            finally
            {
                // remove the newly build node
                var node = rg.GetNode(host, port);
                if (node != null)
                {
                    rg.RemoveNode(host, port, new BsonDocument());
                }
            }
        }
        public void InsertTest_WithId()
        {
            int testTypeSize = 8;

            for (int i = 0; i < testTypeSize; i++)
            {
                // insert
                BsonDocument insertor = new BsonDocument();
                string       date     = DateTime.Now.ToString();
                insertor.Add("operation", "Insert");
                insertor.Add("date", date);
                switch (i)
                {
                case 0:
                    insertor.Add("_id", 3.14);
                    break;

                case 1:
                    insertor.Add("_id", "abcdefg");
                    break;

                case 2:
                    insertor.Add("_id", new BsonDocument("id", "id"));
                    break;

                case 3:
                    insertor.Add("_id", ObjectId.GenerateNewId());
                    break;

                case 4:
                    insertor.Add("_id", true);
                    break;

                case 5:
                    insertor.Add("_id", 1234);
                    break;

                case 6:
                    insertor.Add("_id", 10000L);
                    break;

                case 7:
                    insertor.Add("_id", new BsonTimestamp(1000000000L));
                    break;

                default:
                    continue;
                }
                coll.Delete(null);
                BsonValue value = coll.Insert(insertor);
                Object    id    = null;
                BsonType  type  = value.BsonType;
                if (type == BsonType.Double)
                {
                    id = value.AsDouble;
                }
                else if (type == BsonType.String)
                {
                    id = value.AsString;
                }
                else if (type == BsonType.Document)
                {
                    id = value.AsBsonDocument;
                }
                else if (type == BsonType.Array)
                {
                    id = value.AsBsonArray;
                }
                else if (type == BsonType.Binary)
                {
                    id = value.AsBsonBinaryData;
                }
                else if (type == BsonType.Undefined)
                {
                    id = value.AsBsonUndefined;
                }
                else if (type == BsonType.ObjectId)
                {
                    id = value.AsObjectId;
                }
                else if (type == BsonType.Boolean)
                {
                    id = value.AsBoolean;
                }
                else if (type == BsonType.DateTime)
                {
                    id = value.AsDateTime;
                }
                else if (type == BsonType.Null)
                {
                    ;
                }
                else if (type == BsonType.RegularExpression)
                {
                    id = value.AsRegex;
                }
                else if (type == BsonType.JavaScript)
                {
                    id = value.AsBsonJavaScript;
                }
                else if (type == BsonType.Symbol)
                {
                    id = value.AsBsonSymbol;
                }
                else if (type == BsonType.JavaScriptWithScope)
                {
                    id = value.AsBsonJavaScriptWithScope;
                }
                else if (type == BsonType.Int32)
                {
                    id = value.AsInt32;
                }
                else if (type == BsonType.Timestamp)
                {
                    id = value.AsBsonTimestamp;
                }
                else if (type == BsonType.Int64)
                {
                    id = value.AsInt64;
                }
                else if (type == BsonType.MinKey)
                {
                    id = value.AsBsonMinKey;
                }
                else if (type == BsonType.MaxKey)
                {
                    id = value.AsBsonMaxKey;
                }

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

                BsonValue ret = bson.GetValue("_id");
                type = ret.BsonType;
                if (type == BsonType.Double)
                {
                    Assert.IsTrue(id.Equals(ret.AsDouble));
                }
                else if (type == BsonType.String)
                {
                    Assert.IsTrue(id.Equals(ret.AsString));
                }
                else if (type == BsonType.Document)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonDocument));
                }
                else if (type == BsonType.Array)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonArray));
                }
                else if (type == BsonType.Binary)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonBinaryData));
                }
                else if (type == BsonType.Undefined)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonUndefined));
                }
                else if (type == BsonType.ObjectId)
                {
                    Assert.IsTrue(id.Equals(ret.AsObjectId));
                }
                else if (type == BsonType.Boolean)
                {
                    Assert.IsTrue(id.Equals(ret.AsBoolean));
                }
                else if (type == BsonType.DateTime)
                {
                    Assert.IsTrue(id.Equals(ret.AsDateTime));
                }
                else if (type == BsonType.Null)
                {
                    Assert.IsTrue(id == null);
                }
                else if (type == BsonType.RegularExpression)
                {
                    Assert.IsTrue(id.Equals(ret.AsRegex));
                }
                else if (type == BsonType.JavaScript)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonJavaScript));
                }
                else if (type == BsonType.Symbol)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonSymbol));
                }
                else if (type == BsonType.JavaScriptWithScope)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonJavaScriptWithScope));
                }
                else if (type == BsonType.Int32)
                {
                    Assert.IsTrue(id.Equals(ret.AsInt32));
                }
                else if (type == BsonType.Timestamp)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonTimestamp));
                }
                else if (type == BsonType.Int64)
                {
                    Assert.IsTrue(id.Equals(ret.AsInt64));
                }
                else if (type == BsonType.MinKey)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonMinKey));
                }
                else if (type == BsonType.MaxKey)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonMaxKey));
                }
            }
        }
        public void Transaction_Begin_Commit_delete_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();
            // delete
            BsonDocument matcher = new BsonDocument();

            //matcher.Add("name", new BsonDocument("$et","sam"));
            matcher.Add("name", "sam");
            cl.Delete(matcher);
            // 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 == 1);
            // commit
            sdb.TransactionCommit();
            // check up
            cursor = cl.Query(matcher, null, null, null);
            Assert.IsNotNull(cursor);
            count = 0;
            while (cursor.Next() != null)
            {
                ++count;
                BsonDocument bson = cursor.Current();
                Assert.IsNotNull(bson);
            }
            Assert.IsTrue(count == 0);
            // chech up
            cursor = cl.Query();
            Assert.IsNotNull(cursor);
            count = 0;
            while (cursor.Next() != null)
            {
                ++count;
                BsonDocument bson = cursor.Current();
                Assert.IsNotNull(bson);
            }
            Assert.IsTrue(count == 1);
        }
Exemple #8
0
        public void LobGlobalTest()
        {
            DBLob    lob     = null;
            DBLob    lob2    = null;
            DBLob    lob3    = null;
            bool     flag    = false;
            ObjectId oid1    = ObjectId.Empty;
            ObjectId oid2    = ObjectId.Empty;
            ObjectId oid3    = ObjectId.Empty;
            ObjectId oid4    = ObjectId.Empty;
            long     size1   = 0;
            long     time1   = 0;
            int      bufSize = 1000;
            int      readNum = 0;
            int      retNum  = 0;
            int      offset  = 0;
            int      i       = 0;

            byte[] readBuf = null;
            byte[] buf     = new byte[bufSize];
            for (i = 0; i < bufSize; i++)
            {
                buf[i] = 65;
            }
            DBCursor     cursor  = null;
            BsonDocument record  = null;
            long         lobSize = 0;

            /// case 1: create a new lob
            // CreateLob
            lob = cl.CreateLob();
            Assert.IsNotNull(lob);
            // IsClosed
            flag = true;
            flag = lob.IsClosed();
            Assert.IsFalse(flag);
            // GetID
            oid1 = lob.GetID();
            Assert.IsTrue(ObjectId.Empty != oid1);
            // Write
            lob.Write(buf);
            // Close
            lob.Close();
            // IsClosed
            flag = false;
            flag = lob.IsClosed();
            Assert.IsTrue(flag);

            // case 2: open an exsiting lob
            lob2 = cl.OpenLob(oid1);
            Assert.IsNotNull(lob2);
            // IsClosed
            flag = true;
            flag = lob2.IsClosed();
            Assert.IsFalse(flag);
            // GetID
            oid2 = lob2.GetID();
            Assert.IsTrue(ObjectId.Empty != oid2);
            Assert.IsTrue(oid1 == oid2);
            // GetSize
            size1 = lob2.GetSize();
            Assert.IsTrue(bufSize == size1);
            // GetCreateTime
            time1 = lob2.GetCreateTime();
            Assert.IsTrue(time1 > 0);
            // Read
            readNum = bufSize / 4;
            readBuf = new Byte[readNum];
            retNum  = lob2.Read(readBuf);
            Assert.IsTrue(readNum == retNum);
            // Seek
            offset = bufSize / 2;
            lob2.Seek(offset, DBLob.SDB_LOB_SEEK_CUR);
            // Read
            retNum = 0;
            retNum = lob2.Read(readBuf);
            Assert.IsTrue(readNum == retNum);
            // Close
            lob2.Close();
            // IsClosed
            flag = false;
            flag = lob2.IsClosed();
            Assert.IsTrue(flag);

            /// case 3: create a lob with specified oid
            oid3 = ObjectId.GenerateNewId();
            lob3 = cl.CreateLob(oid3);
            Assert.IsNotNull(lob3);
            // GetID
            oid4 = lob3.GetID();
            Assert.IsTrue(ObjectId.Empty != oid4);
            Assert.IsTrue(oid3 == oid4);
            // Write
            lob3.Write(buf);
            // Close
            lob3.Close();
            // IsClosed
            flag = false;
            flag = lob3.IsClosed();
            Assert.IsTrue(flag);

            /// case 4: test api in cl
            // ListLobs
            cursor = cl.ListLobs();
            Assert.IsNotNull(cursor);
            i = 0;
            while (null != (record = cursor.Next()))
            {
                i++;
                if (record.Contains("Size") && record["Size"].IsInt64)
                {
                    lobSize = record["Size"].AsInt64;
                }
                else
                {
                    Assert.Fail();
                }
            }
            Assert.IsTrue(2 == i);
            // RemoveLobs
            cl.RemoveLob(oid3);
            // ListLobs
            cursor = cl.ListLobs();
            Assert.IsNotNull(cursor);
            i = 0;
            while (null != (record = cursor.Next()))
            {
                i++;
                if (record.Contains("Size") && record["Size"].IsInt64)
                {
                    lobSize = record["Size"].AsInt64;
                }
                else
                {
                    Assert.Fail();
                }
            }
            Assert.IsTrue(1 == i);
        }
        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();
        }
Exemple #10
0
        public void testLobSeekWrite6()
        {
            String str = "Hello, world!";

            byte[] bytes = System.Text.Encoding.Default.GetBytes(str);

            int       begin   = 1024 * 3 + 11;
            int       step    = 1024 * 4 * 2;
            int       max     = 1024 * 256;
            ArrayList posList = new ArrayList();

            for (int pos = begin; pos <= max; pos += step)
            {
                posList.Add(pos);
            }

            Random    rand     = new Random(DateTime.Now.Millisecond);
            ArrayList writePos = new ArrayList(posList);

            ObjectId id  = ObjectId.GenerateNewId();
            DBLob    lob = cl.CreateLob(id);

            while (writePos.Count != 0)
            {
                int         index = rand.Next(writePos.Count);
                IEnumerator ie    = writePos.GetEnumerator(index, 1);
                ie.MoveNext();
                int pos = (int)ie.Current;
                writePos.RemoveAt(index);
                lob.Seek(pos, DBLob.SDB_LOB_SEEK_SET);
                lob.Write(bytes);
            }
            lob.Close();

            long lobSize = lob.GetSize();

            DBCursor     cursor = cl.ListLobs();
            BsonDocument obj    = cursor.Next();

            Assert.IsNotNull(obj);
            ObjectId oid = obj.GetValue("Oid").AsObjectId;

            Assert.AreEqual(id, oid);
            Assert.IsNull(cursor.Next());

            lob = cl.OpenLob(id);
            Assert.AreEqual(lobSize, lob.GetSize());

            ArrayList readPos = new ArrayList(posList);

            while (readPos.Count != 0)
            {
                int index = rand.Next(readPos.Count);
                Console.WriteLine("index is: " + index);
                IEnumerator ie = readPos.GetEnumerator(index, 1);
                ie.MoveNext();
                int pos = (int)ie.Current;
                readPos.RemoveAt(index);
                lob.Seek(pos, DBLob.SDB_LOB_SEEK_SET);
                byte[] bytes2 = new byte[str.Length];
                lob.Read(bytes2);
                String str2 = System.Text.Encoding.Default.GetString(bytes2);
                Assert.AreEqual(str, str2);
            }
            lob.Close();

            cl.RemoveLob(id);
            cursor = cl.ListLobs();
            Assert.IsNull(cursor.Next());
        }
Exemple #11
0
        public void testLobOpenWrite7()
        {
            int bytesNum = 1024 * 1024 * 4;

            byte[] bytes = new byte[bytesNum];
            Random rand  = new Random();

            rand.NextBytes(bytes);

            int offset = bytesNum / 2;

            ObjectId id  = ObjectId.GenerateNewId();
            DBLob    lob = null;

            try
            {
                lob = cl.CreateLob(id);
                lob.Seek(offset, DBLob.SDB_LOB_SEEK_SET);
                lob.Write(bytes, offset, bytesNum - offset);
            }
            finally
            {
                if (lob != null)
                {
                    lob.Close();
                }
            }

            DBCursor cursor = null;

            try
            {
                cursor = cl.ListLobs();
                BsonDocument obj = cursor.Next();
                Assert.IsNotNull(obj);
                ObjectId oid = obj.GetValue("Oid").AsObjectId;
                Assert.AreEqual(id, oid);
                if (obj.Contains(FIELD_HAS_PIECES_INFO))
                {
                    Boolean hasPiecesInfo = obj.GetValue(FIELD_HAS_PIECES_INFO).AsBoolean;
                    Assert.IsTrue(hasPiecesInfo);
                }
                Assert.IsNull(cursor.Next());
            }
            finally
            {
                if (cursor != null)
                {
                    cursor.Close();
                }
            }

            long lobSize;

            lob = null;
            try
            {
                lob = cl.OpenLob(id, DBLob.SDB_LOB_WRITE);
                lob.Write(bytes, 0, offset);
                lobSize = lob.GetSize();
            }
            finally{
                if (lob != null)
                {
                    lob.Close();
                }
            }
            cursor = null;
            try
            {
                cursor = cl.ListLobs();
                BsonDocument obj = cursor.Next();
                Assert.IsNotNull(obj);
                ObjectId oid = obj.GetValue("Oid").AsObjectId;
                Assert.AreEqual(id, oid);
                if (obj.Contains(FIELD_HAS_PIECES_INFO))
                {
                    Boolean hasPiecesInfo = obj.GetValue(FIELD_HAS_PIECES_INFO).AsBoolean;
                    Assert.IsFalse(hasPiecesInfo);
                }
                Assert.IsNull(cursor.Next());
            }
            finally
            {
                cursor.Close();
            }

            lob = null;
            try {
                lob = cl.OpenLob(id);
                Assert.AreEqual(lobSize, lob.GetSize());
                byte[] bytes2 = new byte[(int)lob.GetSize()];
                lob.Read(bytes2);
                Assert.IsTrue(TestHelper.ByteArrayEqual(bytes, bytes2));
            }
            finally
            {
                if (lob != null)
                {
                    lob.Close();
                }
            }

            cl.RemoveLob(id);
            cursor = null;
            try
            {
                cursor = cl.ListLobs();
                Assert.IsNull(cursor.Next());
            }
            finally
            {
                if (cursor != null)
                {
                    cursor.Close();
                }
            }
        }