storage.h 3.4 KB
Newer Older
S
superjom 已提交
1 2
#ifndef VISUALDL_STORAGE_STORAGE_H
#define VISUALDL_STORAGE_STORAGE_H
S
superjom 已提交
3

S
superjom 已提交
4 5
#include <glog/logging.h>
#include <vector>
S
superjom 已提交
6
#include <set>
S
superjom 已提交
7

S
superjom 已提交
8
#include "visualdl/logic/im.h"
S
superjom 已提交
9
#include "visualdl/utils/guard.h"
S
superjom 已提交
10
#include "visualdl/storage/storage.pb.h"
S
superjom 已提交
11
#include "visualdl/storage/tablet.h"
S
superjom 已提交
12
#include "visualdl/utils/filesystem.h"
S
superjom 已提交
13 14 15

namespace visualdl {

S
superjom 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
static const std::string meta_file_name = "storage.meta";

static std::string meta_path(const std::string& dir) {
  CHECK(!dir.empty()) << "dir is empty";
  return dir + "/" + meta_file_name;
}
static std::string tablet_path(const std::string& dir, const std::string& tag) {
  CHECK(!dir.empty()) << "dir should be set first";
  return dir + "/" + tag;
}

struct SimpleSyncMeta {
  void Inc() { counter++; }

  bool ToSync() { return counter % cycle == 0; }

  size_t counter{0};
  int cycle;
};

36
/*
S
superjom 已提交
37
 * Helper for operations on storage::Storage.
38
 */
S
superjom 已提交
39
struct Storage {
S
superjom 已提交
40 41 42 43 44 45
  DECL_GUARD(Storage)

  mutable SimpleSyncMeta meta;

  Storage() { data_ = std::make_shared<storage::Storage>(); }
  Storage(const std::shared_ptr<storage::Storage>& x) : data_(x) {
46 47
    time_t t;
    time(&t);
S
superjom 已提交
48
    data_->set_timestamp(t);
49 50
  }

S
superjom 已提交
51 52
  // write operations
  void AddMode(const std::string& x) {
S
superjom 已提交
53 54
    // avoid duplicate modes.
    if (modes_.count(x) != 0) return;
S
superjom 已提交
55
    *data_->add_modes() = x;
S
superjom 已提交
56
    modes_.insert(x);
S
superjom 已提交
57
    WRITE_GUARD
S
Superjom 已提交
58
  }
59

S
superjom 已提交
60 61 62
  Tablet AddTablet(const std::string& x) {
    CHECK(tablets_.count(x) == 0) << "tablet [" << x << "] has existed";
    tablets_[x] = storage::Tablet();
S
superjom 已提交
63 64
    AddTag(x);
    LOG(INFO) << "really add tag " << x;
S
superjom 已提交
65 66
    WRITE_GUARD
    return Tablet(&tablets_[x], this);
S
superjom 已提交
67
  }
68

S
superjom 已提交
69 70 71 72 73 74
  void SetDir(const std::string& dir) { dir_ = dir; }
  void PersistToDisk() { PersistToDisk(dir_); }
  /*
   * Save memory to disk.
   */
  void PersistToDisk(const std::string& dir) {
S
superjom 已提交
75
    // LOG(INFO) << "persist to disk " << dir;
S
superjom 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88
    CHECK(!dir.empty()) << "dir should be set.";
    fs::TryRecurMkdir(dir);

    fs::SerializeToFile(*data_, meta_path(dir));
    for (auto tag : data_->tags()) {
      auto it = tablets_.find(tag);
      CHECK(it != tablets_.end()) << "tag " << tag << " not exist.";
      fs::SerializeToFile(it->second, tablet_path(dir, tag));
    }
  }

  Storage* parent() { return this; }

S
superjom 已提交
89
protected:
S
superjom 已提交
90 91 92
  void AddTag(const std::string& x) {
    *data_->add_tags() = x;
  }
93

94
private:
S
superjom 已提交
95
  std::string dir_;
96
  std::map<std::string, storage::Tablet> tablets_;
S
superjom 已提交
97
  std::shared_ptr<storage::Storage> data_;
S
superjom 已提交
98
  std::set<std::string> modes_;
S
superjom 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
};

/*
 * Storage reader, each interface will trigger a read.
 */
struct StorageReader {
  StorageReader(const std::string& dir) : dir_(dir) {}

  // read operations
  std::vector<std::string> Tags() {
    storage::Storage storage;
    Reload(storage);
    return std::vector<std::string>(storage.tags().begin(),
                                    storage.tags().end());
  }
  std::vector<std::string> Modes() {
    storage::Storage storage;
    Reload(storage);
    return std::vector<std::string>(storage.modes().begin(),
                                    storage.modes().end());
  }

  TabletReader tablet(const std::string& tag) const {
    auto path = tablet_path(dir_, tag);
    storage::Tablet tablet;
    fs::DeSerializeFromFile(&tablet, path);
    return TabletReader(tablet);
  }

protected:
  void Reload(storage::Storage& storage) {
    const std::string path = meta_path(dir_);
    fs::DeSerializeFromFile(&storage, path);
  }

private:
  std::string dir_;
136 137 138
};

}  // namespace visualdl
S
superjom 已提交
139

S
superjom 已提交
140
#endif