Class PersistenceHelper
Persistable
.
Stores each object as a single .json
file under a class-named directory
(<basePath>/<SimpleClassName>/<id>.json
). Provides CRUD-style helpers:
persist
, load
, loadAll
, and delete
. Serialization uses Gson (pretty printed).
A small normalization step ensures top-level String
fields of Java record
s are non-null.
Versioning: persist(de.seggebaeing.sqlanalyzer.persistence.dto.Persistable)
compares the on-disk version()
with the candidate.
Older or equal versions are not overwritten; newer candidates replace the file. Attempting to save an older
version results in a PersistenceException
.
Threading/Concurrency: This is a static, process-local helper. It is not a full concurrency control mechanism; coordinate concurrent writes at a higher level if multiple threads/processes may persist the same object.
- Since:
- 1.0
- Author:
- Felix Seggebäing
- Implementation Note:
- JSON normalization only affects top-level
String
components onrecord
types. - API Note:
- Call
initializeBasePath(java.nio.file.Path)
exactly once before any other method. The base path is immutable afterward. Directory names are derived fromclazz.getSimpleName()
.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Deletes the persisted file of the given object.static void
initializeBasePath
(Path basePath) Initializes the root directory for de.seggebaeing.sqlanalyzer.persistence operations.static <T extends Persistable>
TLoads a persisted object by class and identifier.static <T extends Persistable>
Set<T> Loads all persisted objects of the given class.static void
Persists aPersistable
object to the file system.
-
Constructor Details
-
PersistenceHelper
public PersistenceHelper()
-
-
Method Details
-
initializeBasePath
Initializes the root directory for de.seggebaeing.sqlanalyzer.persistence operations.Must be called exactly once before any other method of this helper is used. Subsequent calls will throw an
IllegalStateException
.- Parameters:
basePath
- the directory under which all persisted objects will be stored; subdirectories are created per class name- Throws:
IllegalStateException
- if the de.seggebaeing.sqlanalyzer.persistence base path has already been initialized
-
persist
Persists aPersistable
object to the file system.Each object is stored as a JSON file named by its
id()
in a directory specific to its class. If a file with the same ID already exists, the storedversion()
is compared against the candidate:- If the new version is greater, the file is overwritten.
- If the versions are equal, nothing is changed.
- If the existing version is greater, a
PersistenceException
is thrown.
- Parameters:
p
- the persistable object to save- Throws:
PersistenceException
- if file access fails, if the existing JSON cannot be parsed as the expected type, or if a newer version already exists on disk
-
load
Loads a persisted object by class and identifier.Reads the JSON file named
<id>.json
from the directory corresponding to the given class, deserializes it with Gson, and normalizes top-level string fields for record types.- Type Parameters:
T
- the type of the object to load- Parameters:
clazz
- the class of the object, used to resolve the directory and target typeid
- the identifier of the object (file name without extension)- Returns:
- the deserialized object of type
T
- Throws:
PersistenceException
- if the file is missing, unreadable, contains invalid JSON, or does not match the expected type
-
loadAll
Loads all persisted objects of the given class.Scans the directory associated with
clazz
for.json
files, deserializes each into an instance ofT
, and returns them as a set. Invalid or unreadable files are skipped with a logged warning.- Type Parameters:
T
- the type of objects to load- Parameters:
clazz
- the class whose persisted instances should be loaded- Returns:
- a set of deserialized objects of type
T
; may be empty if no files exist - Throws:
PersistenceException
- if the directory listing fails
-
delete
Deletes the persisted file of the given object.Resolves the JSON file path from the object's class and
id()
, then attempts to delete it from disk.- Parameters:
p
- the persistable object whose file should be removed- Throws:
PersistenceException
- if the file does not exist or an I/O error occurs during deletion
-