static IEnumerable <Key> Equal <Key>(BTreeBase <Key, Key> sourceCollection, BinaryExpression binExp) #if WINDOWS_PHONE where Key : new() #endif { SessionBase session = sourceCollection.Session; CompareByField <Key> comparer = sourceCollection.Comparer as CompareByField <Key>; Expression leftSide = binExp.Left; Expression rightSide = binExp.Right; object rightValue = GetRightValue(leftSide, rightSide); if (leftSide.NodeType == ExpressionType.Parameter) { Key key = (Key)rightValue; if (key != null) { BTreeSetIterator <Key> itr = sourceCollection.Iterator(); itr.GoTo(key); while (sourceCollection.Comparer.Compare(itr.Current(), key) == 0) { yield return(itr.Next()); } } } else { if (comparer != null) { MemberExpression returnedEx = null; #if WINDOWS_PHONE || WINDOWS_UWP Key key = (Key)Activator.CreateInstance(typeof(Key)); #else Key key = (Key)FormatterServices.GetUninitializedObject(typeof(Key)); #endif if (comparer.FieldsToCompare == null) { comparer.SetupFieldsToCompare(); } DataMember dataMember = comparer.FieldsToCompare[0]; if (rightValue != null && HasIndexablePropertyOnLeft <Key>(leftSide, sourceCollection, dataMember, out returnedEx)) { MemberExpression propExp = (MemberExpression)returnedEx; MemberInfo property = propExp.Member; dataMember.SetMemberValueWithPossibleConvert(key, rightValue); BTreeSetIterator <Key> itr = sourceCollection.Iterator(); itr.GoTo(key); Key current = itr.Current(); while (current != null && comparer.CompareField(dataMember, key, current, 0) == 0) { yield return(current); current = itr.Next(); } } } } }
static bool GreaterThanOrEqualUseIndex <Key>(BTreeBase <Key, Key> sourceCollection, BinaryExpression binExp) #if WINDOWS_PHONE where Key : new() #endif { SessionBase session = sourceCollection.Session; CompareByField <Key> comparer = sourceCollection.Comparer as CompareByField <Key>; Expression leftSide = binExp.Left; Expression rightSide = binExp.Right; object rightValue = GetRightValue(leftSide, rightSide); if (leftSide.NodeType == ExpressionType.Parameter) { Key key = (Key)rightValue; if (key != null) { if (session.TraceIndexUsage) { Trace.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " Index used with " + sourceCollection.ToString()); } return(true); } } else { if (comparer != null) { //if we were able to create a hash from the right side (likely) MemberExpression returnedEx = null; if (comparer.FieldsToCompare == null) { comparer.SetupFieldsToCompare(); } DataMember dataMember = comparer.FieldsToCompare[0]; if (rightValue != null && HasIndexablePropertyOnLeft <Key>(leftSide, sourceCollection, dataMember, out returnedEx)) { if (session.TraceIndexUsage) { Trace.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " Index used with " + sourceCollection.ToString()); } return(true); } else if (leftSide.NodeType == ExpressionType.Call) { // don't know yet how to handle TODO /*MethodCallExpression expression = leftSide as MethodCallExpression; * Trace.Out.WriteLine("Method: " + expression.Method.Name); * Trace.Out.WriteLine("Args: "); * foreach (var exp in expression.Arguments) * sourceCollection where */ } } } return(false); }
static bool GreaterThanUseIndex <Key>(BTreeBase <Key, Key> sourceCollection, BinaryExpression binExp) #if WINDOWS_PHONE where Key : new() #endif { SessionBase session = sourceCollection.GetSession(); CompareByField <Key> comparer = sourceCollection.Comparer as CompareByField <Key>; Expression leftSide = binExp.Left; Expression rightSide = binExp.Right; object rightValue = GetRightValue(leftSide, rightSide); if (leftSide.NodeType == ExpressionType.Parameter) { Key key = (Key)rightValue; if (key != null) { if (session.TraceIndexUsage) { Trace.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " Index used with " + sourceCollection.ToString()); } return(true); } } else { if (comparer != null) { //if we were able to create a hash from the right side (likely) MemberExpression returnedEx = null; if (comparer.FieldsToCompare == null) { comparer.SetupFieldsToCompare(); } DataMember dataMember = comparer.FieldsToCompare[0]; if (rightValue != null && HasIndexablePropertyOnLeft <Key>(leftSide, sourceCollection, dataMember, out returnedEx)) { if (session.TraceIndexUsage) { Trace.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " Index used with " + sourceCollection.ToString()); } return(true); } } } return(false); }
static bool canUseIndex <Key>(BTreeBase <Key, Key> sourceCollection, BinaryExpression binExp, CompareByField <Key> comparer) #if WINDOWS_PHONE where Key : new() #endif { if (comparer.FieldsToCompare == null) { comparer.SetupFieldsToCompare(); } int indexNumberOfFields = comparer.FieldsToCompare.Length; while (binExp.NodeType == ExpressionType.AndAlso) { BinaryExpression leftExpr = (BinaryExpression)binExp.Left; BinaryExpression rightExpr = (BinaryExpression)binExp.Right; MemberExpression returnedEx = null; if (indexNumberOfFields == 0 || !HasIndexablePropertyOnLeft(rightExpr.Left, sourceCollection, comparer.FieldsToCompare[--indexNumberOfFields], out returnedEx)) { return(false); } binExp = leftExpr; } return(true); }
static IEnumerable <Key> LessThan <Key>(BTreeBase <Key, Key> sourceCollection, BinaryExpression binExp) #if WINDOWS_PHONE where Key : new() #endif { SessionBase session = sourceCollection.Session; Expression leftSide = binExp.Left; Expression rightSide = binExp.Right; object rightValue = GetRightValue(leftSide, rightSide); CompareByField <Key> comparer = sourceCollection.Comparer as CompareByField <Key>; if (leftSide.NodeType == ExpressionType.Parameter) { Key key = (Key)rightValue; if (key != null) { BTreeSetIterator <Key> itr = sourceCollection.Iterator(); if (itr.GoTo(key)) { while (itr.Previous() != null) { yield return(itr.Current()); } } } } else { if (comparer != null) { //if we were able to create a hash from the right side (likely) MemberExpression returnedEx = null; #if WINDOWS_PHONE || WINDOWS_UWP Key key = (Key)Activator.CreateInstance(typeof(Key)); #else Key key = (Key)FormatterServices.GetUninitializedObject(typeof(Key)); #endif if (comparer.FieldsToCompare == null) { comparer.SetupFieldsToCompare(); } DataMember dataMember = comparer.FieldsToCompare[0]; if (rightValue != null && HasIndexablePropertyOnLeft <Key>(leftSide, sourceCollection, dataMember, out returnedEx)) { //cast to MemberExpression - it allows us to get the property MemberExpression propExp = (MemberExpression)returnedEx; MemberInfo property = propExp.Member; foreach (DataMember member in comparer.FieldsToCompare.Skip(1)) { if (member.GetTypeCode == TypeCode.String) { member.SetMemberValue(key, ""); } } dataMember.SetMemberValueWithPossibleConvert(key, rightValue); BTreeSetIterator <Key> itr = sourceCollection.Iterator(); itr.GoTo(key); var v = itr.Current(); if (v == null) { v = itr.Previous(); } while (v != null && comparer.CompareField(dataMember, key, v, 0) == 0) { v = itr.Previous(); } while (v != null) { yield return(v); v = itr.Previous(); } } else if (leftSide.NodeType == ExpressionType.Call) { // don't know yet how to handle TODO /*MethodCallExpression expression = leftSide as MethodCallExpression; * Trace.Out.WriteLine("Method: " + expression.Method.Name); * Trace.Out.WriteLine("Args: "); * foreach (var exp in expression.Arguments) * sourceCollection where */ } } } }