Example #1
0
        /// <summary>
        /// Returns an existing <see cref="Aspect"/> by the given name.
        /// If it is not found, returns null.
        /// </summary>
        /// <param name="name">Name of the <see cref="Aspect"/>.</param>
        public static Aspect LoadAspectByName(string name)
        {
            // IMPORTANT:
            // We can't use external query here because this method is called during indexing!
            // It would mean trying to query the index while writing to it, which would result in a deadlock.
            // For this reason, this method uses its own cache for finding aspects.

            // Try to find the aspect in cache
            var cacheKey = "SN_AspectCacheByName_" + name;
            var aspect   = Cache.Get(cacheKey) as Aspect;

            if (aspect == null)
            {
                // Find aspect via node query.
                // DO NOT replace this call with either Linq or Content Query for the reasons detailed above!
                var result = NodeQuery.QueryNodesByTypeAndName(ActiveSchema.NodeTypes[typeof(Aspect).Name], false, name);
                aspect = result.Nodes.FirstOrDefault() as Aspect;

                // If not found, return null
                if (aspect == null)
                {
                    return(null);
                }

                // Store in cache
                var dependency = CacheDependencyFactory.CreateNodeDependency(aspect);
                Cache.Insert(cacheKey, aspect, dependency);
            }

            return(aspect);
        }
Example #2
0
        /* ----------------------------------------------------------------------------------------------- */

        public Stream GetBinaryStream(int nodeId, int versionId, int propertyTypeId)
        {
            // Try to load cached binary entity
            var cacheKey          = BinaryCacheEntity.GetCacheKey(versionId, propertyTypeId);
            var binaryCacheEntity = (BinaryCacheEntity)Cache.Get(cacheKey);

            if (binaryCacheEntity == null)
            {
                //TODO: [DIBLOB] get the storage service through the constructor later

                // Not in cache, load it from the database
                binaryCacheEntity = Providers.Instance.BlobStorage.LoadBinaryCacheEntityAsync(versionId, propertyTypeId, CancellationToken.None)
                                    .GetAwaiter().GetResult();

                // insert the binary cache entity into the
                // cache only if we know the node id
                if (binaryCacheEntity != null && nodeId != 0)
                {
                    if (!RepositoryEnvironment.WorkingMode.Populating)
                    {
                        var head = NodeHead.Get(nodeId);
                        Cache.Insert(cacheKey, binaryCacheEntity,
                                     CacheDependencyFactory.CreateBinaryDataDependency(nodeId, head.Path, head.NodeTypeId));
                    }
                }
            }

            // Not found even in the database
            if (binaryCacheEntity == null || binaryCacheEntity.Length == -1)
            {
                return(null);
            }

            return(new SnStream(binaryCacheEntity.Context, Providers.Instance.BlobStorage, binaryCacheEntity.RawData));
        }
Example #3
0
        public override void AddPortletDependency()
        {
            base.AddPortletDependency();

            if (!CacheByContext)
            {
                return;
            }

            // this works similarly to GetCacheKey, use GetContextNodeInternal instead of GetContextNode
            Node contextNode;

            using (new SystemAccount())
            {
                contextNode = GetContextNodeInternalSafe();
            }

            if (contextNode == null)
            {
                return;
            }

            var nodeDependency = CacheDependencyFactory.CreateNodeDependency(contextNode);

            Dependencies.Add(nodeDependency);
        }
Example #4
0
        internal static void CacheNodeHead(NodeHead head, string idKey, string pathKey)
        {
            var dependencyForPathKey = CacheDependencyFactory.CreateNodeHeadDependency(head);
            var dependencyForIdKey   = CacheDependencyFactory.CreateNodeHeadDependency(head);

            DistributedApplication.Cache.Insert(idKey, head, dependencyForIdKey);
            DistributedApplication.Cache.Insert(pathKey, head, dependencyForPathKey);
        }
Example #5
0
        private void CacheNodeHead(NodeHead head, string idKey, string pathKey)
        {
            var dependencyForPathKey = CacheDependencyFactory.CreateNodeHeadDependency(head);
            var dependencyForIdKey   = CacheDependencyFactory.CreateNodeHeadDependency(head);

            Cache.Insert(idKey, head, dependencyForIdKey);
            Cache.Insert(pathKey, head, dependencyForPathKey);
        }
Example #6
0
        public override void AddPortletDependency()
        {
            base.AddPortletDependency();

            if (_contentPath != null)
            {
                var nodeHead = NodeHead.Get(_contentPath);
                if (nodeHead != null)
                {
                    var nodeDependency = CacheDependencyFactory.CreateNodeDependency(nodeHead);
                    Dependencies.Add(nodeDependency);
                }
            }
        }
Example #7
0
        internal static void CacheNodeData(NodeData nodeData, string cacheKey = null)
        {
            if (nodeData == null)
            {
                throw new ArgumentNullException("nodeData");
            }
            if (cacheKey == null)
            {
                cacheKey = GenerateNodeDataVersionIdCacheKey(nodeData.VersionId);
            }
            var dependency = CacheDependencyFactory.CreateNodeDataDependency(nodeData);

            DistributedApplication.Cache.Insert(cacheKey, nodeData, dependency);
        }
