Beispiel #1
0
        /// <summary>
        /// Merges books with existing ones in the database, and returns remaining books that were not merged.
        /// </summary>
        async Task <DbBook[]> MergeAsync(DbBook[] books, CancellationToken cancellationToken = default)
        {
            var list = new List <DbBook>(books.Length);

            foreach (var book in books)
            {
                if ((book.PrimaryName != null || book.EnglishName != null) &&
                    (book.TagsArtist?.Length > 0 || book.TagsCircle?.Length > 0))
                {
                    var result = await _client.SearchEntriesAsync(new SimilarQuery(book), cancellationToken);

                    var entry = result.Items?.FirstOrDefault();

                    do
                    {
                        if (entry?.Value == null)
                        {
                            goto add;
                        }

                        entry.Value.MergeFrom(book);
                    }while (!await entry.TryUpdateAsync(cancellationToken));

                    _count.Labels("dynamic").Inc();

                    if (_logger.IsEnabled(LogLevel.Information))
                    {
                        _logger.LogInformation($"Merged book {FormatBook(book)} into similar book {FormatBook(entry.Value)}.");
                    }

                    continue;
                }

add:
                list.Add(book);

                _count.Labels("skipped").Inc();

                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.LogInformation($"Merge skipping for book {FormatBook(book)}.");
                }
            }

            return(list.ToArray());
        }
Beispiel #2
0
        async Task <IDbEntry <DbUser> > GetByIdAsync(ulong id, CancellationToken cancellationToken = default)
        {
            var result = await _client.SearchEntriesAsync(new UserQuery(id), cancellationToken);

            return(result.Items.Length == 0 ? null : result.Items[0]);
        }