[mapdb] Make serialization asynchronous (#14900)

* [mapdb] Make serialization asynchronous

---------

Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
J-N-K
2023-05-01 09:10:28 +02:00
committed by GitHub
parent acb73a2c7b
commit 990700de8d
2 changed files with 19 additions and 18 deletions

View File

@@ -73,7 +73,9 @@ public class MapDbPersistenceService implements QueryablePersistenceService {
private final ExecutorService threadPool = ThreadPoolManager.getPool(getClass().getSimpleName());
/** holds the local instance of the MapDB database */
/**
* holds the local instance of the MapDB database
*/
private @NonNullByDefault({}) DB db;
private @NonNullByDefault({}) Map<String, String> map;
@@ -182,12 +184,12 @@ public class MapDbPersistenceService implements QueryablePersistenceService {
mItem.setName(localAlias);
mItem.setState(state);
mItem.setTimestamp(new Date());
String json = serialize(mItem);
map.put(localAlias, json);
commit();
if (logger.isDebugEnabled()) {
threadPool.submit(() -> {
String json = serialize(mItem);
map.put(localAlias, json);
db.commit();
logger.debug("Stored '{}' with state '{}' as '{}' in MapDB database", localAlias, state, json);
}
});
}
@Override
@@ -217,10 +219,6 @@ public class MapDbPersistenceService implements QueryablePersistenceService {
return Optional.of(item);
}
private void commit() {
threadPool.submit(() -> db.commit());
}
private static <T> Stream<T> streamOptional(Optional<T> opt) {
return opt.isPresent() ? Stream.of(opt.get()) : Stream.empty();
}