public void TestTransactionLogOptions()
        {
            using (TempFile temp = new TempFile())
            {
                temp.Delete();
                var opt = new TransactionLogOptions<int, string>(temp.TempPath,
                                                              PrimitiveSerializer.Int32,
                                                              PrimitiveSerializer.String);
                //FileName
                Assert.AreEqual(temp.TempPath, opt.FileName);
                //Key/Value serializers
                Assert.IsTrue(ReferenceEquals(opt.KeySerializer, PrimitiveSerializer.Int32));
                Assert.IsTrue(ReferenceEquals(opt.ValueSerializer, PrimitiveSerializer.String));
                //FileOptions
                Assert.AreEqual(FileOptions.WriteThrough, opt.FileOptions);
                Assert.AreEqual(FileOptions.WriteThrough | FileOptions.Asynchronous, opt.FileOptions |= FileOptions.Asynchronous);
                //Read Only
                Assert.AreEqual(false, opt.ReadOnly);
                Assert.AreEqual(true, opt.ReadOnly = true);
                //File Buffer
                Assert.AreEqual(8, opt.FileBuffer);
                Assert.AreEqual(0x40000, opt.FileBuffer = 0x40000);
                //Clone
                Assert.IsFalse(ReferenceEquals(opt, opt.Clone()));

                using(TransactionLog<int, string> log = new TransactionLog<int,string>(opt))
                    Assert.AreEqual(0, log.Size);
            }
        }
        public void TestWriteToTemporaryCopy()
        {
            Dictionary<Guid, TestInfo> first, data = new Dictionary<Guid, TestInfo>();
            using(TempFile temp = new TempFile())
            {
                temp.Delete();
                var options = GetOptions(temp);
                options.TransactionLogFileName = Path.ChangeExtension(options.FileName, ".tlog");

                using (var tree = new BPlusTree<Guid, TestInfo>(options))
                {
                    Insert(tree, data, 1, 100, TimeSpan.MaxValue);
                    TestInfo.AssertEquals(data, tree);
                    Assert.IsFalse(temp.Exists);
                }

                // All data commits to output file
                Assert.IsTrue(temp.Exists);
                TestInfo.AssertEquals(data, BPlusTree<Guid, TestInfo>.EnumerateFile(options));

                first = new Dictionary<Guid, TestInfo>(data);
                
                using (var tree = new BPlusTree<Guid, TestInfo>(options))
                {
                    Insert(tree, data, 1, 100, TimeSpan.MaxValue);

                    //We are writing to a backup, the original file still contains 100 items:
                    TestInfo.AssertEquals(first, BPlusTree<Guid, TestInfo>.EnumerateFile(options));

                    //Commit the changes and the original file will now contain our changes:
                    tree.CommitChanges();
                    TestInfo.AssertEquals(data, BPlusTree<Guid, TestInfo>.EnumerateFile(options));

                    //Add a few more records...
                    Insert(tree, data, 1, 100, TimeSpan.MaxValue);
                }
                //Dispose of the tree will commit changes...
                TestInfo.AssertEquals(data, BPlusTree<Guid, TestInfo>.EnumerateFile(options));
            }
        }
        public void TestMergeRandomInFile()
        {
            BPlusTreeOptions<int, string> options = Options;
            using (TempFile temp = new TempFile())
            {
                temp.Delete();
                options.CreateFile = CreatePolicy.Always;
                options.FileName = temp.TempPath;
                options.CalcBTreeOrder(4, 4);
                Stopwatch sw = Stopwatch.StartNew();
                
                Dictionary<int, string> expected = 
                    TestMergeRandom(options, 2, 300);

                Trace.TraceInformation("Creating {0} nodes in {1}.", expected.Count, sw.Elapsed);
                sw = Stopwatch.StartNew();

                options = Options;
                options.CreateFile = CreatePolicy.Never;
                options.FileName = temp.TempPath;
                options.CalcBTreeOrder(4, 4);
                using (BPlusTree<int, string> tree = new BPlusTree<int, string>(options))
                {
                    VerifyDictionary(expected, tree);
                }

                Trace.TraceInformation("Verified {0} nodes in {1}.", expected.Count, sw.Elapsed);
            }
        }
        public void TestMergeSequenceInFile()
        {
            BPlusTreeOptions<int, string> options = Options;
            using (TempFile temp = new TempFile())
            {
                options = Options;
                temp.Delete();
                //options.CreateFile = CreatePolicy.Always;
                //options.FileName = temp.TempPath;
                options.MaximumValueNodes = 14;
                options.MinimumValueNodes = 7;
                options.MaximumChildNodes = 6;
                options.MinimumChildNodes = 2;

                // Just to make sure we don't break some fencepost condition in the future
                for (int i = 0; i <= (options.MaximumValueNodes * options.MaximumChildNodes) + 1; i++)
                    TestMergeSequenceInFile(options.Clone(), i);
                TestMergeSequenceInFile(options.Clone(), options.MaximumValueNodes * options.MaximumChildNodes * options.MaximumChildNodes);
                TestMergeSequenceInFile(options.Clone(), options.MaximumValueNodes * options.MaximumChildNodes * options.MaximumChildNodes + 1);
            }
        }
