im.h 2.0 KB
Newer Older
S
superjom 已提交
1 2 3
#ifndef VISUALDL_BACKEND_LOGIC_IM_H
#define VISUALDL_BACKEND_LOGIC_IM_H

S
superjom 已提交
4 5
#include <glog/logging.h>
#include <memory>
S
superjom 已提交
6 7 8 9 10 11 12
#include <string>

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

namespace visualdl {

/*
S
superjom 已提交
13
 * IM(IM) maintain the Storage singleton in memory,
S
superjom 已提交
14 15 16 17 18 19 20 21 22 23 24 25
 * pre-compute some the statistical information to help visualizaton.
 *
 * There should be two processes and each have an IM, one is the web server
 * which hold one IM to read the storage, the other is the SDK(python or C++),
 * it will get an IM to write latest changes to storage.
 *
 * An IM have an underlying Storage object, which might be a memory based
 * storage or a disk based one, both has the same interfaces those defined by
 * class StorageBase.
 *
 * The SDK's IM will maintain the changes and periodically write to disk, and
 * the web server's IM will periodically read latest storage from disk.
S
superjom 已提交
26
 */
S
superjom 已提交
27
class IM final {
S
superjom 已提交
28
public:
S
superjom 已提交
29
  IM(StorageBase::Type type = StorageBase::Type::kMemory) {
S
superjom 已提交
30 31 32 33 34 35 36 37
    switch (type) {
      case StorageBase::Type::kMemory:
        storage_.reset(new MemoryStorage);
        break;
      default:
        CHECK(false) << "Unsupported storage kind " << type;
    }
  }
S
superjom 已提交
38

S
superjom 已提交
39 40
  static IM &Global() {
    static IM *x = new IM();
S
superjom 已提交
41 42 43 44 45 46 47 48 49 50 51 52
    return *x;
  }

  /*
   * Set the disk path to store the Storage object.
   */
  void SetPersistDest(const std::string &path);

  storage::Tablet *AddTablet(const std::string &tag, int num_samples);

  /*
   * @tag: tag of the target Tablet.
S
superjom 已提交
53
   * @record: a record
S
superjom 已提交
54 55 56 57
   *
   * NOTE pass in the serialized protobuf message will trigger copying, but
   * simpler to support different Tablet data formats.
   */
S
superjom 已提交
58
  void AddRecord(const std::string &tag, const storage::Record &record);
S
superjom 已提交
59

S
superjom 已提交
60 61 62 63 64
  /*
   * delete all the information.
   */
  void Clear();

S
superjom 已提交
65 66 67 68 69
  /*
   * Save the Storage Protobuf to disk.
   */
  void PersistToDisk();

S
superjom 已提交
70
  StorageBase &storage() { return *storage_; }
S
superjom 已提交
71 72

private:
S
superjom 已提交
73
  std::unique_ptr<StorageBase> storage_;
S
superjom 已提交
74 75
};

S
superjom 已提交
76
}  // namespace visualdl
S
superjom 已提交
77

S
superjom 已提交
78
#endif  // VISUALDL_BACKEND_LOGIC_IM_H