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

S
superjom 已提交
4
#include <algorithm>
S
superjom 已提交
5
#include <set>
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
#include "visualdl/utils/guard.h"
S
superjom 已提交
13 14

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

26 27 28
// 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 已提交
29 30 31 32 33 34 35 36 37
struct SimpleSyncMeta {
  void Inc() { counter++; }

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

  size_t counter{0};
  int cycle;
};

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

  mutable SimpleSyncMeta meta;

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

47 48 49
  // 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);
50

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

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

58 59 60 61 62 63 64
  // 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 已提交
65 66
  Storage* parent() { return this; }

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

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

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

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

88 89 90 91 92 93 94 95
  // 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 已提交
96 97

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

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

}  // namespace visualdl
S
superjom 已提交
109

S
superjom 已提交
110
#endif