dglstream.h 1.63 KB
Newer Older
1
/**
2
 *  Copyright (c) 2019 by Contributors
3
4
 * @file graph/serialize/dglstream.h
 * @brief Graph serialization header
5
6
7
8
9
10
11
 */
#ifndef DGL_GRAPH_SERIALIZE_DGLSTREAM_H_
#define DGL_GRAPH_SERIALIZE_DGLSTREAM_H_

#include <dgl/aten/spmat.h>
#include <dmlc/io.h>
#include <dmlc/type_traits.h>
12

13
14
15
16
17
#include <memory>

namespace dgl {
namespace serialize {

18
19
/**
 * @brief DGLStream counts the bytes that already written into the
20
21
22
23
 * underlying stream.
 */
class DGLStream : public dmlc::Stream {
 public:
24
25
26
27
  /** @brief create a new DGLStream instance */
  static DGLStream *Create(
      const char *uri, const char *const flag, bool allow_null,
      dgl_format_code_t formats) {
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    return new DGLStream(uri, flag, allow_null, formats);
  }

  size_t Read(void *ptr, size_t size) override {
    return strm_->Read(ptr, size);
  }

  void Write(const void *ptr, size_t size) override {
    count_ += size;
    strm_->Write(ptr, size);
  }

  using dmlc::Stream::Read;
  using dmlc::Stream::Write;

  bool IsValid() { return strm_.get(); }

  uint64_t Count() const { return count_; }

  uint64_t FormatsToSave() const { return formats_to_save_; }

 private:
50
51
52
53
54
  DGLStream(
      const char *uri, const char *const flag, bool allow_null,
      dgl_format_code_t formats)
      : strm_(dmlc::Stream::Create(uri, flag, allow_null)),
        formats_to_save_(formats) {}
55
56
57
58
59
60
61
62
63
64
65
  // stream for serialization
  std::unique_ptr<dmlc::Stream> strm_;
  // size of already written to stream
  uint64_t count_ = 0;
  // formats to use when saving graph
  const dgl_format_code_t formats_to_save_ = ANY_CODE;
};
}  // namespace serialize
}  // namespace dgl

#endif  // DGL_GRAPH_SERIALIZE_DGLSTREAM_H_