Ejemplo n.º 1
0
        private void EndLoadingCollections(ICollectionPersister persister, IList <LoadingCollectionEntry> matchedCollectionEntries)
        {
            if (matchedCollectionEntries == null || matchedCollectionEntries.Count == 0)
            {
                if (log.IsDebugEnabled())
                {
                    log.Debug("no collections were found in result set for role: {0}", persister.Role);
                }
                return;
            }

            int count = matchedCollectionEntries.Count;

            if (log.IsDebugEnabled())
            {
                log.Debug("{0} collections were found in result set for role: {1}", count, persister.Role);
            }

            var cacheBatcher = new CacheBatcher(LoadContext.PersistenceContext.Session);

            for (int i = 0; i < count; i++)
            {
                EndLoadingCollection(matchedCollectionEntries[i], persister,
                                     data => cacheBatcher.AddToBatch(persister, data));
            }
            cacheBatcher.ExecuteBatch();

            if (log.IsDebugEnabled())
            {
                log.Debug("{0} collections initialized for role: {1}", count, persister.Role);
            }
        }
Ejemplo n.º 2
0
        private async Task EndLoadingCollectionsAsync(ICollectionPersister persister, IList <LoadingCollectionEntry> matchedCollectionEntries, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (matchedCollectionEntries == null || matchedCollectionEntries.Count == 0)
            {
                if (log.IsDebugEnabled())
                {
                    log.Debug("no collections were found in result set for role: {0}", persister.Role);
                }
                return;
            }

            int count = matchedCollectionEntries.Count;

            if (log.IsDebugEnabled())
            {
                log.Debug("{0} collections were found in result set for role: {1}", count, persister.Role);
            }

            var cacheBatcher = new CacheBatcher(LoadContext.PersistenceContext.Session);

            for (int i = 0; i < count; i++)
            {
                await(EndLoadingCollectionAsync(matchedCollectionEntries[i], persister,
                                                data => cacheBatcher.AddToBatch(persister, data), cancellationToken)).ConfigureAwait(false);
            }
            await(cacheBatcher.ExecuteBatchAsync(cancellationToken)).ConfigureAwait(false);

            if (log.IsDebugEnabled())
            {
                log.Debug("{0} collections initialized for role: {1}", count, persister.Role);
            }
        }
Ejemplo n.º 3
0
        protected List <object> DoList()
        {
            bool statsEnabled = session.Factory.Statistics.IsStatisticsEnabled;
            var  stopWatch    = new Stopwatch();

            if (statsEnabled)
            {
                stopWatch.Start();
            }
            int rowCount = 0;

            var results = new List <object>();

            var hydratedObjects = new List <object> [Translators.Count];

            List <EntityKey[]>[] subselectResultKeys = new List <EntityKey[]> [Translators.Count];
            bool[] createSubselects = new bool[Translators.Count];
            var    cacheBatcher     = new CacheBatcher(session);

            try
            {
                using (var reader = resultSetsCommand.GetReader(_timeout))
                {
                    if (log.IsDebugEnabled())
                    {
                        log.Debug("Executing {0} queries", translators.Count);
                    }
                    for (int i = 0; i < translators.Count; i++)
                    {
                        ITranslator     translator = Translators[i];
                        QueryParameters parameter  = Parameters[i];

                        int entitySpan = translator.Loader.EntityPersisters.Length;
                        hydratedObjects[i] = entitySpan > 0 ? new List <object>() : null;
                        RowSelection selection = parameter.RowSelection;
                        int          maxRows   = Loader.Loader.HasMaxRows(selection) ? selection.MaxRows : int.MaxValue;
                        if (!dialect.SupportsLimitOffset || !translator.Loader.UseLimit(selection, dialect))
                        {
                            Loader.Loader.Advance(reader, selection);
                        }

                        if (parameter.HasAutoDiscoverScalarTypes)
                        {
                            translator.Loader.AutoDiscoverTypes(reader, parameter, null);
                        }

                        LockMode[] lockModeArray     = translator.Loader.GetLockModes(parameter.LockModes);
                        EntityKey  optionalObjectKey = Loader.Loader.GetOptionalObjectKey(parameter, session);

                        createSubselects[i]    = translator.Loader.IsSubselectLoadingEnabled;
                        subselectResultKeys[i] = createSubselects[i] ? new List <EntityKey[]>() : null;

                        translator.Loader.HandleEmptyCollections(parameter.CollectionKeys, reader, session);
                        EntityKey[] keys = new EntityKey[entitySpan];                         // we can reuse it each time

                        if (log.IsDebugEnabled())
                        {
                            log.Debug("processing result set");
                        }

                        IList tempResults = new List <object>();
                        int   count;
                        for (count = 0; count < maxRows && reader.Read(); count++)
                        {
                            if (log.IsDebugEnabled())
                            {
                                log.Debug("result set row: {0}", count);
                            }

                            rowCount++;
                            object result = translator.Loader.GetRowFromResultSet(
                                reader, session, parameter, lockModeArray, optionalObjectKey, hydratedObjects[i], keys, true, null, null,
                                (persister, data) => cacheBatcher.AddToBatch(persister, data));
                            tempResults.Add(result);

                            if (createSubselects[i])
                            {
                                subselectResultKeys[i].Add(keys);
                                keys = new EntityKey[entitySpan];                                 //can't reuse in this case
                            }
                        }

                        if (log.IsDebugEnabled())
                        {
                            log.Debug("done processing result set ({0} rows)", count);
                        }

                        results.Add(tempResults);

                        if (log.IsDebugEnabled())
                        {
                            log.Debug("Query {0} returned {1} results", i, tempResults.Count);
                        }

                        reader.NextResult();
                    }

                    for (int i = 0; i < translators.Count; i++)
                    {
                        ITranslator     translator = translators[i];
                        QueryParameters parameter  = parameters[i];

                        translator.Loader.InitializeEntitiesAndCollections(hydratedObjects[i], reader, session, false, cacheBatcher);

                        if (createSubselects[i])
                        {
                            translator.Loader.CreateSubselects(subselectResultKeys[i], parameter, session);
                        }
                    }

                    cacheBatcher.ExecuteBatch();
                }
            }
            catch (Exception sqle)
            {
                log.Error(sqle, "Failed to execute multi query: [{0}]", resultSetsCommand.Sql);
                throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle, "Failed to execute multi query", resultSetsCommand.Sql);
            }

            if (statsEnabled)
            {
                stopWatch.Stop();
                session.Factory.StatisticsImplementor.QueryExecuted(string.Format("{0} queries (MultiQuery)", translators.Count), rowCount, stopWatch.Elapsed);
            }
            return(results);
        }
