Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion owlplug-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* OwlPlug
* Copyright (C) 2021 Arthur <[email protected]>
*
* 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 <https://www.gnu.org/licenses/>.
*/

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<String, String> map = store.openMap(mapName);
map.put(key,value);
store.commit();
}

public void putAll(String mapName, Map<String, String> valueMap) {
MVMap<String, String> map = store.openMap(mapName);
map.putAll(valueMap);
store.commit();
}

public String get(String mapName, String key) {
MVMap<String, String> map = store.openMap(mapName);
return map.get(key);
}

@PreDestroy
public void destroy() {
log.info("Closing persistent key value store, PreDestroy signal received.");
store.close();
}
}