|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using SmartStore.Collections; |
| 6 | + |
| 7 | +namespace SmartStore.Core.Search |
| 8 | +{ |
| 9 | + public class IndexDocument : IIndexDocument |
| 10 | + { |
| 11 | + private readonly Multimap<string, IndexField> _fields; |
| 12 | + |
| 13 | + public IndexDocument(int id) |
| 14 | + { |
| 15 | + _fields = new Multimap<string, IndexField>(StringComparer.OrdinalIgnoreCase); |
| 16 | + _fields.Add("id", new IndexField("id", id).Store()); |
| 17 | + } |
| 18 | + |
| 19 | + public int Id |
| 20 | + { |
| 21 | + get |
| 22 | + { |
| 23 | + return (int)_fields["id"].FirstOrDefault().Value; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + public int Count => _fields.TotalValueCount; |
| 28 | + |
| 29 | + public void Add(IndexField field) |
| 30 | + { |
| 31 | + if (field.Name.IsCaseInsensitiveEqual("id") && _fields.ContainsKey("id")) |
| 32 | + { |
| 33 | + // special treatment for id field: allow only one! |
| 34 | + _fields.RemoveAll("id"); |
| 35 | + } |
| 36 | + _fields.Add(field.Name, field); |
| 37 | + } |
| 38 | + |
| 39 | + public int Remove(string name) |
| 40 | + { |
| 41 | + if (_fields.ContainsKey(name)) |
| 42 | + { |
| 43 | + var num = _fields[name].Count; |
| 44 | + _fields.RemoveAll(name); |
| 45 | + return num; |
| 46 | + } |
| 47 | + |
| 48 | + return 0; |
| 49 | + } |
| 50 | + |
| 51 | + public bool Contains(string name) |
| 52 | + { |
| 53 | + return _fields.ContainsKey(name); |
| 54 | + } |
| 55 | + |
| 56 | + public IEnumerable<IndexField> this[string name] |
| 57 | + { |
| 58 | + get |
| 59 | + { |
| 60 | + if (_fields.ContainsKey(name)) |
| 61 | + { |
| 62 | + return _fields[name]; |
| 63 | + } |
| 64 | + |
| 65 | + return Enumerable.Empty<IndexField>(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + public IEnumerator<IndexField> GetEnumerator() |
| 71 | + { |
| 72 | + return _fields.SelectMany(x => x.Value).GetEnumerator(); |
| 73 | + } |
| 74 | + |
| 75 | + IEnumerator IEnumerable.GetEnumerator() |
| 76 | + { |
| 77 | + return GetEnumerator(); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments