Exemplo n.º 1
0
        CachedDistance GetCachedGeometry(long fromP, long toP, bool checkDB = true)
        {
            CachedDistance distance = null;
            bool           needAdd  = false;

            //Проверяем в локальном кеше
            if (cache.ContainsKey(fromP) && cache[fromP].ContainsKey(toP))
            {
                distance = cache[fromP][toP];
            }
            //Проверяем в базе данных если разрешено.
            if (distance == null && checkDB)
            {
                IList <CachedDistance> list;
                lock (uow)
                {
                    list = CachedDistanceRepository.GetCache(uow, new[] { new WayHash(fromP, toP) });
                }
                distance = list.FirstOrDefault();
            }
            //Не нашли создаем новый.
            if (distance == null)
            {
                distance             = new CachedDistance();
                distance.FromGeoHash = fromP;
                distance.ToGeoHash   = toP;
                needAdd = true;
            }
            if (distance.PolylineGeometry == null)
            {
                if (!UpdateFromProvider(distance))
                {
                    return(null);
                }
            }

            if (distance.PolylineGeometry == null)
            {
                return(null);
            }

            if (needAdd)
            {
                lock (uow)
                {
                    AddNewCacheDistance(distance);
                    uow.TrySave(distance);
                    uow.Commit();
                }
            }

            return(distance);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Метод загружает недостающие расстояния из базы, если это необходимо.
        /// </summary>
        public void LoadDBCacheIfNeed(List <WayHash> ways)
        {
            var prepared = FilterForDBCheck(ways);

            if (prepared.Count > 0)
            {
                IList <CachedDistance> fromDB;
                lock (uow)
                {
                    fromDB = CachedDistanceRepository.GetCache(uow, prepared.ToArray());
                }
                foreach (var loaded in fromDB)
                {
                    AddNewCacheDistance(loaded);
                }
            }
        }
Exemplo n.º 3
0
        /// <param name="provider">Используемый провайдер данных</param>
        /// <param name="points">Точки для первоначального заполенения из базы.</param>
        /// <param name="statisticsTxtAction">Функция для буфера для отображения статистики</param>
        /// <param name="multiThreadLoad">Если <c>true</c> включается моногопоточная загрузка.</param>
        public ExtDistanceCalculator(DistanceProvider provider, DeliveryPoint[] points, Action <string> statisticsTxtAction, bool multiThreadLoad = true)
        {
            this.statisticsTxtAction = statisticsTxtAction;
            UoW.Session.SetBatchSize(SaveBy);
            Provider      = provider;
            MultiTaskLoad = multiThreadLoad;
            Canceled      = false;
            var basesHashes = GeographicGroupRepository.GeographicGroupsWithCoordinates(UoW).Select(CachedDistance.GetHash);

            hashes = points.Select(CachedDistance.GetHash)
                     .Concat(basesHashes)
                     .Distinct()
                     .ToArray();

            cQueue = new ConcurrentQueue <long>(hashes);

            totalPoints       = hashes.Length;
            proposeNeedCached = hashes.Length * (hashes.Length - 1);

            hashPos = hashes.Select((hash, index) => new { hash, index })
                      .ToDictionary(x => x.hash, x => x.index);
#if DEBUG
            matrix      = new CachedDistance[hashes.Length, hashes.Length];
            matrixcount = new int[hashes.Length, hashes.Length];
#endif
            var fromDB = CachedDistanceRepository.GetCache(UoW, hashes);
            startCached = fromDB.Count;
            foreach (var distance in fromDB)
            {
#if DEBUG
                matrix[hashPos[distance.FromGeoHash], hashPos[distance.ToGeoHash]] = distance;
#endif
                AddNewCacheDistance(distance);
            }
            UpdateText();
#if DEBUG
            StringBuilder matrixText = new StringBuilder(" ");
            for (int x = 0; x < matrix.GetLength(1); x++)
            {
                matrixText.Append(x % 10);
            }

            for (int y = 0; y < matrix.GetLength(0); y++)
            {
                matrixText.Append("\n" + y % 10);
                for (int x = 0; x < matrix.GetLength(1); x++)
                {
                    matrixText.Append(matrix[y, x] != null ? 1 : 0);
                }
            }
            logger.Debug(matrixText);

            logger.Debug(string.Join(";", hashes.Select(CachedDistance.GetTextLonLat)));
#endif

            if (MultiTaskLoad && fromDB.Count < proposeNeedCached)
            {
                RunPreCalculation();
            }
            else
            {
                MultiTaskLoad = false;
            }
        }