chunk.h 1.7 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 27
class Chunk {
public:
28
  Chunk() : num_bytes_(0) {}
Y
Yu Yang 已提交
29 30 31 32
  void Add(std::string buf) {
    records_.push_back(buf);
    num_bytes_ += buf.size();
  }
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 40
  bool Write(std::ostream& fo, Compressor ct) const;
  void Clear() {
    records_.clear();
    num_bytes_ = 0;
  }
  void Parse(std::istream& sin);
D
dongzhihong 已提交
41
  size_t NumBytes() { return num_bytes_; }
Y
Yu Yang 已提交
42
  const std::string& Record(int i) const { return records_[i]; }
D
"init"  
dzhwinter 已提交
43 44

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

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

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

D
dongzhihong 已提交
55 56
}  // namespace recordio
}  // namespace paddle