[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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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()); 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({}) DB db;
private @NonNullByDefault({}) Map<String, String> map; private @NonNullByDefault({}) Map<String, String> map;
@ -182,12 +184,12 @@ public class MapDbPersistenceService implements QueryablePersistenceService {
mItem.setName(localAlias); mItem.setName(localAlias);
mItem.setState(state); mItem.setState(state);
mItem.setTimestamp(new Date()); mItem.setTimestamp(new Date());
threadPool.submit(() -> {
String json = serialize(mItem); String json = serialize(mItem);
map.put(localAlias, json); map.put(localAlias, json);
commit(); db.commit();
if (logger.isDebugEnabled()) {
logger.debug("Stored '{}' with state '{}' as '{}' in MapDB database", localAlias, state, json); logger.debug("Stored '{}' with state '{}' as '{}' in MapDB database", localAlias, state, json);
} });
} }
@Override @Override
@ -217,10 +219,6 @@ public class MapDbPersistenceService implements QueryablePersistenceService {
return Optional.of(item); return Optional.of(item);
} }
private void commit() {
threadPool.submit(() -> db.commit());
}
private static <T> Stream<T> streamOptional(Optional<T> opt) { private static <T> Stream<T> streamOptional(Optional<T> opt) {
return opt.isPresent() ? Stream.of(opt.get()) : Stream.empty(); return opt.isPresent() ? Stream.of(opt.get()) : Stream.empty();
} }

View File

@ -23,6 +23,7 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.stream.Stream;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -62,7 +63,9 @@ public class MapDbPersistenceServiceOSGiTest extends JavaOSGiTest {
private static void removeDirRecursive(final String dir) throws IOException { private static void removeDirRecursive(final String dir) throws IOException {
final Path path = Paths.get(dir); final Path path = Paths.get(dir);
if (Files.exists(path)) { if (Files.exists(path)) {
Files.walk(path).map(Path::toFile).sorted().forEach(File::delete); try (Stream<Path> stream = Files.walk(path)) {
stream.map(Path::toFile).sorted().forEach(File::delete);
}
} }
} }
@ -79,12 +82,12 @@ public class MapDbPersistenceServiceOSGiTest extends JavaOSGiTest {
persistenceService.store(item); persistenceService.store(item);
assertThat(persistenceService.getItemInfo(), hasItem(hasProperty("name", equalTo(name)))); waitForAssert(() -> assertThat(persistenceService.getItemInfo(), hasItem(hasProperty("name", equalTo(name)))));
persistenceService.store(item, alias); persistenceService.store(item, alias);
assertThat(persistenceService.getItemInfo(), waitForAssert(() -> assertThat(persistenceService.getItemInfo(),
hasItems(hasProperty("name", equalTo(name)), hasProperty("name", equalTo(alias)))); hasItems(hasProperty("name", equalTo(name)), hasProperty("name", equalTo(alias)))));
} }
@Test @Test
@ -102,8 +105,8 @@ public class MapDbPersistenceServiceOSGiTest extends JavaOSGiTest {
persistenceService.store(item); persistenceService.store(item);
assertThat(persistenceService.query(filter), waitForAssert(() -> assertThat(persistenceService.query(filter),
contains(allOf(hasProperty("name", equalTo(name)), hasProperty("state", equalTo(state))))); contains(allOf(hasProperty("name", equalTo(name)), hasProperty("state", equalTo(state))))));
} }
@Test @Test
@ -127,7 +130,7 @@ public class MapDbPersistenceServiceOSGiTest extends JavaOSGiTest {
persistenceService.store(item, alias); persistenceService.store(item, alias);
assertThat(persistenceService.query(filterByName), is(emptyIterable())); assertThat(persistenceService.query(filterByName), is(emptyIterable()));
assertThat(persistenceService.query(filterByAlias), waitForAssert(() -> assertThat(persistenceService.query(filterByAlias),
contains(allOf(hasProperty("name", equalTo(alias)), hasProperty("state", equalTo(state))))); contains(allOf(hasProperty("name", equalTo(alias)), hasProperty("state", equalTo(state))))));
} }
} }