private List <string> GetValues(SnTerm field) { var fieldValues = new List <string>(); if (field.Name == IndexFieldName.AllText) //TODO: it would be better to use an analyzer { var words = field.StringValue .Split(" \t\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) .Distinct() .Select(s => s.ToLowerInvariant()); fieldValues.AddRange(words); } else { if (field.Type == IndexValueType.StringArray) { fieldValues.AddRange(field.StringArrayValue.Select(s => s.ToLowerInvariant())); } else { fieldValues.Add(IndexValueToString(field)); } } return(fieldValues); }
public void Delete(SnTerm term) { var fieldName = term.Name; // get category by term name if (!IndexData.TryGetValue(fieldName, out var existingFieldData)) { return; } var deletableVersionIds = new List <int>(); var fieldValues = GetValues(term); foreach (var fieldValue in fieldValues) { // get version id set by term value if (!existingFieldData.TryGetValue(fieldValue, out var versionIds)) { continue; } deletableVersionIds.AddRange(versionIds); } // delete all version ids in any depth foreach (var item in IndexData) { foreach (var subItem in item.Value) { var versionIdList = subItem.Value; foreach (var versionId in deletableVersionIds) //TODO: Thread safety. See the comment above. { versionIdList.Remove(versionId); } } } // delete stored data by all version ids foreach (var deletableStoredData in StoredData.Where(s => deletableVersionIds.Contains(s.Item1)).ToArray()) { StoredData.Remove(deletableStoredData); } }
public IEnumerable <Tuple <int, List <IndexField> > > GetStoredFieldsByTerm(SnTerm term) { var fieldName = term.Name; var fieldValues = GetValues(term); if (fieldValues.Count == 0) { return(null); } if (fieldValues.Count > 1) { throw new NotImplementedException(); } var fieldValue = fieldValues[0]; // get category by term name if (!IndexData.TryGetValue(fieldName, out var existingFieldData)) { return(null); } // get version id set by term value if (!existingFieldData.TryGetValue(fieldValue, out var versionIds)) { return(null); } // return with all stored data by version ids without distinct var result = StoredData.Where(d => versionIds.Contains(d.Item1)).ToArray(); if (result.Length == 0) { result = null; } return(result); }
public void Update(SnTerm term, IndexDocument document) { Delete(term); AddDocument(document); }
public void Delete(SnTerm term) { var fieldName = term.Name; // get category by term name if (!IndexData.TryGetValue(fieldName, out var existingFieldData)) { return; } var deletableVersionIds = new List <int>(); var fieldValues = GetValues(term); foreach (var fieldValue in fieldValues) { // get version id set by term value if (!existingFieldData.TryGetValue(fieldValue, out var versionIds)) { continue; } deletableVersionIds.AddRange(versionIds); } // delete all version ids in any depth var indexesToDelete = new List <string>(); foreach (var item in IndexData) { var keysToDelete = new List <string>(); foreach (var subItem in item.Value) { var versionIdList = subItem.Value; foreach (var versionId in deletableVersionIds) //TODO: Thread safety. See the comment above. { Retrier.Retry(3, 1, typeof(ArgumentOutOfRangeException), () => { versionIdList.Remove(versionId); }); } if (versionIdList.Count == 0) { keysToDelete.Add(subItem.Key); } } foreach (var keyToDelete in keysToDelete) { item.Value.Remove(keyToDelete); } if (item.Value.Count == 0) { indexesToDelete.Add(item.Key); } } foreach (var indexToDelete in indexesToDelete) { IndexData.Remove(indexToDelete); } // delete stored data by all version ids foreach (var deletableStoredData in StoredData.Where(s => deletableVersionIds.Contains(s.Item1)).ToArray()) { StoredData.Remove(deletableStoredData); } }
private async Task DeleteDocAsync(string lastId, CancellationToken cancellationToken) { var term = new SnTerm("TestDocId", lastId); var terms = new[] { term }; await _engine.WriteIndexAsync(terms, null, null, cancellationToken); }