forked from organicmaps/organicmaps-tmp
FeaturesOffsets table onload building
This commit is contained in:
parent
7dc3d2e13a
commit
b204554b70
3 changed files with 64 additions and 3 deletions
|
@ -1,5 +1,7 @@
|
|||
#include "features_offsets_table.hpp"
|
||||
|
||||
#include "../indexer/data_header.hpp"
|
||||
#include "../indexer/features_vector.hpp"
|
||||
#include "../coding/file_writer.hpp"
|
||||
#include "../base/assert.hpp"
|
||||
#include "../base/scope_guard.hpp"
|
||||
|
@ -50,6 +52,31 @@ namespace feature
|
|||
return unique_ptr<FeaturesOffsetsTable>(new FeaturesOffsetsTable(std::move(handle)));
|
||||
}
|
||||
|
||||
// static
|
||||
unique_ptr<FeaturesOffsetsTable> FeaturesOffsetsTable::CreateIfNotExistsAndLoad(
|
||||
FilesMappingContainer const & container)
|
||||
{
|
||||
if (container.IsExist(FEATURES_OFFSETS_TABLE_FILE_TAG))
|
||||
return Load(container);
|
||||
|
||||
if (!container.IsExist(HEADER_FILE_TAG))
|
||||
return unique_ptr<FeaturesOffsetsTable>();
|
||||
|
||||
FilesContainerR cont(container.GetName());
|
||||
DataHeader header;
|
||||
header.Load(cont.GetReader(HEADER_FILE_TAG));
|
||||
|
||||
Builder builder;
|
||||
FeaturesVector(cont, header).ForEachOffset([&builder] (FeatureType const &, uint32_t offset)
|
||||
{
|
||||
builder.PushOffset(offset);
|
||||
});
|
||||
unique_ptr<FeaturesOffsetsTable> table(Build(builder));
|
||||
FilesContainerW writeCont(container.GetName(), FileWriter::OP_WRITE_EXISTING);
|
||||
table->Save(writeCont);
|
||||
return table;
|
||||
}
|
||||
|
||||
void FeaturesOffsetsTable::Save(FilesContainerW & container)
|
||||
{
|
||||
string const fileName = container.GetFileName() + "." FEATURES_OFFSETS_TABLE_FILE_TAG;
|
||||
|
|
|
@ -57,6 +57,21 @@ namespace feature
|
|||
/// when it's not possible to load FeaturesOffsetsTable.
|
||||
static unique_ptr<FeaturesOffsetsTable> Load(FilesMappingContainer const & container);
|
||||
|
||||
/// Loads FeaturesOffsetsTable from FilesMappingContainer. Note
|
||||
/// that some part of a file referenced by container will be
|
||||
/// mapped to the memory and used by internal structures of
|
||||
/// FeaturesOffsetsTable.
|
||||
/// If there is no FeaturesOffsetsTable section in the container,
|
||||
/// the function builds it from section devoted to features.
|
||||
///
|
||||
/// \warning May take a lot of time if there is no precomputed section
|
||||
///
|
||||
/// \param container a container with a section devoted to
|
||||
/// FeaturesOffsetsTable
|
||||
/// \return a pointer to an instance of FeaturesOffsetsTable or nullptr
|
||||
/// when it's not possible to create FeaturesOffsetsTable.
|
||||
static unique_ptr<FeaturesOffsetsTable> CreateIfNotExistsAndLoad(FilesMappingContainer const & container);
|
||||
|
||||
FeaturesOffsetsTable(FeaturesOffsetsTable const &) = delete;
|
||||
FeaturesOffsetsTable const & operator=(FeaturesOffsetsTable const &) = delete;
|
||||
|
||||
|
|
|
@ -47,6 +47,27 @@ namespace feature
|
|||
TEST_EQUAL(static_cast<uint64_t>(1024), table->GetFeatureOffset(7), ());
|
||||
}
|
||||
|
||||
UNIT_TEST(FeaturesOffsetsTable_CreateIfNotExistsAndLoad)
|
||||
{
|
||||
Platform & p = GetPlatform();
|
||||
FilesContainerR baseContainer(p.GetReader("minsk-pass" DATA_FILE_EXTENSION));
|
||||
if (baseContainer.IsExist(FEATURES_OFFSETS_TABLE_FILE_TAG))
|
||||
FilesContainerW(baseContainer.GetFileName(), FileWriter::OP_WRITE_EXISTING).DeleteSection(FEATURES_OFFSETS_TABLE_FILE_TAG);
|
||||
FilesMappingContainer mappingContainer(baseContainer.GetFileName());
|
||||
unique_ptr<FeaturesOffsetsTable> table(FeaturesOffsetsTable::CreateIfNotExistsAndLoad(mappingContainer));
|
||||
TEST(table.get(), ());
|
||||
|
||||
feature::DataHeader header;
|
||||
header.Load(baseContainer.GetReader(HEADER_FILE_TAG));
|
||||
|
||||
uint64_t builderSize = 0;
|
||||
FeaturesVector(baseContainer, header).ForEachOffset([&builderSize](FeatureType const & /* type */, uint64_t)
|
||||
{
|
||||
++builderSize;
|
||||
});
|
||||
TEST_EQUAL(builderSize, table->size(), ());
|
||||
}
|
||||
|
||||
UNIT_TEST(FeaturesOffsetsTable_ReadWrite)
|
||||
{
|
||||
Platform & p = GetPlatform();
|
||||
|
@ -55,10 +76,8 @@ namespace feature
|
|||
feature::DataHeader header;
|
||||
header.Load(baseContainer.GetReader(HEADER_FILE_TAG));
|
||||
|
||||
FeaturesVector features(baseContainer, header);
|
||||
|
||||
FeaturesOffsetsTable::Builder builder;
|
||||
features.ForEachOffset([&builder](FeatureType /* type */, uint64_t offset)
|
||||
FeaturesVector(baseContainer, header).ForEachOffset([&builder](FeatureType const & /* type */, uint64_t offset)
|
||||
{
|
||||
builder.PushOffset(offset);
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue