Example #1
0
        public void TryAddContentTest()
        {
            var numberOfPages = 1;

            // generate test pages
            byte[] testArray = new byte[Constants.PAGE_SIZE_INCL_HEADER * numberOfPages];

            Memory <byte> memory = new Memory <byte>(testArray);

            // create dummy headers for the 5 pages
            for (uint i = 0; i < numberOfPages; i++)
            {
                var slice = memory.Slice((int)(Constants.PAGE_SIZE_INCL_HEADER * i));
                SpanExtensions.WriteBytes(slice, 0, UnitTestHelper.GetPageHeader(i, PageType.Data));
            }

            string testContent  = "Hello World!!!";
            var    bytesContent = Encoding.UTF8.GetBytes(testContent);

            var dataPageManager = new DataPageManager(memory);
            var page            = (DataPage)dataPageManager.GetPageById(0);

            page.Should().NotBeNull();
            var id = page.TryAddContent(bytesContent, 0);

            id.Id.Should().NotBe(0);

            var document = page.FindById(id.Id);

            document.Should().NotBeNull();
        }
Example #2
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));
            }
        }
Example #3
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));
            }
        }
Example #4
0
        public void GetPageByIdFailIdNotExistingTest()
        {
            uint id = Constants.PAGES_PER_DATA_BLOCK - 1;
            var  memoryWithPages = GenerateTestPages();

            DataPageManager pageManager = new DataPageManager(memoryWithPages);

            var page = pageManager.GetPageById(id);

            page.Should().NotBeNull();
            page.Should().BeOfType(typeof(NullPage));
            page.Header.PageType.Should().Be(PageType.Null);
            page.Header.PageId.Should().Be(0);
        }
Example #5
0
        public void GetPageByIdSuccessTest()
        {
            ushort id = 3;
            var    memoryWithPages = GenerateTestPages();

            DataPageManager pageManager = new DataPageManager(memoryWithPages);

            var page = pageManager.GetPageById(id);

            page.Should().NotBeNull();
            page.Should().BeOfType(typeof(DataPage));
            page.Header.PageType.Should().Be(PageType.Data);
            page.Header.PageId.Should().Be(id);
        }
Example #6
0
        public void Setup()
        {
            var numberOfPages = 1;

            // generate test pages
            byte[] testArray = new byte[Constants.PAGE_SIZE_INCL_HEADER * numberOfPages];

            Memory <byte> memory = new Memory <byte>(testArray);

            // create dummy headers for the 5 pages
            for (uint i = 0; i < numberOfPages; i++)
            {
                var slice = memory.Slice((int)(Constants.PAGE_SIZE_INCL_HEADER * i));
                SpanExtensions.WriteBytes(slice, 0, UnitTestHelper.GetPageHeader(i, PageType.Data));
            }

            string testContent = "Hello World!!!";

            bytesContent = Encoding.UTF8.GetBytes(testContent);

            var _dataPageManager = new DataPageManager(memory);

            _page  = (DataPage)_dataPageManager.GetPageById(0);
            _guids = new ushort[100];
            for (int i = 0; i < 100; i++)
            {
                var res = _page.TryAddContent(bytesContent, 0);
                _guids[i] = res.Id;
            }

            Random random = new Random();

            for (int i = 0; i < Int16.MaxValue * 10; i++)
            {
                _indexes.Add(random.Next(0, 99));
            }
        }
Example #7
0
        public void TryAddManyContentTest()
        {
            var numberOfPages = 1;

            // generate test pages
            byte[] testArray = new byte[Constants.PAGE_SIZE_INCL_HEADER * numberOfPages];

            Memory <byte> memory = new Memory <byte>(testArray);

            // create dummy headers for the 5 pages
            for (uint i = 0; i < numberOfPages; i++)
            {
                var slice = memory.Slice((int)(Constants.PAGE_SIZE_INCL_HEADER * i));
                slice.WriteBytes(0, UnitTestHelper.GetPageHeader(i, PageType.Data));
            }

            string testContent  = "Hello World!!!";
            var    bytesContent = Encoding.UTF8.GetBytes(testContent);

            var dataPageManager = new DataPageManager(memory);
            var page            = (DataPage)dataPageManager.GetPageById(0);

            page.Should().NotBeNull();
            var _guids = new ushort[100];

            for (int i = 0; i < 100; i++)
            {
                var res = page.TryAddContent(bytesContent, 0);
                _guids[i] = res.Id;
            }

            _guids.Should().NotBeEmpty();

            var document = page.FindById(_guids[55]);

            document.Should().NotBeNull();
        }
Example #8
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();
        }
Example #9
0
 public DataBlock(IOResponse response) : base(response)
 {
     _pageManager = new DataPageManager(_memory);
 }