@@ -432,22 +432,15 @@ unsafe extern "C" fn kv_io_read(
432432 let end_chunk = ( offset + requested_length - 1 ) / kv:: CHUNK_SIZE ;
433433
434434 let mut chunk_keys_to_fetch = Vec :: new( ) ;
435- let mut buffered_chunks: HashMap <usize , Vec <u8 >> = HashMap :: new( ) ;
436435 for chunk_idx in start_chunk..=end_chunk {
437- // Check dirty buffer first (batch mode writes).
438436 if state. batch_mode {
439- if let Some ( buffered) = state. dirty_buffer. get( & ( chunk_idx as u32 ) ) {
440- buffered_chunks. insert( chunk_idx, buffered. clone( ) ) ;
437+ if state. dirty_buffer. contains_key( & ( chunk_idx as u32 ) ) {
441438 continue ;
442439 }
443440 }
444- // Check read cache.
445441 let key = kv:: get_chunk_key( file. file_tag, chunk_idx as u32 ) ;
446- if ctx. read_cache_enabled {
447- if let Some ( cached) = state. read_cache. get( key. as_slice( ) ) {
448- buffered_chunks. insert( chunk_idx, cached. clone( ) ) ;
442+ if ctx. read_cache_enabled && state. read_cache. contains_key( key. as_slice( ) ) {
449443 continue ;
450- }
451444 }
452445 chunk_keys_to_fetch. push( key. to_vec( ) ) ;
453446 }
@@ -475,13 +468,26 @@ unsafe extern "C" fn kv_io_read(
475468 let value_map = build_value_map( & resp) ;
476469
477470 for chunk_idx in start_chunk..=end_chunk {
478- let chunk_data: Option <& [ u8 ] > = buffered_chunks
479- . get( & chunk_idx)
480- . map( |v| v. as_slice( ) )
481- . or_else( || {
482- let key = kv:: get_chunk_key( file. file_tag, chunk_idx as u32 ) ;
483- value_map. get( key. as_slice( ) ) . copied( )
484- } ) ;
471+ let chunk_key = kv:: get_chunk_key( file. file_tag, chunk_idx as u32 ) ;
472+ let chunk_data = if state. batch_mode {
473+ state
474+ . dirty_buffer
475+ . get( & ( chunk_idx as u32 ) )
476+ . map( |buffered| buffered. as_slice( ) )
477+ } else {
478+ None
479+ }
480+ . or_else( || {
481+ if ctx. read_cache_enabled {
482+ state
483+ . read_cache
484+ . get( chunk_key. as_slice( ) )
485+ . map( |cached| cached. as_slice( ) )
486+ } else {
487+ None
488+ }
489+ } )
490+ . or_else( || value_map. get( chunk_key. as_slice( ) ) . copied( ) ) ;
485491 let chunk_offset = chunk_idx * kv:: CHUNK_SIZE ;
486492 let read_start = offset. saturating_sub( chunk_offset) ;
487493 let read_end = std:: cmp:: min( kv:: CHUNK_SIZE , offset + requested_length - chunk_offset) ;
0 commit comments