header.h 2.0 KB
Newer Older
D
"init"  
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//   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 <sstream>

namespace paddle {
namespace recordio {

// MagicNumber for memory checking
constexpr uint32_t kMagicNumber = 0x01020304;

Y
Yu Yang 已提交
25
enum class Compressor : uint32_t {
D
"init"  
dzhwinter 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39
  // NoCompression means writing raw chunk data into files.
  // With other choices, chunks are compressed before written.
  kNoCompress = 0,
  // Snappy had been the default compressing algorithm widely
  // used in Google.  It compromises between speech and
  // compression ratio.
  kSnappy = 1,
  // Gzip is a well-known compression algorithm.  It is
  // recommmended only you are looking for compression ratio.
  kGzip = 2,
};

// Header is the metadata of Chunk
class Header {
40
 public:
D
"init"  
dzhwinter 已提交
41 42 43
  Header();
  Header(uint32_t num, uint32_t sum, Compressor ct, uint32_t cs);

Y
Yu Yang 已提交
44
  void Write(std::ostream& os) const;
Y
Yu Yang 已提交
45 46 47

  // returns true if OK, false if eof
  bool Parse(std::istream& is);
D
"init"  
dzhwinter 已提交
48 49 50 51 52 53

  uint32_t NumRecords() const { return num_records_; }
  uint32_t Checksum() const { return checksum_; }
  Compressor CompressType() const { return compressor_; }
  uint32_t CompressSize() const { return compress_size_; }

54
 private:
D
"init"  
dzhwinter 已提交
55 56 57 58 59 60 61 62 63 64 65 66
  uint32_t num_records_;
  uint32_t checksum_;
  Compressor compressor_;
  uint32_t compress_size_;
};

// Allow Header Loggable
std::ostream& operator<<(std::ostream& os, Header h);
bool operator==(Header l, Header r);

}  // namespace recordio
}  // namespace paddle