im.h 1.8 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 30
  IM(StorageBase::Type type = StorageBase::Type::kMemory,
     StorageBase::Mode mode = StorageBase::Mode::kNone);
S
superjom 已提交
31

S
superjom 已提交
32 33
  static IM &Global() {
    static IM *x = new IM();
S
superjom 已提交
34 35 36 37 38 39 40 41 42 43 44 45
    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 已提交
46
   * @record: a record
S
superjom 已提交
47 48 49 50
   *
   * NOTE pass in the serialized protobuf message will trigger copying, but
   * simpler to support different Tablet data formats.
   */
S
superjom 已提交
51
  void AddRecord(const std::string &tag, const storage::Record &record);
S
superjom 已提交
52

S
superjom 已提交
53 54 55 56 57
  /*
   * delete all the information.
   */
  void Clear();

S
superjom 已提交
58 59 60 61 62
  /*
   * Save the Storage Protobuf to disk.
   */
  void PersistToDisk();

S
superjom 已提交
63
  StorageBase &storage() { return *storage_; }
S
superjom 已提交
64 65

private:
S
superjom 已提交
66
  std::unique_ptr<StorageBase> storage_;
S
superjom 已提交
67 68
};

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

S
superjom 已提交
71
#endif  // VISUALDL_BACKEND_LOGIC_IM_H