Example #8
0
        public void CacheNodeData(NodeData nodeData, string cacheKey = null)
        {
            if (nodeData == null)
            {
                throw new ArgumentNullException(nameof(nodeData));
            }
            if (cacheKey == null)
            {
                cacheKey = CreateNodeDataVersionIdCacheKey(nodeData.VersionId);
            }
            var dependency = CacheDependencyFactory.CreateNodeDataDependency(nodeData);

            Cache.Insert(cacheKey, nodeData, dependency);
        }
Example #9
0
        public override void AddPortletDependency()
        {
            base.AddPortletDependency();

            if (!CacheByContext)
            {
                return;
            }

            //this works similar to GetCacheKey, use GetContextNodeInternal instead of GetContextNode
            var contextNode = this.GetContextNodeInternal();

            if (contextNode != null)
            {
                var nodeDependency = CacheDependencyFactory.CreateNodeDependency(contextNode);
                Dependencies.Add(nodeDependency);
            }
        }
Example #10
0
        internal static Stream GetBinaryStream(int nodeId, int versionId, int propertyTypeId)
        {
            BinaryCacheEntity binaryCacheEntity = null;

            if (TransactionScope.IsActive)
            {
                binaryCacheEntity = DataProvider.Current.LoadBinaryCacheEntity(versionId, propertyTypeId);
                return(new SnStream(binaryCacheEntity.Context, binaryCacheEntity.RawData));
            }

            // Try to load cached binary entity
            var cacheKey = BinaryCacheEntity.GetCacheKey(versionId, propertyTypeId);

            binaryCacheEntity = (BinaryCacheEntity)DistributedApplication.Cache.Get(cacheKey);
            if (binaryCacheEntity == null)
            {
                // Not in cache, load it from the database
                binaryCacheEntity = DataProvider.Current.LoadBinaryCacheEntity(versionId, propertyTypeId);

                // insert the binary cache entity into the
                // cache only if we know the node id
                if (binaryCacheEntity != null && nodeId != 0)
                {
                    if (!RepositoryEnvironment.WorkingMode.Populating)
                    {
                        var head = NodeHead.Get(nodeId);
                        DistributedApplication.Cache.Insert(cacheKey, binaryCacheEntity,
                                                            CacheDependencyFactory.CreateBinaryDataDependency(nodeId, head.Path, head.NodeTypeId));
                    }
                }
            }

            // Not found even in the database
            if (binaryCacheEntity == null)
            {
                return(null);
            }
            if (binaryCacheEntity.Length == -1)
            {
                return(null);
            }
            return(new SnStream(binaryCacheEntity.Context, binaryCacheEntity.RawData));
        }
