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

S
superjom 已提交
4
#include <time.h>
5 6
#include <map>
#include <string>
S
superjom 已提交
7 8 9 10 11

#include "visualdl/backend/storage/storage.pb.h"

namespace visualdl {

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
/*
 * Generate a tablet path in disk from its tag.
 */
inline std::string GenPathFromTag(const std::string &dir,
                                  const std::string &tag);

/*
 * Storage Interface. The might be a bunch of implementations, for example, a
 * MemStorage that keep a copy of all the taplets in memory, can be changed with
 * a higher performance; a DiskStorage that keep all the data in disk, apply to
 * the scenerios where memory consumption should be considered.
 */
class StorageBase {
public:
  const static std::string meta_file_name;

  void SetStorage(const std::string &dir) {
    time_t t;
    time(&t);
    storage_.set_timestamp(t);
    storage_.set_dir(dir);
  }

  /*
   * Create a new Tablet storage.
   */
  virtual storage::Tablet *NewTablet(const std::string &tag,
                                     int num_samples) = 0;

  /*
   * Get a tablet from memory, this can be viewed as a cache, if the storage is
   * in disk, a hash map in memory will first load the corresponding Tablet
   * Protobuf from disk and hold all the changes.
   */
  virtual storage::Tablet *tablet(const std::string &tag) = 0;

  /*
   * Persist the data from cache to disk. Both the memory storage or disk
   * storage should write changes to disk for persistence.
   */
  virtual void PersistToDisk() const = 0;

  /*
   * Load data from disk.
   */
  virtual void LoadFromDisk(const std::string &dir) = 0;

protected:
  storage::Storage storage_;
};

/*
 * Storage in Memory, that will support quick edits on data.
 */
class MemoryStorage final : public StorageBase {
public:
  storage::Tablet *NewTablet(const std::string &tag, int num_samples) override;

  storage::Tablet *tablet(const std::string &tag) override;

  void PersistToDisk() const override;

  void LoadFromDisk(const std::string &dir) override;

private:
  std::map<std::string, storage::Tablet> tablets_;
};

S
superjom 已提交
80
class Storage final {
S
superjom 已提交
81 82 83 84 85 86
public:
  Storage() {
    // set time stamp
    time_t time0;
    time(&time0);
    proto_.set_timestamp(time0);
S
superjom 已提交
87
  }
S
superjom 已提交
88 89 90 91

  /*
   * Add a new tablet named `tag`, the newly added instance will be returned.
   */
S
superjom 已提交
92
  storage::Tablet *Add(const std::string &tag, int num_samples);
S
superjom 已提交
93 94 95 96

  /*
   * Search the tablet named `tag`, if not exist, return nullptr.
   */
S
superjom 已提交
97 98 99 100 101 102 103 104 105 106 107
  storage::Tablet *Find(const std::string &tag);

  /*
   * Append a new record to the tail of tablet.
   */
  storage::Record *NewRecord(const std::string &tag);

  /*
   * Get a record at `offset`, if the offset is not valid, yield a failed CHECK.
   */
  storage::Record *GetRecord(const std::string &tag, int offset);
S
superjom 已提交
108 109 110 111 112 113 114 115 116 117 118

  /*
   * Serialize this object to string and save it to a file.
   */
  void Save(const std::string &path) const;

  /*
   * Load the Protobuf message from a file.
   */
  void Load(const std::string &path);

S
superjom 已提交
119 120 121 122 123
  storage::Storage *mutable_data() { return &proto_; }

  const storage::Storage &data() { return proto_; }

protected:
S
superjom 已提交
124 125 126 127 128 129 130 131 132 133
  /*
   * Serialize the Storage instance to string.
   */
  std::string Serialize() const;

  /*
   * De-serialize from a string and update this Storage instance.
   */
  void DeSerialize(const std::string &data);

S
superjom 已提交
134
private:
S
superjom 已提交
135
  storage::Storage proto_;
S
superjom 已提交
136 137
};

138
}  // namespace visualdl
S
superjom 已提交
139

140
#endif  // VISUALDL_STORAGE_H