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

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

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

namespace visualdl {

class Storage final {
S
superjom 已提交
12 13 14 15 16 17
public:
  Storage() {
    // set time stamp
    time_t time0;
    time(&time0);
    proto_.set_timestamp(time0);
S
superjom 已提交
18
  }
S
superjom 已提交
19 20 21 22

  /*
   * Add a new tablet named `tag`, the newly added instance will be returned.
   */
S
superjom 已提交
23
  storage::Tablet *Add(const std::string &tag, int num_samples);
S
superjom 已提交
24 25 26 27

  /*
   * Search the tablet named `tag`, if not exist, return nullptr.
   */
S
superjom 已提交
28 29 30 31 32 33 34 35 36 37 38
  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 已提交
39 40 41 42 43 44 45 46 47 48 49

  /*
   * 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 已提交
50 51 52 53 54
  storage::Storage *mutable_data() { return &proto_; }

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

protected:
S
superjom 已提交
55 56 57 58 59 60 61 62 63 64
  /*
   * 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 已提交
65
private:
S
superjom 已提交
66
  storage::Storage proto_;
S
superjom 已提交
67 68
};

S
superjom 已提交
69 70
} // namespace visualdl

S
superjom 已提交
71
#endif // VISUALDL_STORAGE_H