/// <summary> /// 简单测试。 /// </summary> /// <param name="sdb"></param> static void Test1(Sequoiadb sdb) { if (sdb.IsCollectionSpaceExist("dbo")) { sdb.DropCollectionSpace("dbo"); } var cs = sdb.CreateCollectionSpace("dbo"); var coll = cs.CreateCollection("foo"); // insert BsonDocument insertor = new BsonDocument(); string date = DateTime.Now.ToString(); insertor.Add("operation", "Insert"); insertor.Add("date", date); Object id = (ObjectId)coll.Insert(insertor); BsonDocument matcher = new BsonDocument(); DBQuery query = new DBQuery(); matcher.Add("date", date); query.Matcher = matcher; DBCursor cursor = coll.Query(query); BsonDocument bson = cursor.Next(); System.Console.WriteLine(bson); System.Console.ReadLine(); }
public void Connect_With_Serval_Arg_Test() { List <string> list = new List <string>(); list.Add("192.168.20.35:12340"); list.Add("192.168.20.36:12340"); list.Add("123:123"); list.Add(""); list.Add("192.168.20.40"); list.Add("192.168.30.161:11810"); list.Add("localhost:50000"); list.Add("192.168.20.42:50000"); list.Add("192.168.20.42:11810"); list.Add("192.168.20.165:11810"); list.Add("localhost:12340"); list.Add("192.168.20.40:12340"); ConfigOptions options = new ConfigOptions(); options.MaxAutoConnectRetryTime = 0; options.ConnectTimeout = 100; // connect Sequoiadb sdb1 = new Sequoiadb(list); sdb1.Connect("", "", options); // set option and change the connect options.ConnectTimeout = 2000; sdb1.ChangeConnectionOptions(options); // check DBCursor cursor = sdb1.GetList(4, null, null, null); Assert.IsTrue(cursor != null); sdb1.Disconnect(); }
public async Task <Listing> GetMoreComments(string subredditId, string linkId, IEnumerable <string> ids) { var targetListing = new Listing { Data = new ListingData { Children = new List <Thing>() } }; DBCursor moreCursor = null; try { foreach (var id in ids) { var keyspace = GenerateDirectKeyspace(subredditId, linkId, id); moreCursor = await _commentsDB.SeekAsync(_commentsDB.GetKeys()[1], keyspace, DBReadFlags.NoLock); await DeserializeCursor(moreCursor, -1, targetListing); await FillInChildren(targetListing); } } finally { if (moreCursor != null) { moreCursor.Dispose(); } } return(targetListing); }
/// <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(); }
public void UpdateInEmptyCLTest() { BsonDocument updater = new BsonDocument(); BsonDocument matcher = new BsonDocument(); BsonDocument modifier = new BsonDocument(); BsonDocument hint = new BsonDocument(); BsonDocument tempMatcher = new BsonDocument(); DBQuery query = new DBQuery(); matcher.Add("operation", new BsonDocument("$et", "Update")); query.Matcher = matcher; long count = coll.GetCount(matcher); Assert.IsTrue(count == 0); // update but insert updater.Add("operation", "Update"); updater.Add("type", "Insert"); modifier.Add("$set", updater); tempMatcher.Add("type", new BsonDocument("$et", "Insert")); coll.Update(matcher, modifier, hint); count = coll.GetCount(tempMatcher); Assert.IsTrue(count == 0); DBCursor cursor = coll.Query(query); Assert.IsNotNull(cursor); BsonDocument rtn = cursor.Next(); Assert.IsNull(rtn); }
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); }
public void CursorGetMoreTest() { 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); BsonDocument obj = new BsonDocument(); // cursor try { while (null != cursor.Next()) { } } catch (BaseException e) { int eno = e.ErrorCode; Assert.IsTrue(e.ErrorType.Equals("SDB_RTN_CONTEXT_NOTEXIST")); } }
private async Task <Listing> DeserializeCursor(DBCursor cursor, int count) { var redditService = ServiceLocator.Current.GetInstance <IRedditService>(); int i = 0; var targetListing = new Listing { Data = new ListingData { Children = new List <Thing>() } }; if (cursor != null) { do { var currentRecord = cursor.Get(); var decodedListing = Encoding.UTF8.GetString(currentRecord, LinkKeySpaceSize, currentRecord.Length - LinkKeySpaceSize); var deserializedLink = JsonConvert.DeserializeObject <Thing>(decodedListing); if (deserializedLink != null && deserializedLink.Data is Link) { redditService.AddFlairInfo(((Link)deserializedLink.Data).Name, ((Link)deserializedLink.Data).Author); } targetListing.Data.Children.Add(deserializedLink); if (i++ > count) { //after type encoding targetListing.Data.After = Encoding.UTF8.GetString(currentRecord, 0, 16); } }while(await cursor.MoveNextAsync()); } return(targetListing); }
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")); }
public void InsertChineseTest() { BsonDocument insertor = new BsonDocument(); insertor.Add("姓名", "林海涛"); insertor.Add("年龄", 24); insertor.Add("id", 2001); // an embedded bson object BsonDocument phone = new BsonDocument { { "0", "10086" }, { "1", "10000" } }; insertor.Add("电话", phone); ObjectId id = (ObjectId)coll.Insert(insertor); BsonDocument matcher = new BsonDocument(); DBQuery query = new DBQuery(); matcher.Add("姓名", "林海涛"); query.Matcher = matcher; DBCursor cursor = coll.Query(query); Assert.IsNotNull(cursor); BsonDocument rtn = cursor.Next(); Assert.IsNotNull(rtn); Assert.IsTrue(id.Equals(rtn["_id"].AsObjectId)); }
private async Task <Listing> DeserializeCursor(DBCursor cursor, int count, Listing existing = null) { var targetListing = existing ?? new Listing { Data = new ListingData { Children = new List <Thing>() } }; int i = 0; if (cursor != null) { do { var currentRecord = cursor.Get(); var decodedListing = Encoding.UTF8.GetString(currentRecord, CommentKeySpaceSize, currentRecord.Length - CommentKeySpaceSize); var deserializedComment = JsonConvert.DeserializeObject <Thing>(decodedListing); targetListing.Data.Children.Add(deserializedComment); if (count == -1) { break; } } while (await cursor.MoveNextAsync()); } return(targetListing); }
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(); }
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")); } }
public void GetSnapshotTest() { Sequoiadb sdb2 = new Sequoiadb(config.conf.Coord.Address); sdb2.Connect(); BsonDocument dummy = new BsonDocument(); DBCursor cursor = sdb2.GetSnapshot(SDBConst.SDB_SNAP_CONTEXTS, dummy, dummy, dummy); Assert.IsNotNull(cursor); BsonDocument bson = cursor.Next(); Assert.IsNotNull(bson); cursor = sdb2.GetSnapshot(SDBConst.SDB_SNAP_CONTEXTS_CURRENT, dummy, dummy, dummy); Assert.IsNotNull(cursor); bson = cursor.Next(); Assert.IsNotNull(bson); cursor = sdb2.GetSnapshot(SDBConst.SDB_SNAP_SESSIONS, dummy, dummy, dummy); Assert.IsNotNull(cursor); bson = cursor.Next(); Assert.IsNotNull(bson); cursor = sdb2.GetSnapshot(SDBConst.SDB_SNAP_SESSIONS_CURRENT, dummy, dummy, dummy); Assert.IsNotNull(cursor); bson = cursor.Next(); Assert.IsNotNull(bson); cursor = sdb2.GetSnapshot(SDBConst.SDB_SNAP_COLLECTIONS, dummy, dummy, dummy); Assert.IsNotNull(cursor); bson = cursor.Next(); Assert.IsNotNull(bson); cursor = sdb2.GetSnapshot(SDBConst.SDB_SNAP_COLLECTIONSPACES, dummy, dummy, dummy); Assert.IsNotNull(cursor); bson = cursor.Next(); Assert.IsNotNull(bson); cursor = sdb2.GetSnapshot(SDBConst.SDB_SNAP_DATABASE, dummy, dummy, dummy); Assert.IsNotNull(cursor); bson = cursor.Next(); Assert.IsNotNull(bson); cursor = sdb2.GetSnapshot(SDBConst.SDB_SNAP_SYSTEM, dummy, dummy, dummy); Assert.IsNotNull(cursor); bson = cursor.Next(); Assert.IsNotNull(bson); if (Constants.isClusterEnv(sdb)) { cursor = sdb.GetSnapshot(SDBConst.SDB_SNAP_CATALOG, dummy, dummy, dummy); Assert.IsNotNull(cursor); BsonDocument obj = cursor.Next(); Assert.IsNotNull(obj); } sdb2.Disconnect(); }
public void QueryExplainTest() { int num = 100; int i = 0; string indexName = "QueryExpalinIndex"; BsonDocument index = new BsonDocument { { "age", 1 } }; // create index coll.CreateIndex(indexName, index, false, false); // insert records for (; i < num; i++) { BsonDocument obj = new BsonDocument { { "firstName", "John" }, { "lastName", "Smith" }, { "age", i } }; coll.Insert(obj); } // query explain BsonDocument cond = new BsonDocument { { "age", new BsonDocument { { "$gt", 50 } } } }; BsonDocument sel = new BsonDocument { { "age", "" } }; BsonDocument orderBy = new BsonDocument { { "age", -1 } }; BsonDocument hint = new BsonDocument { { "", indexName } }; BsonDocument options = new BsonDocument { { "Run", true } }; DBCursor cur = coll.Explain(cond, sel, orderBy, hint, 47, 3, 0, options); Assert.IsNotNull(cur); BsonDocument record = cur.Next(); int indexRead = record["IndexRead"].AsInt32; Assert.IsTrue(50 == indexRead); int dataRead = record["DataRead"].AsInt32; Assert.IsTrue(49 == dataRead); int returnNum = record["ReturnNum"].AsInt32; // it seems not the result we expect. Assert.IsTrue(2 == returnNum); }
public void testLobOpenWrite4() { String str1 = "Hello, world!"; int offset1 = 1024 * 256 * 2 + 1024 * 2; String str2 = "LOB random Write"; int offset2 = offset1 - str2.Length * 2; ObjectId id = ObjectId.GenerateNewId(); DBLob lob = cl.CreateLob(id); lob.Close(); DBLob lob1 = cl.OpenLob(id, DBLob.SDB_LOB_WRITE); DBLob lob2 = cl.OpenLob(id, DBLob.SDB_LOB_WRITE); lob1.LockAndSeek(offset1, str1.Length); lob1.Write(System.Text.Encoding.Default.GetBytes(str1)); lob2.LockAndSeek(offset2, str2.Length); lob2.Write(System.Text.Encoding.Default.GetBytes(str2)); lob1.Close(); lob2.Close(); long lobSize = lob1.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()); byte[] bytes = new byte[(int)lob.GetSize()]; lob.Read(bytes); lob.Close(); String s1 = System.Text.Encoding.Default.GetString(bytes, offset1, str1.Length); Assert.AreEqual(str1, s1); String s2 = System.Text.Encoding.Default.GetString(bytes, offset2, str2.Length); Assert.AreEqual(str2, s2); cl.RemoveLob(id); cursor = cl.ListLobs(); Assert.IsNull(cursor.Next()); }
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")); } }
public void OperationTest() { // Insert BsonDocument insertor = new BsonDocument(); insertor.Add("Last Name", "Lin"); insertor.Add("First Name", "Hetiu"); insertor.Add("Address", "SYSU"); BsonDocument sInsertor = new BsonDocument(); sInsertor.Add("Phone", "10086"); sInsertor.Add("EMail", "*****@*****.**"); insertor.Add("Contact", sInsertor); ObjectId insertID = (ObjectId)coll.Insert(insertor); Assert.IsNotNull(insertID); // Update DBQuery query = new DBQuery(); BsonDocument updater = new BsonDocument(); BsonDocument matcher = new BsonDocument(); BsonDocument modifier = new BsonDocument(); updater.Add("Age", 25); modifier.Add("$set", updater); matcher.Add("First Name", "Hetiu"); query.Matcher = matcher; query.Modifier = modifier; coll.Update(query); // Query DBCursor cursor = coll.Query(query); Assert.IsNotNull(cursor); BsonDocument bson = cursor.Next(); Assert.IsNotNull(bson); Assert.IsTrue(bson["First Name"].AsString.Equals("Hetiu")); Assert.IsTrue(bson["Age"].AsInt32.Equals(25)); // Delete BsonDocument drop = new BsonDocument(); drop.Add("Last Name", "Lin"); coll.Delete(drop); query.Matcher = drop; cursor = coll.Query(query); Assert.IsNotNull(cursor); bson = cursor.Next(); Assert.IsNull(bson); }
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 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(); }
public void testLobSeekWrite5() { int bytesNum = 1024 * 1024 * 2; byte[] bytes = new byte[bytesNum]; Random rand = new Random(); rand.NextBytes(bytes); ObjectId id = ObjectId.GenerateNewId(); DBLob lob = cl.CreateLob(id); lob.Seek(1024 * 256 * 2, DBLob.SDB_LOB_SEEK_SET); lob.Write(bytes); lob.Seek(1024 * 256, 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); if (obj.Contains(FIELD_HAS_PIECES_INFO)) { Boolean hasPiecesInfo = obj.GetValue(FIELD_HAS_PIECES_INFO).AsBoolean; Assert.IsTrue(hasPiecesInfo); } Assert.IsNull(cursor.Next()); lob = cl.OpenLob(id); Assert.AreEqual(lobSize, lob.GetSize()); byte[] bytes2 = new byte[bytesNum]; lob.Seek(1024 * 256, DBLob.SDB_LOB_SEEK_SET); lob.Read(bytes2); lob.Close(); Assert.IsTrue(TestHelper.ByteArrayEqual(bytes, bytes2)); cl.RemoveLob(id); cursor = cl.ListLobs(); Assert.IsNull(cursor.Next()); }
public void AlterCollectionTest() { DBCollection coll = null; BsonDocument options = null; string clName_alter = "alterCLTest"; string fullName = null; int partition = 0; string type = null; int rs = -1; coll = cs.CreateCollection(clName_alter); fullName = coll.CollSpace.Name + "." + clName_alter; // alter collecton attrubute options = new BsonDocument { { "ReplSize", 0 }, { "ShardingKey", new BsonDocument { { "a", 1 } } }, { "ShardingType", "hash" }, { "Partition", 4096 } }; coll.Alter(options); // check BsonDocument matcher = new BsonDocument { { "Name", fullName } }; DBCursor cur = sdb.GetSnapshot(8, matcher, null, null); BsonDocument obj = cur.Next(); if (null == obj) { Assert.Fail(); } try { rs = obj["ReplSize"].AsInt32; Assert.IsTrue(7 == rs); partition = obj["Partition"].AsInt32; Assert.IsTrue(4096 == partition); type = obj["ShardingType"].AsString; Assert.IsTrue(type.Equals("hash")); } catch (System.Exception) { Assert.Fail(); } }
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(); }
public void testLobSeekWrite2() { String str = "Hello, world!"; String str2 = "LOB Seek Write"; int offset = 100; int offset2 = 10; ObjectId id = ObjectId.GenerateNewId(); DBLob lob = cl.CreateLob(id); lob.Seek(offset, DBLob.SDB_LOB_SEEK_SET); lob.Write(System.Text.Encoding.Default.GetBytes(str)); lob.Seek(offset2, DBLob.SDB_LOB_SEEK_SET); lob.Write(System.Text.Encoding.Default.GetBytes(str2)); 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()); byte[] bytes = new byte[(int)lob.GetSize()]; lob.Read(bytes); lob.Close(); String s = System.Text.Encoding.Default.GetString(bytes, offset, bytes.Length - offset); Assert.AreEqual(str, s); String s2 = System.Text.Encoding.Default.GetString(bytes, offset2, str2.Length); Assert.AreEqual(str2, s2); cl.RemoveLob(id); cursor = cl.ListLobs(); Assert.IsNull(cursor.Next()); }
public void NumberLongToStringTest() { string expect1 = ""; string expect2 = ""; BsonDocument result = null; BsonDocument obj = new BsonDocument(); obj.Add("a", 0); obj.Add("b", int.MaxValue); obj.Add("c", int.MinValue); obj.Add("e", int.MaxValue + 1L); obj.Add("f", int.MinValue - 1L); obj.Add("g", long.MaxValue); obj.Add("h", long.MinValue); coll.Insert(obj); cur = coll.Query(null, new BsonDocument().Add("_id", new BsonDocument().Add("$include", 0)), null, null); try { result = cur.Next(); } finally { cur.Close(); } expect1 = "{ \"a\" : 0, \"b\" : 2147483647, \"c\" : -2147483648, \"e\" : 2147483648, \"f\" : -2147483649, \"g\" : 9223372036854775807, \"h\" : -9223372036854775808 }"; expect2 = "{ \"a\" : 0, \"b\" : 2147483647, \"c\" : -2147483648, \"e\" : 2147483648, \"f\" : -2147483649, \"g\" : { \"$numberLong\": \"9223372036854775807\" }, \"h\" : { \"$numberLong\": \"-9223372036854775808\" } }"; // case 1: Console.WriteLine("case1's result is: {0}", result.ToString()); Assert.AreEqual(expect1, result.ToString()); // case 2: BsonDefaults.JsCompatibility = true; Console.WriteLine("case2's result is: {0}", result.ToString()); Assert.AreEqual(expect2, result.ToString()); // case 3: BsonDefaults.JsCompatibility = false; Console.WriteLine("case3's result is: {0}", result.ToString()); Assert.AreEqual(expect1, result.ToString()); // case 4: BsonDefaults.JsCompatibility = true; Console.WriteLine("case4's result is: {0}", result.ToString()); Assert.AreEqual(expect2, result.ToString()); }
public void testLobOpenWrite8() { String str = "1234567890"; String str2 = "abcde"; String str3 = "12345abcde"; ObjectId id = ObjectId.GenerateNewId(); DBLob lob = cl.CreateLob(id); lob.Close(); lob = cl.OpenLob(id, DBLob.SDB_LOB_WRITE); lob.Write(System.Text.Encoding.Default.GetBytes(str)); lob.Close(); lob = cl.OpenLob(id, DBLob.SDB_LOB_WRITE); lob.LockAndSeek(5, -1); lob.Write(System.Text.Encoding.Default.GetBytes(str2)); 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()); byte[] bytes = new byte[(int)lob.GetSize()]; lob.Read(bytes); lob.Close(); String s = System.Text.Encoding.Default.GetString(bytes); Assert.AreEqual(str3, s); cl.RemoveLob(id); cursor = cl.ListLobs(); Assert.IsNull(cursor.Next()); }
public void IndexesTest() { // Insert BsonDocument insertor = new BsonDocument(); insertor.Add("Last Name", "Lin"); insertor.Add("First Name", "Hetiu"); insertor.Add("Address", "SYSU"); BsonDocument sInsertor = new BsonDocument(); sInsertor.Add("Phone", "10086"); sInsertor.Add("EMail", "*****@*****.**"); insertor.Add("Contact", sInsertor); ObjectId insertID = (ObjectId)coll.Insert(insertor); Assert.IsNotNull(insertID); // Create Index BsonDocument key = new BsonDocument(); key.Add("Last Name", 1); key.Add("First Name", 1); string name = "index name"; coll.CreateIndex(name, key, false, false); // Get Indexes DBCursor cursor = coll.GetIndex(name); Assert.IsNotNull(cursor); BsonDocument index = cursor.Next(); Assert.IsNotNull(index); Assert.IsTrue(index["IndexDef"].AsBsonDocument["name"].AsString.Equals("index name")); // Drop Index coll.DropIndex(name); cursor = coll.GetIndex(name); Assert.IsNotNull(cursor); index = cursor.Next(); Assert.IsNull(index); }
// get index on a given collection public static BsonDocument GetIndex(DBCollection dbc, string indexName) { DBCursor cursor = null; try { cursor = dbc.GetIndex(indexName); } catch (BaseException e) { Console.WriteLine("Failed to get index {0} from collection, ErrorType = {1}", indexName, e.ErrorType); Environment.Exit(0); } catch (Exception e) { Console.WriteLine(e.Message); Environment.Exit(0); } return(cursor.Next()); }
public void EmptyCursorTest() { int count = 0; BsonDocument insertor1 = new BsonDocument(); insertor1.Add("Last Name", "怪"); insertor1.Add("First Name", "啊"); insertor1.Add("Address", "SYSU"); coll.Insert(insertor1); BsonDocument dummy = new BsonDocument(); DBCursor cursor = coll.Query(dummy, dummy, dummy, dummy); Assert.IsNotNull(cursor); BsonDocument obj = new BsonDocument(); while (null != (obj = cursor.Next())) { count++; } Assert.AreEqual(1, count); }
private async Task<Listing> DeserializeCursor(DBCursor cursor, int count) { var redditService = ServiceLocator.Current.GetInstance<IRedditService>(); int i = 0; var targetListing = new Listing { Data = new ListingData { Children = new List<Thing>() } }; if(cursor != null) { do { var currentRecord = cursor.Get(); var decodedListing = Encoding.UTF8.GetString(currentRecord, LinkKeySpaceSize, currentRecord.Length - LinkKeySpaceSize); var deserializedLink = JsonConvert.DeserializeObject<Thing>(decodedListing); if (deserializedLink != null && deserializedLink.Data is Link) { redditService.AddFlairInfo(((Link)deserializedLink.Data).Name, ((Link)deserializedLink.Data).Author); } targetListing.Data.Children.Add(deserializedLink); if (i++ > count) { //after type encoding targetListing.Data.After = Encoding.UTF8.GetString(currentRecord, 0, 16); } }while(await cursor.MoveNextAsync()); } return targetListing; }