Example #1
0
        /// <summary>
        /// Загрузка базы данных в память
        /// </summary>
        /// <returns></returns>
        public void Load()
        {
            string      filePath = Path.Combine(_environment.ContentRootPath, _dbFileName);
            BinGeoModel binModel;

            using (FileStream stream = new FileStream(filePath, FileMode.Open))
            {
                Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();

                using (BufferedBinaryReader bufferedReader = new BufferedBinaryReader(stream, 65536))
                {
                    stopwatch.Start();

                    bufferedReader.FillBuffer();

                    int    version         = bufferedReader.ReadInt32();
                    byte[] nameBytes       = bufferedReader.Read(0, 32);
                    ulong  timestamp       = bufferedReader.ReadUInt64();
                    int    records         = bufferedReader.ReadInt32();
                    uint   offsetRanges    = bufferedReader.ReadUInt32();
                    uint   offsetCities    = bufferedReader.ReadUInt32();
                    uint   offsetLocations = bufferedReader.ReadUInt32();

                    binModel = new BinGeoModel(version, nameBytes, timestamp, records, offsetRanges, offsetCities, offsetLocations);

                    int currentIndex = 0;
                    binModel.IpRangeCollection = new BinIpRange[binModel.RecordsCount];
                    while (bufferedReader.FillBuffer() && currentIndex < binModel.RecordsCount)
                    {
                        for (; currentIndex < binModel.RecordsCount && bufferedReader.NumBytesAvailable >= IpRangeBytesCount; currentIndex++)
                        {
                            binModel.IpRangeCollection[currentIndex] = new BinIpRange()
                            {
                                IpFrom        = bufferedReader.ReadUInt32(),
                                IpTo          = bufferedReader.ReadUInt32(),
                                LocationIndex = bufferedReader.ReadUInt32()
                            };
                        }
                    }

                    currentIndex = 0;
                    binModel.LocationCollection = new BinLocation[binModel.RecordsCount];
                    while (bufferedReader.FillBuffer() && currentIndex < binModel.RecordsCount)
                    {
                        for (; currentIndex < binModel.RecordsCount && bufferedReader.NumBytesAvailable >= LocationBytesCount; currentIndex++)
                        {
                            byte[] country      = bufferedReader.Read(0, 8);
                            byte[] region       = bufferedReader.Read(0, 12);
                            byte[] postal       = bufferedReader.Read(0, 12);
                            byte[] city         = bufferedReader.Read(0, 24);
                            byte[] organization = bufferedReader.Read(0, 32);
                            float  latitude     = bufferedReader.ReadSingle();
                            float  longitude    = bufferedReader.ReadSingle();

                            binModel.LocationCollection[currentIndex] = new BinLocation(country, region, postal, city, organization, latitude, longitude);
                        }
                    }

                    currentIndex     = 0;
                    binModel.Indexes = new uint[binModel.RecordsCount];
                    while (bufferedReader.FillBuffer() && currentIndex < binModel.RecordsCount)
                    {
                        for (; currentIndex < binModel.RecordsCount && bufferedReader.NumBytesAvailable >= IndexBytesCount; currentIndex++)
                        {
                            binModel.Indexes[currentIndex] = bufferedReader.ReadUInt32();
                        }
                    }

                    stopwatch.Stop();
                    DatabaseLoadedTimeMs = stopwatch.ElapsedMilliseconds;
                    _logger.LogInformation($"Database loading time: {stopwatch.ElapsedMilliseconds} ms");
                }

                // Отображение объектов сущностей двоичной базы в объекты бизнес сущностей
                GeoModel = _mapper.Map <GeoModel>(binModel);
            }
        }