/// <summary>
        /// Opens the store at the specified commit point (always read-only)
        /// </summary>
        /// <param name="storeLocation"></param>
        /// <param name="commitPointId"></param>
        /// <returns></returns>
        public IStore OpenStore(string storeLocation, ulong commitPointId)
        {
            if (_storeConfiguration.PersistenceType == PersistenceType.Rewrite)
            {
                throw new InvalidOperationException("Rewrite page store does not support opening at a previous commit point");
            }
            Logging.LogInfo("Open Store {0} at commit point {1}", storeLocation, commitPointId);
            var dataFilePath     = Path.Combine(storeLocation, DataFileName);
            var resourceFilePath = Path.Combine(storeLocation, ResourceFileName);
            var commitPoint      = GetMasterFile(storeLocation).GetCommitPoints().FirstOrDefault(cp => cp.LocationOffset == commitPointId);

            if (commitPoint != null)
            {
                if (!_persistenceManager.FileExists(resourceFilePath))
                {
                    throw new StoreManagerException(storeLocation, "Resource file not found");
                }
                if (!_persistenceManager.FileExists(dataFilePath))
                {
                    throw new StoreManagerException(storeLocation, "Data file not found");
                }
                var pageStore     = new AppendOnlyFilePageStore(_persistenceManager, dataFilePath, PageSize, true, _storeConfiguration.DisableBackgroundWrites);
                var resourceStore = new AppendOnlyFilePageStore(_persistenceManager, resourceFilePath, PageSize, true, _storeConfiguration.DisableBackgroundWrites);
                var resourceTable = new ResourceTable(resourceStore);
                var store         = new Store(storeLocation, pageStore, resourceTable, commitPointId, null);
                return(store);
            }
            throw new StoreManagerException(storeLocation, "Commit point not found");
        }
예제 #2
0
        public void TestResourceTableInsert()
        {
            const string testString = "this is a test string";
            ulong pageId;
            byte segmentId;
            using (var pageStore = TestUtils.CreateEmptyPageStore("ResourceTableInsert"))
            {
                var resourceTable = new ResourceTable(pageStore);
                resourceTable.Insert(0, testString, out pageId, out segmentId, null);
                Assert.AreEqual((ulong)1, pageId);
                Assert.AreEqual((byte)0, segmentId);

                // Test we can retrieve it from the currently open page store
                var resource = resourceTable.GetResource(pageId, segmentId, null);
                Assert.AreEqual(testString, resource);
                pageStore.Commit(0, null);
            }

            using (var pageStore = new AppendOnlyFilePageStore(PersistenceManager, "ResourceTableInsert", 4096, true, false))
            {
                var resourceTable = new ResourceTable(pageStore);
                var resource = resourceTable.GetResource(pageId, segmentId, null);
                Assert.AreEqual(testString, resource);
            }
        }
        public IStore OpenStore(string storeLocation, bool readOnly)
        {
            Logging.LogInfo("Open Store {0}", storeLocation);
            var masterFile        = GetMasterFile(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, latestCommitPoint.NextCommitNumber, _storeConfiguration.DisableBackgroundWrites);
                    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");
        }
