Add submodule https://github.com/msgpack/msgpack-c and make Serialize/Deserialize aware of SerializeFormat

This commit is contained in:
Fangrui Song 2018-01-06 15:29:53 -08:00
parent 734f9b6380
commit 3f1cb5c072
8 changed files with 32 additions and 7 deletions

3
.gitmodules vendored
View File

@ -10,3 +10,6 @@
[submodule "third_party/loguru"] [submodule "third_party/loguru"]
path = third_party/loguru path = third_party/loguru
url = https://github.com/emilk/loguru url = https://github.com/emilk/loguru
[submodule "third_party/msgpack-c"]
path = third_party/msgpack-c
url = https://github.com/msgpack/msgpack-c

View File

@ -27,17 +27,27 @@ std::string GetCachedBaseFileName(Config* config,
return config->cacheDirectory + cache_file; return config->cacheDirectory + cache_file;
} }
std::string GetCacheFileName(Config* config, const std::string& base) {
switch (config->cacheFormat) {
case SerializeFormat::Json:
return base + "json";
case SerializeFormat::MessagePack:
return base + "mpack";
}
}
std::unique_ptr<IndexFile> LoadCachedIndex(Config* config, std::unique_ptr<IndexFile> LoadCachedIndex(Config* config,
const std::string& filename) { const std::string& filename) {
if (!config->enableCacheRead) if (!config->enableCacheRead)
return nullptr; return nullptr;
optional<std::string> file_content = optional<std::string> file_content = ReadContent(
ReadContent(GetCachedBaseFileName(config, filename) + ".json"); GetCacheFileName(config, GetCachedBaseFileName(config, filename)));
if (!file_content) if (!file_content)
return nullptr; return nullptr;
return Deserialize(filename, *file_content, IndexFile::kCurrentVersion); return Deserialize(config->cacheFormat, filename, *file_content,
IndexFile::kCurrentVersion);
} }
// Manages loading caches from file paths for the indexer process. // Manages loading caches from file paths for the indexer process.
@ -132,5 +142,5 @@ void WriteToCache(Config* config, IndexFile& file) {
} }
std::string indexed_content = Serialize(file); std::string indexed_content = Serialize(file);
WriteToFile(cache_basename + ".json", indexed_content); WriteToFile(GetCacheFileName(config, cache_basename), indexed_content);
} }

View File

@ -11,6 +11,8 @@ struct Config {
std::string compilationDatabaseDirectory; std::string compilationDatabaseDirectory;
// Cache directory for indexed files. // Cache directory for indexed files.
std::string cacheDirectory; std::string cacheDirectory;
// Cache serialization format
SerializeFormat cacheFormat = SerializeFormat::Json;
// Value to use for clang -resource-dir if not present in // Value to use for clang -resource-dir if not present in
// compile_commands.json. // compile_commands.json.
std::string resourceDirectory; std::string resourceDirectory;

View File

@ -2,6 +2,7 @@
// TODO Move Json* to serializers/json.cc // TODO Move Json* to serializers/json.cc
#include "serializers/json.h" #include "serializers/json.h"
//#include "serializers/msgpack.h"
#include "indexer.h" #include "indexer.h"
@ -208,7 +209,8 @@ std::string Serialize(IndexFile& file) {
return output.GetString(); return output.GetString();
} }
std::unique_ptr<IndexFile> Deserialize(std::string path, std::unique_ptr<IndexFile> Deserialize(SerializeFormat format,
std::string path,
std::string serialized, std::string serialized,
optional<int> expected_version) { optional<int> expected_version) {
rapidjson::Document reader; rapidjson::Document reader;
@ -226,6 +228,8 @@ std::unique_ptr<IndexFile> Deserialize(std::string path,
} }
} }
// TODO msgpack
(void)format;
auto file = MakeUnique<IndexFile>(path); auto file = MakeUnique<IndexFile>(path);
JsonReader json_reader{&reader}; JsonReader json_reader{&reader};
Reflect(json_reader, *file); Reflect(json_reader, *file);

View File

@ -8,6 +8,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
enum class SerializeFormat { Json, MessagePack };
class Reader { class Reader {
public: public:
virtual ~Reader() {} virtual ~Reader() {}
@ -236,7 +238,8 @@ void ReflectMember(Reader& visitor, const char* name, T& value) {
} }
std::string Serialize(IndexFile& file); std::string Serialize(IndexFile& file);
std::unique_ptr<IndexFile> Deserialize(std::string path, std::unique_ptr<IndexFile> Deserialize(SerializeFormat format,
std::string path,
std::string serialized, std::string serialized,
optional<int> expected_version); optional<int> expected_version);

View File

@ -76,7 +76,8 @@ void DiffDocuments(std::string path,
void VerifySerializeToFrom(IndexFile* file) { void VerifySerializeToFrom(IndexFile* file) {
std::string expected = file->ToString(); std::string expected = file->ToString();
std::unique_ptr<IndexFile> result = std::unique_ptr<IndexFile> result =
Deserialize("--.cc", Serialize(*file), nullopt /*expected_version*/); Deserialize(SerializeFormat::Json, "--.cc", Serialize(*file),
nullopt /*expected_version*/);
std::string actual = result->ToString(); std::string actual = result->ToString();
if (expected != actual) { if (expected != actual) {
std::cerr << "Serialization failure" << std::endl; std::cerr << "Serialization failure" << std::endl;

1
third_party/msgpack-c vendored Submodule

@ -0,0 +1 @@
Subproject commit 208595b2620cf6260ce3d6d4cf8543f13b206449

View File

@ -362,6 +362,7 @@ def build(bld):
'third_party/', 'third_party/',
'third_party/doctest/', 'third_party/doctest/',
'third_party/loguru/', 'third_party/loguru/',
'third_party/msgpack-c/include',
'third_party/rapidjson/include/', 'third_party/rapidjson/include/',
'third_party/sparsepp/'] + 'third_party/sparsepp/'] +
(['libclang'] if bld.env['use_clang_cxx'] else []), (['libclang'] if bld.env['use_clang_cxx'] else []),