[Refactoring] Use mwm-file descriptor (string name) in Index. Open FileReader only when necessary.

This commit is contained in:
vng 2011-07-03 20:16:43 +03:00 committed by Alex Zolotarev
parent adde2107d1
commit ae88aa1971
19 changed files with 91 additions and 96 deletions

View file

@ -147,14 +147,18 @@ FileWriter FilesContainerW::GetExistingWriter(Tag const & tag)
MYTHROW(Writer::OpenException, (tag));
}
void FilesContainerW::Append(string const & fName, Tag const & tag)
void FilesContainerW::Append(string const & fPath, Tag const & tag)
{
Append(new FileReader(fPath), tag);
}
void FilesContainerW::Append(ModelReaderPtr reader, Tag const & tag)
{
ASSERT(!m_bFinished, ());
uint64_t const bufferSize = 4*1024;
char buffer[bufferSize];
FileReader reader(fName);
ReaderSource<FileReader> src(reader);
ReaderSource<ModelReaderPtr> src(reader);
FileWriter writer = GetWriter(tag);
uint64_t size = reader.Size();

View file

@ -83,7 +83,8 @@ public:
/// @name Append to existing container.
/// @precondition Container should be constructed with OP_APPEND.
//@{
void Append(string const & fName, Tag const & tag);
void Append(string const & fPath, Tag const & tag);
void Append(ModelReaderPtr reader, Tag const & tag);
void Append(vector<char> const & buffer, Tag const & tag);
//@}

View file

@ -11,11 +11,11 @@ void Reader::ReadAsString(string & s) const
Read(0, &s[0], sz);
}
bool ModelReader::IsEqual(string const & fName) const
bool Reader::IsEqual(string const & name1, string const & name2)
{
#if defined(OMIM_OS_WINDOWS)
return strings::EqualNoCase(fName, m_name);
return strings::EqualNoCase(name1, name2);
#else
return (fName == m_name);
return (name1 == name2);
#endif
}

View file

@ -26,6 +26,8 @@ public:
virtual Reader * CreateSubReader(uint64_t pos, uint64_t size) const = 0;
void ReadAsString(string & s) const;
static bool IsEqual(string const & name1, string const & name2);
};
// Reader from memory.
@ -122,18 +124,9 @@ public:
{
return m_p->CreateSubReader(pos, size);
}
inline bool IsEqual(string const & name) const
{
return m_p->IsEqual(name);
}
inline bool IsEqual(ModelReaderPtr const & file) const
{
return m_p->IsEqual(file.m_p->GetName());
}
};
// Source that reads from a reader.
template <typename ReaderT> class ReaderSource
{

View file

@ -48,10 +48,10 @@ namespace feature
return first.second > second.second;
}
void DumpTypes(string const & datFile)
void DumpTypes(string const & fPath)
{
TypesCollector doClass;
feature::ForEachFromDat(new FileReader(datFile), doClass);
feature::ForEachFromDat(fPath, doClass);
typedef vector<stats_elem_type> vec_to_sort;
vec_to_sort vecToSort(doClass.m_stats.begin(), doClass.m_stats.end());

View file

@ -4,5 +4,5 @@
namespace feature
{
void DumpTypes(string const & datFile);
void DumpTypes(string const & fPath);
}

View file

@ -17,9 +17,9 @@ using namespace feature;
namespace stats
{
void FileContainerStatistic(string const & fName)
void FileContainerStatistic(string const & fPath)
{
FilesContainerR cont(fName);
FilesContainerR cont(fPath);
vector<string> tags;
tags.push_back(DATA_FILE_TAG);
@ -79,10 +79,10 @@ namespace stats
}
};
void CalcStatistic(string const & fName, MapInfo & info)
void CalcStatistic(string const & fPath, MapInfo & info)
{
AccumulateStatistic doProcess(info);
feature::ForEachFromDat(new FileReader(fName), doProcess);
feature::ForEachFromDat(fPath, doProcess);
}
void PrintInfo(char const * prefix, GeneralInfo const & info)

View file

@ -69,8 +69,8 @@ namespace stats
}
};
void FileContainerStatistic(string const & fName);
void FileContainerStatistic(string const & fPath);
void CalcStatistic(string const & fName, MapInfo & info);
void CalcStatistic(string const & fPath, MapInfo & info);
void PrintStatistic(MapInfo & info);
}