예제 #4
0
        public void TestAssertLongLiteral()
        {
            var longStringValue = "Long string value " + new string('!', 100);
            var pageStore = TestUtils.CreateEmptyPageStore("TestAssertLongLiteral.data");
            var resourceStore = TestUtils.CreateEmptyPageStore("TestAssertLongLiteral.resources");
            var resourceTable = new ResourceTable(resourceStore);
            var resourceIndex = new ResourceIndex(pageStore, resourceTable);

            var resourceId = resourceIndex.AssertResourceInIndex(0, longStringValue, true, "http://example.org/datatypes/string",
                                                "en-us");
            // Should now be able to find the resource id
            Assert.AreEqual(resourceId, resourceIndex.GetResourceId(longStringValue, true, "http://example.org/datatypes/string", "en-us", true));
            // data type URI and language code should have been inserted as resources
            Assert.AreNotEqual(StoreConstants.NullUlong, resourceIndex.GetResourceId("http://example.org/datatypes/string", false, null, null, true));
            Assert.AreNotEqual(StoreConstants.NullUlong, resourceIndex.GetResourceId("en-us", true, null, null, true));
            var resource = resourceIndex.GetResource(resourceId, true);
            Assert.IsNotNull(resource);
            Assert.IsTrue(resource.IsLiteral);
            Assert.AreEqual(longStringValue, resource.Value);
            var dtResource = resourceIndex.GetResource(resource.DataTypeId, true);
            Assert.IsNotNull(dtResource);
            Assert.IsFalse(dtResource.IsLiteral);
            Assert.AreEqual("http://example.org/datatypes/string", dtResource.Value);
            var lcResource = resourceIndex.GetResource(resource.LanguageCodeId, true);
            Assert.IsNotNull(lcResource);
            Assert.IsTrue(lcResource.IsLiteral);
            Assert.AreEqual("en-us", lcResource.Value);

            // Persist the index
            var resourceIndexRoot = resourceIndex.RootId;
            resourceIndex.Save(0, null);
            pageStore.Commit(0ul, null);
            pageStore.Close();
            resourceTable.Commit(0ul, null);
            resourceTable.Dispose();

            // Test we can still find the resource after reopening the store
            using (pageStore = TestUtils.OpenPageStore("TestAssertLongLiteral.data", false))
            {
                using (var rt = new ResourceTable(TestUtils.OpenPageStore("TestAssertLongLiteral.resources", false)))
                {
                    resourceIndex = new ResourceIndex(pageStore, rt, resourceIndexRoot);
                    // Should still be able to find the resource id
                    Assert.AreEqual(resourceId,
                                    resourceIndex.GetResourceId(longStringValue, true,
                                                                "http://example.org/datatypes/string", "en-us", true));
                    Assert.AreNotEqual(StoreConstants.NullUlong,
                                       resourceIndex.GetResourceId("http://example.org/datatypes/string", false, null,
                                                                   null,
                                                                   true));
                    Assert.AreNotEqual(StoreConstants.NullUlong,
                                       resourceIndex.GetResourceId("en-us", true, null, null, true));
                }
            }
        }
        public IStore CreateStore(string storeLocation, PersistenceType storePersistenceType, bool readOnly, bool withTransactionLogging)
        {
            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, _storeConfiguration.DisableBackgroundWrites);
                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);
                store.Close();
            }

            if (withTransactionLogging)
            {
                _persistenceManager.CreateFile(Path.Combine(storeLocation, "transactionheaders.bs"));
                _persistenceManager.CreateFile(Path.Combine(storeLocation, "transactions.bs"));
            }

            Logging.LogInfo("Store created at {0}", storeLocation);
            return(OpenStore(storeLocation, readOnly));
        }
