storage.cc 3.4 KB
Newer Older
S
superjom 已提交
1
#include <glog/logging.h>
S
superjom 已提交
2
#include <fstream>
S
superjom 已提交
3 4

#include "visualdl/backend/storage/storage.h"
5
#include "visualdl/backend/utils/filesystem.h"
S
superjom 已提交
6 7 8

namespace visualdl {

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
std::string GenPathFromTag(const std::string &dir, const std::string &tag) {
  return dir + "/" + tag;
}

const std::string StorageBase::meta_file_name = "storage.meta";

storage::Tablet *MemoryStorage::NewTablet(const std::string &tag,
                                          int num_samples) {
  auto it = tablets_.find(tag);
  if (it == tablets_.end()) {
    // create new tablet
    tablets_[tag] = storage::Tablet();
    *storage_.add_tags() = tag;
  } else {
    return &it->second;
  }
  return &tablets_[tag];
}

storage::Tablet *MemoryStorage::tablet(const std::string &tag) {
  auto it = tablets_.find(tag);
  CHECK(it != tablets_.end()) << "tablet tagged as " << tag << " not exists";
  return &it->second;
}

void MemoryStorage::PersistToDisk() const {
  // write storage out
  const auto meta_path = storage_.dir() + "/" + meta_file_name;
  fs::Write(meta_path, storage_.SerializeAsString());
  // write all the tablets
  for (auto tag : storage_.tags()) {
    auto path = GenPathFromTag(storage_.dir(), tag);
    auto it = tablets_.find(tag);
    CHECK(it != tablets_.end());
    fs::Write(path, it->second.SerializeAsString());
  }
}

void MemoryStorage::LoadFromDisk(const std::string &dir) {
  // load storage
  const auto meta_path = storage_.dir() + "/" + meta_file_name;
  storage_.ParseFromString(fs::Read(meta_path));

  // load all the tablets
  for (auto tag : storage_.tags()) {
    auto path = GenPathFromTag(storage_.dir(), tag);
    tablets_[tag];
    tablets_[tag].ParseFromString(fs::Read(path));
  }
}

S
superjom 已提交
60 61 62 63
storage::Tablet *Storage::Add(const std::string &tag, int num_samples) {
  auto *tablet = &(*proto_.mutable_tablets())[tag];
  tablet->set_num_samples(num_samples);
  return tablet;
S
superjom 已提交
64 65
}

S
superjom 已提交
66 67
storage::Tablet *Storage::Find(const std::string &tag) {
  auto it = proto_.mutable_tablets()->find(tag);
S
superjom 已提交
68 69 70 71 72 73
  if (it != proto_.tablets().end()) {
    return &it->second;
  }
  return nullptr;
}

S
superjom 已提交
74 75 76 77 78
storage::Record *Storage::NewRecord(const std::string &tag) {
  auto *tablet = Find(tag);
  CHECK(tablet) << "Tablet" << tag << " should be create first";
  auto *record = tablet->mutable_records()->Add();
  // increase num_records
S
superjom 已提交
79 80
  int num_records = tablet->total_records();
  tablet->set_total_records(num_records + 1);
S
superjom 已提交
81 82 83 84 85 86
  return record;
}
storage::Record *Storage::GetRecord(const std::string &tag, int offset) {
  auto *tablet = Find(tag);
  CHECK(tablet) << "Tablet" << tag << " should be create first";

S
superjom 已提交
87
  auto num_records = tablet->total_records();
S
superjom 已提交
88 89 90 91
  CHECK_LT(offset, num_records) << "invalid offset";
  return tablet->mutable_records()->Mutable(offset);
}

S
superjom 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
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);
}

S
superjom 已提交
109
std::string Storage::Serialize() const { return proto_.SerializeAsString(); }
S
superjom 已提交
110 111 112 113 114

void Storage::DeSerialize(const std::string &data) {
  proto_.ParseFromString(data);
}

S
superjom 已提交
115
}  // namespace visualdl