Where JOID stores files
JOID writes to disk in two folders only: one for the data of your application, one for caches of the machine. Knowing them tells you what to back up, what you can delete, and where to put them in an installed application.
You will learn
- What JOID writes in the data folder and in the cache folder
- Where the two folders are by default
- How to move them, and which setting wins
- How to keep files of your own in the cache, with a retention
Know what JOID writes
Section titled “Know what JOID writes”The data folder belongs to your application: delete it and the users lose their settings.
| Path in the data folder | Written for |
|---|---|
store/<id>.store | each store that lives on the disk |
property/<UI class>.property | the fields of a UI marked UIProperty; <UI class> is the full name of the class |
Keep a store on disk and Remember a field of a UI say when JOID writes them.
The cache folder belongs to the machine: JOID rebuilds anything in it when it is missing. It is split into areas, one folder each:
| Area | Written for | Kept for |
|---|---|---|
msdf/ | the atlases that JOID generates from your fonts | 30 days |
video/ | a copy of each video read from the classpath, a stream or a URL; a video read from a plain file plays from that file | 7 days |
ffmpeg/ | the FFmpeg natives that the first video downloads for the system, unless the classpath already holds them (Images and media) | 30 days |
lwjgl/ | the LWJGL 2 natives, with the LWJGL 2 backend only | 30 days |
JOID creates a folder the first time it writes into it. Each use of an entry renews it; an entry that nobody used for longer than its area keeps it is deleted when JOID starts, on a background thread, which prints the space it freed.
Find the folders
Section titled “Find the folders”| Folder | Default |
|---|---|
| Data | joid, in the working directory of the JVM |
| Cache on Windows | %LOCALAPPDATA%\joid, or AppData\Local\joid in the home folder when the variable is missing |
| Cache on macOS | ~/Library/Caches/joid |
| Cache on Linux | $XDG_CACHE_HOME/joid, or ~/.cache/joid when the variable is missing |
JOID prints both folders in its banner when the backend registers.
Your code reads them with JOIDEnv.getDataDir and JOIDEnv.getCacheDir:
System.out.println("Settings in " + JOIDEnv.getDataDir().getAbsolutePath());System.out.println("Caches in " + JOIDEnv.getCacheDir().getAbsolutePath());Full file: RunningFolders.java
Move the folders
Section titled “Move the folders”The working directory of an installed application is often read-only or shared, so give it a data folder of its own.
Set the folders on the backend, before register():
final File home = new File(System.getProperty("user.home"), ".my-application");return Lwjgl3Backend.create(window).dataDir(new File(home, "data")).cacheDir(new File(home, "cache")).register();Full file: RunningFolders.java
Without code, start the JVM with system properties:
java -Djoid.data=/srv/my-application/data -Djoid.cache=/var/cache/my-application -jar my-application.jarWhen several settings name a folder, the first one present wins:
Backend.dataDirandBackend.cacheDiron the backend, applied byregister();- the system properties
joid.dataandjoid.cache, read when JOID starts; - the defaults of the table above.
Keep files of your own in the cache
Section titled “Keep files of your own in the cache”Your application can keep its own rebuildable files in the cache folder, with the same cleaning as JOID’s.
Declare an area once, with FileCacheArea.of and the time an unused entry stays:
private static final FileCacheArea REPORTS = FileCacheArea.of("reports").retention(Duration.ofDays(7L));Full file: RunningFolders.java
Then ask the area for an entry by its key:
public static File report(final String day) throws IOException { return RunningFolders.REPORTS.compute(day, file -> { final String report = RunningFolders.build(day); Files.write(file.toPath(), report.getBytes(StandardCharsets.UTF_8)); });}Full file: RunningFolders.java
FileCacheArea.compute returns the file of the key when it exists.
Otherwise it gives your writer a temporary path, then moves what the writer created there to the key, so a crash never leaves half a file behind.
Two threads that compute the same key wait for each other, and the second one reads the file of the first.
FileCacheArea.remove deletes an entry, so that the next compute writes it again; FileCacheArea lists the other methods.
The writer creates a file, or a folder for an entry made of several files.
Area names and keys are plain file names: a letter or a digit, then letters, digits, ., - and _.
- The data folder holds the stores that live on the disk and the
@UIPropertyfields: it belongs to the application. - The cache folder holds font atlases, video copies and natives, in areas that JOID cleans at startup.
- The data folder defaults to
joidin the working directory; the cache folder to the cache folder of the system. - Priority:
dataDir/cacheDiron the backend, then-Djoid.data/-Djoid.cache, then the defaults. JOIDEnv.getDataDir()andgetCacheDir()give the folders in use; the banner prints them.FileCacheArea.of(name)keeps files of your own, written once bycomputeand deleted after their retention.
Try it
Section titled “Try it”Settings lost after an update. A user reports that the settings of your application are lost after each update, which installs it in a new folder. What do you change?
Solution
The data folder defaults to joid in the working directory, which moves with each install.
Set dataDir(...) on the backend to a folder in the user’s home, such as new File(System.getProperty("user.home"), ".my-application/data").
Thumbnails for a week. Your application renders a thumbnail for each level, and may delete the ones unused for a week. Declare the area.
Solution
private static final FileCacheArea THUMBNAILS = FileCacheArea.of("thumbnails").retention(Duration.ofDays(7L));Then THUMBNAILS.compute(level, file -> ...) renders a thumbnail the first time and returns the file afterwards.
