Apache Singa
A General Distributed Deep Learning Library
writer.h
1 
19 #ifndef SINGA_IO_WRITER_H_
20 #define SINGA_IO_WRITER_H_
21 
22 #include <cstring>
23 #include <fstream>
24 #include <string>
25 #include "singa/singa_config.h"
26 
27 #ifdef USE_LMDB
28 #include <lmdb.h>
29 #include <sys/stat.h>
30 #include <vector>
31 #endif // USE_LMDB
32 
33 namespace singa {
34 namespace io {
35 
36 using std::string;
37 enum Mode { kCreate, kAppend };
38 
42 class Writer {
43  public:
46  virtual ~Writer() {}
47 
55  virtual bool Open(const std::string &path, Mode mode) = 0;
56 
58  virtual void Close() = 0;
59 
62  virtual bool Write(const std::string &key, const std::string &value) = 0;
63 
65  virtual void Flush() = 0;
66 };
67 
77 class BinFileWriter : public Writer {
78  public:
79  ~BinFileWriter() { Close(); }
81  bool Open(const std::string &path, Mode mode) override;
83  bool Open(const std::string &path, Mode mode, int capacity);
85  void Close() override;
87  bool Write(const std::string &key, const std::string &value) override;
89  void Flush() override;
91  inline std::string path() { return path_; }
92 
93  protected:
95  bool OpenFile();
96 
97  private:
99  std::string path_ = "";
100  Mode mode_;
102  std::ofstream fdat_;
104  char *buf_ = nullptr;
106  int capacity_ = 10485760;
108  int bufsize_ = 0;
110  const char kMagicWord[2] = {'s', 'g'};
111 };
112 
114 class TextFileWriter : public Writer {
115  public:
116  ~TextFileWriter() { Close(); }
118  bool Open(const std::string &path, Mode mode) override;
120  void Close() override;
122  bool Write(const std::string &key, const std::string &value) override;
124  void Flush() override;
126  inline std::string path() { return path_; }
127 
128  private:
130  std::string path_ = "";
131  Mode mode_;
133  std::ofstream fdat_;
134 };
135 
136 #ifdef USE_LMDB
137 class LMDBWriter : public Writer {
139  public:
140  ~LMDBWriter() { Close(); }
142  bool Open(const std::string &path, Mode mode) override;
144  void Close() override;
146  bool Write(const std::string &key, const std::string &value) override;
148  void Flush() override;
150  inline std::string path() { return path_; }
151 
152  protected:
153  void DoubleMapSize();
154  inline void MDB_CHECK(int mdb_status);
155 
156  private:
158  std::string path_ = "";
160  Mode mode_;
162  MDB_env *mdb_env_ = nullptr;
164  std::vector<string> keys, values;
165 };
166 #endif // USE_LMDB
167 
168 } // namespace io
169 } // namespace singa
170 
171 #endif // SINGA_IO_WRITER_H_
General Writer that provides functions for writing tuples.
Definition: writer.h:42
virtual void Close()=0
Release resources.
virtual bool Open(const std::string &path, Mode mode)=0
Open a file.
BinFile stores training/validation/test tuples.
Definition: writer.h:77
std::string path()
return path to binary file
Definition: writer.h:91
TextFileWriter write training/validation/test tuples in CSV file.
Definition: writer.h:114
virtual ~Writer()
In case that users forget to call Close() to release resources, e.g., memory, you can release them he...
Definition: writer.h:46
virtual bool Write(const std::string &key, const std::string &value)=0
Write a key-value tuple.
virtual void Flush()=0
Flush writing buffer if it has.
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: common.h:48
std::string path()
return path to text file
Definition: writer.h:126