/// <summary>
        /// Adds the collection to the batch.
        /// </summary>
        /// <param name="persister">The collection persister.</param>
        /// <param name="ce">The collection entry.</param>
        internal void AddCollection(ICollectionPersister persister, CollectionKey ce)
        {
            if (!_queryCollectionKeys.TryGetValue(persister.Role, out var querySet))
            {
                querySet = new Dictionary <CollectionKey, CollectionEntry>();
                _queryCollectionKeys.Add(persister.Role, querySet);
            }

            if (!querySet.ContainsKey(ce))
            {
                querySet.Add(ce, null);
            }
        }
        /// <summary>
        /// Links the created collection entry with the stored collection key.
        /// </summary>
        /// <param name="ce">The collection entry.</param>
        internal void LinkCollectionEntry(CollectionEntry ce)
        {
            if (!_queryCollectionKeys.TryGetValue(ce.LoadedPersister.Role, out var keys) ||
                keys.Count <= 0)
            {
                return;
            }
            var key = new CollectionKey(ce.LoadedPersister, ce.LoadedKey);

            if (keys.ContainsKey(key))
            {
                keys[key] = ce;
            }
        }
        /// <summary>
        /// Get a batch of all uninitialized collection keys for a given role that are present in the cached query.
        /// Once this method is called the uninitialized collection keys for a given role will be cleared in order to prevent
        /// double checking the same keys.
        /// </summary>
        /// <param name="collectionPersister">The persister for the collection role.</param>
        /// <param name="key">A key that must be included in the batch fetch.</param>
        /// <param name="collectionEntries">An array that will be filled with collection entries if set.</param>
        /// <returns>
        /// An array of collection keys that can be empty if the key was already checked or <see langword="null" />
        /// if the key is not present in the cached query.
        /// </returns>
        internal object[] GetCollectionBatch(ICollectionPersister collectionPersister, object key, out CollectionEntry[] collectionEntries)
        {
            if (!_queryCollectionKeys.TryGetValue(collectionPersister.Role, out var keys))
            {
                collectionEntries = null;
                return(null);                // The collection was not present in the cached query
            }

            var collectionKey = new CollectionKey(collectionPersister, key);

            if (_queryCheckedCollectionKeys.TryGetValue(collectionPersister.Role, out var checkedKeys) &&
                checkedKeys.Contains(collectionKey))
            {
                collectionEntries = null;
                return(Array.Empty <object>());
            }

            if (!keys.TryGetValue(collectionKey, out var collectionEntry) || collectionEntry == null)
            {
                collectionEntries = null;
                return(null);                // The collection was not present in the cached query
            }

            if (checkedKeys == null)
            {
                checkedKeys = new HashSet <CollectionKey>();
                _queryCheckedCollectionKeys.Add(collectionPersister.Role, checkedKeys);
            }

            if (!_queryCheckedCollectionEntries.TryGetValue(collectionPersister.Role, out var checkedEntries))
            {
                checkedEntries = new HashSet <CollectionEntry>();
                _queryCheckedCollectionEntries.Add(collectionPersister.Role, checkedEntries);
            }

            var result = new object[keys.Count];

            collectionEntries = new CollectionEntry[result.Length];
            var i = 0;

            result[i++] = key;

            foreach (var pair in keys)
            {
                if (pair.Value == null || _persistenceContext.GetCollection(pair.Key)?.WasInitialized != false)
                {
                    continue;                     // The collection was not registered or is already initialized
                }

                if (collectionPersister.KeyType.IsEqual(key, pair.Value.LoadedKey, collectionPersister.Factory))
                {
                    collectionEntries[0] = pair.Value;
                    checkedKeys.Add(pair.Key);
                    checkedEntries.Add(pair.Value);
                    continue;
                }

                collectionEntries[i] = pair.Value;
                result[i++]          = pair.Value.LoadedKey;
                checkedKeys.Add(pair.Key);
                checkedEntries.Add(pair.Value);
            }

            keys.Clear();

            return(result);
        }
Ejemplo n.º 4
0
        public override bool Equals(object obj)
        {
            CollectionKey that = (CollectionKey)obj;

            return(that.role.Equals(role) && keyType.IsEqual(that.key, key, factory));
        }