| 1 | #include "HierarchyFormatReader.h" |
| 2 | |
| 3 | #include <IO/ReadHelpers.h> |
| 4 | #include <IO/WriteHelpers.h> |
| 5 | |
| 6 | |
| 7 | bool RegionsHierarchyFormatReader::readNext(RegionEntry & entry) |
| 8 | { |
| 9 | while (!input->eof()) |
| 10 | { |
| 11 | /** Our internal geobase has negative numbers, |
| 12 | * that means "this is garbage, ignore this row". |
| 13 | */ |
| 14 | Int32 read_region_id = 0; |
| 15 | Int32 read_parent_id = 0; |
| 16 | Int8 read_type = 0; |
| 17 | |
| 18 | DB::readIntText(read_region_id, *input); |
| 19 | DB::assertChar('\t', *input); |
| 20 | DB::readIntText(read_parent_id, *input); |
| 21 | DB::assertChar('\t', *input); |
| 22 | DB::readIntText(read_type, *input); |
| 23 | |
| 24 | /** Then there can be a newline (old version) |
| 25 | * or tab, the region's population, line feed (new version). |
| 26 | */ |
| 27 | RegionPopulation population = 0; |
| 28 | if (!input->eof() && *input->position() == '\t') |
| 29 | { |
| 30 | ++input->position(); |
| 31 | UInt64 population_big = 0; |
| 32 | DB::readIntText(population_big, *input); |
| 33 | population = population_big > std::numeric_limits<RegionPopulation>::max() ? std::numeric_limits<RegionPopulation>::max() |
| 34 | : population_big; |
| 35 | } |
| 36 | DB::assertChar('\n', *input); |
| 37 | |
| 38 | if (read_region_id <= 0 || read_type < 0) |
| 39 | continue; |
| 40 | |
| 41 | RegionID region_id = read_region_id; |
| 42 | RegionID parent_id = 0; |
| 43 | |
| 44 | if (read_parent_id >= 0) |
| 45 | parent_id = read_parent_id; |
| 46 | |
| 47 | RegionType type = static_cast<RegionType>(read_type); |
| 48 | |
| 49 | entry.id = region_id; |
| 50 | entry.parent_id = parent_id; |
| 51 | entry.type = type; |
| 52 | entry.population = population; |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | return false; |
| 57 | } |
| 58 | |