public async Task <bool> ReindexInstanceAsync(
            IReadOnlyCollection <ExtendedQueryTagStoreEntry> entries,
            VersionedInstanceIdentifier versionedInstanceId,
            CancellationToken cancellationToken)
        {
            EnsureArg.IsNotNull(entries, nameof(entries));
            EnsureArg.IsNotNull(versionedInstanceId, nameof(versionedInstanceId));

            DicomDataset dataset;

            try
            {
                dataset = await _metadataStore.GetInstanceMetadataAsync(versionedInstanceId, cancellationToken);
            }
            catch (ItemNotFoundException)
            {
                _logger.LogWarning("Could not find metadata for instance with {Identifier}", versionedInstanceId);
                return(false);
            }

            // Only reindex on valid query tags
            IReadOnlyCollection <QueryTag> validQueryTags = await _dicomDatasetReindexValidator.ValidateAsync(
                dataset,
                versionedInstanceId.Version,
                entries.Select(x => new QueryTag(x)).ToList(),
                cancellationToken);

            await _indexDataStore.ReindexInstanceAsync(dataset, versionedInstanceId.Version, validQueryTags, cancellationToken);

            return(true);
        }
        public async Task GivenStudyTag_WhenReindexWithNewInstance_ThenTagValueShouldBeUpdated()
        {
            DicomTag tag       = DicomTag.DeviceSerialNumber;
            string   tagValue1 = "test1";
            string   tagValue2 = "test2";
            string   tagValue3 = "test3";

            string studyUid = TestUidGenerator.Generate();

            DicomDataset dataset1 = Samples.CreateRandomInstanceDataset(studyUid);

            dataset1.Add(tag, tagValue1);
            DicomDataset dataset2 = Samples.CreateRandomInstanceDataset(studyUid);

            dataset2.Add(tag, tagValue2);
            DicomDataset dataset3 = Samples.CreateRandomInstanceDataset(studyUid);

            dataset3.Add(tag, tagValue3);

            Instance instance1 = await CreateInstanceIndexAsync(dataset1);

            Instance instance2 = await CreateInstanceIndexAsync(dataset2);

            Instance instance3 = await CreateInstanceIndexAsync(dataset3);

            var tagStoreEntry = await AddExtendedQueryTagAsync(tag.BuildAddExtendedQueryTagEntry(level: QueryTagLevel.Study));

            QueryTag queryTag = new QueryTag(tagStoreEntry);

            // Simulate re-indexing, which may re-index an instance which may re-index
            // the instances for a particular study or series out-of-order
            await _indexDataStore.ReindexInstanceAsync(dataset2, instance2.Watermark, new[] { queryTag });

            ExtendedQueryTagDataRow row = (await _extendedQueryTagStoreTestHelper.GetExtendedQueryTagDataAsync(ExtendedQueryTagDataType.StringData, tagStoreEntry.Key, instance1.StudyKey, null, null)).Single();

            Assert.Equal(tagValue2, row.TagValue); // Added

            await _indexDataStore.ReindexInstanceAsync(dataset3, instance3.Watermark, new[] { queryTag });

            row = (await _extendedQueryTagStoreTestHelper.GetExtendedQueryTagDataAsync(ExtendedQueryTagDataType.StringData, tagStoreEntry.Key, instance1.StudyKey, null, null)).Single();
            Assert.Equal(tagValue3, row.TagValue); // Overwrite

            await _indexDataStore.ReindexInstanceAsync(dataset1, instance1.Watermark, new[] { queryTag });

            row = (await _extendedQueryTagStoreTestHelper.GetExtendedQueryTagDataAsync(ExtendedQueryTagDataType.StringData, tagStoreEntry.Key, instance1.StudyKey, null, null)).Single();
            Assert.Equal(tagValue3, row.TagValue); // Do not overwrite
        }