2025-01-10 11:35:44 +01:00
|
|
|
// Copyright 2010-2025 Google LLC
|
2011-07-21 18:15:37 +00:00
|
|
|
// 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.
|
|
|
|
|
|
2018-06-08 16:40:43 +02:00
|
|
|
#include "ortools/base/recordio.h"
|
2019-04-08 19:00:46 +02:00
|
|
|
|
2025-07-18 08:11:03 +00:00
|
|
|
#include <zlib.h>
|
|
|
|
|
|
2025-02-24 16:45:22 +01:00
|
|
|
#include <cstdint>
|
2015-08-13 16:00:54 +02:00
|
|
|
#include <memory>
|
2011-12-23 13:23:15 +00:00
|
|
|
#include <string>
|
2019-04-08 19:00:46 +02:00
|
|
|
|
2025-02-24 16:45:22 +01:00
|
|
|
#include "absl/log/check.h"
|
|
|
|
|
#include "absl/log/log.h"
|
|
|
|
|
#include "ortools/base/file.h"
|
2011-07-21 18:15:37 +00:00
|
|
|
|
2017-04-26 17:30:25 +02:00
|
|
|
namespace recordio {
|
2011-07-21 18:15:37 +00:00
|
|
|
const int RecordWriter::kMagicNumber = 0x3ed7230a;
|
|
|
|
|
|
2025-02-24 16:45:22 +01:00
|
|
|
RecordWriter::RecordWriter(File* file) : file_(file), use_compression_(true) {}
|
2011-07-21 18:15:37 +00:00
|
|
|
|
2025-04-06 10:53:43 +02:00
|
|
|
bool RecordWriter::Close() { return file_->Close(file::Defaults()).ok(); }
|
2011-07-21 18:15:37 +00:00
|
|
|
|
2012-01-03 14:06:48 +00:00
|
|
|
void RecordWriter::set_use_compression(bool use_compression) {
|
|
|
|
|
use_compression_ = use_compression;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 13:42:36 +01:00
|
|
|
std::string RecordWriter::Compress(std::string const& s) const {
|
2020-10-22 23:36:58 +02:00
|
|
|
const unsigned long source_size = s.size(); // NOLINT
|
2020-10-28 13:42:36 +01:00
|
|
|
const char* source = s.c_str();
|
2011-12-23 02:08:35 +00:00
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
unsigned long dsize = source_size + (source_size * 0.1f) + 16; // NOLINT
|
2017-04-26 17:30:25 +02:00
|
|
|
std::unique_ptr<char[]> destination(new char[dsize]);
|
2012-01-03 14:06:48 +00:00
|
|
|
// Use compress() from zlib.h.
|
|
|
|
|
const int result =
|
2020-10-28 13:42:36 +01:00
|
|
|
compress(reinterpret_cast<unsigned char*>(destination.get()), &dsize,
|
|
|
|
|
reinterpret_cast<const unsigned char*>(source), source_size);
|
2011-12-23 02:08:35 +00:00
|
|
|
|
|
|
|
|
if (result != Z_OK) {
|
2017-04-26 17:30:25 +02:00
|
|
|
LOG(FATAL) << "Compress error occurred! Error code: " << result;
|
2011-12-23 02:08:35 +00:00
|
|
|
}
|
2011-12-23 13:23:15 +00:00
|
|
|
return std::string(destination.get(), dsize);
|
2011-12-23 02:08:35 +00:00
|
|
|
}
|
|
|
|
|
|
2020-10-28 13:42:36 +01:00
|
|
|
RecordReader::RecordReader(File* const file) : file_(file) {}
|
2011-07-21 18:15:37 +00:00
|
|
|
|
2025-04-06 10:53:43 +02:00
|
|
|
bool RecordReader::Close() { return file_->Close(file::Defaults()).ok(); }
|
2011-12-23 02:08:35 +00:00
|
|
|
|
2021-04-01 12:13:35 +02:00
|
|
|
void RecordReader::Uncompress(const char* const source, uint64_t source_size,
|
2020-10-28 13:42:36 +01:00
|
|
|
char* const output_buffer,
|
2021-04-01 12:13:35 +02:00
|
|
|
uint64_t output_size) const {
|
2020-10-22 23:36:58 +02:00
|
|
|
unsigned long result_size = output_size; // NOLINT
|
2020-10-28 13:42:36 +01:00
|
|
|
// Use uncompress() from zlib.h
|
2012-01-03 14:06:48 +00:00
|
|
|
const int result =
|
2020-10-28 13:42:36 +01:00
|
|
|
uncompress(reinterpret_cast<unsigned char*>(output_buffer), &result_size,
|
|
|
|
|
reinterpret_cast<const unsigned char*>(source), source_size);
|
2011-12-23 13:23:15 +00:00
|
|
|
if (result != Z_OK) {
|
2017-04-26 17:30:25 +02:00
|
|
|
LOG(FATAL) << "Uncompress error occurred! Error code: " << result;
|
2011-12-23 02:08:35 +00:00
|
|
|
}
|
2020-10-22 23:36:58 +02:00
|
|
|
CHECK_LE(result_size, static_cast<unsigned long>(output_size)); // NOLINT
|
2011-12-23 02:08:35 +00:00
|
|
|
}
|
2020-10-22 23:36:58 +02:00
|
|
|
} // namespace recordio
|