Beispiel #1
0
        /// <summary>
        /// Update the catalog page and index.json file.
        /// </summary>
        public static void UpdatePageIndex(JObject catalogIndexJson, JObject currentPageJson, Guid commitId)
        {
            var pages          = JsonUtility.GetItems(catalogIndexJson);
            var currentPageUri = JsonUtility.GetIdUri(currentPageJson);
            var pageCommits    = JsonUtility.GetItems(currentPageJson);

            var pageEntry = pages.Where(e => JsonUtility.GetIdUri(e).Equals(currentPageUri)).Single();

            pageEntry["commitId"]        = commitId.ToString().ToLowerInvariant();
            pageEntry["commitTimeStamp"] = DateTimeOffset.UtcNow.GetDateString();
            pageEntry["count"]           = pageCommits.Count;
            catalogIndexJson["count"]    = pages.Count;
        }
Beispiel #2
0
        /// <summary>
        /// Add an entry to the catalog.
        /// </summary>
        private async Task AddCatalogCommits(IEnumerable <JObject> newPageCommits, string lastUpdatedPropertyName)
        {
            // Add catalog page entry
            var catalogIndexJson = await RootIndexFile.GetJson(_context.Log, _context.Token);

            catalogIndexJson["commitId"]        = _context.CommitId.ToString().ToLowerInvariant();
            catalogIndexJson["commitTimeStamp"] = DateTimeOffset.UtcNow.GetDateString();

            var pages = JsonUtility.GetItems(catalogIndexJson);

            var currentPageUri  = GetCurrentPage(catalogIndexJson);
            var currentPageFile = _context.Source.Get(currentPageUri);

            var pageCommits = new List <JObject>();

            var currentPageJson = await currentPageFile.GetJsonOrNull(_context.Log, _context.Token);

            if (currentPageJson != null)
            {
                pageCommits = JsonUtility.GetItems(currentPageJson);
            }
            else
            {
                var newPageEntry = CatalogUtility.CreateCatalogIndexPageEntry(currentPageUri, _context.CommitId);

                var pageArray = (JArray)catalogIndexJson["items"];
                pageArray.Add(newPageEntry);

                // Update pages
                pages = JsonUtility.GetItems(catalogIndexJson);
            }

            // Add all commits, this might go over the max catalog page size.
            // This could be improved to create new pages once over the limit if needed.
            pageCommits.AddRange(newPageCommits);

            // Write catalog page
            var pageJson = await CatalogUtility.CreateCatalogPageAsync(RootIndexFile.EntityUri, currentPageUri, pageCommits, _context.CommitId);

            await currentPageFile.Write(pageJson, _context.Log, _context.Token);

            // Update index
            CatalogUtility.UpdatePageIndex(catalogIndexJson, pageJson, _context.CommitId);

            catalogIndexJson[lastUpdatedPropertyName] = DateTimeOffset.UtcNow.GetDateString();

            catalogIndexJson = JsonLDTokenComparer.Format(catalogIndexJson);

            await RootIndexFile.Write(catalogIndexJson, _context.Log, _context.Token);
        }
Beispiel #3
0
        /// <summary>
        /// Add an entry to the catalog.
        /// </summary>
        private async Task AddCatalogEntry(JObject pageCommit, string lastUpdatedPropertyName)
        {
            // Add catalog page entry
            var catalogIndexJson = await RootIndexFile.GetJson(_context.Log, _context.Token);

            catalogIndexJson["commitId"]        = _context.CommitId.ToString().ToLowerInvariant();
            catalogIndexJson["commitTimeStamp"] = DateTimeOffset.UtcNow.GetDateString();

            var pages = JsonUtility.GetItems(catalogIndexJson);

            var currentPageUri  = GetCurrentPage(catalogIndexJson);
            var currentPageFile = _context.Source.Get(currentPageUri);

            var pageCommits = new List <JObject>();

            var currentPageJson = await currentPageFile.GetJsonOrNull(_context.Log, _context.Token);

            if (currentPageJson != null)
            {
                pageCommits = JsonUtility.GetItems(currentPageJson);
            }
            else
            {
                var newPageEntry = CatalogUtility.CreateCatalogIndexPageEntry(currentPageUri, _context.CommitId);

                var pageArray = (JArray)catalogIndexJson["items"];
                pageArray.Add(newPageEntry);

                // Update pages
                pages = JsonUtility.GetItems(catalogIndexJson);
            }

            pageCommits.Add(pageCommit);

            // Write catalog page
            var pageJson = await CatalogUtility.CreateCatalogPageAsync(RootIndexFile.EntityUri, currentPageUri, pageCommits, _context.CommitId);

            await currentPageFile.Write(pageJson, _context.Log, _context.Token);

            // Update index
            CatalogUtility.UpdatePageIndex(catalogIndexJson, pageJson, _context.CommitId);

            catalogIndexJson[lastUpdatedPropertyName] = DateTimeOffset.UtcNow.GetDateString();

            catalogIndexJson = JsonLDTokenComparer.Format(catalogIndexJson);

            await RootIndexFile.Write(catalogIndexJson, _context.Log, _context.Token);
        }
Beispiel #4
0
        /// <summary>
        /// Uri of the latest index page.
        /// </summary>
        public Uri GetCurrentPage(JObject indexJson)
        {
            var entries = JsonUtility.GetItems(indexJson);
            var nextId  = entries.Count;

            var latest = entries.OrderByDescending(GetCommitTime).FirstOrDefault();

            if (latest != null)
            {
                if (latest["count"].ToObject <int>() < _context.SourceSettings.CatalogPageSize)
                {
                    return(JsonUtility.GetIdUri(latest));
                }
            }

            // next page
            return(UriUtility.GetPath(CatalogBaseURI, $"page.{nextId}.json"));
        }