From b218efe4c1cc7b589559c82e589448feb333e1b3 Mon Sep 17 00:00:00 2001 From: Alessandro Strada Date: Sat, 19 Nov 2016 14:18:11 +0100 Subject: [PATCH] Recompute cache size at application start --- src/cache.ml | 18 ++++++++++++++++++ src/drive.ml | 15 +++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/cache.ml b/src/cache.ml index ea64a93..9ec7829 100644 --- a/src/cache.ml +++ b/src/cache.ml @@ -1236,3 +1236,21 @@ let clean_up_cache cache = (Sys.readdir cache.cache_dir) end +let compute_cache_size cache = + if Sys.file_exists cache.cache_dir && + Sys.is_directory cache.cache_dir then begin + Array.fold_left + (fun size file -> + try + let path = Filename.concat cache.cache_dir file in + if Sys.file_exists path && path <> cache.db_path then begin + let stats = Unix.LargeFile.stat path in + let file_size = stats.Unix.LargeFile.st_size in + Int64.add size file_size + end else size + with e -> Utils.log_exception e; size + ) + 0L + (Sys.readdir cache.cache_dir) + end else 0L + diff --git a/src/drive.ml b/src/drive.ml index 58511b2..d346e03 100644 --- a/src/drive.ml +++ b/src/drive.ml @@ -728,13 +728,28 @@ let get_metadata () = SessionM.return updated_metadata in + let resync_cache_size db_metadata = + let old_cache_size = db_metadata.Cache.Metadata.cache_size in + Utils.log_with_header + "BEGIN: Recalculating cache size (old value=%Ld)\n%!" + old_cache_size; + let cache_size = Cache.compute_cache_size context.Context.cache in + Utils.log_with_header + "END: Recalculating cache size (new value=%Ld)\n%!" + cache_size; + db_metadata |> Cache.Metadata.cache_size ^= cache_size + in + with_metadata_lock (fun () -> let metadata = + let context = Context.get_ctx () in if Option.is_none context.Context.metadata then begin Utils.log_with_header "BEGIN: Loading metadata from db\n%!"; let db_metadata = Cache.Metadata.select_metadata context.Context.cache in + let db_metadata = + Option.map resync_cache_size db_metadata in Context.update_ctx (Context.metadata ^= db_metadata); db_metadata end else begin