View file

@ -2,6 +2,7 @@
#include "features_vector.hpp"
#include "../coding/file_reader.hpp"
#include "../coding/file_container.hpp"
#include "../std/bind.hpp"
@ -10,13 +11,19 @@
namespace feature
{
template <class ToDo>
void ForEachFromDat(ModelReaderPtr const & file, ToDo & toDo)
void ForEachFromDat(ModelReaderPtr reader, ToDo & toDo)
{
FilesContainerR container(file);
FilesContainerR container(reader);
FeaturesVector featureSource(container);
featureSource.ForEachOffset(bind<void>(ref(toDo), _1, _2));
}
template <class ToDo>
void ForEachFromDat(string const & fPath, ToDo & toDo)
{
ForEachFromDat(new FileReader(fPath), toDo);
}
/// Read feature from feature source.
template <class TSource>
void ReadFromSourceRowFormat(TSource & src, FeatureBuilder1 & f)

View file

@ -8,6 +8,8 @@
#include "../../defines.hpp"
#include "../platform/platform.hpp"
#include "../geometry/rect2d.hpp"
#include "../coding/file_container.hpp"
@ -134,7 +136,7 @@ public:
}
}
void Add(ModelReaderPtr const & file)
void Add(string const & file)
{
threads::MutexGuard mutexGuard(m_mutex);
UNUSED_VALUE(mutexGuard);
@ -148,14 +150,14 @@ public:
UpdateIndexes();
}
void Remove(string const & path)
void Remove(string const & file)
{
threads::MutexGuard mutexGuard(m_mutex);
UNUSED_VALUE(mutexGuard);
for (size_t i = 0; i < m_indexes.size(); ++i)
{
if (m_indexes[i]->IsMyData(path))
if (m_indexes[i]->IsMyData(file))
m_indexes[i]->m_action = IndexProxy::INDEX_REMOVE;
}
@ -218,15 +220,12 @@ private:
class IndexProxy
{
public:
typedef ModelReaderPtr ReaderT;
explicit IndexProxy(ReaderT const & file)
explicit IndexProxy(string const & file)
: m_action(INDEX_DO_NOTHING), m_file(file), m_pIndex(NULL), m_lockCount(0),
m_queriesSkipped(0)
{
// TODO: If path is cellid-style-square, make rect from cellid and don't open the file.
feature::DataHeader header;
header.Load(FilesContainerR(m_file).GetReader(HEADER_FILE_TAG));
header.Load(FilesContainerR(GetPlatform().GetReader(m_file)).GetReader(HEADER_FILE_TAG));
m_rect = header.GetBounds();
m_scaleRange = header.GetScaleRange();
@ -266,13 +265,9 @@ private:
return m_lockCount == 0;
}
bool IsMyData(string const & path) const
bool IsMyData(string const & file) const
{
return m_file.IsEqual(path);
}
bool IsMyData(ReaderT const & file) const
{
return m_file.IsEqual(file);
return Reader::IsEqual(m_file, file);
}
void CloseIfUnlocked()
@ -312,8 +307,7 @@ private:
{
if (!m_pIndex)
{
// LOG(LINFO, (m_Path));
FilesContainerR container(m_file);
FilesContainerR container(GetPlatform().GetReader(m_file));
m_pIndex = new IndexT(container);
}
}
@ -329,7 +323,7 @@ private:
}
}
ReaderT m_file;
string m_file;
m2::RectD m_rect;
pair<int, int> m_scaleRange;

View file

