storage.h 3.4 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2017 VisualDL Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

S
superjom 已提交
15 16
#ifndef VISUALDL_STORAGE_STORAGE_H
#define VISUALDL_STORAGE_STORAGE_H
S
superjom 已提交
17

Q
Qiao Longfei 已提交
18
#include "visualdl/utils/logging.h"
S
superjom 已提交
19
#include <algorithm>
S
superjom 已提交
20
#include <set>
S
superjom 已提交
21
#include <vector>
S
superjom 已提交
22

S
superjom 已提交
23
#include "visualdl/logic/im.h"
S
superjom 已提交
24
#include "visualdl/storage/storage.pb.h"
S
superjom 已提交
25
#include "visualdl/storage/tablet.h"
S
superjom 已提交
26
#include "visualdl/utils/filesystem.h"
S
superjom 已提交
27
#include "visualdl/utils/guard.h"
S
superjom 已提交
28 29

namespace visualdl {
S
superjom 已提交
30 31 32 33 34 35 36
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) {
37
  CHECK(!dir.empty()) << "dir is empty";
S
superjom 已提交
38 39 40
  return dir + "/" + tag;
}

41 42 43
// 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 已提交
44 45 46 47 48 49 50 51 52
struct SimpleSyncMeta {
  void Inc() { counter++; }

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

  size_t counter{0};
  int cycle;
};

53
// Helper class for operations on storage::Storage.
S
superjom 已提交
54
struct Storage {
S
superjom 已提交
55 56 57 58
  DECL_GUARD(Storage)

  mutable SimpleSyncMeta meta;

59 60
  Storage();
  Storage(const Storage& other);
61

62 63 64
  // 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);
65

66 67
  // Add a tablet which tag is `x`.
  Tablet AddTablet(const std::string& x);
68

69
  // Set storage's directory.
S
superjom 已提交
70
  void SetDir(const std::string& dir) { *dir_ = dir; }
S
superjom 已提交
71
  std::string dir() const { return *dir_; }
S
superjom 已提交
72

73 74 75 76 77 78 79
  // 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 已提交
80 81
  Storage* parent() { return this; }

S
superjom 已提交
82
protected:
83
  // Add a tag which content is `x`.
S
superjom 已提交
84 85
  void AddTag(const std::string& x) {
    *data_->add_tags() = x;
S
superjom 已提交
86
    WRITE_GUARD
S
superjom 已提交
87
  }
88

89
private:
S
superjom 已提交
90
  std::shared_ptr<std::string> dir_;
S
superjom 已提交
91
  std::shared_ptr<std::map<std::string, storage::Tablet>> tablets_;
S
superjom 已提交
92
  std::shared_ptr<storage::Storage> data_;
S
superjom 已提交
93
  std::shared_ptr<std::set<std::string>> modes_;
S
superjom 已提交
94 95
};

96
// Storage reader, each method will trigger a reading from disk.
S
superjom 已提交
97 98 99
struct StorageReader {
  StorageReader(const std::string& dir) : dir_(dir) {}

100 101
  // Get all tags of the storage.
  std::vector<std::string> all_tags();
S
superjom 已提交
102

103 104 105 106 107 108 109 110
  // 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 已提交
111 112

protected:
113
  // Load meta from disk to memory.
S
superjom 已提交
114 115 116 117 118 119 120
  void Reload(storage::Storage& storage) {
    const std::string path = meta_path(dir_);
    fs::DeSerializeFromFile(&storage, path);
  }

private:
  std::string dir_;
121 122 123
};

}  // namespace visualdl
S
superjom 已提交
124

S
superjom 已提交
125
#endif