From 01ba48384b9d10982a1e33dbaab44e878ef64d0f Mon Sep 17 00:00:00 2001 From: superjom Date: Fri, 17 Nov 2017 10:38:56 +0800 Subject: [PATCH] add implementation of storage --- CMakeLists.txt | 19 +++++++++ visualdl/backend/storage/storage.cc | 45 +++++++++++++++++++++ visualdl/backend/storage/storage.cpp | 5 --- visualdl/backend/storage/storage.h | 59 ++++++++++++++++++++++++++-- 4 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 visualdl/backend/storage/storage.cc delete mode 100644 visualdl/backend/storage/storage.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..bb45c59e --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.8) +project(VisualDL) + +set(CMAKE_CXX_STANDARD 11) + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) +include_directories(thirdparty/local/include) +link_directories(thirdparty/local/lib) +add_subdirectory(thirdparty/pybind11-2.2.1) + +set(SOURCE_FILES + visualdl/backend/storage/storage.cc visualdl/backend/storage/storage.h + visualdl/backend/storage/storage.pb.h + visualdl/backend/storage/storage.pb.cc + ) + +add_library(storage visualdl/backend/storage/storage.cc + visualdl/backend/storage/storage.pb.cc) +add_executable(VisualDL ${SOURCE_FILES}) \ No newline at end of file diff --git a/visualdl/backend/storage/storage.cc b/visualdl/backend/storage/storage.cc new file mode 100644 index 00000000..5af049d9 --- /dev/null +++ b/visualdl/backend/storage/storage.cc @@ -0,0 +1,45 @@ +#include +#include + +#include "visualdl/backend/storage/storage.h" + +namespace visualdl { + +storage::Tablet *Storage::Add(const std::string &tag) { + return &proto_.mutable_tablets()->at(tag); +} + +const storage::Tablet *Storage::Find(const std::string &tag) const { + auto it = proto_.tablets().find(tag); + if (it != proto_.tablets().end()) { + return &it->second; + } + return nullptr; +} + +void Storage::Save(const std::string &path) const { + std::ofstream file(path, file.binary | file.out); + CHECK(file.is_open()) << "can't open path " << path; + auto str = Serialize(); + file.write(str.c_str(), str.size()); +} + +void Storage::Load(const std::string &path) { + std::ifstream file(path, file.binary); + CHECK(file.is_open()) << "can't open path " << path; + size_t size = file.tellg(); + std::string buffer(size, ' '); + file.seekg(0); + file.read(&buffer[0], size); + DeSerialize(buffer); +} + +std::string Storage::Serialize() const { + return proto_.SerializeAsString(); +} + +void Storage::DeSerialize(const std::string &data) { + proto_.ParseFromString(data); +} + +} // namespace visualdl diff --git a/visualdl/backend/storage/storage.cpp b/visualdl/backend/storage/storage.cpp deleted file mode 100644 index 222a32a3..00000000 --- a/visualdl/backend/storage/storage.cpp +++ /dev/null @@ -1,5 +0,0 @@ -// -// Created by superjom on 17-11-16. -// - -#include "storage.h" diff --git a/visualdl/backend/storage/storage.h b/visualdl/backend/storage/storage.h index 06ab7bec..aec08a87 100644 --- a/visualdl/backend/storage/storage.h +++ b/visualdl/backend/storage/storage.h @@ -1,12 +1,65 @@ #ifndef VISUALDL_STORAGE_H #define VISUALDL_STORAGE_H -class Storage { +#include +#include + +#include "visualdl/backend/storage/storage.pb.h" + +namespace visualdl { + +class Storage final { public: - Storage& Global() { - static Storage* instance = new Storage(); + /* + * There should be only one Storage instance in memory. + */ + Storage &Global() { + static Storage *instance = new Storage(); return *instance; } + + /* + * Add a new tablet named `tag`, the newly added instance will be returned. + */ + storage::Tablet *Add(const std::string &tag); + + /* + * Search the tablet named `tag`, if not exist, return nullptr. + */ + const storage::Tablet *Find(const std::string &tag) const; + + /* + * Serialize this object to string and save it to a file. + */ + void Save(const std::string &path) const; + + /* + * Load the Protobuf message from a file. + */ + void Load(const std::string &path); + + protected: + /* + * Serialize the Storage instance to string. + */ + std::string Serialize() const; + + /* + * De-serialize from a string and update this Storage instance. + */ + void DeSerialize(const std::string &data); + + Storage() { + // set time stamp + time_t time0; + time(&time0); + proto_.set_timestamp(time0); + } + + private: + storage::Storage proto_; }; +} // namespace visualdl + #endif //VISUALDL_STORAGE_H -- GitLab