From 14a783fd6fbbf25fe503f12d08099da3e1ed3bee Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Mon, 10 Feb 2025 18:34:04 +0800 Subject: [PATCH] sf_binlog_writer_rotate_file can skip empty file --- src/sf_binlog_writer.c | 20 +++++++++++++------- src/sf_binlog_writer.h | 9 ++++++++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/sf_binlog_writer.c b/src/sf_binlog_writer.c index 11e6334..5597f5b 100644 --- a/src/sf_binlog_writer.c +++ b/src/sf_binlog_writer.c @@ -168,6 +168,7 @@ static int deal_binlog_records(SFBinlogWriterThread *thread, SFBinlogWriterBuffer *wb_head, uint32_t *last_timestamp) { int result; + bool skip_empty_file; SFBinlogWriterBuffer *wbuffer; SFBinlogWriterBuffer *current; @@ -196,11 +197,14 @@ static int deal_binlog_records(SFBinlogWriterThread *thread, thread->mblock, current); break; case SF_BINLOG_BUFFER_TYPE_ROTATE_FILE: - if ((result=sf_file_writer_set_binlog_write_index(¤t-> - writer->fw, current->writer->fw.binlog. - last_index + 1)) != 0) - { - return result; + skip_empty_file = current->version.first; + if (!(skip_empty_file && current->writer->fw.file.size == 0)) { + if ((result=sf_file_writer_set_binlog_write_index(¤t-> + writer->fw, current->writer->fw.binlog. + last_index + 1)) != 0) + { + return result; + } } fast_mblock_free_object(¤t->writer-> thread->mblock, current); @@ -623,10 +627,12 @@ int sf_binlog_writer_change_write_index(SFBinlogWriterInfo *writer, SF_BINLOG_BUFFER_TYPE_SET_WRITE_INDEX, write_index); } -int sf_binlog_writer_rotate_file(SFBinlogWriterInfo *writer) +int sf_binlog_writer_rotate_file_ex(SFBinlogWriterInfo *writer, + const bool skip_empty_file) { return sf_binlog_writer_push_directive(writer, - SF_BINLOG_BUFFER_TYPE_ROTATE_FILE, 0); + SF_BINLOG_BUFFER_TYPE_ROTATE_FILE, + skip_empty_file ? 1 : 0); } int sf_binlog_writer_flush_file(SFBinlogWriterInfo *writer) diff --git a/src/sf_binlog_writer.h b/src/sf_binlog_writer.h index 478a7f5..6086466 100644 --- a/src/sf_binlog_writer.h +++ b/src/sf_binlog_writer.h @@ -235,7 +235,14 @@ static inline int sf_binlog_writer_get_thread_waiting_count( return waiting_count; } -int sf_binlog_writer_rotate_file(SFBinlogWriterInfo *writer); +int sf_binlog_writer_rotate_file_ex(SFBinlogWriterInfo *writer, + const bool skip_empty_file); + +static inline int sf_binlog_writer_rotate_file(SFBinlogWriterInfo *writer) +{ + const bool skip_empty_file = false; + return sf_binlog_writer_rotate_file_ex(writer, skip_empty_file); +} int sf_binlog_writer_flush_file(SFBinlogWriterInfo *writer);