Skip to content

Commit

Permalink
Working cache now.
Browse files Browse the repository at this point in the history
Open topic: Handle changed files
  • Loading branch information
lanthale committed Nov 19, 2023
1 parent e7696af commit f5e2f86
Show file tree
Hide file tree
Showing 4 changed files with 443 additions and 126 deletions.
9 changes: 8 additions & 1 deletion PhotoSlide/src/main/java/org/photoslide/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import javafx.stage.Stage;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
Expand Down Expand Up @@ -85,7 +87,7 @@ public void init() throws Exception {
checkSearchDBStructure();
} catch (ClassNotFoundException | SQLException e) {
if (e.getMessage().startsWith("Unsupported database")) {
new File(Utility.getAppData() + File.separator + "SearchMediaFilesDB").delete();
new File(Utility.getAppData() + File.separator + "SearchMediaFilesDB").delete();
} else {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, e);
}
Expand All @@ -104,6 +106,11 @@ public void init() throws Exception {
} catch (UnsatisfiedLinkError e) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, e);
}
//create cache dir
if (Files.exists(Path.of(Utility.getAppData() + File.separatorChar + "cache")) == false) {
Files.createDirectory(Path.of(Utility.getAppData() + File.separatorChar + "cache"));
}

notifyPreloader(new ProgressNotification(0.8));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -79,24 +82,23 @@ protected MediaFile call() throws Exception {

updateTitle("Reading cache...");
//restore cache
File inPath = new File(Utility.getAppData() + File.separatorChar + selectedPath.toFile().getName() + ".bin");
File inPath = new File(Utility.getAppData() + File.separatorChar + "cache" + File.separatorChar + createMD5Hash(selectedPath.toString()+"-") + selectedPath.toFile().getName() + ".bin");
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(inPath);
ObjectInputStream objectInputStream
= new ObjectInputStream(fileInputStream);
List<MediaFile> e2 = (ArrayList<MediaFile>) objectInputStream.readObject();
objectInputStream.close();
cacheList.addAll(e2);
objectInputStream.close();
cacheList.addAll(e2);
Platform.runLater(() -> {
factory.setListFilesActive(false);
fullMediaList.addAll(cacheList);
});
} catch (IOException | ClassNotFoundException ex) {
}
updateTitle("Reading cache...finished");

Platform.runLater(() -> {
factory.setListFilesActive(false);
fullMediaList.addAll(cacheList);
});

updateTitle("Counting mediafiles...");