Ejemplo n.º 4
0
        private void GetResultsFromDatabase(IList results)
        {
            Stopwatch stopWatch = null;

            if (session.Factory.Statistics.IsStatisticsEnabled)
            {
                stopWatch = Stopwatch.StartNew();
            }
            int rowCount     = 0;
            var cacheBatcher = new CacheBatcher(session);

            try
            {
                using (var reader = resultSetsCommand.GetReader(_timeout))
                {
                    var hydratedObjects = new List <object> [loaders.Count];
                    List <EntityKey[]>[] subselectResultKeys = new List <EntityKey[]> [loaders.Count];
                    bool[] createSubselects = new bool[loaders.Count];
                    for (int i = 0; i < loaders.Count; i++)
                    {
                        CriteriaLoader loader     = loaders[i];
                        int            entitySpan = loader.EntityPersisters.Length;
                        hydratedObjects[i] = entitySpan == 0 ? null : new List <object>(entitySpan);
                        EntityKey[]     keys            = new EntityKey[entitySpan];
                        QueryParameters queryParameters = parameters[i];
                        IList           tmpResults      = new List <object>();

                        RowSelection selection = parameters[i].RowSelection;
                        createSubselects[i]    = loader.IsSubselectLoadingEnabled;
                        subselectResultKeys[i] = createSubselects[i] ? new List <EntityKey[]>() : null;
                        int maxRows = Loader.Loader.HasMaxRows(selection) ? selection.MaxRows : int.MaxValue;
                        if (!dialect.SupportsLimitOffset || !loader.UseLimit(selection, dialect))
                        {
                            Loader.Loader.Advance(reader, selection);
                        }
                        int count;
                        for (count = 0; count < maxRows && reader.Read(); count++)
                        {
                            rowCount++;

                            object o =
                                loader.GetRowFromResultSet(reader, session, queryParameters, loader.GetLockModes(queryParameters.LockModes),
                                                           null, hydratedObjects[i], keys, true, null, null,
                                                           (persister, data) => cacheBatcher.AddToBatch(persister, data));
                            if (createSubselects[i])
                            {
                                subselectResultKeys[i].Add(keys);
                                keys = new EntityKey[entitySpan];                                 //can't reuse in this case
                            }
                            tmpResults.Add(o);
                        }

                        results.Add(tmpResults);
                        reader.NextResult();
                    }

                    for (int i = 0; i < loaders.Count; i++)
                    {
                        CriteriaLoader loader = loaders[i];
                        loader.InitializeEntitiesAndCollections(hydratedObjects[i], reader, session, session.DefaultReadOnly, cacheBatcher);

                        if (createSubselects[i])
                        {
                            loader.CreateSubselects(subselectResultKeys[i], parameters[i], session);
                        }
                    }

                    cacheBatcher.ExecuteBatch();
                }
            }
            catch (Exception sqle)
            {
                log.Error(sqle, "Failed to execute multi criteria: [{0}]", resultSetsCommand.Sql);
                throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle, "Failed to execute multi criteria", resultSetsCommand.Sql);
            }
            if (stopWatch != null)
            {
                stopWatch.Stop();
                session.Factory.StatisticsImplementor.QueryExecuted(string.Format("{0} queries (MultiCriteria)", loaders.Count), rowCount, stopWatch.Elapsed);
            }
        }