chunk.h 1.8 KB
Newer Older
D
"init"  
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
#include <string>
Y
Yu Yang 已提交
17
#include <vector>
D
"init"  
dzhwinter 已提交
18

Y
Yu Yang 已提交
19
#include "paddle/fluid/platform/macros.h"
D
dongzhihong 已提交
20 21 22 23 24 25
#include "paddle/fluid/recordio/header.h"

namespace paddle {
namespace recordio {

// A Chunk contains the Header and optionally compressed records.
D
"init"  
dzhwinter 已提交
26
class Chunk {
27
 public:
28
  Chunk() : num_bytes_(0) {}
Y
Yu Yang 已提交
29
  void Add(const std::string& buf) {
Y
Yu Yang 已提交
30
    num_bytes_ += buf.size();
Y
Yu Yang 已提交
31
    records_.emplace_back(buf);
Y
Yu Yang 已提交
32
  }
D
dongzhihong 已提交
33 34
  // dump the chunk into w, and clears the chunk and makes it ready for
  // the next add invocation.
Y
Yu Yang 已提交
35 36 37 38 39
  bool Write(std::ostream& fo, Compressor ct) const;
  void Clear() {
    records_.clear();
    num_bytes_ = 0;
  }
Y
Yu Yang 已提交
40 41 42 43 44

  // returns true if ok, false if eof
  bool Parse(std::istream& sin);
  size_t NumBytes() const { return num_bytes_; }
  size_t NumRecords() const { return records_.size(); }
Y
Yu Yang 已提交
45
  const std::string& Record(int i) const { return records_[i]; }
D
"init"  
dzhwinter 已提交
46

Y
Yu Yang 已提交
47 48
  bool Empty() const { return records_.empty(); }

49
 private:
Y
Yu Yang 已提交
50
  std::vector<std::string> records_;
D
dongzhihong 已提交
51
  // sum of record lengths in bytes.
D
"init"  
dzhwinter 已提交
52
  size_t num_bytes_;
D
dongzhihong 已提交
53
  DISABLE_COPY_AND_ASSIGN(Chunk);
D
"init"  
dzhwinter 已提交
54 55
};

D
dongzhihong 已提交
56
size_t CompressData(const char* in, size_t in_length, Compressor ct, char* out);
D
"init"  
dzhwinter 已提交
57

D
dongzhihong 已提交
58
void DeflateData(const char* in, size_t in_length, Compressor ct, char* out);
D
"init"  
dzhwinter 已提交
59

D
dongzhihong 已提交
60 61
}  // namespace recordio
}  // namespace paddle