From 3f1cb5c072f0b2c16b6a8c26ea94f1ae6f63e73f Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sat, 6 Jan 2018 15:29:53 -0800 Subject: [PATCH] Add submodule https://github.com/msgpack/msgpack-c and make Serialize/Deserialize aware of SerializeFormat --- .gitmodules | 3 +++ src/cache_manager.cc | 18 ++++++++++++++---- src/config.h | 2 ++ src/serializer.cc | 6 +++++- src/serializer.h | 5 ++++- src/test.cc | 3 ++- third_party/msgpack-c | 1 + wscript | 1 + 8 files changed, 32 insertions(+), 7 deletions(-) create mode 160000 third_party/msgpack-c diff --git a/.gitmodules b/.gitmodules index 27ff39f0..e2c0733d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "third_party/loguru"] path = third_party/loguru url = https://github.com/emilk/loguru +[submodule "third_party/msgpack-c"] + path = third_party/msgpack-c + url = https://github.com/msgpack/msgpack-c diff --git a/src/cache_manager.cc b/src/cache_manager.cc index 9060482c..cc0e0c27 100644 --- a/src/cache_manager.cc +++ b/src/cache_manager.cc @@ -27,17 +27,27 @@ std::string GetCachedBaseFileName(Config* config, 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 LoadCachedIndex(Config* config, const std::string& filename) { if (!config->enableCacheRead) return nullptr; - optional file_content = - ReadContent(GetCachedBaseFileName(config, filename) + ".json"); + optional file_content = ReadContent( + GetCacheFileName(config, GetCachedBaseFileName(config, filename))); if (!file_content) 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. @@ -132,5 +142,5 @@ void WriteToCache(Config* config, IndexFile& file) { } std::string indexed_content = Serialize(file); - WriteToFile(cache_basename + ".json", indexed_content); + WriteToFile(GetCacheFileName(config, cache_basename), indexed_content); } diff --git a/src/config.h b/src/config.h index 7835e651..1ef56944 100644 --- a/src/config.h +++ b/src/config.h @@ -11,6 +11,8 @@ struct Config { std::string compilationDatabaseDirectory; // Cache directory for indexed files. std::string cacheDirectory; + // Cache serialization format + SerializeFormat cacheFormat = SerializeFormat::Json; // Value to use for clang -resource-dir if not present in // compile_commands.json. std::string resourceDirectory; diff --git a/src/serializer.cc b/src/serializer.cc index 36da86a8..3b5c824b 100644 --- a/src/serializer.cc +++ b/src/serializer.cc @@ -2,6 +2,7 @@ // TODO Move Json* to serializers/json.cc #include "serializers/json.h" +//#include "serializers/msgpack.h" #include "indexer.h" @@ -208,7 +209,8 @@ std::string Serialize(IndexFile& file) { return output.GetString(); } -std::unique_ptr Deserialize(std::string path, +std::unique_ptr Deserialize(SerializeFormat format, + std::string path, std::string serialized, optional expected_version) { rapidjson::Document reader; @@ -226,6 +228,8 @@ std::unique_ptr Deserialize(std::string path, } } + // TODO msgpack + (void)format; auto file = MakeUnique(path); JsonReader json_reader{&reader}; Reflect(json_reader, *file); diff --git a/src/serializer.h b/src/serializer.h index 2edaa809..b79055b5 100644 --- a/src/serializer.h +++ b/src/serializer.h @@ -8,6 +8,8 @@ #include #include +enum class SerializeFormat { Json, MessagePack }; + class Reader { public: virtual ~Reader() {} @@ -236,7 +238,8 @@ void ReflectMember(Reader& visitor, const char* name, T& value) { } std::string Serialize(IndexFile& file); -std::unique_ptr Deserialize(std::string path, +std::unique_ptr Deserialize(SerializeFormat format, + std::string path, std::string serialized, optional expected_version); diff --git a/src/test.cc b/src/test.cc index 7ea0d898..23a5d5a8 100644 --- a/src/test.cc +++ b/src/test.cc @@ -76,7 +76,8 @@ void DiffDocuments(std::string path, void VerifySerializeToFrom(IndexFile* file) { std::string expected = file->ToString(); std::unique_ptr result = - Deserialize("--.cc", Serialize(*file), nullopt /*expected_version*/); + Deserialize(SerializeFormat::Json, "--.cc", Serialize(*file), + nullopt /*expected_version*/); std::string actual = result->ToString(); if (expected != actual) { std::cerr << "Serialization failure" << std::endl; diff --git a/third_party/msgpack-c b/third_party/msgpack-c new file mode 160000 index 00000000..208595b2 --- /dev/null +++ b/third_party/msgpack-c @@ -0,0 +1 @@ +Subproject commit 208595b2620cf6260ce3d6d4cf8543f13b206449 diff --git a/wscript b/wscript index 90a48fee..0a550e6d 100644 --- a/wscript +++ b/wscript @@ -362,6 +362,7 @@ def build(bld): 'third_party/', 'third_party/doctest/', 'third_party/loguru/', + 'third_party/msgpack-c/include', 'third_party/rapidjson/include/', 'third_party/sparsepp/'] + (['libclang'] if bld.env['use_clang_cxx'] else []),