Skip to content

Commit

Permalink
Implementation of a cache for directories
Browse files Browse the repository at this point in the history
  • Loading branch information
lanthale committed Nov 18, 2023
1 parent eedf320 commit e7696af
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 116 deletions.
7 changes: 1 addition & 6 deletions PhotoSlide/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,7 @@
<groupId>io.github.typhon0</groupId>
<artifactId>AnimateFX</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>one.microstream</groupId>
<artifactId>microstream-storage-embedded</artifactId>
<version>08.01.01-MS-GA</version>
</dependency>
</dependency>

<!-- This downloads the platform specific JavaFX libraries used by the project. -->
<!-- For the final installable, the JavaFX dependencies are provided via modules, but by including these here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
*/
package org.photoslide.browserlighttable;

import boofcv.alg.feature.detect.interest.EasyGeneralFeatureDetector;
import org.photoslide.datamodel.MediaGridCell;
import org.photoslide.datamodel.MediaFile;
import org.photoslide.MainViewController;
import org.photoslide.Utility;
import org.photoslide.browsermetadata.MetadataController;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.time.Instant;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -89,8 +92,6 @@
import javafx.scene.CacheHint;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.SplitPane;
import one.microstream.storage.embedded.types.EmbeddedStorage;
import one.microstream.storage.embedded.types.EmbeddedStorageManager;
import org.photoslide.ThreadFactoryBuilder;

