Ejemplo n.º 1
0
        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
            }
        }
Ejemplo n.º 3
0
 public BinaryPageAdapter(BinaryFilePageStore store, BinaryFilePage page, ulong currentTransactionId, bool isModified)
 {
     _store      = store;
     _page       = page;
     _isModified = isModified;
     if (_page.FirstTransactionId > currentTransactionId && _page.SecondTransactionId > currentTransactionId)
     {
         // This is an error condition that would happen if a store is kept open while two successive writes are committed
         throw new ReadWriteStoreModifiedException();
     }
     _data = isModified ? _page.GetWriteBuffer(currentTransactionId) : _page.GetReadBuffer(currentTransactionId);
 }
 public BinaryPageAdapter(BinaryFilePageStore store, BinaryFilePage page, ulong currentTransactionId, bool isModified)
 {
     _store = store;
     _page = page;
     _isModified = isModified;
     if (_page.FirstTransactionId > currentTransactionId && _page.SecondTransactionId > currentTransactionId)
     {
         // This is an error condition that would happen if a store is kept open while two successive writes are committed
         throw new ReadWriteStoreModifiedException();
     }
     _data = isModified ? _page.GetWriteBuffer(currentTransactionId) : _page.GetReadBuffer(currentTransactionId);
 }
        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));
            }
        }
Ejemplo n.º 6
0
        public IStore CreateStore(string storeLocation, PersistenceType storePersistenceType, bool readOnly)
        {
            Logging.LogInfo("Create Store {0} with persistence type {1}", storeLocation, storePersistenceType);
            if (_persistenceManager.DirectoryExists(storeLocation))
            {
                throw new StoreManagerException(storeLocation, "Store already exists");
            }

            _persistenceManager.CreateDirectory(storeLocation);

            var dataFilePath = Path.Combine(storeLocation, DataFileName);
            _persistenceManager.CreateFile(dataFilePath);

            var resourceFilePath = Path.Combine(storeLocation, ResourceFileName);
            _persistenceManager.CreateFile(resourceFilePath);

            var targetStoreConfiguration = _storeConfiguration.Clone() as StoreConfiguration;
            targetStoreConfiguration.PersistenceType = storePersistenceType;
            MasterFile.Create(_persistenceManager, storeLocation, targetStoreConfiguration, Guid.NewGuid());
            IPageStore dataPageStore = null;
            switch (storePersistenceType)
            {
                case PersistenceType.AppendOnly:
                    dataPageStore = new AppendOnlyFilePageStore(_persistenceManager, dataFilePath, PageSize, false, _storeConfiguration.DisableBackgroundWrites);
                    break;
                case PersistenceType.Rewrite:
                    dataPageStore = new BinaryFilePageStore(_persistenceManager, dataFilePath, PageSize, false, 0, 1);
                    break;
            }
            IPageStore resourcePageStore = new AppendOnlyFilePageStore(_persistenceManager, resourceFilePath, PageSize, false, _storeConfiguration.DisableBackgroundWrites);
            var resourceTable = new ResourceTable(resourcePageStore);
            using (var store = new Store(storeLocation, dataPageStore, resourceTable))
            {
                store.Commit(Guid.Empty);
            }

            Logging.LogInfo("Store created at {0}", storeLocation);
            return OpenStore(storeLocation, readOnly);
        }
        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();
                }
            }
        }
Ejemplo n.º 8
0
 public IStore OpenStore(string storeLocation, bool readOnly)
 {
     Logging.LogInfo("Open Store {0}", storeLocation);
     var masterFile = MasterFile.Open(_persistenceManager, storeLocation);
     var latestCommitPoint = masterFile.GetLatestCommitPoint();
     var dataFilePath = Path.Combine(storeLocation, DataFileName);
     var resourceFilePath = Path.Combine(storeLocation, ResourceFileName);
     if (_persistenceManager.FileExists(dataFilePath))
     {
         IPageStore dataPageStore = null;
         switch (masterFile.PersistenceType)
         {
                 case PersistenceType.AppendOnly:
                 dataPageStore = new AppendOnlyFilePageStore(_persistenceManager, dataFilePath, PageSize, readOnly, _storeConfiguration.DisableBackgroundWrites);
                 break;
                 case PersistenceType.Rewrite:
                 dataPageStore = new BinaryFilePageStore(_persistenceManager, dataFilePath, PageSize, readOnly, latestCommitPoint.CommitNumber);
                 break;
         }
         var resourcePageStore = new AppendOnlyFilePageStore(_persistenceManager, resourceFilePath, PageSize, readOnly, _storeConfiguration.DisableBackgroundWrites);
         var resourceTable = new ResourceTable(resourcePageStore);
         var store = new Store(storeLocation, dataPageStore, resourceTable, latestCommitPoint.LocationOffset, null);
         Logging.LogInfo("Store {0} opened successfully", storeLocation);
         return store;
     }
     throw new StoreManagerException(storeLocation, "Data file not found");
 }
Ejemplo n.º 9
0
        public void CreateSnapshot(string srcStoreLocation, string destStoreLocation,
                                   PersistenceType storePersistenceType, ulong commitPointId = StoreConstants.NullUlong)
        {
            Logging.LogInfo("Snapshot store {0} to new store {1} with persistence type {2}", srcStoreLocation,
                            destStoreLocation, storePersistenceType);
            if (_persistenceManager.DirectoryExists(destStoreLocation))
            {
                throw new StoreManagerException(destStoreLocation, "Store already exists");
            }

            // Open the source store for reading
            using (IStore srcStore = commitPointId == StoreConstants.NullUlong
                                         ? OpenStore(srcStoreLocation, true)
                                         : OpenStore(srcStoreLocation, commitPointId))
            {

                // Create the directory for the destination store
                _persistenceManager.CreateDirectory(destStoreLocation);

                // Create empty data file
                var dataFilePath = Path.Combine(destStoreLocation, DataFileName);
                _persistenceManager.CreateFile(dataFilePath);

                // Create initial master file
                var destStoreConfiguration = _storeConfiguration.Clone() as StoreConfiguration;
                destStoreConfiguration.PersistenceType = storePersistenceType;
                var destMasterFile = MasterFile.Create(_persistenceManager, destStoreLocation, destStoreConfiguration,
                                                       Guid.NewGuid());

                // Copy resource files from source store
                var resourceFilePath = Path.Combine(destStoreLocation, ResourceFileName);
                _persistenceManager.CopyFile(Path.Combine(srcStoreLocation, ResourceFileName), resourceFilePath, true);

                // Initialize data page store
                IPageStore destPageStore = null;
                switch (storePersistenceType)
                {
                    case PersistenceType.AppendOnly:
                        destPageStore = new AppendOnlyFilePageStore(_persistenceManager, dataFilePath, PageSize, false,
                                                                    _storeConfiguration.DisableBackgroundWrites);
                        break;
                    case PersistenceType.Rewrite:
                        destPageStore = new BinaryFilePageStore(_persistenceManager, dataFilePath, PageSize, false, 0, 1, _storeConfiguration.DisableBackgroundWrites);
                        break;
                    default:
                        throw new BrightstarInternalException("Unrecognized target store type: " + storePersistenceType);
                }
                
                // Copy Data
                ulong destStorePageId = srcStore.CopyTo(destPageStore, 1ul);

                destPageStore.Close();

                destMasterFile.AppendCommitPoint(
                    new CommitPoint(destStorePageId, 1ul, DateTime.UtcNow, Guid.Empty), true);
            }
        }