@@ -87,55 +87,60 @@ impl SequentialCache {
8787
8888/// A MetadataFetch implementation that caches fetched data in exponentially growing chunks,
8989/// sequentially from the beginning of the file.
90- pub struct ExponentialMetadataCache < F : MetadataFetch > {
90+ pub struct ReadAheadMetadataCache < F : MetadataFetch > {
9191 inner : F ,
9292 cache : Arc < Mutex < SequentialCache > > ,
93+
94+ initial_size : u64 ,
95+ increment_multiple : u64 ,
9396}
9497
95- impl < F : MetadataFetch > ExponentialMetadataCache < F > {
96- /// Create a new ExponentialMetadataCache wrapping the given MetadataFetch
97- pub fn new ( inner : F ) -> AsyncTiffResult < Self > {
98+ impl < F : MetadataFetch > ReadAheadMetadataCache < F > {
99+ /// Create a new EagerMetadataCache wrapping the given MetadataFetch
100+ pub fn new ( inner : F , initial_size : u64 , increment_multiple : u64 ) -> AsyncTiffResult < Self > {
98101 Ok ( Self {
99102 inner,
100103 cache : Arc :: new ( Mutex :: new ( SequentialCache :: new ( ) ) ) ,
104+ initial_size,
105+ increment_multiple,
101106 } )
102107 }
103- }
104108
105- fn next_fetch_size ( existing_len : u64 ) -> u64 {
106- if existing_len == 0 {
107- 64 * 1024
108- } else {
109- existing_len * 2
109+ fn next_fetch_size ( & self , current_len : u64 ) -> u64 {
110+ if current_len == 0 {
111+ self . initial_size
112+ } else {
113+ current_len * self . increment_multiple
114+ }
110115 }
111116}
112117
113- impl < F : MetadataFetch + Send + Sync > MetadataFetch for ExponentialMetadataCache < F > {
118+ impl < F : MetadataFetch + Send + Sync > MetadataFetch for ReadAheadMetadataCache < F > {
114119 fn fetch ( & self , range : Range < u64 > ) -> BoxFuture < ' _ , AsyncTiffResult < Bytes > > {
115120 let cache = self . cache . clone ( ) ;
116121
117122 Box :: pin ( async move {
118- let mut g = cache. lock ( ) . await ;
123+ let mut cache = cache. lock ( ) . await ;
119124
120125 // First check if we already have the range cached
121- if g . contains ( range. start ..range. end ) {
122- return Ok ( g . slice ( range) ) ;
126+ if cache . contains ( range. start ..range. end ) {
127+ return Ok ( cache . slice ( range) ) ;
123128 }
124129
125130 // Compute the correct fetch range
126- let start_len = g . len ;
131+ let start_len = cache . len ;
127132 let needed = range. end . saturating_sub ( start_len) ;
128- let fetch_size = next_fetch_size ( start_len) . max ( needed) ;
133+ let fetch_size = & self . next_fetch_size ( start_len) . max ( needed) ;
129134 let fetch_range = start_len..start_len + fetch_size;
130135
131136 // Perform the fetch while holding mutex
132137 // (this is OK because the mutex is async)
133138 let bytes = self . inner . fetch ( fetch_range) . await ?;
134139
135140 // Now append safely
136- g . append_buffer ( bytes) ;
141+ cache . append_buffer ( bytes) ;
137142
138- Ok ( g . slice ( range) )
143+ Ok ( cache . slice ( range) )
139144 } )
140145 }
141146}
0 commit comments