Skip to content

Commit

Permalink
add implementation of storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Superjomn committed Nov 17, 2017
1 parent d072761 commit e5c1ef4
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 8 deletions.
19 changes: 19 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
45 changes: 45 additions & 0 deletions visualdl/backend/storage/storage.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <fstream>
#include <glog/logging.h>

#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
5 changes: 0 additions & 5 deletions visualdl/backend/storage/storage.cpp

This file was deleted.

59 changes: 56 additions & 3 deletions visualdl/backend/storage/storage.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,65 @@
#ifndef VISUALDL_STORAGE_H
#define VISUALDL_STORAGE_H

class Storage {
#include <string>
#include <time.h>

#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

0 comments on commit e5c1ef4

Please sign in to comment.