public void TestLargeish() { var di = new DirectoryInfo("testdb"); using (var db = new PlaneSet(di, FileMode.CreateNew, planeDBOptions)) { for (var i = 0; i < 10000; ++i) { Assert.IsTrue(db.TryAdd(BitConverter.GetBytes(i))); } } using (var db = new PlaneSet(di, FileMode.Open, planeDBOptions)) { for (var i = 0; i < 10000; ++i) { Assert.IsTrue(db.Contains(BitConverter.GetBytes(i))); } } using (var db = new PlaneSet(di, FileMode.Open, planeDBOptions)) { Assert.AreEqual(db.Count, 10000); Assert.IsTrue(db.Remove(BitConverter.GetBytes(1000))); Assert.AreEqual(db.Count, 9999); Assert.IsFalse(db.Remove(BitConverter.GetBytes(1000))); var read = db.Select((e, i) => new KeyValuePair <byte[], int>(e, i)).Count(); Assert.AreEqual(db.Count, read); db.Clear(); Assert.AreEqual(db.Count, 0); } using (var db = new PlaneSet(di, FileMode.Open, planeDBOptions)) { Assert.AreEqual(db.Count, 0); } }
public void TestSetThreadUnsafe() { var opts = planeDBOptions.DisableThreadSafety(); using (var db = new PlaneSet(new DirectoryInfo("testdb"), FileMode.CreateNew, opts)) { for (var i = 0; i < COUNT; ++i) { var k = Encoding.UTF8.GetBytes(i.ToString()); Assert.IsTrue(db.TryAdd(k)); } } using (var db = new PlaneSet(new DirectoryInfo("testdb"), FileMode.Open, opts)) { for (var i = 0; i < COUNT; ++i) { var k = Encoding.UTF8.GetBytes(i.ToString()); Assert.IsTrue(db.Contains(k)); } Assert.IsTrue(db.Remove(Encoding.UTF8.GetBytes("0"))); } using (var db = new PlaneSet(new DirectoryInfo("testdb"), FileMode.Open, opts)) { for (var i = 1; i < COUNT; ++i) { var k = Encoding.UTF8.GetBytes(i.ToString()); Assert.IsTrue(db.Contains(k)); } } using (var db = new PlaneSet(new DirectoryInfo("testdb"), FileMode.Open, opts)) { for (var i = 0; i < COUNT; ++i) { var k = Encoding.UTF8.GetBytes(i.ToString()); if (i == 0) { Assert.IsFalse(db.Contains(k)); } else { Assert.IsTrue(db.Contains(k)); } } } }
public void TestSetConcurrent(int concurrency) { int j = 0; using (var db = new PlaneSet(new DirectoryInfo("testdb"), FileMode.CreateNew, planeDBOptions.UsingTableSpace(concurrency.ToString()))) { void Adder() { int i; while ((i = Interlocked.Increment(ref j)) < COUNT) { var k = Encoding.UTF8.GetBytes(i.ToString()); Assert.IsTrue(db.TryAdd(k)); } } var threads = Enumerable.Range(0, concurrency).Select(_ => new Thread(Adder)).ToArray(); foreach (var thread in threads) { thread.Start(); } foreach (var thread in threads) { thread.Join(); } } j = 0; using (var db = new PlaneSet(new DirectoryInfo("testdb"), FileMode.Open, planeDBOptions.UsingTableSpace(concurrency.ToString()))) { void Reader() { int i; while ((i = Interlocked.Increment(ref j)) < COUNT) { var k = Encoding.UTF8.GetBytes(i.ToString()); Assert.IsTrue(db.Contains(k)); } } var threads = Enumerable.Range(0, concurrency).Select(_ => new Thread(Reader)).ToArray(); foreach (var thread in threads) { thread.Start(); } foreach (var thread in threads) { thread.Join(); } } }