Skip to content

Commit

Permalink
Use while-loop
Browse files Browse the repository at this point in the history
  • Loading branch information
antonio2368 committed Mar 5, 2024
1 parent 0992489 commit 0bd4c6a
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions src/Storages/StorageS3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,45 +258,46 @@ class StorageS3Source::DisclosedGlobIterator::Impl : WithContext

KeyWithInfoPtr nextAssumeLocked()
{
if (buffer_iter != buffer.end())
do
{
auto answer = *buffer_iter;
++buffer_iter;

/// If url doesn't contain globs, we didn't list s3 bucket and didn't get object info for the key.
/// So we get object info lazily here on 'next()' request.
if (!answer->info)
if (buffer_iter != buffer.end())
{
answer->info = S3::getObjectInfo(*client, globbed_uri.bucket, answer->key, globbed_uri.version_id, request_settings);
if (file_progress_callback)
file_progress_callback(FileProgress(0, answer->info->size));
}
auto answer = *buffer_iter;
++buffer_iter;

return answer;
}
/// If url doesn't contain globs, we didn't list s3 bucket and didn't get object info for the key.
/// So we get object info lazily here on 'next()' request.
if (!answer->info)
{
answer->info = S3::getObjectInfo(*client, globbed_uri.bucket, answer->key, globbed_uri.version_id, request_settings);
if (file_progress_callback)
file_progress_callback(FileProgress(0, answer->info->size));
}

if (is_finished)
return {};
return answer;
}

try
{
fillInternalBufferAssumeLocked();
}
catch (...)
{
/// In case of exception thrown while listing new batch of files
/// iterator may be partially initialized and its further using may lead to UB.
/// Iterator is used by several processors from several threads and
/// it may take some time for threads to stop processors and they
/// may still use this iterator after exception is thrown.
/// To avoid this UB, reset the buffer and return defaults for further calls.
is_finished = true;
buffer.clear();
buffer_iter = buffer.begin();
throw;
}
if (is_finished)
return {};

return nextAssumeLocked();
try
{
fillInternalBufferAssumeLocked();
}
catch (...)
{
/// In case of exception thrown while listing new batch of files
/// iterator may be partially initialized and its further using may lead to UB.
/// Iterator is used by several processors from several threads and
/// it may take some time for threads to stop processors and they
/// may still use this iterator after exception is thrown.
/// To avoid this UB, reset the buffer and return defaults for further calls.
is_finished = true;
buffer.clear();
buffer_iter = buffer.begin();
throw;
}
} while (true);
}

void fillInternalBufferAssumeLocked()
Expand Down

0 comments on commit 0bd4c6a

Please sign in to comment.