IEnumerable <L> ExecuteQuery <L>(IndexQueryArgs <K> args, Func <K, IKeyNode, L> selector) { var query = from i in _tree.Enum(args) select selector(i.Key, i); if (args.Skip != null) { query = query.Skip(args.Skip.GetValueOrDefault()); } if (args.Take != null) { query = query.Take(args.Take.GetValueOrDefault()); } return(query); }
IEnumerable <L> ExecuteQuery <L>(IndexQueryArgs <K> args, Func <K, IKeyNode, L> selector) { var index = _table.KeyIndex; var query = from i in _tree.Enum(args) from k in i.Keys select selector(i.Key, k); if (args.Skip != null) { query = query.Skip(args.Skip.Value); } if (args.Take != null) { query = query.Take(args.Take.Value); } return(query); }
/* * class TreeScanner * { * TKey _min, _max; * bool _minInclusive, _maxInclusive; * Action<TNode> _callback; * IComparer<TKey> _comparer; * * public TreeScanner(Action<TNode> callback, IComparer<TKey> comparer) * { * _callback = callback; * _comparer = comparer; * } * * public void ScanAll(TNode node) * { * while (node != null) * { * ScanAll(node.Left); * _callback(node); * node = node.Right; * } * } * * public void ScanMin(TNode node, TKey min, bool inclusive) * { * _min = min; * _minInclusive = inclusive; * EnumMin(node); * } * * public void ScanMax(TNode node, TKey max, bool inclusive) * { * _max = max; * _maxInclusive = inclusive; * EnumMax(node); * } * * public void ScanMinMax(TNode node, TKey min, bool minInclusive, TKey max, bool maxInclusive) * { * _min = min; * _minInclusive = minInclusive; * * _max = max; * _maxInclusive = maxInclusive; * * EnumMinMax(node); * } * * void EnumMax(TNode node) * { * while (node != null) * { * var cmp = _comparer.Compare(node.Key, _max); * * if (cmp > 0) * node = node.Left; * else * { * ScanAll(node.Left); * * if (cmp < 0) * { * _callback(node); * EnumMax(node.Right); * } * else * { * if (_maxInclusive) * _callback(node); * } * * break; * } * } * } * * void EnumMin(TNode node) * { * while (node != null) * { * var cmp = _comparer.Compare(node.Key, _min); * if (cmp < 0) * node = node.Right; * else * { * if (cmp > 0) * { * EnumMin(node.Left); * _callback(node); * } * else * { * if (_minInclusive) * _callback(node); * } * * ScanAll(node.Right); * * break; * } * } * } * * void EnumMinMax(TNode node) * { * while (node != null) * { * var lowCmp = _comparer.Compare(node.Key, _min); * * if (lowCmp < 0) * node = node.Right; * else * { * if (lowCmp == 0) * { * if (_minInclusive) * { * var highCmp = _comparer.Compare(node.Key, _max); * if (highCmp < 0) * { * _callback(node); * EnumMax(node.Right); * } * else if (highCmp == 0 && _maxInclusive) * _callback(node); * } * else * EnumMax(node.Right); * } * else * { * var highCmp = _comparer.Compare(node.Key, _max); * * if (highCmp > 0) * EnumMinMax(node.Left); * else if (highCmp < 0) * { * EnumMin(node.Left); * _callback(node); * EnumMax(node.Right); * } * else * { * EnumMin(node.Left); * * if (_maxInclusive) * _callback(node); * } * } * break; * } * } * } * } */ #endregion public IEnumerable <TNode> Enum(IndexQueryArgs <TKey> args) { if (args.MinInclusive == null) { if (args.MaxInclusive == null) { return(WrapFilter(args, this)); } return(WrapFilter(args, EnumMax(args.Max, args.MaxInclusive.Value))); } if (args.MaxInclusive == null) { return(WrapFilter(args, EnumMin(args.Min, args.MinInclusive.Value))); } if (Comparer.Compare(args.Min, args.Max) <= 0) { return(WrapFilter(args, EnumMinMax(args.Min, args.MinInclusive.Value, args.Max, args.MaxInclusive.Value))); } return(Enumerable.Empty <TNode>()); }
static IEnumerable <TNode> WrapFilter(IndexQueryArgs <TKey> args, IEnumerable <TNode> source) { var filter = args.Filter; return(filter == null ? source : source.Where(i => filter(i.Key))); }
public List <T> ExecuteToList(IndexQueryArgs <K> args) { using (var scope = _table.ReadScope()) return(ExecuteQuery(args, (k, pk) => _table.LoadByKeyNode(scope, pk)).ToList()); }
public List <L> ExecuteToList <L>(IndexQueryArgs <K> args, Func <K, IKeyNode, L> selector) { using (_table.ReadScope()) return(ExecuteQuery(args, selector).ToList()); }
public int ExecuteCount(IndexQueryArgs <K> args) { using (_table.ReadScope()) return(ExecuteQuery(args, (k, pk) => pk).Count()); }