storage.cc 2.9 KB
Newer Older
S
superjom 已提交
1
#include <glog/logging.h>
S
superjom 已提交
2
#include <visualdl/backend/utils/concurrency.h>
S
superjom 已提交
3
#include <fstream>
S
superjom 已提交
4 5

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

namespace visualdl {

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

12 13 14 15 16 17 18 19 20
std::string StorageBase::meta_path() const {
  CHECK(!storage_.dir().empty()) << "storage.dir should be set first";
  return storage_.dir() + "/" + meta_file_name;
}
std::string StorageBase::tablet_path(const std::string &tag) const {
  CHECK(!storage_.dir().empty()) << "storage.dir should be set first";
  return storage_.dir() + "/" + tag;
}

21 22 23 24 25 26
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();
S
superjom 已提交
27
    tablets_[tag].set_tag(tag);
28 29 30 31 32 33 34 35 36 37 38 39 40
    *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;
}

41
// TODO add some checksum to avoid unnecessary saving
42
void MemoryStorage::PersistToDisk() const {
43 44 45 46
  LOG(INFO) << "inside dir " << storage_.dir() << " "
            << (!storage_.dir().empty());
  CHECK(!storage_.dir().empty()) << "storage's dir should be set first";
  LOG(INFO) << "after check dir";
S
superjom 已提交
47 48 49
  VLOG(3) << "persist storage to disk path " << storage_.dir();
  // make a directory if not exist
  fs::TryMkdir(storage_.dir());
50
  // write storage out
51
  fs::SerializeToFile(storage_, meta_path());
52 53 54 55
  // write all the tablets
  for (auto tag : storage_.tags()) {
    auto it = tablets_.find(tag);
    CHECK(it != tablets_.end());
56
    fs::SerializeToFile(it->second, tablet_path(tag));
57 58 59
  }
}

60
// TODO add some checksum to avoid unnecessary loading
61
void MemoryStorage::LoadFromDisk(const std::string &dir) {
S
superjom 已提交
62 63
  VLOG(3) << "load storage from disk path " << dir;
  CHECK(!dir.empty()) << "dir is empty";
64
  storage_.set_dir(dir);
65
  // load storage
66 67
  CHECK(fs::DeSerializeFromFile(&storage_, meta_path()))
      << "parse from " << meta_path() << " failed";
68 69

  // load all the tablets
S
superjom 已提交
70
  for (int i = 0; i < storage_.tags_size(); i++) {
71 72
    auto tag = storage_.tags(i);
    CHECK(fs::DeSerializeFromFile(&tablets_[tag], tablet_path(tag)));
S
superjom 已提交
73 74 75
  }
}

S
superjom 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
void MemoryStorage::StartReadService() {
  cc::PeriodExector::task_t task = [this] {
    LOG(INFO) << "loading from " << storage_.dir();
    LoadFromDisk(storage_.dir());
    return true;
  };
  cc::PeriodExector::Global()(std::move(task), 2512);
}

void MemoryStorage::StartWriteSerice() {
  cc::PeriodExector::task_t task = [this] {
    LOG(INFO) << "writing to " << storage_.dir();
    PersistToDisk();
    return true;
  };
  cc::PeriodExector::Global()(std::move(task), 2000);
}
S
superjom 已提交
93
}  // namespace visualdl