diff --git a/owlplug-client/pom.xml b/owlplug-client/pom.xml index 0512b4e6..aacace87 100644 --- a/owlplug-client/pom.xml +++ b/owlplug-client/pom.xml @@ -70,7 +70,6 @@ com.h2database h2 - runtime ch.qos.logback diff --git a/owlplug-client/src/main/java/com/owlplug/core/components/PersistentKeyValueStore.java b/owlplug-client/src/main/java/com/owlplug/core/components/PersistentKeyValueStore.java new file mode 100644 index 00000000..b2b39258 --- /dev/null +++ b/owlplug-client/src/main/java/com/owlplug/core/components/PersistentKeyValueStore.java @@ -0,0 +1,73 @@ +/* OwlPlug + * Copyright (C) 2021 Arthur + * + * This file is part of OwlPlug. + * + * OwlPlug is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 + * as published by the Free Software Foundation. + * + * OwlPlug is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with OwlPlug. If not, see . + */ + +package com.owlplug.core.components; + +import jakarta.annotation.PreDestroy; +import java.util.Map; +import org.h2.mvstore.MVMap; +import org.h2.mvstore.MVStore; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +/** + * The PersistentKeyValueStore is a key-value based storage manager + * to persist user data in the owlplug workspace (usually ~/.owlplug). + * The owlplug default data-source is an H2 SQL database semi-persistent. + * A relational database allow for some query optimization and full JPA / Spring data support. + * Because schema migration is not managed by OwlPlug, the database is cleared + * when breaking changes or changes not managed by the hibernate ddl-update parameter are introduced. + * All data that need to persist between OwlPlug updates must be stored in this KeyValue Store. + */ +@Component +public class PersistentKeyValueStore { + + private final Logger log = LoggerFactory.getLogger(this.getClass()); + private final MVStore store; + + public PersistentKeyValueStore() { + store = new MVStore.Builder() + // TODO configure from application properties + .fileName("~/.owlplug/kvstore.mv.db") + .open(); + } + + public void put(String mapName, String key, String value) { + MVMap map = store.openMap(mapName); + map.put(key,value); + store.commit(); + } + + public void putAll(String mapName, Map valueMap) { + MVMap map = store.openMap(mapName); + map.putAll(valueMap); + store.commit(); + } + + public String get(String mapName, String key) { + MVMap map = store.openMap(mapName); + return map.get(key); + } + + @PreDestroy + public void destroy() { + log.info("Closing persistent key value store, PreDestroy signal received."); + store.close(); + } +}