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

S
superjom 已提交
4 5 6
#include "visualdl/storage/storage.h"
#include "visualdl/utils/concurrency.h"
#include "visualdl/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
  CHECK(!storage_.dir().empty()) << "storage's dir should be set first";
S
superjom 已提交
44 45 46
  VLOG(3) << "persist storage to disk path " << storage_.dir();
  // make a directory if not exist
  fs::TryMkdir(storage_.dir());
47
  // write storage out
48
  fs::SerializeToFile(storage_, meta_path());
49 50 51 52
  // write all the tablets
  for (auto tag : storage_.tags()) {
    auto it = tablets_.find(tag);
    CHECK(it != tablets_.end());
53
    fs::SerializeToFile(it->second, tablet_path(tag));
54 55 56
  }
}

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

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

S
superjom 已提交
72 73
void MemoryStorage::StartReadService() {
  cc::PeriodExector::task_t task = [this] {
S
superjom 已提交
74
    VLOG(3) << "loading from " << storage_.dir();
S
superjom 已提交
75 76 77
    LoadFromDisk(storage_.dir());
    return true;
  };
S
superjom 已提交
78 79
  cc::PeriodExector::Global().Start();
  VLOG(3) << "push read task";
S
superjom 已提交
80 81 82 83
  cc::PeriodExector::Global()(std::move(task), 2512);
}

void MemoryStorage::StartWriteSerice() {
S
superjom 已提交
84
  cc::PeriodExector::Global().Start();
S
superjom 已提交
85 86 87 88 89 90
  cc::PeriodExector::task_t task = [this] {
    PersistToDisk();
    return true;
  };
  cc::PeriodExector::Global()(std::move(task), 2000);
}
S
superjom 已提交
91
}  // namespace visualdl