Example #1
0
        /// <summary>
        /// Get all the ids for the parts of this word.
        /// If the part already exists, get the id, otherwise we will insert and then get the id.
        /// </summary>
        /// <param name="word"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        private async Task <IList <long> > GetOrInsertParts(IWord word, CancellationToken token)
        {
            // get the parts.
            var parts = word.Parts;

            // if we have not words... then move on.
            if (!parts.Any())
            {
                return(new List <long>());
            }

            Contract.Assert(_partsHelper != null);
            Contract.Assert(_partsSearchHelper != null);

            // try and insert the values and get the ids.
            // the return values are string+id
            // if the id is -1, then we had an error
            var partValuesAndIds = await _partsHelper.InsertAndGetAsync(parts.ToList(), token).ConfigureAwait(false);

            // then add it to the helpers.
            await _partsSearchHelper.InsertAsync(partValuesAndIds.Where(v => v.Id != -1).ToList(), token).ConfigureAwait(false);

            // get all the non -1 ids
            var partIds = partValuesAndIds.Where(v => v.Id != -1).Select(p => p.Id).ToList();

            // and log all the ids that did not work.
            foreach (var partValue in partValuesAndIds.Where(v => v.Id == -1).Select(p => p.Value))
            {
                _logger.Error($"There was an issue adding part: {partValue} to persister");
            }

            // return all the ids that we either
            // added or that already exist.
            return(partIds);
        }