sdk.h 4.2 KB
Newer Older
S
superjom 已提交
1 2
#ifndef VISUALDL_LOGIC_SDK_H
#define VISUALDL_LOGIC_SDK_H
S
superjom 已提交
3

S
superjom 已提交
4 5
#include <glog/logging.h>
#include <time.h>
S
superjom 已提交
6 7
#include <map>

S
superjom 已提交
8
#include "visualdl/logic/im.h"
S
superjom 已提交
9

S
superjom 已提交
10 11
namespace visualdl {

S
superjom 已提交
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
/*
 * Utility helper for storage::Entry.
 */
template <typename T>
struct EntryHelper {
  // use pointer to avoid copy
  storage::Entry *entry{nullptr};

  EntryHelper() {}
  explicit EntryHelper(storage::Entry *entry) : entry(entry) {}
  void operator()(storage::Entry *entry) { this->entry = entry; }

  /*
   * Set a single value.
   */
  void Set(T v);

  /*
   * Add a value to repeated message field.
   */
  void Add(T v);

  /*
   * Get a single value.
   */
  T Get() const;

  /*
   * Get repeated field.
   */
  std::vector<T> GetMulti() const;
};

S
superjom 已提交
45 46 47
class TabletHelper {
public:
  // basic member getter and setter
S
superjom 已提交
48 49 50
  std::string record_buffer(int idx) const {
    return data_->records(idx).SerializeAsString();
  }
S
superjom 已提交
51 52 53 54 55
  size_t records_size() const { return data_->records_size(); }
  std::string buffer() const { return data_->SerializeAsString(); }
  std::string human_readable_buffer() const;
  void SetBuffer(const storage::Tablet &t) { *data_ = t; }
  void SetBuffer(const std::string &b) { data_->ParseFromString(b); }
S
superjom 已提交
56
  storage::Tablet &data() const { return *data_; }
S
superjom 已提交
57 58 59 60

  // constructor that enable concurrency.
  TabletHelper(storage::Tablet *t) : data_(t) {}
  // data updater that resuage of one instance.
S
superjom 已提交
61 62 63 64
  TabletHelper &operator()(storage::Tablet *t) {
    data_ = t;
    return *this;
  }
S
superjom 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

private:
  storage::Tablet *data_;
};

class StorageHelper {
public:
  StorageHelper(storage::Storage *s) : data_(s) {}
  StorageHelper &operator()(storage::Storage *s) {
    data_ = s;
    return *this;
  }

  void SetBuffer(const storage::Storage &buffer) { *data_ = buffer; }
  void SetBuffer(const std::string &buffer) { data_->ParseFromString(buffer); }
S
superjom 已提交
80 81 82 83
  void SetDir(const std::string &dir) {
    CHECK(data_) << "no storage instance hold";
    data_->set_dir(dir);
  }
S
superjom 已提交
84 85 86 87 88 89 90 91

  int64_t timestamp() const { return data_->timestamp(); }
  std::string dir() const { return data_->dir(); }
  int tablets_size() const { return data_->tablets_size(); }
  std::string buffer() const { return data_->SerializeAsString(); }
  std::string human_readable_buffer() const;

private:
S
superjom 已提交
92
  storage::Storage *data_{nullptr};
S
superjom 已提交
93 94 95 96
};

class ImHelper {
public:
S
Superjom 已提交
97 98 99
  // TODO(ChunweiYan) decouple helper with resource.
  ImHelper() { im_.reset(new IM); }
  ImHelper(std::unique_ptr<IM> im) : im_(std::move(im)) {}
S
superjom 已提交
100

S
superjom 已提交
101
  StorageHelper storage() {
S
Superjom 已提交
102
    return StorageHelper(im_->storage().mutable_data());
S
superjom 已提交
103 104
  }
  TabletHelper tablet(const std::string &tag) {
S
Superjom 已提交
105
    return TabletHelper(im_->storage().tablet(tag));
S
superjom 已提交
106 107
  }
  TabletHelper AddTablet(const std::string &tag, int num_samples) {
S
Superjom 已提交
108
    return TabletHelper(im_->AddTablet(tag, num_samples));
S
superjom 已提交
109
  }
S
Superjom 已提交
110 111 112 113 114
  std::mutex &handler() { return im_->handler(); }
  void ClearTablets() { im_->storage().mutable_data()->clear_tablets(); }
  void StartReadService(const std::string &dir, int msecs) {
    im_->SetPersistDest(dir);
    im_->MaintainRead(dir, msecs);
S
superjom 已提交
115
  }
S
Superjom 已提交
116 117 118 119 120 121
  void StartWriteSerice(const std::string &dir, int msecs) {
    im_->SetPersistDest(dir);
    im_->MaintainWrite(dir, msecs);
  }
  void StopService() { im_->executor().Quit(); }
  void PersistToDisk() const { im_->PersistToDisk(); }
S
superjom 已提交
122

S
Superjom 已提交
123 124
private:
  std::unique_ptr<IM> im_;
S
superjom 已提交
125 126 127 128
};

namespace components {

S
Superjom 已提交
129 130
#define ACQUIRE_HANDLER(handler) std::lock_guard<std::mutex> ____(*handler);

S
superjom 已提交
131 132 133 134 135 136
/*
 * Read and write support for Scalar component.
 */
template <typename T>
class ScalarHelper {
public:
S
Superjom 已提交
137 138 139 140
  ScalarHelper(storage::Tablet *tablet, std::mutex *handler = nullptr)
      : data_(tablet), handler_(handler) {}
  ScalarHelper(TabletHelper &tablet, std::mutex *handler = nullptr)
      : data_(&tablet.data()), handler_(handler) {}
S
superjom 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

  void SetCaptions(const std::vector<std::string> &captions);

  void AddRecord(int id, const std::vector<T> &values);

  std::vector<std::vector<T>> GetRecords() const;

  std::vector<int> GetIds() const;

  std::vector<int> GetTimestamps() const;

  std::vector<std::string> GetCaptions() const;

  size_t GetSize() const { return data_->records_size(); }

private:
  storage::Tablet *data_;
S
Superjom 已提交
158
  std::mutex *handler_;
S
superjom 已提交
159 160 161
};

}  // namespace components
S
superjom 已提交
162
}  // namespace visualdl
S
superjom 已提交
163 164

#endif  // VISUALDL_BACKEND_LOGIC_SDK_H