Example #1
0
        public override async Task <bool> StoreSnapshotAsync(Snapshot snapshot, CancellationToken cancellationToken = default)
        {
            _ = snapshot ?? throw new ArgumentNullException(nameof(snapshot));

            var indexName = _indexPrefix + snapshot.Name;

            // Start building a bulk request
            var operations = new List <IBulkOperation>();

            // Store Symbols First
            foreach (var symbol in snapshot.SymbolDefinitions)
            {
                _logger.LogTrace("Queuing index of SymbolDefinition: {SymbolPath}.", symbol.Path);
                operations.Add(new BulkIndexOperation <SymbolDefinitionSurrogate>(new SymbolDefinitionSurrogate(symbol)));
            }

            foreach (var symbol in snapshot.SymbolReferences)
            {
                _logger.LogTrace("Queuing index of SymbolReference: {SymbolPath}.", symbol.Path);
                operations.Add(new BulkIndexOperation <SymbolReferenceSurrogate>(new SymbolReferenceSurrogate(symbol)));
            }

            // Store Projects (and their documents)
            foreach (var project in snapshot.Projects)
            {
                _logger.LogTrace("Queuing index of Project: {Name}.", project.Name);
                operations.Add(new BulkIndexOperation <ProjectSurrogate>(new ProjectSurrogate(project)));

                foreach (var document in project.Documents)
                {
                    _logger.LogTrace("Queuing index of Document: {Name}.", document.Name);
                    operations.Add(new BulkIndexOperation <DocumentSurrogate>(new DocumentSurrogate(document)));
                }
            }

            // Store the snapshot metadata itself
            _logger.LogTrace("Queuing index of Snapshot: {Name}.", snapshot.Name);
            operations.Add(new BulkIndexOperation <SnapshotSurrogate>(new SnapshotSurrogate(snapshot)));

            // Send the request
            var request = new BulkRequest(indexName)
            {
                Operations = operations
            };

            _logger.LogInformation("Writing {DocumentCount} documents to Elasticsearch.", request.Operations.Count);
            var response = await _client.BulkAsync(request, cancellationToken);

            if (response.IsValid)
            {
                _logger.LogInformation("Successfully wrote {DocumentCount} documents to Elasticsearch.", request.Operations.Count);
                return(true);
            }
            else
            {
                foreach (var errorItem in response.ItemsWithErrors)
                {
                    _logger.LogError("Document '{Id}' failed to write: {Error}.", errorItem.Id, errorItem.Error);
                }
                return(false);
            }
        }
Example #2
0
 public SnapshotSurrogate(Snapshot snapshot)
 {
     Name = snapshot.Name;
 }