private void PrepareToLoadEntities(TranslationResult translationResult)
        {
            // cancelling the previous index creation, if there was one
            this.CurrentIndexCreator = null;

            // skipping added and removed entities
            this.ClearModifications();

            // if a HashKey value was explicitly specified
            string hashKeyName = this.TableDefinition.HashKeys[0];

            if (this.HashKeyValue != null && !translationResult.Conditions.ContainsKey(hashKeyName))
            {
                // then adding a condition for it
                translationResult.Conditions.AddCondition
                (
                    hashKeyName,
                    new SearchCondition(ScanOperator.Equal, this.HashKeyValue)
                );
            }

#if DEBUG
            this._loadOperationStopwatch = new Stopwatch();
            this._loadOperationStopwatch.Start();
#endif
        }
        private bool TryLoadFromCache(TranslationResult translationResult, Type entityType, out object result)
        {
            result = null;

            var conditions4Cache = translationResult.Conditions;

            // if an explicit HashKey value is specified for table, then
            // we need to remove a condition for it from SearchConditions before passing them to cache
            // implementation. That's because the entity's Type doesn't contain a HashKey property.
            if (this.HashKeyValue != null)
            {
                conditions4Cache = conditions4Cache.ExcludeField(this.TableDefinition.HashKeys[0]);
            }

            // implementing Count()
            if (translationResult.CountRequested)
            {
                int?countFromCache = this.Cache.GetCount(conditions4Cache);
                if (countFromCache.HasValue)
                {
                    result = countFromCache.Value;
                    return(true);
                }
            }
            else
            {
                // getting the entities themselves from cache
                var docsFromCache = this.Cache.GetEntities(conditions4Cache, translationResult.AttributesToGet, translationResult.OrderByColumn, translationResult.OrderByDesc);
                if (docsFromCache != null)
                {
                    result = this.CreateDocArrayReader(docsFromCache, entityType, translationResult.ProjectionFunc);
                    return(true);
                }
            }

            // If we failed to get from cache, then start filling an index in cache
            // (this should be started before querying DynamoDb and only when table (full) entities are requested)
            this.CurrentIndexCreator =
                (translationResult.ProjectionFunc == null)
                ?
                this.Cache.StartCreatingIndex(conditions4Cache)
                :
                this.Cache.StartCreatingProjectionIndex(conditions4Cache, translationResult.AttributesToGet)
            ;

            return(false);
        }
        protected virtual void InitReader(ISupervisableEnumerable reader)
        {
            // We need to detect the moment, when enumeration is finished. To put an index to cache
            reader.EnumerationFinished += () =>
            {
#if DEBUG
                if (this._loadOperationStopwatch != null)
                {
                    this._loadOperationStopwatch.Stop();
                    this.Log("Load operation took {0} ms", this._loadOperationStopwatch.ElapsedMilliseconds);
                    this._loadOperationStopwatch = null;
                }
#endif

                // storing the filled index to cache
                var curIndexCreator = this.CurrentIndexCreator;
                if (curIndexCreator != null)
                {
                    curIndexCreator.Dispose();
                }
                this.CurrentIndexCreator = null;
            };
        }
        private bool TryLoadFromCache(TranslationResult translationResult, Type entityType, out object result)
        {
            result = null;

            var conditions4Cache = translationResult.Conditions;
            // if an explicit HashKey value is specified for table, then
            // we need to remove a condition for it from SearchConditions before passing them to cache
            // implementation. That's because the entity's Type doesn't contain a HashKey property.
            if (this.HashKeyValue != null)
            {
                conditions4Cache = conditions4Cache.ExcludeField(this.TableDefinition.HashKeys[0]);
            }

            // implementing Count()
            if (translationResult.CountRequested)
            {
                int? countFromCache = this.Cache.GetCount(conditions4Cache);
                if (countFromCache.HasValue)
                {
                    result = countFromCache.Value;
                    return true;
                }
            }
            else
            {
                // getting the entities themselves from cache
                var docsFromCache = this.Cache.GetEntities(conditions4Cache, translationResult.AttributesToGet, translationResult.OrderByColumn, translationResult.OrderByDesc);
                if (docsFromCache != null)
                {
                    result = this.CreateDocArrayReader(docsFromCache, entityType, translationResult.ProjectionFunc);
                    return true;
                }
            }

            // If we failed to get from cache, then start filling an index in cache
            // (this should be started before querying DynamoDb and only when table (full) entities are requested)
            this.CurrentIndexCreator =
                (translationResult.ProjectionFunc == null)
                ?
                this.Cache.StartCreatingIndex(conditions4Cache)
                :
                this.Cache.StartCreatingProjectionIndex(conditions4Cache, translationResult.AttributesToGet)
            ;

            return false;
        }
        protected virtual void InitReader(ISupervisableEnumerable reader)
        {
            // We need to detect the moment, when enumeration is finished. To put an index to cache
            reader.EnumerationFinished += () =>
            {
            #if DEBUG
                if (this._loadOperationStopwatch != null)
                {
                    this._loadOperationStopwatch.Stop();
                    this.Log("Load operation took {0} ms", this._loadOperationStopwatch.ElapsedMilliseconds);
                    this._loadOperationStopwatch = null;
                }
            #endif

                // storing the filled index to cache
                var curIndexCreator = this.CurrentIndexCreator;
                if (curIndexCreator != null)
                {
                    curIndexCreator.Dispose();
                }
                this.CurrentIndexCreator = null;
            };
        }
        /// <summary>
        /// Executes a get/query/scan request against the table
        /// </summary>
        internal object LoadEntities(TranslationResult translationResult, Type entityType)
        {
            // cancelling the previous index creation, if there was one
            this.CurrentIndexCreator = null;

            // skipping added and removed entities
            this.ClearModifications();

            // if a HashKey value was explicitly specified
            if (this.HashKeyValue != null)
            {
                // then adding a condition for it
                translationResult.Conditions.AddCondition
                    (
                        this.TableDefinition.HashKeys[0],
                        new SearchCondition(ScanOperator.Equal, this.HashKeyValue)
                    );
            }

            #if DEBUG
            this._loadOperationStopwatch = new Stopwatch();
            this._loadOperationStopwatch.Start();
            #endif

            return this.InternalLoadEntities(translationResult, entityType);
        }
 public SetIndexOnEmailStartupTask(IIndexCreator indexCreator) => _indexCreator = indexCreator;