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

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

S
superjom 已提交
9
#include "visualdl/logic/im.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
#include "visualdl/utils/guard.h"
S
superjom 已提交
14 15

namespace visualdl {
S
superjom 已提交
16 17 18 19 20 21 22
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) {
23
  CHECK(!dir.empty()) << "dir is empty";
S
superjom 已提交
24 25 26
  return dir + "/" + tag;
}

27 28 29
// A simple logic to sync storage between memory and disk. Each writing
// operation will trigger an `Inc`, and check whether `ToSync`, if true, write
// memory to disk.
S
superjom 已提交
30 31 32 33 34 35 36 37 38
struct SimpleSyncMeta {
  void Inc() { counter++; }

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

  size_t counter{0};
  int cycle;
};

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

  mutable SimpleSyncMeta meta;

45 46
  Storage();
  Storage(const Storage& other);
47

48 49 50
  // Add a mode. Mode is similar to TB's FileWriter, It can be "train" or "test"
  // or something else.
  void AddMode(const std::string& x);
51

52 53
  // Add a tablet which tag is `x`.
  Tablet AddTablet(const std::string& x);
54

55
  // Set storage's directory.
S
superjom 已提交
56
  void SetDir(const std::string& dir) { *dir_ = dir; }
S
superjom 已提交
57
  std::string dir() const { return *dir_; }
S
superjom 已提交
58

59 60 61 62 63 64 65
  // Save content in memory to `dir_`.
  void PersistToDisk();

  // Save content in memory to `dir`.
  void PersistToDisk(const std::string& dir);

  // A trick help to retrieve the storage's `SimpleSyncMeta`.
S
superjom 已提交
66 67
  Storage* parent() { return this; }

S
superjom 已提交
68
protected:
69
  // Add a tag which content is `x`.
S
superjom 已提交
70 71
  void AddTag(const std::string& x) {
    *data_->add_tags() = x;
S
superjom 已提交
72
    WRITE_GUARD
S
superjom 已提交
73
  }
74

75
private:
S
superjom 已提交
76
  std::shared_ptr<std::string> dir_;
S
superjom 已提交
77
  std::shared_ptr<std::map<std::string, storage::Tablet>> tablets_;
S
superjom 已提交
78
  std::shared_ptr<storage::Storage> data_;
S
superjom 已提交
79
  std::shared_ptr<std::set<std::string>> modes_;
S
superjom 已提交
80 81
};

82
// Storage reader, each method will trigger a reading from disk.
S
superjom 已提交
83 84 85
struct StorageReader {
  StorageReader(const std::string& dir) : dir_(dir) {}

86 87
  // Get all tags of the storage.
  std::vector<std::string> all_tags();
S
superjom 已提交
88

89 90 91 92 93 94 95 96
  // Get all the tags of the tablet of the kind of `component`.
  std::vector<std::string> tags(Tablet::Type component);

  // Get all the modes of the storage.
  std::vector<std::string> modes();

  // Get a tablet whose tag is `tag`.
  TabletReader tablet(const std::string& tag) const;
S
superjom 已提交
97 98

protected:
99
  // Load meta from disk to memory.
S
superjom 已提交
100 101 102 103 104 105 106
  void Reload(storage::Storage& storage) {
    const std::string path = meta_path(dir_);
    fs::DeSerializeFromFile(&storage, path);
  }

private:
  std::string dir_;
107 108 109
};

}  // namespace visualdl
S
superjom 已提交
110

S
superjom 已提交
111
#endif