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

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

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

namespace visualdl {

S
superjom 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
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;
};

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

  mutable SimpleSyncMeta meta;

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

S
superjom 已提交
50 51 52 53
  // write operations
  void AddMode(const std::string& x) {
    *data_->add_modes() = x;
    WRITE_GUARD
S
Superjom 已提交
54
  }
55

S
superjom 已提交
56 57 58 59
  Tablet AddTablet(const std::string& x) {
    AddTag(x);
    CHECK(tablets_.count(x) == 0) << "tablet [" << x << "] has existed";
    tablets_[x] = storage::Tablet();
S
superjom 已提交
60 61
    WRITE_GUARD
    return Tablet(&tablets_[x], this);
S
superjom 已提交
62
  }
63

S
superjom 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  void SetDir(const std::string& dir) { dir_ = dir; }
  void PersistToDisk() { PersistToDisk(dir_); }
  /*
   * Save memory to disk.
   */
  void PersistToDisk(const std::string& dir) {
    LOG(INFO) << "persist to disk " << dir;
    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 已提交
84
protected:
S
superjom 已提交
85 86 87 88
  void AddTag(const std::string& x) {
    WRITE_GUARD
    *data_->add_tags() = x;
  }
89

90
private:
S
superjom 已提交
91
  std::string dir_;
92
  std::map<std::string, storage::Tablet> tablets_;
S
superjom 已提交
93 94 95 96 97 98 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
  std::shared_ptr<storage::Storage> data_;
};

/*
 * 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_;
131 132 133
};

}  // namespace visualdl
S
superjom 已提交
134

S
superjom 已提交
135
#endif