PageRenderTime 1ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/mcs/class/System.Data.Linq/src/DbLinq/Util/CacheChecker.cs

https://github.com/ekovalenko-softheme/mono
C# | 111 lines | 83 code | 11 blank | 17 comment | 38 complexity | f57a8117abd21fd261f1c8c7e5e38b82 MD5 | raw file
  1using System;
  2using System.Reflection;
  3using System.Collections.Generic;
  4using System.Linq;
  5using System.Linq.Expressions;
  6using System.Data.Linq.Mapping;
  7using System.Text;
  8
  9#if MONO_STRICT
 10using System.Data.Linq;
 11using System.Data.Linq.Identity;
 12#else
 13using DbLinq.Data.Linq;
 14using DbLinq.Data.Linq.Identity;
 15#endif
 16
 17using DbLinq.Linq;
 18using DbLinq.Util;
 19
 20namespace DbLinq.Util
 21{
 22    internal class CacheChecker
 23    {
 24        /// <summary>
 25        /// Quote from MSDN:
 26        /// If the object requested by the query is easily identifiable as one
 27        /// already retrieved, no query is executed. The identity table acts as a cache
 28        /// of all previously retrieved objects
 29
 30        /// From Matt Warren: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=345635&SiteID=1
 31        /// The cache is checked when the query is a simple table.Where(pred) or table.First(pred) where the 
 32        /// predicate refers only to the primary key.  Otherwise the query is always sent and the cache only checked 
 33        /// after the results are retrieved. 
 34        /// The DLINQ cache is not distributed or shared, it is local and contained within the context.  It is only a 
 35        /// referential identity cache used to guarantee that two reads of the same entity return the same instance. 
 36        /// You are not expected to hold the cache for an extended duration (except possibly for a client scenario), 
 37        /// or share it across threads, processes, or machines in a cluster. 
 38        /// </summary>
 39        public static bool TryRetrieveFromCache<S>(SessionVarsParsed vars, Expression scalarExpr, out S cachedRow)
 40        {
 41            cachedRow = default(S);
 42            int count1 = vars.ExpressionChain.Count;
 43            MethodCallExpression scalarMethodCall = scalarExpr.XMethodCall();
 44
 45            bool isSingleWhere = false;
 46            LambdaExpression whereLambda = null;
 47
 48            if (count1 == 1 && scalarMethodCall.Arguments.Count == 1)
 49            {
 50                MethodCallExpression call0 = vars.ExpressionChain[0];
 51                if (call0.Method.Name == "Where" && call0.Arguments.Count == 2)
 52                {
 53                    //db.Customers.Where(id==1).Single()
 54                    isSingleWhere = true;
 55                    whereLambda = call0.Arguments[1].XLambda();
 56                }
 57            }
 58            else if (count1 == 0 && scalarMethodCall.Arguments.Count == 2)
 59            {
 60                //db.Customers.Single(id==1)
 61                isSingleWhere = true;
 62                whereLambda = scalarMethodCall.Arguments[1].XLambda();
 63            }
 64
 65            if ((!isSingleWhere) || whereLambda == null)
 66                return false;
 67            if (whereLambda.Parameters.Count != 1 || whereLambda.Parameters[0].NodeType != ExpressionType.Parameter)
 68                return false;
 69            if (whereLambda.Body.NodeType != ExpressionType.Equal)
 70                return false;
 71            BinaryExpression equals = (BinaryExpression)whereLambda.Body;
 72            Expression left = equals.Left;
 73            Expression right = equals.Right;
 74
 75            MemberExpression member;
 76            ConstantExpression consta;
 77            if (left.NodeType == ExpressionType.MemberAccess && right.NodeType == ExpressionType.Constant)
 78            {
 79                member = left.XMember();
 80                consta = right.XConstant();
 81            }
 82            else if (left.NodeType == ExpressionType.Constant && right.NodeType == ExpressionType.Parameter)
 83            {
 84                member = right.XMember();
 85                consta = left.XConstant();
 86            }
 87            else
 88            {
 89                return false;
 90            }
 91
 92            if (member.Expression.NodeType != ExpressionType.Parameter)
 93                return false;
 94
 95            //check that it's a primary key field
 96            ColumnAttribute colAttrib = AttribHelper.GetColumnAttribute(member.Member);
 97            if (colAttrib == null)
 98                return false;
 99            if (!colAttrib.IsPrimaryKey)
100                return false;
101
102            IdentityKey key = new IdentityKey(typeof(S), consta.Value);
103            DataContext context = vars.Context;
104            object cachedEntity = context.GetRegisteredEntityByKey(key);
105            if (cachedEntity == null)
106                return false;
107            cachedRow = (S)cachedEntity;
108            return true;
109        }
110    }
111}