Esempio n. 1
0
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Gets all.
        /// </summary>
        /// <param name="key">
        ///  The key.
        /// </param>
        /// <param name="skip">
        ///  The skip.
        /// </param>
        /// <returns>
        ///  An enumerator that allows foreach to be used to process all items in this collection.
        /// </returns>
        ///-------------------------------------------------------------------------------------------------
        public IEnumerable <Identity> GetAll(object key, int skip)
        {
            if (key == null)
            {
                return(null);
            }

            using (var session = EnsuresRunInSession())
            {
                HypergraphTransaction tx = null;
                var graph = _graph.Target as HyperGraph;
                if (graph != null)
                {
                    tx = graph.CurrentTransaction;
                }

                _sync.EnterReadLock();
                try
                {
                    var cx     = 0;
                    var result = new List <Identity>();
                    HashSet <Identity> list;
                    if (_index.TryGetValue(key, out list))
                    {
                        foreach (var id in list)
                        {
                            if (tx == null || tx.IsValidInTransaction(id))
                            {
                                if (cx++ >= skip)
                                {
                                    result.Add(id);
                                }
                            }
                        }
                    }

                    if (tx != null && cx <= skip)
                    {
                        foreach (var id in tx.GetPendingIndexFor(Name, key))
                        {
                            if (cx++ >= skip)
                            {
                                result.Add(id);
                            }
                        }
                    }
                    return(result);
                }
                finally
                {
                    _sync.ExitReadLock();
                    if (session != null)
                    {
                        session.AcceptChanges();
                    }
                }
            }
        }
Esempio n. 2
0
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Gets the specified key.
        /// </summary>
        /// <param name="key">
        ///  The key.
        /// </param>
        /// <returns>
        ///  An Identity.
        /// </returns>
        ///-------------------------------------------------------------------------------------------------
        public Identity Get(object key)
        {
            if (key == null)
            {
                return(null);
            }

            using (var session = EnsuresRunInSession())
            {
                HypergraphTransaction tx = null;
                var graph = _graph.Target as HyperGraph;
                if (graph != null)
                {
                    tx = graph.CurrentTransaction;
                }

                _sync.EnterReadLock();
                try
                {
                    HashSet <Identity> list;
                    if (_index.TryGetValue(key, out list))
                    {
                        foreach (var id in list)
                        {
                            if (tx == null || tx.IsValidInTransaction(id))
                            {
                                return(id);
                            }
                        }
                    }

                    // Il se peut que l'index ne soit pas encore mis à jour dans le contexte d'une transaction.
                    // On va voir si ce n'est pas le cas
                    if (tx != null)
                    {
                        return(tx.GetPendingIndexFor(Name, key)
                               .FirstOrDefault());
                    }
                    return(null);
                }
                finally
                {
                    _sync.ExitReadLock();
                    if (session != null)
                    {
                        session.AcceptChanges();
                    }
                }
            }
        }