Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
recordio.cc
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
15
16#include <cstdint>
17#include <memory>
18#include <string>
19
20#include "absl/log/check.h"
21#include "absl/log/log.h"
22#include "ortools/base/file.h"
23#include "zlib.h"
24
25namespace recordio {
26const int RecordWriter::kMagicNumber = 0x3ed7230a;
27
28RecordWriter::RecordWriter(File* file) : file_(file), use_compression_(true) {}
29
30bool RecordWriter::Close() { return file_->Close(file::Defaults()).ok(); }
31
32void RecordWriter::set_use_compression(bool use_compression) {
33 use_compression_ = use_compression;
34}
35
36std::string RecordWriter::Compress(std::string const& s) const {
37 const unsigned long source_size = s.size(); // NOLINT
38 const char* source = s.c_str();
39
40 unsigned long dsize = source_size + (source_size * 0.1f) + 16; // NOLINT
41 std::unique_ptr<char[]> destination(new char[dsize]);
42 // Use compress() from zlib.h.
43 const int result =
44 compress(reinterpret_cast<unsigned char*>(destination.get()), &dsize,
45 reinterpret_cast<const unsigned char*>(source), source_size);
46
47 if (result != Z_OK) {
48 LOG(FATAL) << "Compress error occurred! Error code: " << result;
49 }
50 return std::string(destination.get(), dsize);
51}
52
54
55bool RecordReader::Close() { return file_->Close(file::Defaults()).ok(); }
56
57void RecordReader::Uncompress(const char* const source, uint64_t source_size,
58 char* const output_buffer,
59 uint64_t output_size) const {
60 unsigned long result_size = output_size; // NOLINT
61 // Use uncompress() from zlib.h
62 const int result =
63 uncompress(reinterpret_cast<unsigned char*>(output_buffer), &result_size,
64 reinterpret_cast<const unsigned char*>(source), source_size);
65 if (result != Z_OK) {
66 LOG(FATAL) << "Uncompress error occurred! Error code: " << result;
67 }
68 CHECK_LE(result_size, static_cast<unsigned long>(output_size)); // NOLINT
69}
70} // namespace recordio
Definition file.h:29
RecordReader(File *const file)
Definition recordio.cc:53
bool Close()
Closes the underlying file.
Definition recordio.cc:55
void set_use_compression(bool use_compression)
Definition recordio.cc:32
bool Close()
Closes the underlying file.
Definition recordio.cc:30
static const int kMagicNumber
Magic number when reading and writing protocol buffers.
Definition recordio.h:36
RecordWriter(File *file)
Definition recordio.cc:28
Definition file.cc:326
Options Defaults()
Definition file.h:85