public void CanGetFilesByIndex() { var index = new Index("index 1"); var sources = new List<IndexSource>(); var allFileNames = new HashSet<string>(); var filesInformation = new List<FileInformation>(); var resultFileNames = new HashSet<string>(); this.mockIndexChecker .Setup(x => x.IsIndexAcceptable(index)) .Returns(true); this.mockSourceStorage .Setup(x => x.GetAllSources()) .Returns(sources); this.mockFileSearcher .Setup(x => x.GetAllFileNames(sources)) .Returns(allFileNames); this.mockFileInformationExtractor .Setup(x => x.GetFileInformation(allFileNames)) .Returns(filesInformation); this.mockIndexedCache .Setup(x => x.RebuildCache(index, filesInformation)) .Returns(resultFileNames); this.service.GetFilesByIndex(index); }
/// <summary> /// Вернуть актуальный список файлов, удовлетворяющих индексу. /// </summary> /// <param name="index">Индекс, по которому ищем.</param> /// <param name="filesInformation">Информация о файлах.</param> /// <returns>Актуальный список файлов, удовлетворяющих индексу.</returns> public ISet<string> RebuildCache( Index index, IEnumerable<FileInformation> filesInformation) { this.indexesSyncObjects.TryAdd(index, new object()); lock (this.indexesSyncObjects[index]) { return this.cache.RebuildCache(index, filesInformation); } }
/// <summary> /// Подходит ли строка под индекс. /// </summary> /// <param name="index">Индекс, по которому ищем. Значение не null и прошло IIndexChecker.</param> /// <param name="row">Строка из файла.</param> /// <returns>true если подходит.</returns> public bool IsRowAcceptable(Index index, string row) { if (string.IsNullOrWhiteSpace(row)) { return false; } if (ReferenceEquals(index, null)) { // добавить логирование. такого не должно быть никогда. return false; } return row.Contains(index.Word); }
/// <summary> /// Проверка того, подходит ли нам индекс. /// </summary> /// <param name="index">Индес для проверки.</param> /// <returns>true - если индекс подходит.</returns> public bool IsIndexAcceptable(Index index) { if (ReferenceEquals(index, null)) { return false; } if (index.GetType() != typeof (Index)) { return false; } if (string.IsNullOrWhiteSpace(index.Word) || index.Word.Contains(" ")) { return false; } return true; }
public void ChangeCachesIfSomeFilesWereChangedOrAddedOrDeleted() { var index = new Index("index 1"); var filesInformation = new List<FileInformation>(); var filesState = new List<FileState>(); DateTime now = DateTime.UtcNow; filesState.Add(new FileState(new FileInformation("file1", now), FileChangingState.Added)); filesState.Add(new FileState(new FileInformation("file2", now), FileChangingState.Changed)); filesState.Add(new FileState(new FileInformation("file3", now), FileChangingState.Deleted)); var fileNames = new HashSet<string>(); this.mockFilesInformationByIndexCache .Setup(x => x.CheckLastChanged(filesInformation)) .Returns(filesState); this.mockFilesForIndexCache .Setup(x => x.GetAcceptableFiles()) .Returns(fileNames); this.mockFileDataCache .Setup(x => x.GetActualData(It.IsAny<FileInformation>())) .Returns(new FileInternalData(string.Empty, new[] {"fake row"})); this.mockRowChecker .Setup(x => x.IsRowAcceptable(It.IsAny<Index>(), It.IsAny<string>())) .Returns(true); ISet<string> resultedFileNames = this.cache.RebuildCache(index, filesInformation); Assert.AreSame(fileNames, resultedFileNames); this.mockFilesInformationByIndexCacheFactory.Verify(x => x.GetIFilesInformationByIndexCache(), Times.Once()); this.mockFilesForIndexCacheFactory.Verify(x => x.GetFilesForIndexCache(), Times.Once()); this.mockFilesForIndexCache.Verify(x => x.AddFile(It.IsAny<string>(), It.IsAny<bool>()), Times.Exactly(2)); this.mockFilesForIndexCache.Verify(x => x.DeleteFile(It.IsAny<string>()), Times.Exactly(2)); this.mockRowChecker.Verify(x => x.IsRowAcceptable(It.IsAny<Index>(), It.IsAny<string>()), Times.Exactly(2)); }
/// <summary> /// Вернуть актуальный список файлов, удовлетворяющих индексу. /// </summary> /// <param name="index">Индекс, по которому ищем.</param> /// <param name="filesInformation">Информация о файлах.</param> /// <returns>Актуальный список файлов, удовлетворяющих индексу.</returns> public ISet<string> RebuildCache( Index index, IEnumerable<FileInformation> filesInformation) { if (!this.filesInfoByIndexes.ContainsKey(index)) { this.filesInfoByIndexes[index] = this.filesInformationByIndexCacheFactory.GetIFilesInformationByIndexCache(); } if (!this.filesForByIndexes.ContainsKey(index)) { this.filesForByIndexes[index] = this.filesForIndexCacheFactory.GetFilesForIndexCache(); } IEnumerable<FileState> filesState = this.CheckFilesChanged(index, filesInformation); foreach (FileState fileState in filesState) { switch (fileState.FileChangingState) { case FileChangingState.Added: this.AddFile(index, fileState); break; case FileChangingState.Changed: this.DeleteFile(index, fileState.FileInformation.FileName); this.AddFile(index, fileState); break; case FileChangingState.Deleted: this.DeleteFile(index, fileState.FileInformation.FileName); break; default: throw new ArgumentException("Unknown file changing state"); } } return this.filesForByIndexes[index].GetAcceptableFiles(); }
/// <summary> /// Получить имена файлов, удовлетворяющих индексу. /// </summary> /// <param name="index">Индекс, по которому ищем файлы.</param> /// <returns>Полные пути к файлам, удовлетворяющих индексу.</returns> /// <exception cref="ArgumentException">Если index не валиден.</exception> public ISet<string> GetFilesByIndex(Index index) { if (ReferenceEquals(index, null) || !this.indexChecker.IsIndexAcceptable(index)) { throw new ArgumentException("Index isn't acceptable for this service implementation"); } IEnumerable<IndexSource> allSources = this.sourceStorage.GetAllSources(); ISet<string> fileNames = this.fileSearcher.GetAllFileNames(allSources); IEnumerable<FileInformation> currentFilesInformation = this.fileInformationExtractor.GetFileInformation(fileNames); return this.indexedCache.RebuildCache(index, currentFilesInformation); }
/// <summary> /// Подходит ли файл под индекс. /// </summary> /// <param name="index">Индекс, для которого анализируем.</param> /// <param name="data">Данные файла.</param> /// <returns>true если подходит.</returns> private bool IsFileAcceptableForIndex(Index index, FileInternalData data) { return data.Rows.Any(row => this.rowChecker.IsRowAcceptable(index, row)); }
/// <summary> /// Удаляем файл из индекса. /// </summary> /// <param name="index">Индекс, для которого удаляем.</param> /// <param name="fileName">Имя файла.</param> private void DeleteFile(Index index, string fileName) { this.filesForByIndexes[index].DeleteFile(fileName); }
/// <summary> /// Проверить последние изменения файлов. /// </summary> /// <param name="index">Индекс, для которого ищем.</param> /// <param name="filesInformation">Текущая информация о файлах.</param> /// <returns>Изменение файлов.</returns> private IEnumerable<FileState> CheckFilesChanged(Index index, IEnumerable<FileInformation> filesInformation) { return this.filesInfoByIndexes[index].CheckLastChanged(filesInformation); }
/// <summary> /// Добавить файл к индексу. /// </summary> /// <param name="index">Индекс, для которого добавляем.</param> /// <param name="fileState">Данные о файле.</param> private void AddFile(Index index, FileState fileState) { FileInternalData data = this.fileDataCache.GetActualData(fileState.FileInformation); bool isFileAcceptable = this.IsFileAcceptableForIndex(index, data); this.filesForByIndexes[index].AddFile( fileState.FileInformation.FileName, isFileAcceptable); }
public void DoesNotAddOrDeleteFilesIfNothingChanged() { var index = new Index("index 1"); var filesInformation = new List<FileInformation>(); var filesState = new List<FileState>(); var fileNames = new HashSet<string>(); this.mockFilesInformationByIndexCache .Setup(x => x.CheckLastChanged(filesInformation)) .Returns(filesState); this.mockFilesForIndexCache .Setup(x => x.GetAcceptableFiles()) .Returns(fileNames); ISet<string> resultedFileNames = this.cache.RebuildCache(index, filesInformation); Assert.AreSame(fileNames, resultedFileNames); this.mockFilesInformationByIndexCacheFactory.Verify(x => x.GetIFilesInformationByIndexCache(), Times.Once()); this.mockFilesForIndexCacheFactory.Verify(x => x.GetFilesForIndexCache(), Times.Once()); this.mockFilesForIndexCache.Verify(x => x.AddFile(It.IsAny<string>(), It.IsAny<bool>()), Times.Never()); this.mockFilesForIndexCache.Verify(x => x.DeleteFile(It.IsAny<string>()), Times.Never()); }
public void CanThrowOnBadIndex() { var index = new Index("I'm a bad index"); this.mockIndexChecker .Setup(x => x.IsIndexAcceptable(index)) .Returns(false); this.service.GetFilesByIndex(index); }