writer.cc 1.6 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
//   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.

#include "paddle/fluid/recordio/writer.h"

namespace paddle {
namespace recordio {

D
dongzhihong 已提交
20
Writer::Writer(Stream* fo) : stream_(fo), max_chunk_size_(0), compressor_(0) {}
D
"init"  
dzhwinter 已提交
21

D
dongzhihong 已提交
22 23
Writer::Writer(Stream* fo, int maxChunkSize, int compressor)
    : stream_(fo),
D
"init"  
dzhwinter 已提交
24
      max_chunk_size_(maxChunkSize),
D
dongzhihong 已提交
25
      compressor_(static_cast<Compressor>(compressor)) {
D
"init"  
dzhwinter 已提交
26 27 28
  chunk_.reset(new Chunk);
}

D
dongzhihong 已提交
29
size_t Writer::Write(const char* buf, size_t length) {
D
dongzhihong 已提交
30 31 32 33
  if (stream_ == nullptr) {
    LOG(WARNING) << "Cannot write since writer had been closed.";
    return 0;
  }
D
dongzhihong 已提交
34
  if ((length + chunk_->NumBytes()) > max_chunk_size_) {
D
dongzhihong 已提交
35 36
    chunk_->Dump(stream_, compressor_);
  }
D
dongzhihong 已提交
37 38
  chunk_->Add(buf, length);
  return length;
D
"init"  
dzhwinter 已提交
39 40
}

D
dongzhihong 已提交
41 42 43 44 45
// size_t Writer::Write(const char* buf, size_t length) {
//   return Write(std::string(buf, length));
// }

// size_t Writer::Write(std::string&& buf) {}
D
dongzhihong 已提交
46

D
"init"  
dzhwinter 已提交
47
void Writer::Close() {
D
dongzhihong 已提交
48 49
  chunk_->Dump(stream_, compressor_);
  stream_ = nullptr;
D
"init"  
dzhwinter 已提交
50 51 52 53
}

}  // namespace recordio
}  // namespace paddle