Beispiel #1
0
 /// <inheritdoc />
 public async Task <IList <long> > AddOrGetWordsAsync(interfaces.IO.IWords words, CancellationToken token)
 {
     using (_counterAddOrUpdate.Start())
     {
         return(await InsertWordsAsync(words, token).ConfigureAwait(false));
     }
 }
Beispiel #2
0
        /// <summary>
        /// Given a list of words, re-create the ones that we need to insert.
        /// </summary>
        /// <param name="words"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        private async Task <IList <long> > InsertWordsAsync(
            interfaces.IO.IWords words,
            CancellationToken token)
        {
            // if we have nothing to do... we are done.
            if (!words.Any())
            {
                return(new List <long>());
            }

            // the ids of the words we just added
            var ids = new List <long>(words.Count);

            try
            {
                foreach (var word in words)
                {
                    // get out if needed.
                    token.ThrowIfCancellationRequested();

                    // if the word is not valid, there isn't much we can do.
                    if (!IsValidWord(word))
                    {
                        continue;
                    }

                    // the word we want to insert.
                    var wordId = await InsertWordAsync(word, token).ConfigureAwait(false);

                    if (-1 == wordId)
                    {
                        // we log errors in the insert function
                        continue;
                    }

                    // we added this id.
                    ids.Add(wordId);
                }

                // all done, return whatever we did.
                return(ids);
            }
            catch (OperationCanceledException)
            {
                _logger.Warning("Received cancellation request - Insert multiple words");
                throw;
            }
            catch (Exception ex)
            {
                _logger.Exception(ex);
                throw;
            }
        }