public void TestCreateAndUpdatePage()
        {
            ulong newPage;
            using (var pageStore = CreateEmptyPageStore("TestCreateAndUpdatePage.dat"))
            {
                newPage = pageStore.Create();
                pageStore.Write(1, newPage, _testBuffer1);
                pageStore.Commit(1, null);
            }
            Assert.AreEqual(1ul, newPage);
            using (var fs = new FileStream("TestCreateAndUpdatePage.dat", FileMode.Open, FileAccess.Read))
            {
                Assert.AreEqual(8192L, fs.Length);
            }

            using (var pageStore = new BinaryFilePageStore(_pm, "TestCreateAndUpdatePage.dat", 4096, true, 1))
            {
                var pageData = pageStore.Retrieve(newPage, null);
                Assert.AreEqual(0, _testBuffer1.Compare(pageData));
                pageStore.Write(2, newPage, _testBuffer2);
                pageStore.Commit(2, null);
            }

            using (var pageStore = new BinaryFilePageStore(_pm, "TestCreateAndUpdatePage.dat", 4096, true, 2))
            {
                var pageData = pageStore.Retrieve(newPage, null);
                Assert.AreEqual(0, _testBuffer2.Compare(pageData));
            }
        }
        public void TestStoreModifiedExceptionThrown()
        {
            ulong pageId;
            IPage page;
            byte[] buffer1 = new byte[] {1, 1, 1, 1};
            byte[] buffer2 = new byte[] {2, 2, 2, 2};
            using (
                var pageStore = TestUtils.CreateEmptyPageStore("TestStoreModifiedExceptionThrown.data",
                                                               PersistenceType.Rewrite))
            {
                page = pageStore.Create(1);
                pageId = page.Id;
                page.SetData(buffer1, 0, 0, 4);
                pageStore.Commit(1, null);
            }

            using (
                var readStore = new BinaryFilePageStore(TestUtils.PersistenceManager,
                                                        "TestStoreModifiedExceptionThrown.data",
                                                        BPlusTreeStoreManager.PageSize, true, 1))
            {
                page = readStore.Retrieve(page.Id, null);
                Assert.AreEqual(1, page.Data[0]);

                using (
                    var writeStore = new BinaryFilePageStore(TestUtils.PersistenceManager,
                                                             "TestStoreModifiedExceptionThrown.data",
                                                             BPlusTreeStoreManager.PageSize, true, 1))
                {
                    var writePage = writeStore.Retrieve(pageId, null);
                    Assert.IsFalse(writeStore.IsWriteable(writePage));
                    writePage =  writeStore.GetWriteablePage(2, writePage);
                    writePage.SetData(buffer2, 0, 0, 4);
                    writeStore.Commit(2, null);
                }

                page = readStore.Retrieve(page.Id, null);
                Assert.AreEqual(1, page.Data[0]);

                using (
                    var writeStore = new BinaryFilePageStore(TestUtils.PersistenceManager,
                                                             "TestStoreModifiedExceptionThrown.data",
                                                             BPlusTreeStoreManager.PageSize, true, 2))
                {
                    var writePage = writeStore.Retrieve(pageId, null);
                    writePage = writeStore.GetWriteablePage(3, writePage);
                    writePage.SetData(buffer2, 0, 0, 4);
                    writeStore.Commit(3, null);
                }

                page = readStore.Retrieve(1, null);
                Assert.AreEqual(1, page.Data[0]);
                // We should not reach this assert because the ReadWriteStoreModifiedException should get thrown
            }
        }
        public void TestCreateAndUpdatePage()
        {
            using (var pageStore = CreateEmptyPageStore("TestCreateAndUpdatePage.dat"))
            {
                var newPage = pageStore.Create(1);
                newPage = pageStore.GetWriteablePage(1, newPage);
                newPage.SetData(_testBuffer1);
                pageStore.Commit(1, null);
                Assert.AreEqual(1ul, newPage.Id);
            }

            using (var fs = new FileStream("TestCreateAndUpdatePage.dat", FileMode.Open, FileAccess.Read))
            {
                Assert.AreEqual(8192L, fs.Length);
            }

            using (var pageStore = new BinaryFilePageStore(_pm, "TestCreateAndUpdatePage.dat", 4096, true, 1))
            {
                var page = pageStore.Retrieve(1ul, null);
                Assert.IsFalse(pageStore.IsWriteable(page));
                try
                {
                    page.SetData(_testBuffer2);
                    Assert.Fail("Expected an InvalidOperationException when attempting to write to a non-writeable page");
                }
                catch (InvalidOperationException)
                {
                    // Expected
                }

                page = pageStore.GetWriteablePage(2, page);
                Assert.IsTrue(pageStore.IsWriteable(page));
                Assert.AreEqual(0, _testBuffer1.Compare(page.Data));
                page.SetData(_testBuffer2);
                pageStore.Commit(2, null);
            }

            using (var pageStore = new BinaryFilePageStore(_pm, "TestCreateAndUpdatePage.dat", 4096, true, 2))
            {
                var page = pageStore.Retrieve(1ul, null);
                Assert.AreEqual(0, _testBuffer2.Compare(page.Data));
            }
        }
        public void TestReopenAfterReadWriteStoreModifiedException()
        {
            ulong pageId;
            var buffer1 = new byte[] {1, 1, 1, 1};
            var buffer2 = new byte[] {2, 2, 2, 2};
            var buffer3 = new byte[] {3, 3, 3, 3};

            using (
                var pageStore = TestUtils.CreateEmptyPageStore("TestStoreModifiedExceptionThrown.data",
                                                               PersistenceType.Rewrite))
            {
                var page = pageStore.Create(1);
                pageId = page.Id;
                page.SetData(buffer1, 0, 0, 4);
                pageStore.Commit(1, null);
            }

            var readStore = new BinaryFilePageStore(TestUtils.PersistenceManager,
                                                    "TestStoreModifiedExceptionThrown.data",
                                                    BPlusTreeStoreManager.PageSize, true, 1);
            try
            {
                var page = readStore.Retrieve(1, null);
                Assert.AreEqual(1, page.Data[0]);

                using (
                    var writeStore = new BinaryFilePageStore(TestUtils.PersistenceManager,
                                                             "TestStoreModifiedExceptionThrown.data",
                                                             BPlusTreeStoreManager.PageSize, true, 1))
                {
                    var writePage = writeStore.Retrieve(pageId, null);
                    writePage = writeStore.GetWriteablePage(2, writePage);
                    writePage.SetData(buffer2, 0, 0, 4);
                    writeStore.Commit(2, null);
                }

                page = readStore.Retrieve(1, null);
                Assert.AreEqual(1, page.Data[0]);

                using (
                    var writeStore = new BinaryFilePageStore(TestUtils.PersistenceManager,
                                                             "TestStoreModifiedExceptionThrown.data",
                                                             BPlusTreeStoreManager.PageSize, true, 2))
                {
                    var writePage = writeStore.Retrieve(pageId, null);
                    writePage = writeStore.GetWriteablePage(3, writePage);
                    writePage.SetData(buffer3, 0, 0, 4);
                    writeStore.Commit(3, null);
                }

                try
                {
                    readStore.Retrieve(pageId, null);
                    Assert.Fail("Expected ReadWriteStoreModifiedException to be thrown");
                }
                catch (ReadWriteStoreModifiedException)
                {
                    readStore.Close();
                    readStore.Dispose();
                    readStore = null;
                    readStore = new BinaryFilePageStore(TestUtils.PersistenceManager,
                                                        "TestStoreModifiedExceptionThrown.data",
                                                        BPlusTreeStoreManager.PageSize, true, 3);
                    page = readStore.Retrieve(pageId, null);
                    Assert.AreEqual(3, page.Data[0]);
                }
            }
            finally
            {
                if (readStore != null)
                {
                    readStore.Dispose();
                }
            }
        }