/**
Expand Down Expand Up @@ -220,38 +221,13 @@ public void initialize(URL url, ResourceBundle rb) {
sortOptions = FXCollections.observableArrayList("Filename", "Capture time", "File creation time");
sortOrderComboBox.setItems(sortOptions);
sortOrderComboBox.getSelectionModel().selectFirst();
executor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNamePrefix("lightTableController").build());
executorSchedule = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder().setPriority(8).setNamePrefix("lightTableControllerScheduled").build());
executor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setPriority(6).setNamePrefix("lightTableController").build());
executorSchedule = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder().setPriority(6).setNamePrefix("lightTableControllerScheduled").build());
executorParallel = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNamePrefix("lightTableControllerSelection").build());
//executorParallel = Executors.newVirtualThreadPerTaskExecutor();
dialogIcon = new Image(getClass().getResourceAsStream("/org/photoslide/img/Installericon.png"));
showPreviewPaneToggle.selectedProperty().addListener((o) -> {
if (showPreviewPaneToggle.isSelected()) {
final Timeline timeline = new Timeline();
timeline.setCycleCount(1);
timeline.setAutoReverse(false);
final KeyValue kv = new KeyValue(splitPane.getDividers().get(0).positionProperty(), 0.65);
final KeyFrame kf = new KeyFrame(Duration.millis(500), kv);
timeline.getKeyFrames().add(kf);
final KeyValue kv2 = new KeyValue(zoomSlider.valueProperty(), 0);
final KeyFrame kf2 = new KeyFrame(Duration.millis(500), kv2);
timeline.getKeyFrames().add(kf2);
timeline.play();
zoomSlider.setValue(0);
} else {
final Timeline timeline = new Timeline();
timeline.setCycleCount(1);
timeline.setAutoReverse(false);
final KeyValue kv = new KeyValue(splitPane.getDividers().get(0).positionProperty(), 0.1);
final KeyFrame kf = new KeyFrame(Duration.millis(500), kv);
timeline.getKeyFrames().add(kf);
if (zoomSlider.getValue() < 12) {
final KeyValue kv2 = new KeyValue(zoomSlider.valueProperty(), 12);
final KeyFrame kf2 = new KeyFrame(Duration.millis(500), kv2);
timeline.getKeyFrames().add(kf2);
}
timeline.play();
}
previewPaneAnimation();
});
}

Expand Down Expand Up @@ -359,7 +335,7 @@ public void setSelectedPath(Path sPath) {
mainController.getStatusLabelRight().setText("Finished MediaLoading Task.");
util.hideNodeAfterTime(mainController.getStatusLabelRight(), 2, true);
mainController.getProgressPane().setVisible(false);
mainController.getStatusLabelLeft().setText("");
mainController.getStatusLabelLeft().setText("");
});
taskMLoading.setOnFailed((t2) -> {
Logger.getLogger(LighttableController.class.getName()).log(Level.SEVERE, null, t2.getSource().getException());
Expand All @@ -370,7 +346,7 @@ public void setSelectedPath(Path sPath) {
});
taskMLoading.setOnScheduled((t) -> {
mainController.getProgressPane().setVisible(true);
mainController.getStatusLabelLeft().setVisible(true);
mainController.getStatusLabelLeft().setVisible(true);
});
Platform.runLater(() -> {
mainController.getStatusLabelRight().textProperty().bind(taskMLoading.messageProperty());
Expand Down Expand Up @@ -1330,6 +1306,35 @@ public ToggleSwitch getShowPreviewPaneToggle() {
return showPreviewPaneToggle;
}

private void previewPaneAnimation() {
if (showPreviewPaneToggle.isSelected()) {
final Timeline timeline = new Timeline();
timeline.setCycleCount(1);
timeline.setAutoReverse(false);
final KeyValue kv = new KeyValue(splitPane.getDividers().get(0).positionProperty(), 0.65);
final KeyFrame kf = new KeyFrame(Duration.millis(500), kv);
timeline.getKeyFrames().add(kf);
final KeyValue kv2 = new KeyValue(zoomSlider.valueProperty(), 0);
final KeyFrame kf2 = new KeyFrame(Duration.millis(500), kv2);
timeline.getKeyFrames().add(kf2);
timeline.play();
zoomSlider.setValue(0);
} else {
final Timeline timeline = new Timeline();
timeline.setCycleCount(1);
timeline.setAutoReverse(false);
final KeyValue kv = new KeyValue(splitPane.getDividers().get(0).positionProperty(), 0.1);
final KeyFrame kf = new KeyFrame(Duration.millis(500), kv);
timeline.getKeyFrames().add(kf);
if (zoomSlider.getValue() < 12) {
final KeyValue kv2 = new KeyValue(zoomSlider.valueProperty(), 12);
final KeyFrame kf2 = new KeyFrame(Duration.millis(500), kv2);
timeline.getKeyFrames().add(kf2);
}
timeline.play();
}
}

@FXML
private void faceRecognitationAction(ActionEvent event) {
/*HaarCascadeDetector detector = new HaarCascadeDetector(100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
*/
package org.photoslide.browserlighttable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import org.photoslide.datamodel.MediaFileLoader;
import org.photoslide.MainViewController;
import org.photoslide.datamodel.FileTypes;
import org.photoslide.datamodel.MediaFile;
import org.photoslide.browsermetadata.MetadataController;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -68,39 +74,61 @@ public MediaLoadingTask(ObservableList<MediaFile> fullMediaList, MediaGridCellFa
protected MediaFile call() throws Exception {
final long qty;
List<MediaFile> content = new ArrayList<>();
try {
updateTitle("Counting mediafiles...");

ArrayList<MediaFile> cacheList = new ArrayList<>();
try {

updateTitle("Reading cache...");
//restore cache
File inPath = new File(Utility.getAppData() + File.separatorChar + 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);
} 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) -> {
return FileTypes.isValidType(t.getFileName().toString());
}).sorted(new FilenameComparator());

Stream<Path> fileListCount = Files.list(selectedPath).filter((t) -> {
return FileTypes.isValidType(t.getFileName().toString());
});

qty = fileListCount.count();
updateTitle("Counting mediafiles...finished");
if (qty == 0) {
updateTitle("0 mediafiles found.");
Platform.runLater(() -> {
mainController.getProgressPane().setVisible(false);
mainController.getStatusLabelLeft().setVisible(false);
mediaQTYLabel.setText(qty + " media files.");
mediaQTYLabel.setText(qty + " media files.");
});
return null;
} else {
Platform.runLater(() -> {
} else {
Platform.runLater(() -> {
mainController.getProgressPane().setVisible(true);
mainController.getStatusLabelLeft().setVisible(true);
mediaQTYLabel.setText(qty + " media files");
mainController.getStatusLabelRight().setVisible(true);
});
updateTitle(qty + " files found - Loading...");
}
}
Logger.getLogger(LighttableController.class.getName()).log(Level.INFO, "Starting collecting..." + selectedPath);
long starttime = System.currentTimeMillis();

AtomicInteger iatom = new AtomicInteger(1);
fileList.parallel().forEach((fileItem) -> {
if (this.isCancelled()) {
Expand All @@ -113,23 +141,46 @@ protected MediaFile call() throws Exception {
m.setName(fileItem.getFileName().toString());
m.setPathStorage(fileItem);
m.setMediaType(MediaFile.MediaTypes.IMAGE);
if (Utility.nativeMemorySize > 4194500) {
Thread.ofVirtual().start(() -> {
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);
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);
}
}
}
updateMessage(iatom.get() + " / " + qty);
Expand All @@ -145,6 +196,27 @@ protected MediaFile call() throws Exception {
if (this.isCancelled()) {
return null;
}

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

FileOutputStream fileOutputStream;
try {
fileOutputStream = new FileOutputStream(outpath, false);
ObjectOutputStream objectOutputStream
= new ObjectOutputStream(fileOutputStream);
updateTitle("Save cache..." + cacheList.size());
objectOutputStream.writeObject(cacheList);
objectOutputStream.flush();
objectOutputStream.close();
updateTitle("Save cache...finished.");
} catch (FileNotFoundException ex) {
Logger.getLogger(LighttableController.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(LighttableController.class.getName()).log(Level.SEVERE, null, ex);
}
//end save to disk

long endtime = System.currentTimeMillis();
Logger.getLogger(LighttableController.class.getName()).log(Level.INFO, "Collect Time in s: " + (endtime - starttime) / 1000 + " " + selectedPath);
} catch (IOException ex) {
Expand Down
Loading

0 comments on commit e7696af

Please sign in to comment.