Example #1
0
        private static void Generate(string tempFile, SerializationEngine serializer, List<TestClass> list,
                                 Action<Action<int>> loop)
        {
            using (var stream = new FileStream(tempFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
              {
            var data = new StreamAccessor(stream);
            var lFile = new LowLevelAppendOnlyFile(data);
            var file = new GenericAppendOnlyFile<TestClass>(lFile, serializer);

            // append COUNT records
            ShowTime("append", () => loop(i => file.Append(list[i])));

            // read all the records
            ShowTime("read all", () => { var records = file.ReadFrom(0).ToList(); });

            // read all the records, one at a time
            ShowTime("read all, one at a time", () => loop(i => file.Read(i)));
              }

              // close and reopen the file (rebuilds the index)
              ShowTime("rebuild index", () =>
              {
            using (var stream = new FileStream(tempFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
            {
              var data = new StreamAccessor(stream);
              var lFile = new LowLevelAppendOnlyFile(data);
            }
              });
        }
Example #2
0
        private static void Main(string[] args)
        {
            var serializer = new ProtoBufSerializationEngine();

              var list = new List<TestClass>();
              for (var i = 0; i < COUNT; i++)
              {
            var c = new TestClass {Name = GenerateRandomString(20), Address = GenerateRandomString(40)};
            list.Add(c);
              }

              var tempFile = Path.GetTempFileName();
              Console.WriteLine("Using {0} - sequentially writing and reading {1} records", tempFile, COUNT);
              Generate(tempFile, serializer, list, SeqForLoop);

              // verify the data
              using (var stream = new FileStream(tempFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
              {
            var data = new StreamAccessor(stream);
            var lFile = new LowLevelAppendOnlyFile(data);
            var file = new GenericAppendOnlyFile<TestClass>(lFile, serializer);

            var records = file.ReadFrom(0).ToList();

            // verify that the index is built correctly
            for (var i = 0; i < COUNT; i++)
            {
              var r = records[i];
              Debug.Assert(r.Name == list[i].Name);
              Debug.Assert(r.Address == list[i].Address);
            }
              }

              // multi-threaded test
              tempFile = Path.GetTempFileName();
              Console.WriteLine("Using {0} - multi-threaded writing and reading {1} records", tempFile, COUNT);
              Generate(tempFile, serializer, list, ParForLoop);
        }
Example #3
0
        public void GenericAppendOnlyFileSmokeTest()
        {
            var stream = new MemoryStream();
              var data = new StreamAccessor(stream);
              var file = new LowLevelAppendOnlyFile(data);
              var serializer = new ProtoBufSerializationEngine();
              var sut = new GenericAppendOnlyFile<TestClass>(file, serializer);

              sut.Append(new TestClass {Name = "Marcel", Address = "Kennesaw, GA"});
              sut.Append(new TestClass {Name = "Gigi Meseriasu", Address = "Washington, DC"});

              var r1 = sut.Read(0);
              Assert.AreEqual("Marcel", r1.Name);
              Assert.AreEqual("Kennesaw, GA", r1.Address);

              var rs = sut.ReadFrom(0).ToList();
              var r2 = rs[1];
              Assert.AreEqual("Gigi Meseriasu", r2.Name);
              Assert.AreEqual("Washington, DC", r2.Address);

              Assert.AreEqual(2, sut.Index.Length);
              Assert.AreEqual(0, sut.Index[0]);
        }