Stream<Path> fileList = Files.list(selectedPath).filter((t) -> {
Expand Down Expand Up @@ -130,76 +132,65 @@ protected MediaFile call() throws Exception {
long starttime = System.currentTimeMillis();

AtomicInteger iatom = new AtomicInteger(1);
fileList.parallel().forEach((fileItem) -> {
if (this.isCancelled()) {
return;
if (qty != cacheList.size()) {
if (!cacheList.isEmpty()) {
Platform.runLater(() -> {
mainController.getProgressPane().setVisible(false);
mainController.getStatusLabelLeft().setVisible(false);
});
}
if (this.isCancelled() == false) {
if (Files.isDirectory(fileItem) == false) {
if (FileTypes.isValidType(fileItem.toString())) {
MediaFile m = new MediaFile();
m.setName(fileItem.getFileName().toString());
m.setPathStorage(fileItem);
m.setMediaType(MediaFile.MediaTypes.IMAGE);
if (fullMediaList.contains(m) == false) {
if (Utility.nativeMemorySize > 4194500) {
Thread.ofVirtual().start(() -> {
fileList.parallel().forEach((fileItem) -> {
if (this.isCancelled()) {
return;
}
if (this.isCancelled() == false) {
if (Files.isDirectory(fileItem) == false) {
if (FileTypes.isValidType(fileItem.toString())) {
MediaFile m = new MediaFile();
m.setName(fileItem.getFileName().toString());
m.setPathStorage(fileItem);
m.setMediaType(MediaFile.MediaTypes.IMAGE);
if (fullMediaList.contains(m) == false) {
if (Utility.nativeMemorySize > 4194500) {
Thread.ofVirtual().start(() -> {
try {
loadItem(fileItem, m);
updateValue(m);
} catch (IOException ex) {
m.setMediaType(MediaFile.MediaTypes.NONE);
}
});
} else {
try {
loadItem(fileItem, m);
updateValue(m);
} catch (IOException ex) {
m.setMediaType(MediaFile.MediaTypes.NONE);
}
});
} else {
try {
loadItem(fileItem, m);
updateValue(m);
} catch (IOException ex) {
m.setMediaType(MediaFile.MediaTypes.NONE);
}
}
} else {
if (Utility.nativeMemorySize > 4194500) {
Thread.ofVirtual().start(() -> {
try {
loadItem(fileItem, m);
//updateValue(m);
} catch (IOException ex) {
m.setMediaType(MediaFile.MediaTypes.NONE);
}
});
} else {
try {
loadItem(fileItem, m);
//updateValue(m);
} catch (IOException ex) {
m.setMediaType(MediaFile.MediaTypes.NONE);
}
if (cacheList.contains(m) == false) {
cacheList.add(m);
}
}
if (cacheList.contains(m) == false) {
cacheList.add(m);
}
}
}
updateMessage(iatom.get() + " / " + qty);
iatom.addAndGet(1);
if (qty > 1000) {
double percentage = (double) iatom.get() / qty * 100;
if (percentage >= loadingLimit) {
factory.setListFilesActive(false);
updateMessage(iatom.get() + " / " + qty);
iatom.addAndGet(1);
if (qty > 1000) {
double percentage = (double) iatom.get() / qty * 100;
if (percentage >= loadingLimit) {
factory.setListFilesActive(false);
}
}
}
});
if (this.isCancelled()) {
return null;
}
});
if (this.isCancelled()) {
return null;
}

//save cache to disk
File outpath = new File(Utility.getAppData() + File.separatorChar + selectedPath.toFile().getName() + ".bin");
File outpath = new File(Utility.getAppData() + File.separatorChar + "cache" + File.separatorChar + createMD5Hash(selectedPath.toString()+"-") + selectedPath.toFile().getName() + ".bin");

FileOutputStream fileOutputStream;
try {
fileOutputStream = new FileOutputStream(outpath, false);
Expand Down Expand Up @@ -308,4 +299,26 @@ protected void succeeded() {
executorParallel.shutdown();
}

public String createMD5Hash(final String input)
throws NoSuchAlgorithmException {

String hashtext = null;
MessageDigest md = MessageDigest.getInstance("MD5");
// Compute message digest of the input
byte[] messageDigest = md.digest(input.getBytes());

hashtext = convertToHex(messageDigest);

return hashtext;
}

private String convertToHex(final byte[] messageDigest) {
BigInteger bigint = new BigInteger(1, messageDigest);
String hexText = bigint.toString(16);
while (hexText.length() < 32) {
hexText = "0".concat(hexText);
}
return hexText;
}

}
45 changes: 41 additions & 4 deletions PhotoSlide/src/main/java/org/photoslide/datamodel/MediaFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ public class MediaFile implements Serializable {
private transient double gpsHeight;
private transient SimpleBooleanProperty bookmarked;

public enum MediaTypes {
public static enum MediaTypes {
IMAGE,
VIDEO,
NONE
}

public enum VideoTypes {
public static enum VideoTypes {
SUPPORTED,
UNSUPPORTED
}
Expand All @@ -111,13 +111,17 @@ public enum VideoTypes {

private void writeObject(ObjectOutputStream oos)
throws IOException {
oos.defaultWriteObject();
//oos.defaultWriteObject();
MediaFile_Serializable convertToSerializable = MediaFile_Serializable.convertToSerializable(this);
oos.writeObject(convertToSerializable);
}

private void readObject(ObjectInputStream ois)
throws ClassNotFoundException, IOException {
ois.defaultReadObject();
//ois.defaultReadObject();
initData();
MediaFile_Serializable mserial=(MediaFile_Serializable)ois.readObject();
MediaFile_Serializable.convertToMediaFile(mserial, this);
}

public MediaFile() {
Expand Down Expand Up @@ -900,6 +904,39 @@ public SimpleBooleanProperty bookmarkedProperty() {
return bookmarked;
}

public SimpleStringProperty getPlace() {
return place;
}

public void setPlace(String place) {
this.place.set(place);
}

public SimpleStringProperty getFaces() {
return faces;
}

public void setFaces(String faces) {
this.faces.set(faces);
}

public SimpleStringProperty getTitle() {
return title;
}

public SimpleStringProperty getCamera() {
return camera;
}

public SimpleStringProperty getComments() {
return comments;
}

public void setComments(String comments) {
this.comments.set(comments);
}


public void removeAllEdits() {
String fileNameWithExt = getEditFilePath().toString();
File editFile = new File(fileNameWithExt);
Expand Down
Loading

0 comments on commit f5e2f86

Please sign in to comment.