public ListDimensionBuilder(ObjectConfiguration configuration, DimensionItem newDimension)
        {
            this._configuration = configuration;
            this._newDimension  = newDimension;

            _lookupRefinement = ListDimensionCache.Get(_configuration.repositoryId, _newDimension.DIdx, _configuration.query);

            //Try to get the Count objects from the cache
            //If not found it will be calculated below
            this.IsCachehit = (_lookupRefinement != null);
        }
        public Task Load()
        {
            return(Task.Factory.StartNew(() =>
            {
                //Nothing to do
                if (!_configuration.query.IncludeDimensions && !_configuration.query.IncludeRecords)
                {
                    return;
                }
                if (_dsList == null)
                {
                    return;
                }

                if (_dsList.Tables.Count > 0)
                {
                    //Put these in a lookup dictionary as there maybe thousands or more
                    //It makes lookup below much faster for these large sets
                    if (!this.IsCachehit || (_lookupRefinement == null) || (_lookupRefinement?.Count == 0 && _newDimension.RefinementList.Any()))
                    {
                        _lookupRefinement = _newDimension.RefinementList.ToDictionary(x => x.DVIdx, z => z);
                    }

                    var recordCache = new Dictionary <long, List <string> >();
                    if (_configuration.query.IncludeRecords)
                    {
                        foreach (DataRow dr in _dsList.Tables[0].Rows)
                        {
                            var recordIdx = (long)dr[0];
                            var dvidx = (long)dr[1];
                            if (!recordCache.ContainsKey(recordIdx))
                            {
                                recordCache.Add(recordIdx, new List <string>());
                            }
                            recordCache[recordIdx].Add(_lookupRefinement[dvidx].FieldValue);
                        }
                    }

                    if (!this.IsCachehit)
                    {
                        foreach (DataRow dr in _dsList.Tables[1].Rows)
                        {
                            var dvidx = (long)dr[0];
                            var count = (int)dr[1];
                            if (_lookupRefinement.ContainsKey(dvidx))
                            {
                                _lookupRefinement[dvidx].Count = count;
                            }
                            else
                            {
                                LoggerCQ.LogWarning($"Cannot find DVIdx={dvidx}");
                            }
                        }
                        ListDimensionCache.Add(_configuration.repositoryId, _newDimension.DIdx, _configuration.query, _lookupRefinement);
                    }

                    //Only look in here if there are items
                    if (_lookupRefinement.Keys.Count != 0)
                    {
                        //This lambda has been replaced. There was some instanec where the
                        //dictionary did NOT have the DVIdx value in it so it blew up
                        //Try to catch this case and log it
                        //_newDimension.RefinementList.ForEach(x => x.Count = _lookupRefinement[x.DVIdx].Count);
                        foreach (var ritem in _newDimension.RefinementList)
                        {
                            if (_lookupRefinement.ContainsKey(ritem.DVIdx))
                            {
                                ritem.Count = _lookupRefinement[ritem.DVIdx].Count;
                            }
                            else
                            {
                                LoggerCQ.LogWarning($"Missing Dictionary Value: ID={_configuration.schema.ID}, DIdx={ritem.DIdx}, DVIdx={ritem.DVIdx}, Value={ritem.FieldValue}");
                            }
                        }
                    }

                    //Now setup values for records
                    if (_configuration.query.IncludeRecords)
                    {
                        var fieldIndex = _configuration.schema.FieldList.Select(x => x.Name).ToList().IndexOf(_newDimension.Name);
                        //NOTE: there is a lock around "retval" as this is the only external object that is modified
                        lock (_configuration.retval)
                        {
                            foreach (var record in _configuration.retval.RecordList)
                            {
                                if (recordCache.ContainsKey(record.__RecordIndex))
                                {
                                    record.ItemArray[fieldIndex] = recordCache[record.__RecordIndex].ToArray();
                                }
                                else
                                {
                                    record.ItemArray[fieldIndex] = new string[] { }
                                };
                            }
                        }
                    }

                    if (!_configuration.query.IncludeEmptyDimensions)
                    {
                        _newDimension.RefinementList.RemoveAll(x => x.Count == 0);
                    }
                    _newDimension.RefinementList.RemoveAll(x => _configuration.query.DimensionValueList.Contains(x.DVIdx));

                    if (_configuration.query.IncludeDimensions)
                    {
                        lock (_configuration.retval)
                        {
                            _configuration.retval.DimensionList.Add(_newDimension);
                        }
                    }

                    lock (_configuration)
                    {
                        _configuration.PerfLoadLDim++;
                    }
                }
            }));
        }