dglstream.h 1.65 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
/*!
 *  Copyright (c) 2019 by Contributors
 * \file graph/serialize/dglstream.h
 * \brief Graph serialization header
 */
#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>
#include <memory>

namespace dgl {
namespace serialize {

/*!
 * \brief DGLStream counts the bytes that already written into the
 * underlying stream.
 */
class DGLStream : public dmlc::Stream {
 public:
  /*! \brief create a new DGLStream instance */
  static DGLStream *Create(const char *uri, const char *const flag,
                                  bool allow_null, dgl_format_code_t formats) {
    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:
  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) {
  }
  // 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_