storage.cc 3.4 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";

S
Superjom 已提交
12 13 14
std::string StorageBase::meta_path(const std::string &dir) const {
  CHECK(!dir.empty()) << "dir is empty";
  return dir + "/" + meta_file_name;
15
}
S
Superjom 已提交
16 17 18 19
std::string StorageBase::tablet_path(const std::string &dir,
                                     const std::string &tag) const {
  CHECK(!dir.empty()) << "dir should be set first";
  return dir + "/" + tag;
20 21
}

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

42
// TODO add some checksum to avoid unnecessary saving
S
Superjom 已提交
43 44 45
void MemoryStorage::PersistToDisk(const std::string &dir) {
  CHECK(!dir.empty());
  storage_.set_dir(dir);
S
superjom 已提交
46
  // make a directory if not exist
47
  fs::TryRecurMkdir(dir);
48
  // write storage out
S
superjom 已提交
49
  VLOG(2) << "to serize meta to dir " << dir;
S
Superjom 已提交
50
  fs::SerializeToFile(storage_, meta_path(dir));
S
superjom 已提交
51
  VLOG(2) << "serize meta to dir " << dir;
52 53 54 55
  // write all the tablets
  for (auto tag : storage_.tags()) {
    auto it = tablets_.find(tag);
    CHECK(it != tablets_.end());
S
Superjom 已提交
56
    fs::SerializeToFile(it->second, tablet_path(dir, tag));
57 58 59
  }
}

60
// TODO add some checksum to avoid unnecessary loading
61
void MemoryStorage::LoadFromDisk(const std::string &dir) {
S
superjom 已提交
62
  CHECK(!dir.empty()) << "dir is empty";
63
  storage_.set_dir(dir);
64
  // load storage
S
Superjom 已提交
65 66
  CHECK(fs::DeSerializeFromFile(&storage_, meta_path(dir)))
      << "parse from " << meta_path(dir) << " failed";
67
  // load all the tablets
S
superjom 已提交
68
  for (int i = 0; i < storage_.tags_size(); i++) {
69
    auto tag = storage_.tags(i);
S
Superjom 已提交
70
    CHECK(fs::DeSerializeFromFile(&tablets_[tag], tablet_path(dir, tag)));
S
superjom 已提交
71 72 73
  }
}

S
Superjom 已提交
74 75 76 77 78 79
void MemoryStorage::StartReadService(const std::string &dir,
                                     int msecs,
                                     std::mutex *handler) {
  CHECK(executor_ != nullptr);
  CHECK(!dir.empty()) << "dir should be set first";
  cc::PeriodExector::task_t task = [dir, this, handler] {
S
superjom 已提交
80
    VLOG(1) << "loading from " << dir;
S
Superjom 已提交
81 82 83 84 85 86
    if (handler != nullptr) {
      std::lock_guard<std::mutex> _(*handler);
      LoadFromDisk(dir);
    } else {
      LoadFromDisk(dir);
    }
S
superjom 已提交
87 88
    return true;
  };
S
Superjom 已提交
89
  // executor_.Start();
S
superjom 已提交
90
  VLOG(1) << "push read task";
S
Superjom 已提交
91
  (*executor_)(std::move(task), msecs);
S
superjom 已提交
92 93
}

S
superjom 已提交
94 95 96
void MemoryStorage::StartWriteService(const std::string &dir,
                                      int msecs,
                                      std::mutex *handler) {
S
Superjom 已提交
97 98 99 100 101
  CHECK(executor_ != nullptr);
  CHECK(!dir.empty()) << "dir should be set first";
  storage_.set_dir(dir);
  // executor_.Start();
  cc::PeriodExector::task_t task = [dir, handler, this] {
S
superjom 已提交
102
    VLOG(2) << "persist to disk";
S
Superjom 已提交
103 104 105 106 107 108
    if (handler != nullptr) {
      std::lock_guard<std::mutex> _(*handler);
      PersistToDisk(dir);
    } else {
      PersistToDisk(dir);
    }
S
superjom 已提交
109 110
    return true;
  };
S
Superjom 已提交
111
  (*executor_)(std::move(task), msecs);
S
superjom 已提交
112
}
S
superjom 已提交
113
}  // namespace visualdl