tablet.h 3.3 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 17 18 19 20
#ifndef VISUALDL_TABLET_H
#define VISUALDL_TABLET_H

#include "visualdl/logic/im.h"
#include "visualdl/storage/record.h"
#include "visualdl/storage/storage.pb.h"
T
Thuan Nguyen 已提交
21
#include "visualdl/utils/logging.h"
S
superjom 已提交
22 23 24 25
#include "visualdl/utils/string.h"

namespace visualdl {

S
superjom 已提交
26 27
struct TabletReader;

S
superjom 已提交
28 29 30 31 32 33 34 35 36 37
/*
 * 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 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50
  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 已提交
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
  // 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>
76 77
  Entry MutableMeta() {
    Entry x(data_->mutable_meta(), parent());
S
superjom 已提交
78 79 80
  }

  void SetCaptions(const std::vector<std::string>& xs) {
S
superjom 已提交
81
    data_->clear_captions();
S
superjom 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    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
  }

S
superjom 已提交
98 99
  TabletReader reader();

S
superjom 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  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()); }
S
superjom 已提交
116
  int64_t total_records() const { return data_.records_size(); }
S
superjom 已提交
117 118 119
  int32_t num_samples() const { return data_.num_samples(); }
  RecordReader record(int i) const { return RecordReader(data_.records(i)); }
  template <typename T>
120 121
  EntryReader meta() const {
    return EntryReader(data_.meta());
S
superjom 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  }
  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