Class DTODAO<T extends Persistable>
- Type Parameters:
T
- the DTO type
- All Implemented Interfaces:
DAO<T>
- Direct Known Subclasses:
GeneratedQueryDAOImpl
,LLMDAOImpl
,PromptDAOImpl
,PromptTypeDAOImpl
,SampleQueryDAOImpl
Persistable
DTOs backed by the file system.
Provides a simple in-memory cache (id → DTO) synchronized from disk via
PersistenceHelper
. Subclasses specify the DTO type by
implementing getDtoClass()
.
Notes: Cache synchronization is eager on construction and on selected operations; this class is not thread-safe.
- Since:
- 1.0
- Author:
- Felix Seggebäing
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
DTODAO()
Constructs a new DAO and initializes the in-memory cache by synchronizing with the de.seggebaeing.sqlanalyzer.persistence layer. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Deletes the given entity from the de.seggebaeing.sqlanalyzer.persistence layer and removes it from the cache.getAll()
Returns all cached entities.getByID
(int id) Retrieves an entity by its identifier.Returns the DTO class managed by this DAO.int
Generates a free identifier not currently used in the cache.void
saveOrUpdate
(T dto) Persists or updates the given entity and refreshes the cache entry.
-
Field Details
-
cache
In-memory cache mapping DTO identifiers to their instances.Keeps recently loaded or persisted objects to reduce file system access.
-
-
Constructor Details
-
DTODAO
protected DTODAO()Constructs a new DAO and initializes the in-memory cache by synchronizing with the de.seggebaeing.sqlanalyzer.persistence layer.
-
-
Method Details
-
getAll
Returns all cached entities.The result is a snapshot copy of the current cache contents.
- Specified by:
getAll
in interfaceDAO<T extends Persistable>
- Returns:
- a set of all entities; may be empty if none are cached
-
getByID
Retrieves an entity by its identifier.First checks the in-memory cache; if not present, attempts to load the entity from the de.seggebaeing.sqlanalyzer.persistence layer and refreshes the cache. Returns
null
if the id is-1
, not found, or loading fails.- Specified by:
getByID
in interfaceDAO<T extends Persistable>
- Parameters:
id
- the identifier of the entity- Returns:
- the entity with the given id, or
null
if not found or load failed
-
delete
Deletes the given entity from the de.seggebaeing.sqlanalyzer.persistence layer and removes it from the cache.Logs a warning if deletion fails.
- Specified by:
delete
in interfaceDAO<T extends Persistable>
- Parameters:
dto
- the entity to delete, must not benull
- Throws:
NullPointerException
- ifdto
isnull
-
saveOrUpdate
Persists or updates the given entity and refreshes the cache entry.Logs a warning if the operation fails.
- Specified by:
saveOrUpdate
in interfaceDAO<T extends Persistable>
- Parameters:
dto
- the entity to save or update, must not benull
- Throws:
NullPointerException
- ifdto
isnull
-
getFreeId
public int getFreeId()Generates a free identifier not currently used in the cache.Uses random numbers in the positive
int
range until an unused id is found. This implementation is simple but not guaranteed to be efficient.- Returns:
- a free identifier
-
getDtoClass
Returns the DTO class managed by this DAO.- Returns:
- the class object of the managed DTO type
-