tablet.h 2.7 KB
Newer Older
S
superjom 已提交
1 2 3
#ifndef VISUALDL_TABLET_H
#define VISUALDL_TABLET_H

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

S
superjom 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include "visualdl/logic/im.h"
#include "visualdl/storage/record.h"
#include "visualdl/storage/storage.pb.h"
#include "visualdl/utils/string.h"

namespace visualdl {

/*
 * Tablet is a helper for operations on storage::Tablet.
 */
struct Tablet {
  enum Type { kScalar = 0, kHistogram = 1, kImage = 2 };

  DECL_GUARD(Tablet);

  Tablet(storage::Tablet* x, Storage* parent) : data_(x), x_(parent) {}

S
superjom 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35
  static Type type(const std::string& name) {
    if (name == "scalar") {
      return kScalar;
    }
    if (name == "histogram") {
      return kHistogram;
    }
    if (name == "image") {
      return kImage;
    }
    LOG(ERROR) << "unknown component: " << name;
  }

S
superjom 已提交
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
  // write operations.
  void SetNumSamples(int x) {
    data_->set_num_samples(x);
    WRITE_GUARD
  }

  void SetType(Type type) {
    data_->set_component(static_cast<storage::Tablet::Type>(type));
    WRITE_GUARD
  }

  void SetTag(const std::string& mode, const std::string& tag) {
    auto internal_tag = mode + "/" + tag;
    string::TagEncode(internal_tag);
    data_->set_tag(internal_tag);
    WRITE_GUARD
  }

  Record AddRecord() {
    IncTotalRecords();
    WRITE_GUARD
    return Record(data_->add_records(), parent());
  }

  template <typename T>
  Entry<T> MutableMeta() {
    Entry<T> x(data_->mutable_meta(), parent());
  }

  void SetCaptions(const std::vector<std::string>& xs) {
S
superjom 已提交
66
    data_->clear_captions();
S
superjom 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    for (const auto& x : xs) {
      *data_->add_captions() = x;
    }
    WRITE_GUARD
  }

  void SetDescription(const std::string& x) {
    data_->set_description(x);
    WRITE_GUARD
  }

  void IncTotalRecords() {
    data_->set_total_records(data_->total_records() + 1);
    WRITE_GUARD
  }

  Storage* parent() const { return x_; }

private:
  Storage* x_;
  storage::Tablet* data_{nullptr};
};

/*
 * Tablet reader, it will hold the protobuf object.
 */
struct TabletReader {
  TabletReader(storage::Tablet x) : data_(x) {}

  // read operations.
  std::string tag() const { return data_.tag(); }
  Tablet::Type type() const { return Tablet::Type(data_.component()); }
  int64_t total_records() const { return data_.total_records(); }
  int32_t num_samples() const { return data_.num_samples(); }
  RecordReader record(int i) const { return RecordReader(data_.records(i)); }
  template <typename T>
  EntryReader<T> meta() const {
    return EntryReader<T>(data_.meta());
  }
  std::vector<std::string> captions() const {
    std::vector<std::string> x(data_.captions().begin(),
                               data_.captions().end());
    return x;
  }
  std::string description() const { return data_.description(); }

private:
  storage::Tablet data_;
};

}  // namespace visualdl

#endif