Skip to content

Commit

Permalink
[mapdb] Make serialization asynchronous (openhab#14900)
Browse files Browse the repository at this point in the history
* [mapdb] Make serialization asynchronous

---------

Signed-off-by: Jan N. Klug <[email protected]>
Signed-off-by: Matt Myers <[email protected]>
  • Loading branch information
J-N-K authored and matchews committed Aug 9, 2023
1 parent 4a8f7b3 commit 4f8f955
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -182,12 +184,12 @@ public void store(Item item, @Nullable String alias) {
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
Expand Down Expand Up @@ -217,10 +219,6 @@ private Optional<MapDbItem> deserialize(String json) {
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -62,7 +63,9 @@ public static void tearDown() throws IOException {
private static void removeDirRecursive(final String dir) throws IOException {
final Path path = Paths.get(dir);
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);
}
}
}

Expand All @@ -79,12 +82,12 @@ public void storeShouldStoreTheItem() {

persistenceService.store(item);

assertThat(persistenceService.getItemInfo(), hasItem(hasProperty("name", equalTo(name))));
waitForAssert(() -> assertThat(persistenceService.getItemInfo(), hasItem(hasProperty("name", equalTo(name)))));

persistenceService.store(item, alias);

assertThat(persistenceService.getItemInfo(),
hasItems(hasProperty("name", equalTo(name)), hasProperty("name", equalTo(alias))));
waitForAssert(() -> assertThat(persistenceService.getItemInfo(),
hasItems(hasProperty("name", equalTo(name)), hasProperty("name", equalTo(alias)))));
}

@Test
Expand All @@ -102,8 +105,8 @@ public void queryShouldFindStoredItemsByName() {

persistenceService.store(item);

assertThat(persistenceService.query(filter),
contains(allOf(hasProperty("name", equalTo(name)), hasProperty("state", equalTo(state)))));
waitForAssert(() -> assertThat(persistenceService.query(filter),
contains(allOf(hasProperty("name", equalTo(name)), hasProperty("state", equalTo(state))))));
}

@Test
Expand All @@ -127,7 +130,7 @@ public void queryShouldFindStoredItemsByAlias() {
persistenceService.store(item, alias);

assertThat(persistenceService.query(filterByName), is(emptyIterable()));
assertThat(persistenceService.query(filterByAlias),
contains(allOf(hasProperty("name", equalTo(alias)), hasProperty("state", equalTo(state)))));
waitForAssert(() -> assertThat(persistenceService.query(filterByAlias),
contains(allOf(hasProperty("name", equalTo(alias)), hasProperty("state", equalTo(state))))));
}
}

0 comments on commit 4f8f955

Please sign in to comment.