Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvng <viktor.govako@gmail.com>2011-11-25 00:28:17 +0400
committerAlex Zolotarev <alex@maps.me>2015-09-23 01:28:44 +0300
commit65530f86918698ec5cc6dbdd408e6295c28f7c1a (patch)
tree2b95e76f79c16a60b38e0b1a95ac72bebadfba8f
parentc11367ac7accbbff694befc754132c3da8b76540 (diff)
Avoid overhead in classificator reading (read to string -> istringstream).
Add reading of binary proto drawing rules.
-rw-r--r--indexer/classificator.cpp18
-rw-r--r--indexer/classificator.hpp6
-rw-r--r--indexer/classificator_loader.cpp82
-rw-r--r--indexer/classificator_loader.hpp4
-rw-r--r--indexer/drawing_rules.cpp20
-rw-r--r--indexer/drawing_rules.hpp4
-rw-r--r--indexer/tree_structure.hpp2
-rw-r--r--indexer/types_mapping.cpp19
-rw-r--r--indexer/types_mapping.hpp3
9 files changed, 104 insertions, 54 deletions
diff --git a/indexer/classificator.cpp b/indexer/classificator.cpp
index 47c1f03068..f010ad9572 100644
--- a/indexer/classificator.cpp
+++ b/indexer/classificator.cpp
@@ -427,14 +427,12 @@ bool ClassifObject::IsDrawableLike(FeatureGeoType ft) const
return false;
}
-void Classificator::ReadClassificator(string const & buffer)
+void Classificator::ReadClassificator(istream & s)
{
- istringstream iss(buffer);
-
m_root.Clear();
ClassifObject::LoadPolicy policy(&m_root);
- tree::LoadTreeAsText(iss, policy);
+ tree::LoadTreeAsText(s, policy);
m_root.Sort();
@@ -455,12 +453,10 @@ void Classificator::PrintClassificator(char const * fPath)
#endif
}
-void Classificator::ReadVisibility(string const & buffer)
+void Classificator::ReadVisibility(istream & s)
{
- istringstream iss(buffer);
-
ClassifObject::VisLoadPolicy policy(&m_root);
- tree::LoadTreeAsText(iss, policy);
+ tree::LoadTreeAsText(s, policy);
}
void Classificator::PrintVisibility(char const * fPath)
@@ -502,10 +498,10 @@ uint32_t Classificator::GetTypeByPath(vector<string> const & path) const
return type;
}
-void Classificator::ReadTypesMapping(string const & buffer)
+void Classificator::ReadTypesMapping(istream & s)
{
- m_i2t.Load(buffer);
- m_t2i.Load(buffer);
+ m_i2t.Load(s);
+ m_t2i.Load(s);
}
void Classificator::Clear()
diff --git a/indexer/classificator.hpp b/indexer/classificator.hpp
index 8012a8f55c..cc0249215f 100644
--- a/indexer/classificator.hpp
+++ b/indexer/classificator.hpp
@@ -217,13 +217,13 @@ public:
/// @name Serialization-like functions.
//@{
- void ReadClassificator(string const & buffer);
+ void ReadClassificator(istream & s);
void PrintClassificator(char const * fPath);
- void ReadVisibility(string const & buffer);
+ void ReadVisibility(istream & s);
void PrintVisibility(char const * fPath);
- void ReadTypesMapping(string const & buffer);
+ void ReadTypesMapping(istream & s);
void SortClassificator();
//@}
diff --git a/indexer/classificator_loader.cpp b/indexer/classificator_loader.cpp
index bec3a794fc..5ad83f25e3 100644
--- a/indexer/classificator_loader.cpp
+++ b/indexer/classificator_loader.cpp
@@ -4,41 +4,51 @@
#include "../../platform/platform.hpp"
-#include "../coding/file_reader_stream.hpp"
-#include "../coding/file_reader.hpp"
+#include "../coding/reader_streambuf.hpp"
#include "../base/logging.hpp"
+#include "../std/fstream.hpp"
+
namespace classificator
{
- void ReadCommon(ReaderType const & classificator,
- ReaderType const & visibility,
- ReaderType const & types)
+ void ReadCommon(Reader * classificator,
+ Reader * visibility,
+ Reader * types)
{
- string buffer;
-
Classificator & c = classif();
c.Clear();
- //LOG(LINFO, ("Reading classificator"));
- classificator.ReadAsString(buffer);
- c.ReadClassificator(buffer);
+ {
+ //LOG(LINFO, ("Reading classificator"));
+ ReaderStreamBuf buffer(classificator);
+
+ istream s(&buffer);
+ c.ReadClassificator(s);
+ }
- //LOG(LINFO, ("Reading visibility"));
- visibility.ReadAsString(buffer);
- c.ReadVisibility(buffer);
+ {
+ //LOG(LINFO, ("Reading visibility"));
+ ReaderStreamBuf buffer(visibility);
- //LOG(LINFO, ("Reading types mapping"));
- types.ReadAsString(buffer);
- c.ReadTypesMapping(buffer);
+ istream s(&buffer);
+ c.ReadVisibility(s);
+ }
+
+ {
+ //LOG(LINFO, ("Reading types mapping"));
+ ReaderStreamBuf buffer(types);
+
+ istream s(&buffer);
+ c.ReadTypesMapping(s);
+ }
}
void ReadVisibility(string const & fPath)
{
- string buffer;
- ReaderType(new FileReader(fPath)).ReadAsString(buffer);
- classif().ReadVisibility(buffer);
+ ifstream s(fPath.c_str());
+ classif().ReadVisibility(s);
}
void Load()
@@ -52,11 +62,35 @@ namespace classificator
p.GetReader("types.txt"));
//LOG(LINFO, ("Reading of drawing rules"));
-
- // Load from protobuffer text file.
- string buffer;
- ReaderType(p.GetReader("drules_proto.txt")).ReadAsString(buffer);
- drule::rules().LoadFromTextProto(buffer);
+ drule::RulesHolder & rules = drule::rules();
+
+ try
+ {
+ // Load from protobuffer binary file.
+ ReaderStreamBuf buffer(p.GetReader("drules_proto.bin"));
+
+ istream s(&buffer);
+ rules.LoadFromBinaryProto(s);
+ }
+ catch (Reader::OpenException const &)
+ {
+ try
+ {
+ // Load from protobuffer text file.
+ string buffer;
+ ModelReaderPtr(p.GetReader("drules_proto.txt")).ReadAsString(buffer);
+
+ rules.LoadFromTextProto(buffer);
+
+ // Uncomment this to save actual drawing rules to binary proto format.
+ //ofstream s(p.WritablePathForFile("drules_proto.bin").c_str(), ios::out | ios::binary);
+ //rules.SaveToBinaryProto(buffer, s);
+ }
+ catch (Reader::OpenException const &)
+ {
+ LOG(LERROR, ("No drawing rules found"));
+ }
+ }
LOG(LINFO, ("Reading of classificator finished"));
}
diff --git a/indexer/classificator_loader.hpp b/indexer/classificator_loader.hpp
index 627ba690b2..c49876456a 100644
--- a/indexer/classificator_loader.hpp
+++ b/indexer/classificator_loader.hpp
@@ -1,14 +1,10 @@
#pragma once
-#include "../coding/reader.hpp"
-
#include "../std/string.hpp"
namespace classificator
{
- typedef ReaderPtr<Reader> ReaderType;
-
void ReadVisibility(string const & fPath);
void Load();
diff --git a/indexer/drawing_rules.cpp b/indexer/drawing_rules.cpp
index 6d6b29b43e..684b8b8be7 100644
--- a/indexer/drawing_rules.cpp
+++ b/indexer/drawing_rules.cpp
@@ -273,6 +273,26 @@ void RulesHolder::LoadFromTextProto(string const & buffer)
DoSetIndex doSet(*this);
google::protobuf::TextFormat::ParseFromString(buffer, &doSet.m_cont);
+
+ classif().GetMutableRoot()->ForEachObject(bind<void>(ref(doSet), _1));
+}
+
+void RulesHolder::SaveToBinaryProto(string const & buffer, ostream & s)
+{
+ ContainerProto cont;
+ google::protobuf::TextFormat::ParseFromString(buffer, &cont);
+
+ CHECK ( cont.SerializeToOstream(&s), ("Error in proto saving!") );
+}
+
+void RulesHolder::LoadFromBinaryProto(istream & s)
+{
+ Clean();
+
+ DoSetIndex doSet(*this);
+
+ CHECK ( doSet.m_cont.ParseFromIstream(&s), ("Error in proto loading!") );
+
classif().GetMutableRoot()->ForEachObject(bind<void>(ref(doSet), _1));
}
diff --git a/indexer/drawing_rules.hpp b/indexer/drawing_rules.hpp
index 15b66cfb86..61c97f3a4c 100644
--- a/indexer/drawing_rules.hpp
+++ b/indexer/drawing_rules.hpp
@@ -8,6 +8,7 @@
#include "../std/vector.hpp"
#include "../std/array.hpp"
#include "../std/string.hpp"
+#include "../std/fstream.hpp"
class LineDefProto;
@@ -119,6 +120,9 @@ namespace drule
void LoadFromTextProto(string const & buffer);
+ static void SaveToBinaryProto(string const & buffer, ostream & s);
+ void LoadFromBinaryProto(istream & s);
+
template <class ToDo> void ForEachRule(ToDo toDo)
{
for (rules_map_t::const_iterator i = m_rules.begin(); i != m_rules.end(); ++i)
diff --git a/indexer/tree_structure.hpp b/indexer/tree_structure.hpp
index fb4a0f9800..605fbd2c30 100644
--- a/indexer/tree_structure.hpp
+++ b/indexer/tree_structure.hpp
@@ -59,7 +59,7 @@ namespace tree
}
template <class ToDo>
- bool LoadTreeAsText(istringstream & s, ToDo & toDo)
+ bool LoadTreeAsText(istream & s, ToDo & toDo)
{
string name;
s >> name;
diff --git a/indexer/types_mapping.cpp b/indexer/types_mapping.cpp
index e25029d3a0..ed9a8382f7 100644
--- a/indexer/types_mapping.cpp
+++ b/indexer/types_mapping.cpp
@@ -5,26 +5,25 @@
#include "../base/stl_add.hpp"
-void BaseTypeMapper::Load(string const & buffer)
+void BaseTypeMapper::Load(istream & s)
{
- istringstream ss(buffer);
Classificator const & c = classif();
string v;
vector<string> path;
uint32_t ind = 0;
- while (true)
+ while (s.good())
{
- ss >> v;
+ s >> v;
- if (ss.eof())
- break;
+ if (!v.empty())
+ {
+ path.clear();
+ strings::Tokenize(v, "|", MakeBackInsertFunctor(path));
- path.clear();
- strings::Tokenize(v, "|", MakeBackInsertFunctor(path));
-
- Add(ind++, c.GetTypeByPath(path));
+ Add(ind++, c.GetTypeByPath(path));
+ }
}
}
diff --git a/indexer/types_mapping.hpp b/indexer/types_mapping.hpp
index 57605c4278..79eb5a72c7 100644
--- a/indexer/types_mapping.hpp
+++ b/indexer/types_mapping.hpp
@@ -3,6 +3,7 @@
#include "../std/vector.hpp"
#include "../std/map.hpp"
+#include "../std/fstream.hpp"
class BaseTypeMapper
@@ -11,7 +12,7 @@ protected:
virtual void Add(uint32_t ind, uint32_t type) = 0;
public:
- void Load(string const & buffer);
+ void Load(istream & s);
};
class Index2Type : public BaseTypeMapper