Skip to content

Commit

Permalink
#169: FileCacheing
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Apr 27, 2021
1 parent 5091fa3 commit 11b0d88
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
60 changes: 31 additions & 29 deletions app/src/main/java/de/k3b/android/io/DocumentFileTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ public class DocumentFileTranslator {
*/
private final Map<File, DocumentFile> dirCache = new HashMap<>();
protected DocumentFileCache documentFileCache = new DocumentFileCache();


private static final File internalRootCandidate = new File("/storage/emulated/0");
// for debugging
private static int id = 1;
Expand All @@ -88,24 +86,34 @@ public static DocumentFileTranslator create(Context context) {
root = new Root(context.getApplicationContext());

}
return new DocumentFileTranslator(context, "").init();
return new DocumentFileTranslator(context, "").initCache();
}

/**
* Livecycle: Indicate that {@link #getDirCache()} is invalid and must be reloaded
*
* @param eventSourceOrNull the class that initiated the invalidate
*/
public static void invalidateDirCache(Object eventSourceOrNull) {
if (eventSourceOrNull instanceof DocumentFileTranslator) {
((DocumentFileTranslator) eventSourceOrNull).dirCacheGeneratetionID++;
}
dirCacheGlobalGeneratetionID++;
}

private DocumentFileTranslator init() {
private DocumentFileTranslator initDirCache() {
File rootFile = getInternalStorageRoot();
String debugContext = "init";
if (rootFile != null) {
rootFile = rootFile.getAbsoluteFile();
DocumentFile docRoot = DocumentFile.fromFile(rootFile);
if ((docRoot != null) && docRoot.exists() && docRoot.isDirectory() && docRoot.canWrite()) {
add(debugContext, rootFile, docRoot);
add2DirCache(debugContext, rootFile, docRoot);
}
}
for (Map.Entry<String, String> enty : root.dir2uri.entrySet()) {
add(debugContext, new File(enty.getKey()), DocumentFile.fromTreeUri(context, Uri.parse(enty.getValue())));
add2DirCache(debugContext, new File(enty.getKey()), DocumentFile.fromTreeUri(context, Uri.parse(enty.getValue())));
}
documentFileCache = new DocumentFileCache();

return this;
}

Expand All @@ -117,16 +125,10 @@ private static File getInternalStorageRoot() {
return null;
}

/**
* Livecycle: Indicate that {@link #getDirCache()} is invalid and must be reloaded
*
* @param eventSourceOrNull the class that initiated the invalidate
*/
public static void invalidate(Object eventSourceOrNull) {
if (eventSourceOrNull instanceof DocumentFileTranslator) {
((DocumentFileTranslator) eventSourceOrNull).dirCacheGeneratetionID++;
}
dirCacheGlobalGeneratetionID ++;
private DocumentFileTranslator initCache() {
initDirCache();
documentFileCache = new DocumentFileCache();
return this;
}

public boolean isKnownRoot(File candidate) {
Expand All @@ -140,26 +142,26 @@ public boolean isKnownRoot(File candidate) {
return false;
}

public DocumentFileTranslator addRoot(File directory, Uri documentRootUri) {
public DocumentFileTranslator addRoot2DirCache(File directory, Uri documentRootUri) {
if (root.add(directory.getAbsolutePath(), documentRootUri.toString())) {
add("addRoot", directory, DocumentFile.fromTreeUri(context, documentRootUri));
invalidate(this);
add2DirCache("addRoot2DirCache", directory, DocumentFile.fromTreeUri(context, documentRootUri));
invalidateDirCache(this);
root.saveToPrefs();
}
return this;
}

protected Map<File, DocumentFile> getDirCache() {
private Map<File, DocumentFile> getDirCache() {
if (dirCacheGeneratetionID != dirCacheGlobalGeneratetionID) {
// dirCache was invalidated from outside. Must be re-created
dirCache.clear();
dirCacheGeneratetionID = dirCacheGlobalGeneratetionID;
init();
initCache();
}
return dirCache;
}

protected DocumentFile getFromCache(String debugContext, File fileOrDir, boolean isDir) {
private DocumentFile getFromDirCache(String debugContext, File fileOrDir, boolean isDir) {
DocumentFile result = getDirCache().get(fileOrDir);
if (result == null && DocumentFileTranslator.debugLogSAFCache) {
Log.i(FileFacade.LOG_TAG,
Expand All @@ -176,7 +178,7 @@ protected DocumentFile getFromCache(String debugContext, File fileOrDir, boolean
/**
* add mapping from file-sdcard, -usbstick, -networkstorage to documentFileDirRoot
*/
private DocumentFileTranslator add(String debugContext, File directory, DocumentFile documentFileDir) {
private DocumentFileTranslator add2DirCache(String debugContext, File directory, DocumentFile documentFileDir) {
if ((documentFileDir != null) && documentFileDir.isDirectory()) {
if (FileFacade.debugLogSAFFacade || DocumentFileTranslator.debugLogSAFCache) {
Uri uri = (documentFileDir != null) ? documentFileDir.getUri() : null;
Expand All @@ -191,7 +193,7 @@ private DocumentFileTranslator add(String debugContext, File directory, Document
private DocumentFile getDocumentFileOrDirImpl(String debugContext, File fileOrDir, boolean isDir) {
DocumentFile result = null;
if (fileOrDir != null) {
result = getFromCache(debugContext, fileOrDir, isDir);
result = getFromDirCache(debugContext, fileOrDir, isDir);
if (result == null) {
DocumentFile parent = getDocumentFileOrDirImpl(debugContext, fileOrDir.getParentFile(), true);
if (parent != null) {
Expand All @@ -216,7 +218,7 @@ private DocumentFile findFile(String debugContext, DocumentFile parentDoc, File
foundDoc = childDoc;
}
if (childDoc.isDirectory()) {
add(debugContext + " findFile ", new File(parentFile, childDocName), foundDoc);
add2DirCache(debugContext + " findFile ", new File(parentFile, childDocName), foundDoc);
}
}
return foundDoc;
Expand All @@ -233,7 +235,7 @@ private DocumentFile findFile(String debugContext, DocumentFile parentDoc, File
public DocumentFile getOrCreateDirectory(String debugContext, File directory) {
DocumentFile result = null;
if (directory != null) {
result = getFromCache(debugContext, directory, true);
result = getFromDirCache(debugContext, directory, true);
if (result == null) {
DocumentFile parent = getOrCreateDirectory(debugContext, directory.getParentFile());
if ((parent != null) && parent.isDirectory()) {
Expand All @@ -247,7 +249,7 @@ public DocumentFile getOrCreateDirectory(String debugContext, File directory) {

Global.android_DocumentFile_find_cache = false;
result = parent.createDirectory(directory.getName());
add(debugContext + " created dir ", directory, result);
add2DirCache(debugContext + " created dir ", directory, result);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private void onRootUriResult(Uri documentRootUri) {
currentRootFileRequest = null;

if ((rootFile != null) && (documentRootUri != null)) {
getDocumentFileTranslator().addRoot(rootFile, documentRootUri);
getDocumentFileTranslator().addRoot2DirCache(rootFile, documentRootUri);
if (permissionGrantedHandler != null) {
permissionGrantedHandler.afterGrant(this);
}
Expand Down

0 comments on commit 11b0d88

Please sign in to comment.