예제 #6
0
        public void TestInsertLongResources()
        {
            var longResourceA = new string('A', 3070);
            var longResourceB = new string('B', 3070);
            var longResourceC = new string('C', 3070);
            ulong pageA, pageB, pageC;
            byte segA, segB, segC;

            using (var pageStore = TestUtils.CreateEmptyPageStore("ResourceTableInsertLong"))
            {
                var resourceTable = new ResourceTable(pageStore);
                resourceTable.Insert(0, longResourceA, out pageA, out segA, null);
                resourceTable.Insert(0, longResourceB, out pageB, out segB, null);
                resourceTable.Insert(0, longResourceC, out pageC, out segC, null);

                // Test we can retrieve the long resources from the currently open page store
                var resource = resourceTable.GetResource(pageA, segA, null);
                Assert.AreEqual(longResourceA, resource);
                resource = resourceTable.GetResource(pageB, segB, null);
                Assert.AreEqual(longResourceB, resource);
                resource = resourceTable.GetResource(pageC, segC, null);
                Assert.AreEqual(longResourceC, resource);
                pageStore.Commit(0, null);
            }

            using (var pageStore = new AppendOnlyFilePageStore(PersistenceManager, "ResourceTableInsertLong", 4096, true, false))
            {
                var resourceTable = new ResourceTable(pageStore);
                var resource = resourceTable.GetResource(pageA, segA, null);
                Assert.AreEqual(longResourceA, resource);
                resource = resourceTable.GetResource(pageB, segB, null);
                Assert.AreEqual(longResourceB, resource);
                resource = resourceTable.GetResource(pageC, segC, null);
                Assert.AreEqual(longResourceC, resource);
            }

        }
        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);

            MasterFile.Create(_persistenceManager, storeLocation, _storeConfiguration, 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);
                    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 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");
 }
        /// <summary>
        /// Opens the store at the specified commit point (always read-only)
        /// </summary>
        /// <param name="storeLocation"></param>
        /// <param name="commitPointId"></param>
        /// <returns></returns>
        public IStore OpenStore(string storeLocation, ulong commitPointId)
        {
            if (_storeConfiguration.PersistenceType == PersistenceType.Rewrite)
            {
                throw new InvalidOperationException("Rewrite page store does not support opening at a previous commit point");
            }
            Logging.LogInfo("Open Store {0} at commit point {1}", storeLocation, commitPointId);
            var dataFilePath = Path.Combine(storeLocation, DataFileName);
            var resourceFilePath = Path.Combine(storeLocation, ResourceFileName);
            var commitPoint = GetMasterFile(storeLocation).GetCommitPoints().FirstOrDefault(cp => cp.LocationOffset == commitPointId);
            if (commitPoint != null)
            {
                if (!_persistenceManager.FileExists(resourceFilePath))
                {
                    throw new StoreManagerException(storeLocation, "Resource file not found");
                }
                if (!_persistenceManager.FileExists(dataFilePath))
                {
                    throw new StoreManagerException(storeLocation, "Data file not found");
                }
                var pageStore = new AppendOnlyFilePageStore(_persistenceManager, dataFilePath, PageSize, true, _storeConfiguration.DisableBackgroundWrites);
                var resourceStore = new AppendOnlyFilePageStore(_persistenceManager, resourceFilePath, PageSize, true, _storeConfiguration.DisableBackgroundWrites);
                var resourceTable = new ResourceTable(resourceStore);
                var store = new Store(storeLocation, pageStore, resourceTable, commitPointId, null);
                return store;

            }
            throw new StoreManagerException(storeLocation, "Commit point not found");
        }
예제 #10
0
        public void TestAssertLongUri()
        {
            IPageStore pageStore;
            ulong resourceId, resourceIndexRoot;
            var shortenedUri = "p0:" + Guid.Empty + Guid.NewGuid();
            using (pageStore = TestUtils.CreateEmptyPageStore("TestAssertLongUri.data"))
            {
                using (var resourceTable = new ResourceTable(TestUtils.CreateEmptyPageStore("TestAssertLongUri.resources")))
                {
                    var resourceIndex = new ResourceIndex(pageStore, resourceTable);
                    resourceId = resourceIndex.AssertResourceInIndex(0, shortenedUri);
                    Assert.AreEqual(resourceId, resourceIndex.GetResourceId(shortenedUri, false, null, null, true));
                    var resource = resourceIndex.GetResource(resourceId, true);
                    Assert.IsNotNull(resource);
                    Assert.AreEqual(shortenedUri, resource.Value);
                    Assert.IsFalse(resource.IsLiteral);
                    resourceIndexRoot = resourceIndex.RootId;
                    resourceIndex.Save(0, null);
                    pageStore.Commit(0ul, null);
                    resourceTable.Commit(0ul, null);
                }
            }

            using (pageStore = TestUtils.OpenPageStore("TestAssertLongUri.data", false))
            {
                using (var resourceTable = new ResourceTable(TestUtils.OpenPageStore("TestAssertLongUri.resources", true)))
                {
                    var resourceIndex = new ResourceIndex(pageStore, resourceTable, resourceIndexRoot);
                    Assert.AreEqual(resourceId, resourceIndex.GetResourceId(shortenedUri, false, null, null, true));
                    var resource = resourceIndex.GetResource(resourceId, true);
                    Assert.IsNotNull(resource);
                    Assert.AreEqual(shortenedUri, resource.Value);
                    Assert.IsFalse(resource.IsLiteral);
                }
            }
        }