33import com .fasterxml .jackson .databind .ObjectMapper ;
44import com .google .common .base .Optional ;
55import com .google .common .base .Stopwatch ;
6+ import com .google .common .cache .CacheBuilder ;
7+ import com .google .common .cache .CacheLoader ;
8+ import com .google .common .cache .LoadingCache ;
69import com .google .common .collect .Lists ;
710import io .dropwizard .util .Duration ;
811import lombok .AllArgsConstructor ;
912import lombok .extern .slf4j .Slf4j ;
1013import org .apache .commons .io .DirectoryWalker ;
11- import org .apache .commons .lang3 .time .StopWatch ;
1214import org .joda .time .DateTime ;
1315
1416import java .io .File ;
@@ -30,6 +32,16 @@ public class BlobDataDirectoryCleaner extends DirectoryWalker<String> implements
3032 private final FileSystemJsonBlobManager fileSystemJsonBlobManager ;
3133 private final ObjectMapper om ;
3234
35+ private final LoadingCache <String , BlobMetadataContainer > blobMetadataContainerCache = CacheBuilder .newBuilder ()
36+ .expireAfterWrite (1 , TimeUnit .HOURS )
37+ .build (new CacheLoader <String , BlobMetadataContainer >() {
38+ @ Override
39+ public BlobMetadataContainer load (String key ) throws Exception {
40+ File metadataFile = new File (key );
41+ return metadataFile .exists () ? om .readValue (fileSystemJsonBlobManager .readFile (metadataFile ), BlobMetadataContainer .class ) : new BlobMetadataContainer ();
42+ }
43+ });
44+
3345 @ Override
3446 protected void handleFile (File file , int depth , Collection <String > results ) throws IOException {
3547 log .debug ("Processing {}" , file .getAbsolutePath ());
@@ -40,29 +52,24 @@ protected void handleFile(File file, int depth, Collection<String> results) thro
4052 return ;
4153 }
4254
43- try {
44- BlobMetadataContainer metadataContainer = metadataFile .exists () ? om .readValue (fileSystemJsonBlobManager .readFile (metadataFile ), BlobMetadataContainer .class ) : new BlobMetadataContainer ();
55+ BlobMetadataContainer metadataContainer = blobMetadataContainerCache .getUnchecked (metadataFile .getAbsolutePath ());
4556
46- Optional <DateTime > lastAccessed = fileSystemJsonBlobManager .resolveTimestamp (blobId );
47- if (metadataContainer .getLastAccessedByBlobId ().containsKey (blobId )) {
48- lastAccessed = Optional .of (metadataContainer .getLastAccessedByBlobId ().get (blobId ));
49- }
50-
51- if (!lastAccessed .isPresent ()) {
52- log .warn ("Couldn't get last accessed timestamp for blob {}" , blobId );
53- return ;
54- }
57+ Optional <DateTime > lastAccessed = fileSystemJsonBlobManager .resolveTimestamp (blobId );
58+ if (metadataContainer .getLastAccessedByBlobId ().containsKey (blobId )) {
59+ lastAccessed = Optional .of (metadataContainer .getLastAccessedByBlobId ().get (blobId ));
60+ }
5561
56- log .debug ("Blob {} was last accessed {}" , blobId , lastAccessed .get ());
62+ if (!lastAccessed .isPresent ()) {
63+ log .warn ("Couldn't get last accessed timestamp for blob {}" , blobId );
64+ return ;
65+ }
5766
58- if (lastAccessed .get ().plusMillis ((int ) blobAccessTtl .toMilliseconds ()).isBefore (DateTime .now ())) {
59- log .debug ("Blob {} is older than {}, so it's going to be deleted" , blobId , blobAccessTtl );
60- file .delete ();
61- results .add (blobId );
62- }
67+ log .debug ("Blob {} was last accessed {}" , blobId , lastAccessed .get ());
6368
64- } catch (IOException e ) {
65- log .warn ("Couldn't load metadata file from {}" , file .getParentFile ().getAbsolutePath (), e );
69+ if (lastAccessed .get ().plusMillis ((int ) blobAccessTtl .toMilliseconds ()).isBefore (DateTime .now ())) {
70+ log .info ("Blob {} is older than {} (last accessed {}), so it's going to be deleted" , blobId , blobAccessTtl , lastAccessed .get ());
71+ file .delete ();
72+ results .add (blobId );
6673 }
6774 }
6875
0 commit comments