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 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 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); }
public void LobReadTest() { DBLob lob = null; DBLob lob2 = null; bool flag = false; ObjectId oid1 = ObjectId.Empty; int bufSize = 1024 * 1024 * 100; int readNum = 0; int retNum = 0; int i = 0; byte[] readBuf = null; byte[] buf = new byte[bufSize]; for (i = 0; i < bufSize; i++) { buf[i] = 65; } long lobSize = 0; // CreateLob lob = cl.CreateLob(); Assert.IsNotNull(lob); // GetID oid1 = lob.GetID(); Assert.IsTrue(ObjectId.Empty != oid1); // Write lob.Write(buf); lobSize = lob.GetSize(); Assert.AreEqual(bufSize, lobSize); // Close lob.Close(); // Open lob lob2 = cl.OpenLob(oid1); lobSize = lob2.GetSize(); Assert.AreEqual(bufSize, lobSize); // Read int skipNum = 1024 * 1024 * 50; lob2.Seek(skipNum, DBLob.SDB_LOB_SEEK_SET); // after this, the offset is 1024*1024*50 readNum = 1024 * 1024 * 10; readBuf = new byte[readNum]; retNum = lob2.Read(readBuf); // after this, the offset is 1024*1024*60 Assert.IsTrue(readNum == retNum); // check for (i = 0; i < readBuf.Length; i++) { Assert.IsTrue(readBuf[i] == 65); } skipNum = 1024 * 1024 * 10; lob2.Seek(skipNum, DBLob.SDB_LOB_SEEK_CUR); // after this, the offset is 1024*1024*70 readBuf = new byte[readNum]; retNum = lob2.Read(readBuf); Assert.IsTrue(readNum == retNum); // after this, the offset is 1024*1024*80 // check for (i = 0; i < readBuf.Length; i++) { Assert.IsTrue(readBuf[i] == 65); } skipNum = 1024 * 1024 * 20; lob2.Seek(skipNum, DBLob.SDB_LOB_SEEK_END); readNum = 1024 * 1024 * 30; // will only read 1024*1024*20 readBuf = new byte[readNum]; retNum = lob2.Read(readBuf); // after this, the offset is 1024*1024*100 Assert.AreEqual(readNum, (retNum + 1024 * 1024 * 10)); //Assert.AreEqual 失败。应为: <31457280>,实际为: <10746880>。 // Close lob2.Close(); // IsClosed flag = false; flag = lob.IsClosed(); Assert.IsTrue(flag); }
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()); }
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(); } } }