Example #11
0
        public static User Load(string domain, string name, ExecutionHint hint)
        {
            domain = string.IsNullOrWhiteSpace(domain) ? IdentityManagement.DefaultDomain : domain;
            if (domain == null)
            {
                throw new ArgumentNullException(nameof(domain));
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            // look for the user ID in the cache by the doman-username key
            var ck           = GetUserCacheKey(domain, name);
            var userIdobject = DistributedApplication.Cache.Get(ck);

            if (userIdobject != null)
            {
                var userId     = Convert.ToInt32(userIdobject);
                var cachedUser = Node.Load <User>(userId);
                if (cachedUser != null)
                {
                    return(cachedUser);
                }
            }

            var domainPath = string.Concat(RepositoryStructure.ImsFolderPath, RepositoryPath.PathSeparator, domain);
            var type       = ActiveSchema.NodeTypes[typeof(User).Name];

            User user;
            bool forceCql;

            switch (hint)
            {
            case ExecutionHint.None:
                forceCql = RepositoryInstance.ContentQueryIsAllowed; break;

            case ExecutionHint.ForceIndexedEngine:
                forceCql = true; break;

            case ExecutionHint.ForceRelationalEngine:
                forceCql = false; break;

            default:
                throw new SnNotSupportedException("Unknown ExecutionHint: " + hint);
            }

            try
            {
                if (forceCql)
                {
                    var userResult = ContentQuery.Query(SafeQueries.UsersByLoginName, QuerySettings.AdminSettings, domainPath, name);

                    // non-unique user, do not allow login
                    if (userResult.Count > 1)
                    {
                        return(null);
                    }

                    user = userResult.Count == 0 ? null : userResult.Nodes.Cast <User>().FirstOrDefault();
                }
                else
                {
                    var queryProps = new List <QueryPropertyData>
                    {
                        new QueryPropertyData {
                            PropertyName = LOGINNAME, QueryOperator = Operator.Equal, Value = name
                        }
                    };

                    var userResult = NodeQuery.QueryNodesByTypeAndPathAndProperty(type, false, domainPath, false, queryProps);

                    // non-unique user, do not allow login
                    if (userResult.Count > 1)
                    {
                        return(null);
                    }

                    user = userResult.Count == 0 ? null : userResult.Nodes.Cast <User>().FirstOrDefault();
                }
            }
            catch (Exception e)
            {
                SnLog.WriteException(e);
                return(null);
            }

            if (user == null)
            {
                return(null);
            }

            // insert id into cache
            if (DistributedApplication.Cache.Get(ck) == null)
            {
                DistributedApplication.Cache.Insert(ck, user.Id, CacheDependencyFactory.CreateNodeDependency(user));
            }

            return(user);
        }
Example #12
0
        public static User Load(string domain, string name, ExecutionHint hint)
        {
            if (domain == null)
            {
                throw new ArgumentNullException("domain");
            }
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            //look for the user ID in the cache by the doman-username key
            var ck           = GetUserCacheKey(domain, name);
            var userIdobject = DistributedApplication.Cache.Get(ck);

            if (userIdobject != null)
            {
                var userId     = Convert.ToInt32(userIdobject);
                var cachedUser = Node.Load <User>(userId);
                if (cachedUser != null)
                {
                    //Trace.WriteLine("##UCACHE#>> User ID found in cache: " + ck);
                    return(cachedUser);
                }
            }

            //Trace.WriteLine("##UCACHE#>> User ID NOT found in cache: " + ck);

            var path = String.Concat(Repository.ImsFolderPath, RepositoryPath.PathSeparator, domain);
            var type = ActiveSchema.NodeTypes[typeof(User).Name];

            IEnumerable <Node> users;

            switch (hint)
            {
            case ExecutionHint.None:
                if (StorageContext.Search.IsOuterEngineEnabled && StorageContext.Search.SearchEngine != InternalSearchEngine.Instance)
                {
                    users = SearchInLucene(path, type.Name, name);
                }
                else
                {
                    users = NodeQuery.QueryNodesByTypeAndPathAndName(type, false, path, false, name).Nodes;
                }
                break;

            case ExecutionHint.ForceRelationalEngine:
                users = NodeQuery.QueryNodesByTypeAndPathAndName(type, false, path, false, name).Nodes;
                break;

            case ExecutionHint.ForceIndexedEngine:
                users = SearchInLucene(path, type.Name, name);
                break;

            default:
                throw new NotImplementedException();
            }

            var count = users.Count();

            if (count == 0)
            {
                return(null);
            }

            if (count > 1)
            {
                throw new ApplicationException(string.Format("The Username (='Domain\\Name', in this case '{0}\\{1}') should be unique. {2} matching users have been found.", domain, name, count));
            }

            var user = users.First() as User;

            //insert id into cache
            if (user != null && DistributedApplication.Cache.Get(ck) == null)
            {
                DistributedApplication.Cache.Insert(ck, user.Id, CacheDependencyFactory.CreateNodeDependency(user));
                //Trace.WriteLine("##UCACHE#>> User ID inserted into cache: " + ck);
            }

            return(user);
        }
Example #13
0
        public static User Load(string domain, string name, ExecutionHint hint)
        {
            if (domain == null)
            {
                throw new ArgumentNullException("domain");
            }
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            //look for the user ID in the cache by the doman-username key
            var ck           = GetUserCacheKey(domain, name);
            var userIdobject = DistributedApplication.Cache.Get(ck);

            if (userIdobject != null)
            {
                var userId     = Convert.ToInt32(userIdobject);
                var cachedUser = Node.Load <User>(userId);
                if (cachedUser != null)
                {
                    return(cachedUser);
                }
            }

            var path = String.Concat(Repository.ImsFolderPath, RepositoryPath.PathSeparator, domain);
            var type = ActiveSchema.NodeTypes[typeof(User).Name];


            IEnumerable <Node> users;
            var forceCql = false;

            if (hint == ExecutionHint.None)
            {
                forceCql = StorageContext.Search.IsOuterEngineEnabled && StorageContext.Search.SearchEngine != InternalSearchEngine.Instance;
            }
            else if (hint == ExecutionHint.ForceIndexedEngine)
            {
                forceCql = true;
            }
            else if (hint == ExecutionHint.ForceRelationalEngine)
            {
                forceCql = false;
            }
            else
            {
                throw new NotImplementedException("Unknown ExecutionHint: " + hint);
            }

            try
            {
                users = forceCql
                    ? ContentQuery.Query(SafeQueries.InTreeAndTypeIsAndName, null, path, type.Name, name).Nodes
                    : users = NodeQuery.QueryNodesByTypeAndPathAndName(type, false, path, false, name).Nodes;
            }
            catch (Exception e)
            {
                Logger.WriteException(e);
                return(null);
            }

            var count = users.Count();

            if (count != 1)
            {
                return(null);
            }

            var user = users.First() as User;

            //insert id into cache
            if (user != null && DistributedApplication.Cache.Get(ck) == null)
            {
                DistributedApplication.Cache.Insert(ck, user.Id, CacheDependencyFactory.CreateNodeDependency(user));
            }

            return(user);
        }