@@ -88,6 +88,8 @@ use crate::CargoResult;
88
88
use crate :: GlobalContext ;
89
89
90
90
use super :: split;
91
+ use super :: Summaries ;
92
+ use super :: MaybeIndexSummary ;
91
93
use super :: INDEX_V_MAX ;
92
94
93
95
/// The current version of [`SummariesCache`].
@@ -231,18 +233,27 @@ impl<'a> SummariesCache<'a> {
231
233
/// An abstraction of the actual cache store.
232
234
trait CacheStore {
233
235
/// Gets the cache associated with the key.
234
- fn get ( & self , key : & str ) -> Option < Vec < u8 > > ;
236
+ fn get ( & self , key : & str ) -> Option < MaybeSummaries > ;
235
237
236
238
/// Associates the value with the key.
237
239
fn put ( & self , key : & str , value : & [ u8 ] ) ;
238
240
241
+ /// Associates the value with the key + version tuple.
242
+ fn put_summary ( & self , key : ( & str , & Version ) , value : & [ u8 ] ) ;
243
+
239
244
/// Invalidates the cache associated with the key.
240
245
fn invalidate ( & self , key : & str ) ;
241
246
}
242
247
248
+ pub enum MaybeSummaries {
249
+ Unparsed ( Vec < u8 > ) ,
250
+ Parsed ( Summaries ) ,
251
+ }
252
+
243
253
/// Manages the on-disk index caches.
244
254
pub struct CacheManager < ' gctx > {
245
255
store : Box < dyn CacheStore + ' gctx > ,
256
+ is_sqlite : bool ,
246
257
}
247
258
248
259
impl < ' gctx > CacheManager < ' gctx > {
@@ -258,11 +269,15 @@ impl<'gctx> CacheManager<'gctx> {
258
269
} else {
259
270
Box :: new ( LocalFileSystem :: new ( cache_root, gctx) )
260
271
} ;
261
- CacheManager { store }
272
+ CacheManager { store, is_sqlite : use_sqlite }
273
+ }
274
+
275
+ pub fn is_sqlite ( & self ) -> bool {
276
+ self . is_sqlite
262
277
}
263
278
264
279
/// Gets the cache associated with the key.
265
- pub fn get ( & self , key : & str ) -> Option < Vec < u8 > > {
280
+ pub fn get ( & self , key : & str ) -> Option < MaybeSummaries > {
266
281
self . store . get ( key)
267
282
}
268
283
@@ -271,6 +286,11 @@ impl<'gctx> CacheManager<'gctx> {
271
286
self . store . put ( key, value)
272
287
}
273
288
289
+ /// Associates the value with the key + version tuple.
290
+ pub fn put_summary ( & self , key : ( & str , & Version ) , value : & [ u8 ] ) {
291
+ self . store . put_summary ( key, value)
292
+ }
293
+
274
294
/// Invalidates the cache associated with the key.
275
295
pub fn invalidate ( & self , key : & str ) {
276
296
self . store . invalidate ( key)
@@ -301,10 +321,10 @@ impl LocalFileSystem<'_> {
301
321
}
302
322
303
323
impl CacheStore for LocalFileSystem < ' _ > {
304
- fn get ( & self , key : & str ) -> Option < Vec < u8 > > {
324
+ fn get ( & self , key : & str ) -> Option < MaybeSummaries > {
305
325
let cache_path = & self . cache_path ( key) ;
306
326
match fs:: read ( cache_path) {
307
- Ok ( contents) => Some ( contents) ,
327
+ Ok ( contents) => Some ( MaybeSummaries :: Unparsed ( contents) ) ,
308
328
Err ( e) => {
309
329
tracing:: debug!( ?cache_path, "cache missing: {e}" ) ;
310
330
None
@@ -324,6 +344,10 @@ impl CacheStore for LocalFileSystem<'_> {
324
344
}
325
345
}
326
346
347
+ fn put_summary ( & self , _key : ( & str , & Version ) , _value : & [ u8 ] ) {
348
+ panic ! ( "unsupported" ) ;
349
+ }
350
+
327
351
fn invalidate ( & self , key : & str ) {
328
352
let cache_path = & self . cache_path ( key) ;
329
353
if let Err ( e) = fs:: remove_file ( cache_path) {
@@ -341,7 +365,7 @@ struct LocalDatabase<'gctx> {
341
365
/// Connection to the SQLite database.
342
366
conn : OnceCell < Option < RefCell < Connection > > > ,
343
367
/// [`GlobalContext`] reference for convenience.
344
- deferred_writes : RefCell < BTreeMap < String , Vec < u8 > > > ,
368
+ deferred_writes : RefCell < BTreeMap < String , Vec < ( String , Vec < u8 > ) > > > ,
345
369
gctx : & ' gctx GlobalContext ,
346
370
}
347
371
@@ -351,7 +375,7 @@ impl LocalDatabase<'_> {
351
375
LocalDatabase {
352
376
cache_root,
353
377
conn : OnceCell :: new ( ) ,
354
- deferred_writes : RefCell :: new ( BTreeMap :: new ( ) ) ,
378
+ deferred_writes : Default :: default ( ) ,
355
379
gctx,
356
380
}
357
381
}
@@ -386,9 +410,11 @@ impl LocalDatabase<'_> {
386
410
let mut conn = conn. borrow_mut ( ) ;
387
411
let tx = conn. transaction ( ) ?;
388
412
let mut stmt =
389
- tx. prepare_cached ( "INSERT OR REPLACE INTO summaries (name, value) VALUES (?, ?)" ) ?;
390
- for ( key, value) in self . deferred_writes . borrow ( ) . iter ( ) {
391
- stmt. execute ( params ! ( key, value) ) ?;
413
+ tx. prepare_cached ( "INSERT OR REPLACE INTO summaries (name, version, value) VALUES (?, ?, ?)" ) ?;
414
+ for ( name, summaries) in self . deferred_writes . borrow ( ) . iter ( ) {
415
+ for ( version, value) in summaries {
416
+ stmt. execute ( params ! ( name, version, value) ) ?;
417
+ }
392
418
}
393
419
drop ( stmt) ;
394
420
tx. commit ( ) ?;
@@ -406,19 +432,36 @@ impl Drop for LocalDatabase<'_> {
406
432
}
407
433
408
434
impl CacheStore for LocalDatabase < ' _ > {
409
- fn get ( & self , key : & str ) -> Option < Vec < u8 > > {
435
+ fn get ( & self , key : & str ) -> Option < MaybeSummaries > {
410
436
self . conn ( ) ?
411
437
. borrow ( )
412
- . prepare_cached ( "SELECT value FROM summaries WHERE name = ? LIMIT 1" )
413
- . and_then ( |mut stmt| stmt. query_row ( [ key] , |row| row. get ( 0 ) ) )
414
- . map_err ( |e| tracing:: debug!( key, "cache missing: {e}" ) )
438
+ . prepare_cached ( "SELECT version, value FROM summaries WHERE name = ?" )
439
+ . and_then ( |mut stmt| {
440
+ let rows = stmt. query_map ( [ key] , |row| Ok ( ( row. get ( 0 ) ?, row. get ( 1 ) ?) ) ) ?;
441
+ let mut summaries = Summaries :: default ( ) ;
442
+ for row in rows {
443
+ let ( version, raw_data) : ( String , Vec < u8 > ) = row?;
444
+ let version = Version :: parse ( & version) . expect ( "semver" ) ;
445
+ summaries. versions . insert ( version, MaybeIndexSummary :: UnparsedData ( raw_data) ) ;
446
+ }
447
+ Ok ( MaybeSummaries :: Parsed ( summaries) )
448
+ } )
449
+ . map_err ( |e| {
450
+ tracing:: debug!( key, "cache missing: {e}" ) ;
451
+ } )
415
452
. ok ( )
416
453
}
417
454
418
- fn put ( & self , key : & str , value : & [ u8 ] ) {
455
+ fn put ( & self , _key : & str , _value : & [ u8 ] ) {
456
+ panic ! ( "unsupported" ) ;
457
+ }
458
+
459
+ fn put_summary ( & self , ( name, version) : ( & str , & Version ) , value : & [ u8 ] ) {
419
460
self . deferred_writes
420
461
. borrow_mut ( )
421
- . insert ( key. into ( ) , value. into ( ) ) ;
462
+ . entry ( name. into ( ) )
463
+ . or_insert ( Default :: default ( ) )
464
+ . push ( ( version. to_string ( ) , value. to_vec ( ) ) ) ;
422
465
}
423
466
424
467
fn invalidate ( & self , key : & str ) {
@@ -440,8 +483,10 @@ impl CacheStore for LocalDatabase<'_> {
440
483
fn migrations ( ) -> Vec < Migration > {
441
484
vec ! [ basic_migration(
442
485
"CREATE TABLE IF NOT EXISTS summaries (
443
- name TEXT PRIMARY KEY NOT NULL,
444
- value BLOB NOT NULL
486
+ name TEXT NOT NULL,
487
+ version TEXT NOT NULL,
488
+ value BLOB NOT NULL,
489
+ PRIMARY KEY (name, version)
445
490
)" ,
446
491
) ]
447
492
}
0 commit comments