@ -4,9 +4,13 @@
#include "../index_builder.hpp"
#include "../classificator_loader.hpp"
#include "../features_vector.hpp"
#include "../../defines.hpp"
#include "../../platform/platform.hpp"
#include "../../coding/file_container.hpp"
#include "../../base/stl_add.hpp"
@ -17,7 +21,7 @@ UNIT_TEST(BuildIndexTest)
p.GetReader("classificator.txt"),
p.GetReader("visibility.txt"));
FilesContainerR originalContainer(p.WritablePathForFile("minsk-pass" DATA_FILE_EXTENSION));
FilesContainerR originalContainer(p.GetReader("minsk-pass" DATA_FILE_EXTENSION));
// Build index.
vector<char> serialIndex;
@ -30,26 +34,18 @@ UNIT_TEST(BuildIndexTest)
// Create a new mwm file.
string const fileName = "build_index_test" DATA_FILE_EXTENSION;
FileWriter::DeleteFileX(fileName);
string const filePath = p.WritablePathForFile(fileName);
FileWriter::DeleteFileX(filePath);
// Copy original mwm file and replace index in it.
{
FilesContainerW containerWriter(fileName);
FilesContainerW containerWriter(filePath);
vector<string> tags;
originalContainer.ForEachTag(MakeBackInsertFunctor(tags));
for (size_t i = 0; i < tags.size(); ++i)
{
if (tags[i] != INDEX_FILE_TAG)
{
FilesContainerR::ReaderT reader = originalContainer.GetReader(tags[i]);
size_t const sz = static_cast<size_t>(reader.Size());
if (sz > 0)
{
vector<char> data(sz);
reader.Read(0, &data[0], sz);
containerWriter.Append(data, tags[i]);
}
}
containerWriter.Append(originalContainer.GetReader(tags[i]), tags[i]);
}
containerWriter.Append(serialIndex, INDEX_FILE_TAG);
}
@ -57,12 +53,12 @@ UNIT_TEST(BuildIndexTest)
{
// Check that index actually works.
Index<ModelReaderPtr>::Type index;
index.Add(new FileReader(fileName));
index.Add(fileName);
// Make sure that index is actually parsed.
index.ForEachInScale(NoopFunctor(), 15);
}
// Clean after the test.
FileWriter::DeleteFileX(fileName);
FileWriter::DeleteFileX(filePath);
}

View file

@ -1,17 +1,16 @@
#include "../../testing/testing.hpp"
#include "../index.hpp"
#include "../index_builder.hpp"
#include "../../platform/platform.hpp"
#include "../../coding/file_container.hpp"
#include "../../base/macros.hpp"
#include "../../base/stl_add.hpp"
#include "../../std/string.hpp"
UNIT_TEST(IndexParseTest)
{
Index<ModelReaderPtr>::Type index;
index.Add(GetPlatform().GetReader("minsk-pass" DATA_FILE_EXTENSION));
index.Add("minsk-pass" DATA_FILE_EXTENSION);
// Make sure that index is actually parsed.
index.ForEachInScale(NoopFunctor(), 15);

View file

@ -37,7 +37,7 @@ void FeaturesFetcher::InitClassificator()
}
}
void FeaturesFetcher::AddMap(ReaderT const & file)
void FeaturesFetcher::AddMap(string const & file)
{
try
{

View file

@ -37,10 +37,9 @@ namespace model
mutable index_t::Query m_multiIndexQuery;
public:
void InitClassificator();
void AddMap(ReaderT const & file);
void AddMap(string const & file);
void RemoveMap(string const & fName);
void Clean();
void ClearCaches();

View file

@ -286,19 +286,15 @@ void FrameWork<TModel>::AddRedrawCommandSure()
}
template <typename TModel>
void FrameWork<TModel>::AddMap(ReaderT const & file)
void FrameWork<TModel>::AddMap(string const & file)
{
// update rect for Show All button
feature::DataHeader header;
header.Load(FilesContainerR(file).GetReader(HEADER_FILE_TAG));
header.Load(FilesContainerR(GetPlatform().GetReader(file)).GetReader(HEADER_FILE_TAG));
m_model.AddWorldRect(header.GetBounds());
m2::RectD bounds = header.GetBounds();
m_model.AddWorldRect(bounds);
{
threads::MutexGuard lock(m_modelSyn);
m_model.AddMap(file);
}
threads::MutexGuard lock(m_modelSyn);
m_model.AddMap(file);
}
template <typename TModel>
@ -508,7 +504,7 @@ void FrameWork<TModel>::AddRedrawCommandSure()
class ReadersAdder
{
protected:
typedef vector<ModelReaderPtr> maps_list_t;
typedef vector<string> maps_list_t;
private:
Platform & m_pl;
@ -517,9 +513,9 @@ void FrameWork<TModel>::AddRedrawCommandSure()
public:
ReadersAdder(Platform & pl, maps_list_t & lst) : m_pl(pl), m_lst(lst) {}
void operator() (string const & f)
void operator() (string const & name)
{
m_lst.push_back(m_pl.GetReader(f));
m_lst.push_back(name);
}
};

View file

@ -183,7 +183,7 @@ class FrameWork
typedef typename TModel::ReaderT ReaderT;
void AddMap(ReaderT const & file);
void AddMap(string const & file);
void RemoveMap(string const & datFile);
void OnGpsUpdate(location::GpsInfo const & info);
@ -202,7 +202,7 @@ public:
model_t & get_model();
typedef vector<ReaderT> maps_list_t;
typedef vector<string> maps_list_t;
void EnumLocalMaps(maps_list_t & filesList);
void EnumBenchmarkMaps(maps_list_t & filesList);

View file

@ -2,8 +2,6 @@
#include "../../testing/testing.hpp"
#include "../../geometry/rect_intersect.hpp"
#include "../../platform/platform.hpp"
#include "../../map/feature_vec_model.hpp"
@ -14,6 +12,8 @@
#include "../../indexer/feature_processor.hpp"
#include "../../indexer/classificator.hpp"
#include "../../geometry/rect_intersect.hpp"
#include "../../base/logging.hpp"
#include "../../std/string.hpp"
@ -227,14 +227,15 @@ namespace
}
};
void RunTest(ModelReaderPtr const & file)
void RunTest(string const & file)
{
model::FeaturesFetcher src1;
src1.InitClassificator();
src1.AddMap(file);
feature::DataHeader mapInfo;
mapInfo.Load(FilesContainerR(file).GetReader(HEADER_FILE_TAG));
ModelReaderPtr reader = GetPlatform().GetReader(file);
mapInfo.Load(FilesContainerR(reader).GetReader(HEADER_FILE_TAG));
vector<m2::RectD> rects;
rects.push_back(mapInfo.GetBounds());
@ -247,7 +248,7 @@ namespace
feature_cont_t v1, v2;
for_each_in_rect<AccumulatorBase>(src1, v1, r);
file_source_t src2(file);
file_source_t src2(reader);
for_each_in_rect<AccumulatorEtalon>(src2, v2, r);
int const level = scales::GetScaleLevel(r);
@ -278,7 +279,7 @@ namespace
char c;
cin >> c;
if (c == 'y')
RunTest(GetPlatform().GetReader(fName + DATA_FILE_EXTENSION));
RunTest(fName + DATA_FILE_EXTENSION);
}
}

