Skip to content

Commit

Permalink
Optimisation: Startet Global dir cache OSDirectory.OsDirFileCache
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Feb 14, 2021
1 parent 84091d9 commit 7cf7ca2
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 250 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2020 by k3b.
* Copyright (c) 2015-2021 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -188,7 +188,7 @@ protected void addItem(DirectoryBuilder builder, String path, int markerItemCoun
}

private void compressLatLon(IDirectory result) {
IDirectory[] children = (result != null) ? result.getChildren() : null;
IDirectory[] children = (result != null) ? result.getChildDirs() : null;

if (children != null) {
for (IDirectory _child: children) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2020 by k3b.
* Copyright (c) 2015-2021 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -744,7 +744,7 @@ private void reloadDirGuiIfAvailable(String why) {
// scroll to right where deepest child is
if (first != null) mParentPathBarScroller.requestChildFocus(mParentPathBar, first);

IDirectory[] children = selectedChild.getChildren();
IDirectory[] children = selectedChild.getChildDirs();
if (children != null) {
for (IDirectory child : children) {
Button button = createPathButton(child);
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/de/k3b/android/util/OsUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2020 by k3b.
* Copyright (c) 2015-2021 by k3b.
*
* This file is part of AndroFotoFinder.
*
Expand Down Expand Up @@ -88,7 +88,7 @@ public static OSDirectory getRootOSDirectory(OSDirectory factory) {
OSDirectory rootDir = createOsDirectory(
FileFacade.convert(context + 1, "/"), factory);

if (rootDir.getChildren() == null || rootDir.getChildren().length == 0) {
if (rootDir.getChildDirs(null) == null || rootDir.getChildDirs(null).length == 0) {
// load on demand has failed on non rooted device

// externalStorageFile
Expand Down
10 changes: 10 additions & 0 deletions fotolib2/src/main/java/de/k3b/io/AlbumFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.ArrayList;
import java.util.List;

import de.k3b.io.filefacade.FileFacade;

/**
* Created by k3b on 17.04.2018.
*/
Expand All @@ -30,6 +32,14 @@ public class AlbumFile {
public static final String SUFFIX_VALBUM = ".album";
public static final String SUFFIX_QUERY = ".query";

static {
init();
}

public static void init() {
FileFacade.includeFileSuffixesForListDir(SUFFIX_QUERY, SUFFIX_VALBUM);
}

public static boolean isQueryFile(String uri) {
if (uri != null) {
return isQueryFile(uri, SUFFIX_VALBUM) || isQueryFile(uri, SUFFIX_QUERY);
Expand Down
146 changes: 77 additions & 69 deletions fotolib2/src/main/java/de/k3b/io/Directory.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,20 @@ public Directory(String relPath, IDirectory parent, int nonDirItemCount) {
setNonDirItemCount(nonDirItemCount);
}

public static IDirectory[] removeChild(IDirectory parent, IDirectory[] children, IDirectory... child) {
public static <T extends IDirectory> T[] removeChild(IDirectory<T> parent, T[] children, T... child) {
List<Integer> positions = new ArrayList<>();
for (int i = 0; i < child.length; i++) {
int index = parent.childIndexOf(child[i]);
if (index >= 0 && !positions.contains(index)) {
positions.add(index);
}
}
return removeChild(children, positions);
return removeChild(parent, children, positions);
}

@Override
public void destroy() {
if (children != null) {
for (IDirectory child : getChildren()) {
child.destroy();
}
children = null;
}
parent = null;
}

public static IDirectory[] removeChild(IDirectory[] children, List<Integer> removePositions) {
public static <T extends IDirectory> T[] removeChild(IDirectory<T> factory, T[] children, List<Integer> removePositions) {
if (removePositions != null && removePositions.size() > 0) {
IDirectory[] newArray = new IDirectory[children.length - removePositions.size()];
T[] newArray = factory.createOsDirectoryArray(children.length - removePositions.size());
int in = children.length;
int out = newArray.length;
while (--in >= 0) {
Expand All @@ -101,17 +90,22 @@ public static IDirectory[] removeChild(IDirectory[] children, List<Integer> remo
return children;
}

public static IDirectory[] add(IDirectory[] oldChildren, IDirectory... newChildren) {
public static <T extends IDirectory> T[] add(IDirectory<T> factory, T[] oldChildren, T... newChildren) {
if (newChildren == null || newChildren.length == 0) return oldChildren;
if (oldChildren == null || oldChildren.length == 0) return newChildren;

IDirectory[] result = new IDirectory[newChildren.length + oldChildren.length];
T[] result = factory.createOsDirectoryArray(newChildren.length + oldChildren.length);
System.arraycopy(oldChildren, 0, result, 0, oldChildren.length);

System.arraycopy(newChildren, 0, result, oldChildren.length, newChildren.length);
return result;
}

public static int getChildCount(IDirectory item) {
if ((item != null) && (item.getChildDirs() != null)) return item.getChildDirs().length;
return 0;
}

public static <T> int childIndexOf(T[] children, T child) {
if (child != null && children != null) {
for (int i = children.length - 1; i >= 0; i--) {
Expand All @@ -121,16 +115,68 @@ public static <T> int childIndexOf(T[] children, T child) {
return -1;
}

public static int getChildCount(IDirectory item) {
if ((item != null) && (item.getChildren() != null)) return item.getChildren().length;
return 0;
protected static StringBuilder toTreeString(StringBuilder result, Directory item, String delimiter, int options) {
if (item != null) {
result.append(item.getRelPath());
appendCount(result, item, options);
result.append(delimiter);

if (item.getChildDirs() != null) {
for (IDirectory child : item.getChildDirs()) {
toTreeString(result, (Directory) child, delimiter, options);
}
}
}
return result;
}

private static IDirectory find(IDirectory parent, StringBuilder path) {
while (path.indexOf(PATH_DELIMITER) == 0) {
path.delete(0, PATH_DELIMITER.length());
}

int pathLen = path.length();
if (pathLen == 0) return parent;

if (parent.getChildDirs() != null) {
for (IDirectory child : parent.getChildDirs()) {
if (path.indexOf(child.getRelPath()) == 0) {
int childLen = child.getRelPath().length();
if (childLen == pathLen) return child; // found last path element
int end = path.indexOf(PATH_DELIMITER, childLen);

if (end == childLen) {
path.delete(0, childLen);
return find(child, path);
}
}
}
}

return null;
}

@Override
public void destroy() {
if (children != null) {
for (IDirectory child : getChildDirs()) {
child.destroy();
}
children = null;
}
parent = null;
}

@Override
public void removeChild(IDirectory... child) {
children = Directory.removeChild(this, children, child);
}

/**
* factory method to be overwrittern by derived classes, if tree should consist of derived classes.
*/
@Override
public IDirectory createOsDirectory(IFile file, IDirectory parent, IDirectory[] children) {
public Directory createOsDirectory(IFile file, IDirectory parent, IDirectory[] children) {
final Directory result = new Directory(file.getName(), parent, 0);

if (children != null) {
Expand All @@ -141,15 +187,6 @@ public IDirectory createOsDirectory(IFile file, IDirectory parent, IDirectory[]
return result;
}

@Override
public void removeChild(IDirectory... child) {
children = Directory.removeChild(this, children, child);
}

public void addChild(IDirectory... child) {
this.children = add(this.children, child);
}

/*------------------- simple properties ------------------------*/

@Override
Expand Down Expand Up @@ -182,9 +219,12 @@ public int childIndexOf(IDirectory child) {
return childIndexOf(children, child);
}

/**
* factory method to be overwrittern by derived classes.
*/
@Override
public IDirectory[] getChildren() {
return children;
public Directory[] createOsDirectoryArray(int size) {
return new Directory[size];
}

/*------------------- formatting ------------------------*/
Expand All @@ -205,19 +245,8 @@ public String getAbsolute() {
return result.toString();
}

protected static StringBuilder toTreeString(StringBuilder result, Directory item, String delimiter, int options) {
if (item != null) {
result.append(item.getRelPath());
appendCount(result, item, options);
result.append(delimiter);

if (item.getChildren() != null) {
for (IDirectory child : item.getChildren()) {
toTreeString(result, (Directory) child, delimiter, options);
}
}
}
return result;
public void addChild(IDirectory... child) {
this.children = add(this, this.children, child);
}

public static void appendCount(StringBuilder result, IDirectory _item, int options) {
Expand Down Expand Up @@ -307,30 +336,9 @@ public IDirectory find(String path) {
return null;
}

private static IDirectory find(IDirectory parent, StringBuilder path) {
while (path.indexOf(PATH_DELIMITER) == 0) {
path.delete(0, PATH_DELIMITER.length());
}

int pathLen = path.length();
if (pathLen == 0) return parent;

if (parent.getChildren() != null) {
for(IDirectory child : parent.getChildren()) {
if (path.indexOf(child.getRelPath()) == 0) {
int childLen = child.getRelPath().length();
if (childLen == pathLen) return child; // found last path element
int end = path.indexOf(PATH_DELIMITER, childLen);

if (end == childLen) {
path.delete(0,childLen);
return find(child, path);
}
}
}
}

return null;
@Override
public IDirectory[] getChildDirs() {
return children;
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions fotolib2/src/main/java/de/k3b/io/DirectoryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public DirectoryBuilder() {
}

private static List<IDirectory> getChildren(Directory dir) {
return (dir != null && dir.getChildren() != null) ? Arrays.asList(dir.getChildren()) : null;
IDirectory[] childDirs = (dir != null) ? dir.getChildDirs() : null;
return (childDirs != null) ? Arrays.asList(childDirs) : null;
}

public static void createStatistics(List<IDirectory> children) {
Expand Down Expand Up @@ -98,7 +99,7 @@ private Directory mergeDirWithChildIfPossible(Directory firstChild) {
firstChild.setRelPath(firstChild.getRelPath() + Directory.PATH_DELIMITER + child.getRelPath());
firstChild.setNonDirItemCount(firstChild.getNonDirItemCount() + child.getNonDirItemCount());

firstChild.setChildren(child.getChildren());
firstChild.setChildren(child.getChildDirs());

child.setParent(null);
child.setChildren(null);
Expand Down
8 changes: 4 additions & 4 deletions fotolib2/src/main/java/de/k3b/io/DirectoryNavigator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2020 by k3b.
* Copyright (c) 2015-2021 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager
*
Expand Down Expand Up @@ -72,7 +72,7 @@ public IDirectory getChild(int groupIndex, int childIndex) {
"): index must be 0 .. " +
(Directory.getChildCount(group) - 1));
}
return group.getChildren()[childIndex];
return group.getChildDirs()[childIndex];
}

@Override
Expand All @@ -84,7 +84,7 @@ public IDirectory getGroup(int groupIndex) {
"): index must be 0 .. " +
(getGroupCount() - 1));
}
return currentGrandFather.getChildren()[groupIndex];
return currentGrandFather.getChildDirs()[groupIndex];
}

public void setCurrentGrandFather(IDirectory currentGrandFather) {
Expand Down Expand Up @@ -126,7 +126,7 @@ public IDirectory getSubChild(int... indexes) {
IDirectory found = this.getRoot();
if (indexes != null) {
for (int index : indexes) {
IDirectory[] children = (found != null) ? found.getChildren() : null;
IDirectory[] children = (found != null) ? found.getChildDirs() : null;
int childCount = (children != null) ? children.length : 0;
if ((index < 0) || (index >= childCount)) {
return null;
Expand Down
Loading

0 comments on commit 7cf7ca2

Please sign in to comment.