2024-04-28 11:20:59 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <mutex>
|
2022-08-31 12:53:26 +00:00
|
|
|
#include <Interpreters/TemporaryDataOnDisk.h>
|
|
|
|
|
2022-08-31 17:17:31 +00:00
|
|
|
#include <IO/WriteBufferFromFile.h>
|
|
|
|
#include <Compression/CompressedWriteBuffer.h>
|
2024-01-24 17:26:11 +00:00
|
|
|
#include <Interpreters/Cache/FileCache.h>
|
2022-08-31 17:17:31 +00:00
|
|
|
#include <Formats/NativeWriter.h>
|
2022-09-01 12:22:49 +00:00
|
|
|
#include <Core/ProtocolDefines.h>
|
2022-12-06 16:51:08 +00:00
|
|
|
#include <Disks/SingleDiskVolume.h>
|
|
|
|
#include <Disks/DiskLocal.h>
|
2023-04-11 15:36:05 +00:00
|
|
|
#include <Disks/IO/WriteBufferFromTemporaryFile.h>
|
2022-09-01 12:22:49 +00:00
|
|
|
|
2023-10-02 11:09:40 +00:00
|
|
|
#include <Core/Defines.h>
|
2022-12-06 17:27:05 +00:00
|
|
|
#include <Interpreters/Cache/WriteBufferToFileSegment.h>
|
2024-04-28 11:20:59 +00:00
|
|
|
#include "Common/Exception.h"
|
2022-08-31 12:53:26 +00:00
|
|
|
|
2023-11-27 19:52:17 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
|
|
|
extern const Event ExternalProcessingFilesTotal;
|
|
|
|
}
|
|
|
|
|
2022-08-31 12:53:26 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2022-09-01 12:22:49 +00:00
|
|
|
|
2022-08-31 12:53:26 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2022-09-21 12:51:46 +00:00
|
|
|
extern const int TOO_MANY_ROWS_OR_BYTES;
|
2022-08-31 17:17:31 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2022-09-01 12:22:49 +00:00
|
|
|
extern const int NOT_ENOUGH_SPACE;
|
2022-08-31 12:53:26 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 16:51:08 +00:00
|
|
|
|
2022-10-07 10:46:45 +00:00
|
|
|
void TemporaryDataOnDiskScope::deltaAllocAndCheck(ssize_t compressed_delta, ssize_t uncompressed_delta)
|
2022-08-31 12:53:26 +00:00
|
|
|
{
|
|
|
|
if (parent)
|
2022-09-21 12:51:46 +00:00
|
|
|
parent->deltaAllocAndCheck(compressed_delta, uncompressed_delta);
|
|
|
|
|
|
|
|
/// check that we don't go negative
|
|
|
|
if ((compressed_delta < 0 && stat.compressed_size < static_cast<size_t>(-compressed_delta)) ||
|
|
|
|
(uncompressed_delta < 0 && stat.uncompressed_size < static_cast<size_t>(-uncompressed_delta)))
|
|
|
|
{
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Negative temporary data size");
|
|
|
|
}
|
|
|
|
|
2024-04-27 17:54:37 +00:00
|
|
|
size_t new_consumption = stat.compressed_size + compressed_delta;
|
|
|
|
if (compressed_delta > 0 && settings.max_size_on_disk && new_consumption > settings.max_size_on_disk)
|
2022-12-06 10:04:15 +00:00
|
|
|
throw Exception(ErrorCodes::TOO_MANY_ROWS_OR_BYTES,
|
2024-04-27 17:54:37 +00:00
|
|
|
"Limit for temporary files size exceeded (would consume {} / {} bytes)", new_consumption, settings.max_size_on_disk);
|
2022-09-21 12:51:46 +00:00
|
|
|
|
|
|
|
stat.compressed_size += compressed_delta;
|
|
|
|
stat.uncompressed_size += uncompressed_delta;
|
2022-08-31 12:53:26 +00:00
|
|
|
}
|
|
|
|
|
2023-03-19 16:23:46 +00:00
|
|
|
TemporaryDataOnDisk::TemporaryDataOnDisk(TemporaryDataOnDiskScopePtr parent_)
|
2024-03-18 15:02:54 +00:00
|
|
|
: TemporaryDataOnDiskScope(parent_, parent_->getSettings())
|
2023-03-19 16:23:46 +00:00
|
|
|
{}
|
|
|
|
|
2023-04-01 09:17:23 +00:00
|
|
|
TemporaryDataOnDisk::TemporaryDataOnDisk(TemporaryDataOnDiskScopePtr parent_, CurrentMetrics::Metric metric_scope)
|
2024-03-18 15:02:54 +00:00
|
|
|
: TemporaryDataOnDiskScope(parent_, parent_->getSettings())
|
2023-03-19 16:23:46 +00:00
|
|
|
, current_metric_scope(metric_scope)
|
|
|
|
{}
|
|
|
|
|
2023-11-27 19:39:08 +00:00
|
|
|
std::unique_ptr<WriteBufferFromFileBase> TemporaryDataOnDisk::createRawStream(size_t max_file_size)
|
2023-04-11 15:36:05 +00:00
|
|
|
{
|
|
|
|
if (file_cache)
|
|
|
|
{
|
|
|
|
auto holder = createCacheFile(max_file_size);
|
2023-11-27 19:39:08 +00:00
|
|
|
return std::make_unique<WriteBufferToFileSegment>(std::move(holder));
|
2023-04-11 15:36:05 +00:00
|
|
|
}
|
|
|
|
else if (volume)
|
|
|
|
{
|
|
|
|
auto tmp_file = createRegularFile(max_file_size);
|
2023-11-27 19:39:08 +00:00
|
|
|
return std::make_unique<WriteBufferFromTemporaryFile>(std::move(tmp_file));
|
2023-04-11 15:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "TemporaryDataOnDiskScope has no cache and no volume");
|
|
|
|
}
|
|
|
|
|
2022-12-06 17:27:05 +00:00
|
|
|
TemporaryFileStream & TemporaryDataOnDisk::createStream(const Block & header, size_t max_file_size)
|
2022-12-06 10:04:15 +00:00
|
|
|
{
|
2022-12-06 17:27:05 +00:00
|
|
|
if (file_cache)
|
2023-04-11 15:36:05 +00:00
|
|
|
{
|
|
|
|
auto holder = createCacheFile(max_file_size);
|
|
|
|
|
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
TemporaryFileStreamPtr & tmp_stream = streams.emplace_back(std::make_unique<TemporaryFileStream>(std::move(holder), header, this));
|
|
|
|
return *tmp_stream;
|
|
|
|
}
|
2022-12-06 17:27:05 +00:00
|
|
|
else if (volume)
|
2023-04-11 15:36:05 +00:00
|
|
|
{
|
|
|
|
auto tmp_file = createRegularFile(max_file_size);
|
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
TemporaryFileStreamPtr & tmp_stream = streams.emplace_back(std::make_unique<TemporaryFileStream>(std::move(tmp_file), header, this));
|
|
|
|
return *tmp_stream;
|
|
|
|
}
|
2022-12-06 17:27:05 +00:00
|
|
|
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "TemporaryDataOnDiskScope has no cache and no volume");
|
2022-12-06 10:04:15 +00:00
|
|
|
}
|
|
|
|
|
2023-04-22 15:14:11 +00:00
|
|
|
FileSegmentsHolderPtr TemporaryDataOnDisk::createCacheFile(size_t max_file_size)
|
2022-12-06 17:27:05 +00:00
|
|
|
{
|
|
|
|
if (!file_cache)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "TemporaryDataOnDiskScope has no cache");
|
2022-12-06 17:27:05 +00:00
|
|
|
|
2023-11-27 19:52:17 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::ExternalProcessingFilesTotal);
|
|
|
|
|
2023-04-23 10:37:04 +00:00
|
|
|
const auto key = FileSegment::Key::random();
|
2024-01-17 11:57:40 +00:00
|
|
|
auto holder = file_cache->set(
|
|
|
|
key, 0, std::max(10_MiB, max_file_size),
|
|
|
|
CreateFileSegmentSettings(FileSegmentKind::Temporary, /* unbounded */ true), FileCache::getCommonUser());
|
2023-12-18 20:02:20 +00:00
|
|
|
|
|
|
|
chassert(holder->size() == 1);
|
2024-04-24 16:51:45 +00:00
|
|
|
holder->back().getKeyMetadata()->createBaseDirectory(/* throw_if_failed */true);
|
2023-12-18 20:02:20 +00:00
|
|
|
|
2023-04-23 10:37:04 +00:00
|
|
|
return holder;
|
2022-12-06 17:27:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 15:36:05 +00:00
|
|
|
TemporaryFileOnDiskHolder TemporaryDataOnDisk::createRegularFile(size_t max_file_size)
|
2022-08-31 12:53:26 +00:00
|
|
|
{
|
2022-12-06 16:51:08 +00:00
|
|
|
if (!volume)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "TemporaryDataOnDiskScope has no volume");
|
2022-12-06 16:51:08 +00:00
|
|
|
|
|
|
|
DiskPtr disk;
|
2022-12-06 17:27:05 +00:00
|
|
|
if (max_file_size > 0)
|
2022-12-06 16:51:08 +00:00
|
|
|
{
|
|
|
|
auto reservation = volume->reserve(max_file_size);
|
|
|
|
if (!reservation)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NOT_ENOUGH_SPACE, "Not enough space on temporary disk");
|
2022-12-06 16:51:08 +00:00
|
|
|
disk = reservation->getDisk();
|
|
|
|
}
|
2022-08-31 12:53:26 +00:00
|
|
|
else
|
2022-12-06 16:51:08 +00:00
|
|
|
{
|
|
|
|
disk = volume->getDisk();
|
|
|
|
}
|
2023-11-27 19:52:17 +00:00
|
|
|
/// We do not increment ProfileEvents::ExternalProcessingFilesTotal here because it is incremented in TemporaryFileOnDisk constructor.
|
2023-04-11 15:36:05 +00:00
|
|
|
return std::make_unique<TemporaryFileOnDisk>(disk, current_metric_scope);
|
2022-08-31 12:53:26 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 10:22:16 +00:00
|
|
|
std::vector<TemporaryFileStream *> TemporaryDataOnDisk::getStreams() const
|
2022-09-15 10:19:39 +00:00
|
|
|
{
|
|
|
|
std::vector<TemporaryFileStream *> res;
|
|
|
|
std::lock_guard lock(mutex);
|
2022-09-28 10:22:16 +00:00
|
|
|
res.reserve(streams.size());
|
|
|
|
for (const auto & stream : streams)
|
2022-09-15 10:19:39 +00:00
|
|
|
res.push_back(stream.get());
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-09-21 12:51:46 +00:00
|
|
|
bool TemporaryDataOnDisk::empty() const
|
|
|
|
{
|
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
return streams.empty();
|
|
|
|
}
|
|
|
|
|
2024-03-18 15:02:54 +00:00
|
|
|
static inline CompressionCodecPtr getCodec(const TemporaryDataOnDiskSettings & settings)
|
|
|
|
{
|
|
|
|
if (settings.compression_codec.empty())
|
|
|
|
return CompressionCodecFactory::instance().get("NONE");
|
|
|
|
|
|
|
|
return CompressionCodecFactory::instance().get(settings.compression_codec);
|
|
|
|
}
|
|
|
|
|
2022-08-31 12:53:26 +00:00
|
|
|
struct TemporaryFileStream::OutputWriter
|
|
|
|
{
|
2024-03-18 15:02:54 +00:00
|
|
|
OutputWriter(std::unique_ptr<WriteBuffer> out_buf_, const Block & header_, const TemporaryDataOnDiskSettings & settings)
|
2022-12-06 17:27:05 +00:00
|
|
|
: out_buf(std::move(out_buf_))
|
2024-03-18 15:02:54 +00:00
|
|
|
, out_compressed_buf(*out_buf, getCodec(settings))
|
2022-12-06 17:27:05 +00:00
|
|
|
, out_writer(out_compressed_buf, DBMS_TCP_PROTOCOL_VERSION, header_)
|
|
|
|
{
|
2022-08-31 12:53:26 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 10:04:15 +00:00
|
|
|
size_t write(const Block & block)
|
2022-08-31 12:53:26 +00:00
|
|
|
{
|
|
|
|
if (finalized)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot write to finalized stream");
|
2022-12-06 10:04:15 +00:00
|
|
|
size_t written_bytes = out_writer.write(block);
|
2022-10-05 16:35:10 +00:00
|
|
|
num_rows += block.rows();
|
2022-12-06 10:04:15 +00:00
|
|
|
return written_bytes;
|
2022-08-31 12:53:26 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 17:27:05 +00:00
|
|
|
void flush()
|
|
|
|
{
|
|
|
|
if (finalized)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot flush finalized stream");
|
2022-12-06 17:27:05 +00:00
|
|
|
|
|
|
|
out_compressed_buf.next();
|
|
|
|
out_buf->next();
|
|
|
|
out_writer.flush();
|
|
|
|
}
|
|
|
|
|
2022-08-31 12:53:26 +00:00
|
|
|
void finalize()
|
|
|
|
{
|
|
|
|
if (finalized)
|
|
|
|
return;
|
2022-09-21 12:51:46 +00:00
|
|
|
|
|
|
|
/// if we called finalize() explicitly, and got an exception,
|
|
|
|
/// we don't want to get it again in the destructor, so set finalized flag first
|
2022-09-14 11:21:25 +00:00
|
|
|
finalized = true;
|
2022-09-21 12:51:46 +00:00
|
|
|
|
|
|
|
out_writer.flush();
|
|
|
|
out_compressed_buf.finalize();
|
2022-12-06 17:27:05 +00:00
|
|
|
out_buf->finalize();
|
2022-08-31 12:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~OutputWriter()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
finalize();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-06 17:27:05 +00:00
|
|
|
std::unique_ptr<WriteBuffer> out_buf;
|
2022-08-31 12:53:26 +00:00
|
|
|
CompressedWriteBuffer out_compressed_buf;
|
|
|
|
NativeWriter out_writer;
|
|
|
|
|
2022-10-05 16:35:10 +00:00
|
|
|
std::atomic_size_t num_rows = 0;
|
|
|
|
|
2022-08-31 12:53:26 +00:00
|
|
|
bool finalized = false;
|
|
|
|
};
|
|
|
|
|
2024-05-07 20:43:40 +00:00
|
|
|
TemporaryFileStream::Reader::Reader(const String & path, const Block & header_, size_t size)
|
2024-04-28 11:20:59 +00:00
|
|
|
: in_file_buf(path, size ? std::min<size_t>(DBMS_DEFAULT_BUFFER_SIZE, size) : DBMS_DEFAULT_BUFFER_SIZE)
|
|
|
|
, in_compressed_buf(in_file_buf)
|
|
|
|
, in_reader(in_compressed_buf, header_, DBMS_TCP_PROTOCOL_VERSION)
|
2022-08-31 12:53:26 +00:00
|
|
|
{
|
2024-04-28 11:20:59 +00:00
|
|
|
LOG_TEST(getLogger("TemporaryFileStream"), "Reading {} from {}", header_.dumpStructure(), path);
|
|
|
|
}
|
2022-08-31 17:17:31 +00:00
|
|
|
|
2024-05-07 20:43:40 +00:00
|
|
|
TemporaryFileStream::Reader::Reader(const String & path, size_t size)
|
2024-04-28 11:20:59 +00:00
|
|
|
: in_file_buf(path, size ? std::min<size_t>(DBMS_DEFAULT_BUFFER_SIZE, size) : DBMS_DEFAULT_BUFFER_SIZE)
|
|
|
|
, in_compressed_buf(in_file_buf)
|
|
|
|
, in_reader(in_compressed_buf, DBMS_TCP_PROTOCOL_VERSION)
|
|
|
|
{
|
|
|
|
LOG_TEST(getLogger("TemporaryFileStream"), "Reading from {}", path);
|
|
|
|
}
|
2022-08-31 12:53:26 +00:00
|
|
|
|
2024-05-07 20:43:40 +00:00
|
|
|
Block TemporaryFileStream::Reader::read()
|
2024-04-28 11:20:59 +00:00
|
|
|
{
|
|
|
|
return in_reader.read();
|
|
|
|
}
|
2022-08-31 12:53:26 +00:00
|
|
|
|
2022-12-06 17:27:05 +00:00
|
|
|
TemporaryFileStream::TemporaryFileStream(TemporaryFileOnDiskHolder file_, const Block & header_, TemporaryDataOnDisk * parent_)
|
2022-08-31 17:17:31 +00:00
|
|
|
: parent(parent_)
|
2022-09-15 10:19:39 +00:00
|
|
|
, header(header_)
|
2022-09-01 12:22:49 +00:00
|
|
|
, file(std::move(file_))
|
2024-03-18 15:02:54 +00:00
|
|
|
, out_writer(std::make_unique<OutputWriter>(std::make_unique<WriteBufferFromFile>(file->getAbsolutePath()), header, parent->settings))
|
2022-08-31 17:17:31 +00:00
|
|
|
{
|
2024-01-23 17:04:50 +00:00
|
|
|
LOG_TEST(getLogger("TemporaryFileStream"), "Writing to temporary file {}", file->getAbsolutePath());
|
2022-08-31 17:17:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 19:57:42 +00:00
|
|
|
TemporaryFileStream::TemporaryFileStream(FileSegmentsHolderPtr segments_, const Block & header_, TemporaryDataOnDisk * parent_)
|
2022-12-06 17:27:05 +00:00
|
|
|
: parent(parent_)
|
|
|
|
, header(header_)
|
|
|
|
, segment_holder(std::move(segments_))
|
|
|
|
{
|
2023-01-04 19:57:42 +00:00
|
|
|
if (segment_holder->size() != 1)
|
2022-12-06 17:27:05 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "TemporaryFileStream can be created only from single segment");
|
2023-01-04 19:57:42 +00:00
|
|
|
auto out_buf = std::make_unique<WriteBufferToFileSegment>(&segment_holder->front());
|
2023-04-11 15:36:05 +00:00
|
|
|
|
2024-01-23 17:04:50 +00:00
|
|
|
LOG_TEST(getLogger("TemporaryFileStream"), "Writing to temporary file {}", out_buf->getFileName());
|
2024-03-18 15:02:54 +00:00
|
|
|
out_writer = std::make_unique<OutputWriter>(std::move(out_buf), header, parent_->settings);
|
2022-12-06 17:27:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 10:04:15 +00:00
|
|
|
size_t TemporaryFileStream::write(const Block & block)
|
2022-08-31 17:17:31 +00:00
|
|
|
{
|
|
|
|
if (!out_writer)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Writing has been finished");
|
2022-08-31 17:17:31 +00:00
|
|
|
|
2022-09-21 12:51:46 +00:00
|
|
|
updateAllocAndCheck();
|
2022-12-06 10:04:15 +00:00
|
|
|
size_t bytes_written = out_writer->write(block);
|
|
|
|
return bytes_written;
|
2022-08-31 17:17:31 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 17:27:05 +00:00
|
|
|
void TemporaryFileStream::flush()
|
|
|
|
{
|
|
|
|
if (!out_writer)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Writing has been finished");
|
2022-12-06 17:27:05 +00:00
|
|
|
|
|
|
|
out_writer->flush();
|
|
|
|
}
|
|
|
|
|
2022-09-21 13:44:35 +00:00
|
|
|
TemporaryFileStream::Stat TemporaryFileStream::finishWriting()
|
2022-08-31 17:17:31 +00:00
|
|
|
{
|
2022-10-05 16:35:10 +00:00
|
|
|
if (isWriteFinished())
|
|
|
|
return stat;
|
|
|
|
|
2022-08-31 17:17:31 +00:00
|
|
|
if (out_writer)
|
|
|
|
{
|
|
|
|
out_writer->finalize();
|
2022-09-21 12:51:46 +00:00
|
|
|
/// The amount of written data can be changed after finalization, some buffers can be flushed
|
|
|
|
/// Need to update the stat
|
|
|
|
updateAllocAndCheck();
|
2022-09-01 12:22:49 +00:00
|
|
|
out_writer.reset();
|
2022-08-31 17:17:31 +00:00
|
|
|
|
2022-09-22 17:39:30 +00:00
|
|
|
/// reader will be created at the first read call, not to consume memory before it is needed
|
2022-08-31 17:17:31 +00:00
|
|
|
}
|
2022-09-15 10:19:39 +00:00
|
|
|
return stat;
|
2022-08-31 17:17:31 +00:00
|
|
|
}
|
|
|
|
|
2024-04-28 11:20:59 +00:00
|
|
|
TemporaryFileStream::Stat TemporaryFileStream::finishWritingAsyncSafe()
|
|
|
|
{
|
2024-05-07 20:43:40 +00:00
|
|
|
std::call_once(finish_writing, [this]{ finishWriting(); });
|
2024-04-28 11:20:59 +00:00
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
|
2022-08-31 17:17:31 +00:00
|
|
|
bool TemporaryFileStream::isWriteFinished() const
|
|
|
|
{
|
2022-09-22 17:39:30 +00:00
|
|
|
assert(in_reader == nullptr || out_writer == nullptr);
|
2022-08-31 17:17:31 +00:00
|
|
|
return out_writer == nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Block TemporaryFileStream::read()
|
|
|
|
{
|
2022-09-21 12:51:46 +00:00
|
|
|
if (!isWriteFinished())
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Writing has been not finished");
|
2022-08-31 17:17:31 +00:00
|
|
|
|
2022-10-05 16:35:10 +00:00
|
|
|
if (isEof())
|
2022-09-22 17:39:30 +00:00
|
|
|
return {};
|
|
|
|
|
|
|
|
if (!in_reader)
|
|
|
|
{
|
2024-05-07 20:43:40 +00:00
|
|
|
in_reader = std::make_unique<Reader>(getPath(), header, getSize());
|
2022-09-22 17:39:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Block block = in_reader->read();
|
|
|
|
if (!block)
|
|
|
|
{
|
|
|
|
/// finalize earlier to release resources, do not wait for the destructor
|
2022-10-05 16:35:10 +00:00
|
|
|
this->release();
|
2022-09-22 17:39:30 +00:00
|
|
|
}
|
|
|
|
return block;
|
2022-08-31 17:17:31 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 20:43:40 +00:00
|
|
|
std::unique_ptr<TemporaryFileStream::Reader> TemporaryFileStream::getReadStream()
|
2024-04-28 11:20:59 +00:00
|
|
|
{
|
|
|
|
if (!isWriteFinished())
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Writing has been not finished");
|
|
|
|
|
|
|
|
if (isEof())
|
|
|
|
return nullptr;
|
|
|
|
|
2024-05-07 20:43:40 +00:00
|
|
|
return std::make_unique<Reader>(getPath(), header, getSize());
|
2024-04-28 11:20:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 12:51:46 +00:00
|
|
|
void TemporaryFileStream::updateAllocAndCheck()
|
2022-09-01 12:22:49 +00:00
|
|
|
{
|
2022-09-14 11:21:25 +00:00
|
|
|
assert(out_writer);
|
2022-09-01 12:22:49 +00:00
|
|
|
size_t new_compressed_size = out_writer->out_compressed_buf.getCompressedBytes();
|
|
|
|
size_t new_uncompressed_size = out_writer->out_compressed_buf.getUncompressedBytes();
|
|
|
|
|
2022-09-15 10:19:39 +00:00
|
|
|
if (unlikely(new_compressed_size < stat.compressed_size || new_uncompressed_size < stat.uncompressed_size))
|
2022-09-01 12:22:49 +00:00
|
|
|
{
|
2022-09-21 12:51:46 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR,
|
2022-09-01 12:22:49 +00:00
|
|
|
"Temporary file {} size decreased after write: compressed: {} -> {}, uncompressed: {} -> {}",
|
2022-12-06 17:27:05 +00:00
|
|
|
getPath(), new_compressed_size, stat.compressed_size, new_uncompressed_size, stat.uncompressed_size);
|
2022-09-01 12:22:49 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 12:51:46 +00:00
|
|
|
parent->deltaAllocAndCheck(new_compressed_size - stat.compressed_size, new_uncompressed_size - stat.uncompressed_size);
|
2022-09-15 10:19:39 +00:00
|
|
|
stat.compressed_size = new_compressed_size;
|
|
|
|
stat.uncompressed_size = new_uncompressed_size;
|
2022-10-05 16:35:10 +00:00
|
|
|
stat.num_rows = out_writer->num_rows;
|
2022-09-01 12:22:49 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 16:35:10 +00:00
|
|
|
bool TemporaryFileStream::isEof() const
|
2022-09-22 17:39:30 +00:00
|
|
|
{
|
2023-01-06 14:18:59 +00:00
|
|
|
return file == nullptr && !segment_holder;
|
2022-09-22 17:39:30 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 16:35:10 +00:00
|
|
|
void TemporaryFileStream::release()
|
2022-09-22 17:39:30 +00:00
|
|
|
{
|
|
|
|
if (in_reader)
|
|
|
|
in_reader.reset();
|
|
|
|
|
|
|
|
if (out_writer)
|
|
|
|
{
|
|
|
|
out_writer->finalize();
|
|
|
|
out_writer.reset();
|
|
|
|
}
|
2022-12-06 17:27:05 +00:00
|
|
|
|
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
file.reset();
|
|
|
|
parent->deltaAllocAndCheck(-stat.compressed_size, -stat.uncompressed_size);
|
|
|
|
}
|
|
|
|
|
2023-01-06 14:18:59 +00:00
|
|
|
if (segment_holder)
|
|
|
|
segment_holder.reset();
|
2022-12-06 17:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String TemporaryFileStream::getPath() const
|
|
|
|
{
|
|
|
|
if (file)
|
2023-06-27 15:10:48 +00:00
|
|
|
return file->getAbsolutePath();
|
2023-01-06 14:18:59 +00:00
|
|
|
if (segment_holder && !segment_holder->empty())
|
2024-01-17 11:57:40 +00:00
|
|
|
return segment_holder->front().getPath();
|
2022-12-06 17:27:05 +00:00
|
|
|
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "TemporaryFileStream has no file");
|
2022-09-22 17:39:30 +00:00
|
|
|
}
|
|
|
|
|
2023-10-02 11:09:40 +00:00
|
|
|
size_t TemporaryFileStream::getSize() const
|
|
|
|
{
|
|
|
|
if (file)
|
|
|
|
return file->getDisk()->getFileSize(file->getRelativePath());
|
|
|
|
if (segment_holder && !segment_holder->empty())
|
|
|
|
return segment_holder->front().getReservedSize();
|
|
|
|
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "TemporaryFileStream has no file");
|
|
|
|
}
|
|
|
|
|
2022-09-01 12:22:49 +00:00
|
|
|
TemporaryFileStream::~TemporaryFileStream()
|
2022-08-31 17:17:31 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-10-05 16:35:10 +00:00
|
|
|
release();
|
2022-08-31 17:17:31 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
2022-09-21 12:51:46 +00:00
|
|
|
assert(false); /// deltaAllocAndCheck with negative can't throw exception
|
2022-08-31 17:17:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 12:53:26 +00:00
|
|
|
}
|