Пример #1
0
        public void SinglePageWriteTest()
        {
            const string pageManagerName = "DataPageManager-2";

            PrepareFor(pageManagerName);

            using (var dataPageManager = new DataPageManager(new DataPageManagerConfig(pageManagerName, PageSize, TempFilesDirectory)))
            {
                Assert.Multiple(() =>
                {
                    Assert.AreEqual(0, dataPageManager.DataPagesCount);
                    Assert.IsNull(dataPageManager.Read(1));
                    Assert.IsNull(dataPageManager.Read(2));
                    Assert.IsNull(dataPageManager.Read(3));
                });

                var data1 = new string('1', 50);
                var data2 = new string('2', 70);
                var dr1   = Core.Models.DataRecord.Create(Encoding.UTF8.GetBytes(data1));
                var dr2   = Core.Models.DataRecord.Create(Encoding.UTF8.GetBytes(data2));
                dataPageManager.Save(dr1);
                dataPageManager.Save(dr2);

                var dataRecord1 = dataPageManager.Read(dr1.Id);
                var dataRecord2 = dataPageManager.Read(dr2.Id);

                Assert.AreEqual(data1, Encoding.UTF8.GetString(dataRecord1.Body));
                Assert.AreEqual(data2, Encoding.UTF8.GetString(dataRecord2.Body));
            }
        }
Пример #2
0
        public void MultiPageWriteTest()
        {
            const string pageManagerName = "DataPageManager-1";

            PrepareFor(pageManagerName);

            using (var dataPageManager = new DataPageManager(new DataPageManagerConfig(pageManagerName, PageSize, TempFilesDirectory)))
            {
                Assert.Multiple(() =>
                {
                    Assert.AreEqual(0, dataPageManager.DataPagesCount);
                    Assert.IsNull(dataPageManager.Read(1));
                    Assert.IsNull(dataPageManager.Read(2));
                    Assert.IsNull(dataPageManager.Read(3));
                });

                dataPageManager.Save(Core.Models.DataRecord.Create(Encoding.UTF8.GetBytes(new string('1', 100))));

                var data       = new string('5', 500);
                var dataRecord = Core.Models.DataRecord.Create(Encoding.UTF8.GetBytes(data));
                dataPageManager.Save(dataRecord);

                var readedDataRecord = dataPageManager.Read(dataRecord.Id);

                Assert.AreEqual(data, Encoding.UTF8.GetString(readedDataRecord.Body));
            }
        }
Пример #3
0
        private static void Main()
        {
            var dpmConfig =
                new DataPageManagerConfig(
                    "DataPageManager-1",
                    100,
                    Path.Combine(Directory.GetCurrentDirectory())
                    );

            using (var dataPageManager = new DataPageManager(dpmConfig))
            {
                var dog = new Dog
                {
                    Age  = 5,
                    Id   = 10420,
                    Name = "A dog"
                };
                var anotherDog = new Dog
                {
                    Age  = 4,
                    Id   = 10421,
                    Name = "Another dog"
                };
                var anotherOneDog = new Dog
                {
                    Age  = 1,
                    Id   = 10422,
                    Name = "Another one dog 3"
                };

                dataPageManager.Save(DataRecord.Create(dog.GetBytes()));
                dataPageManager.Save(DataRecord.Create(anotherDog.GetBytes()));
                dataPageManager.Save(DataRecord.Create(anotherOneDog.GetBytes()));

                Thread.Sleep(1000); // даём время на срабатывание автосохранения.
                var dataRecord1 = dataPageManager.Read(1);
                var dataRecord2 = dataPageManager.Read(2);
                var dataRecord3 = dataPageManager.Read(3);

                if (dataRecord1?.Body != null)
                {
                    Debug.Assert(dog.GetBytes().SequenceEqual(dataRecord1.Body));
                    var animal = Dog.ReadFrom(dataRecord1.Body);
                }

                if (dataRecord2?.Body != null)
                {
                    Debug.Assert(anotherDog.GetBytes().SequenceEqual(dataRecord2.Body));
                    var animal = Dog.ReadFrom(dataRecord2.Body);
                }

                if (dataRecord3?.Body != null)
                {
                    Debug.Assert(anotherOneDog.GetBytes().SequenceEqual(dataRecord3.Body));
                    var animal = Dog.ReadFrom(dataRecord3.Body);
                }

                foreach (var record in dataPageManager.AsEnumerable(1))
                {
                    var animal = Dog.ReadFrom(record.Body);
                }
            }

            Console.ReadLine();
        }