Skip to content

Commit

Permalink
[BugFix] Fix the invalid skip distance when coping io buffer in datac…
Browse files Browse the repository at this point in the history
…ache. (StarRocks#31135)

Signed-off-by: Gavin <yangguansuo@starrocks.com>
  • Loading branch information
GavinMar authored Sep 18, 2023
1 parent 1e1bf69 commit c4753e5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion be/src/block_cache/io_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ size_t IOBuffer::copy_to(void* data, ssize_t size, size_t pos) const {
skip -= sp.size();
continue;
}
size_t to_cp = std::min(sp.size(), bytes_to_copy - bytes_copied);
size_t to_cp = std::min(sp.size() - skip, bytes_to_copy - bytes_copied);
strings::memcpy_inlined((char*)data + bytes_copied, sp.data() + skip, to_cp);
bytes_copied += to_cp;
if (bytes_copied >= size) {
break;
}
skip = 0;
}
}
return bytes_copied;
Expand Down
28 changes: 28 additions & 0 deletions be/test/block_cache/block_cache_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,34 @@ TEST_F(BlockCacheTest, auto_create_disk_cache_path) {
cache->shutdown();
}

TEST_F(BlockCacheTest, copy_to_iobuf) {
// Create an iobuffer which contains 3 blocks
const size_t buf_block_size = 100;
void* data1 = malloc(buf_block_size);
void* data2 = malloc(buf_block_size);
void* data3 = malloc(buf_block_size);
memset(data1, 1, buf_block_size);
memset(data2, 2, buf_block_size);
memset(data3, 3, buf_block_size);

IOBuffer buffer;
buffer.append_user_data(data1, buf_block_size, nullptr);
buffer.append_user_data(data2, buf_block_size, nullptr);
buffer.append_user_data(data3, buf_block_size, nullptr);

// Copy the last 150 bytes of iobuffer to a target buffer
const off_t offset = 150;
const size_t size = 150;
char result[size] = {0};
buffer.copy_to(result, size, offset);

// Check the target buffer content
char expect[size] = {0};
memset(expect, 2, 50);
memset(expect + 50, 3, 100);
ASSERT_EQ(memcmp(result, expect, size), 0);
}

#ifdef WITH_STARCACHE
TEST_F(BlockCacheTest, hybrid_cache) {
std::unique_ptr<BlockCache> cache(new BlockCache);
Expand Down

0 comments on commit c4753e5

Please sign in to comment.