View file

@ -53,7 +53,6 @@ namespace storage
m_removeMap = removeFunc;
m_updateRect = updateRectFunc;
typedef vector<ModelReaderPtr> map_list_t;
map_list_t filesList;
enumMapsFunc(filesList);
@ -170,8 +169,7 @@ namespace storage
}
void operator()(TTile const & tile)
{
string const file = m_workingDir + tile.first;
m_removeFn(file);
m_removeFn(tile.first);
}
};
@ -353,12 +351,18 @@ namespace storage
if (size.second != 0)
m_countryProgress.m_current = size.first;
/// @todo Get file reader from download framework.
// activate downloaded map piece
m_addMap(new FileReader(result.m_file));
// get file descriptor
string file = result.m_file;
string::size_type const i = file.find_last_of('/');
if (i != string::npos)
file = file.substr(i+1);
// activate downloaded map piece
m_addMap(file);
// update rect from downloaded file
feature::DataHeader header;
header.Load(FilesContainerR(result.m_file).GetReader(HEADER_FILE_TAG));
header.Load(FilesContainerR(GetPlatform().GetReader(file)).GetReader(HEADER_FILE_TAG));
m_updateRect(header.GetBounds());
}
DownloadNextCountryFromQueue();

View file

@ -88,11 +88,12 @@ namespace storage
/// @name Communicate with Framework
//@{
typedef vector<string> map_list_t;
public:
typedef function<void (ModelReaderPtr const &)> TAddMapFunction;
typedef function<void (string const &)> TAddMapFunction;
typedef function<void (string const &)> TRemoveMapFunction;
typedef function<void (m2::RectD const & r)> TUpdateRectFunction;
typedef function<void (vector<ModelReaderPtr> &)> TEnumMapsFunction;
typedef function<void (map_list_t &)> TEnumMapsFunction;
private:
TAddMapFunction m_addMap;