public void TestDeletion() { var d1 = GenerateRandomData (1029); var d2 = GenerateRandomData (14 * 1024 * 4); var d3 = GenerateRandomData (3591); var d4 = GenerateRandomData (4444); var d5 = GenerateRandomData (5555); var d6 = GenerateRandomData (6666); using (var ms = new MemoryStream()) { var recordStorage = new RecordStorage(new BlockStorage(ms)); var r1 = recordStorage.Create (d1); var r2 = recordStorage.Create (d2); var r3 = recordStorage.Create (d3); Assert.AreEqual (1, r1); Assert.AreEqual (2, r2); Assert.AreEqual (4, r3); Assert.True (recordStorage.Find(r1).SequenceEqual(d1)); Assert.True (recordStorage.Find(r2).SequenceEqual(d2)); Assert.True (recordStorage.Find(r3).SequenceEqual(d3)); // Delete off 2, free 2 records recordStorage.Delete (r2); Assert.True (recordStorage.Find(r2) == null); // INsert 2 new records, should take id of 2,3,4 var r4 = recordStorage.Create (d4); var r5 = recordStorage.Create (d5); var r6 = recordStorage.Create (d6); Assert.AreEqual (3, r4); Assert.AreEqual (2, r5); Assert.AreEqual (5, r6); // Check that data is not being corrupted if we use the reusable block Assert.True (recordStorage.Find(r4).SequenceEqual(d4)); Assert.True (recordStorage.Find(r5).SequenceEqual(d5)); Assert.True (recordStorage.Find(r6).SequenceEqual(d6)); // Test persistance var recordStorage2 = new RecordStorage(new BlockStorage(ms)); Assert.True (recordStorage2.Find(r1).SequenceEqual(d1)); Assert.True (recordStorage2.Find(r3).SequenceEqual(d3)); Assert.True (recordStorage2.Find(r4).SequenceEqual(d4)); Assert.True (recordStorage2.Find(r5).SequenceEqual(d5)); Assert.True (recordStorage2.Find(r6).SequenceEqual(d6)); } }
public void TestTrackingOfLargeFreeBlockList () { var tmp = Path.Combine (System.IO.Path.GetTempPath(), "data.bin"); try { using (var ms = new FileStream(tmp, FileMode.Create)) { var recordStorage = new RecordStorage(new BlockStorage(ms)); // Create 10,000 records var ids = new Dictionary<uint, bool>(); for (var i = 0; i < 15342; i++) { ids.Add (recordStorage.Create (new byte[]{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }), true); } // Now delete them all foreach (var kv in ids) { recordStorage.Delete (kv.Key); } // Create records and make sure they reuse these ids, // And make use it reuses the second block (15343) of free block tracking record as well var reusedSecondFreeBlockTracker = false; for (var i = 15342; i >= 0; i--) { var id = recordStorage.Create (new byte[]{ 0x00, 0x01, 0x02, 0x03}); if (id == 15343) { reusedSecondFreeBlockTracker = true; } else { Assert.IsTrue (ids.ContainsKey(id)); } } Assert.True (reusedSecondFreeBlockTracker); // Make sure it still continue increment the id.. Assert.AreEqual (15344, recordStorage.Create (new byte[]{ 0x00, 0x01, 0x02, 0x03})); } } finally { if (File.Exists(tmp)) { File.Delete (tmp); Console.WriteLine ("Deleted: " + tmp); } } }