Ejemplo n.º 5
0
		public void TestReadWrite()
		{
			string test = "Hello World!\u1255";
			TempFile file = new TempFile();
			File.WriteAllBytes(file.TempPath, Encoding.UTF8.GetBytes(test));

			Assert.AreEqual(Encoding.UTF8.GetBytes(test), file.ReadAllBytes());
			Assert.AreEqual(test, file.ReadAllText());

			file.Delete();
			Assert.IsFalse(File.Exists(file.TempPath));
			Assert.IsFalse(file.Exists);
			file.WriteAllBytes(Encoding.UTF8.GetBytes(test));
			Assert.AreEqual(test, file.ReadAllText());

			file.Delete();
			Assert.IsFalse(File.Exists(file.TempPath));
			Assert.IsFalse(file.Exists);
			file.WriteAllText(test);
			Assert.AreEqual(test, file.ReadAllText());
		}
Ejemplo n.º 6
0
		public void TestLength()
		{
			using (TempFile f = new TempFile())
			{
				Assert.AreEqual(0, f.Length);
				f.Delete();
				Assert.AreEqual(0, f.Length);
				f.Length = 255;
				Assert.AreEqual(255, f.Length);
				f.Length = 0;
				Assert.AreEqual(0, f.Length);
				Assert.IsTrue(f.Exists);
			}
		}
Ejemplo n.º 7
0
        public void TestDerivedFileName()
        {
            string path;
            using(DisposingList l = new DisposingList())
            {
                TempFile ftarget = new TempFile();
                ftarget.Delete();

                l.Add(ftarget);
                TempFile fbigone = TempFile.Attach(String.Format("{0}.~{1:x4}", ftarget.TempPath, 0x10008 - 25));
                fbigone.Create().Dispose();
                Assert.IsTrue(fbigone.Exists);
                l.Add(fbigone);
                path = ftarget.TempPath;

                string tmpName;
                Dictionary<string, object> names = new Dictionary<string,object>(StringComparer.OrdinalIgnoreCase);

                for( int i=0; i < 25; i++ )
                {
                    Stream s = ReplaceFile.CreateDerivedFile(ftarget.TempPath, out tmpName);
                    l.Add(TempFile.Attach(tmpName));
                    l.Add(s);
                    names.Add(tmpName, null);
                }
                
                fbigone.Delete();
                Assert.AreEqual(25, Directory.GetFiles(Path.GetDirectoryName(path), Path.GetFileName(path) + "*").Length);
            }
            Assert.AreEqual(0, Directory.GetFiles(Path.GetDirectoryName(path), Path.GetFileName(path) + "*").Length);
        }