public List <string> FindMatchesInPosts(string searchText, Action <SearchResult <string> > emitResult, CancellationToken cancellationToken) { var fileStream = new FileStream(_BulkDataFiles.PostsPath, FileMode.Open, FileAccess.Read); var watcher = new StreamWatcher(fileStream); var reader = XmlReader.Create(watcher); var resultList = new List <string>(); var recordsSearchedCount = 0; while (reader.ReadToFollowing("row")) { if (cancellationToken.IsCancellationRequested) { var result = new SearchResult <string>(); result.RecordsSearchedCount = recordsSearchedCount; result.UpdateStatus = SearchUpdateStatusEnum.Cancelled; emitResult(result); return(resultList); } if (fileStream.Position < 32000) { continue; } var rowAttributes = new Dictionary <string, string>(); while (reader.MoveToNextAttribute()) { rowAttributes.Add(reader.Name, reader.Value); } recordsSearchedCount++; if (rowAttributes.ContainsKey("Body") && rowAttributes["Body"].Contains(searchText)) { resultList.Add(rowAttributes["Body"]); var result = new SearchResult <string>(); result.RecordsSearchedCount = recordsSearchedCount; result.Result = rowAttributes["Body"]; result.UpdateStatus = SearchUpdateStatusEnum.FoundResult; emitResult(result); } if (recordsSearchedCount % _searchUpdateInterval == 0) { var result = new SearchResult <string>(); result.RecordsSearchedCount = recordsSearchedCount; result.UpdateStatus = SearchUpdateStatusEnum.Searching; emitResult(result); } } var completeResult = new SearchResult <string>(); completeResult.RecordsSearchedCount = recordsSearchedCount; completeResult.UpdateStatus = SearchUpdateStatusEnum.Complete; emitResult(completeResult); return(resultList); }
public Dictionary <string, string> GetPostByID(int id, int startPosition, int maxAttempts) { var fileStream = new FileStream(_BulkDataFiles.PostsPath, FileMode.Open, FileAccess.Read); var watcher = new StreamWatcher(fileStream); var byteStartPosition = (long)8192 * startPosition; fileStream.Position = byteStartPosition; var reader = XmlReader.Create(watcher, new XmlReaderSettings() { ValidationType = ValidationType.None, ConformanceLevel = ConformanceLevel.Fragment }); var attemptCount = 0; while (reader.ReadToFollowing("row")) { var rowAttributes = new Dictionary <string, string>(); while (reader.MoveToNextAttribute()) { rowAttributes.Add(reader.Name, reader.Value); } if (rowAttributes.ContainsKey("Id") && rowAttributes["Id"] == id.ToString()) { var position = fileStream.Position / 8192; return(rowAttributes); } else { attemptCount++; if (attemptCount >= maxAttempts) { return(null); } } } return(null); }