2022-03-12 16:18:12 +00:00
|
|
|
package lib;
|
|
|
|
|
|
|
|
import haxe.Serializer;
|
|
|
|
import haxe.Unserializer;
|
|
|
|
import kernel.fs.FS;
|
|
|
|
import haxe.ds.StringMap;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Key value store with persistence.
|
|
|
|
**/
|
|
|
|
class KVStore {
|
2023-07-30 13:55:22 +00:00
|
|
|
private var kvStore:StringMap<Dynamic> = new StringMap();
|
|
|
|
|
2023-05-29 10:49:42 +00:00
|
|
|
public final namespace:String;
|
2022-03-12 16:18:12 +00:00
|
|
|
|
2023-07-30 13:55:22 +00:00
|
|
|
public function new(namespace:String) {
|
2022-03-12 16:18:12 +00:00
|
|
|
this.namespace = namespace;
|
2023-06-04 13:43:24 +00:00
|
|
|
this.load();
|
2022-03-12 16:18:12 +00:00
|
|
|
}
|
|
|
|
|
2023-07-30 13:55:22 +00:00
|
|
|
public static function removeNamespace(namespace:String):Void {
|
2023-05-25 21:44:08 +00:00
|
|
|
var nsFile = getNamespaceFile(namespace);
|
|
|
|
FS.delete(nsFile);
|
|
|
|
}
|
|
|
|
|
2023-05-29 10:49:42 +00:00
|
|
|
public static function getStoreForClass(?pos:haxe.PosInfos) {
|
|
|
|
var className = pos.className;
|
|
|
|
return new KVStore(className);
|
|
|
|
}
|
|
|
|
|
2023-07-30 13:55:22 +00:00
|
|
|
private static function getNamespaceFile(namespace:String):String {
|
2023-03-27 22:55:23 +00:00
|
|
|
return '/var/ns/$namespace';
|
2022-03-12 16:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function load() {
|
2023-03-27 22:55:23 +00:00
|
|
|
var nsFile = getNamespaceFile(this.namespace);
|
2023-07-30 13:55:22 +00:00
|
|
|
if (FS.exists(nsFile)) {
|
2023-03-27 22:55:23 +00:00
|
|
|
var handle = FS.openRead(nsFile);
|
2022-03-12 16:18:12 +00:00
|
|
|
parseFile(handle.readAll());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function save() {
|
|
|
|
var handle = FS.openWrite(getNamespaceFile(this.namespace));
|
|
|
|
|
|
|
|
handle.write(Serializer.run(this.kvStore));
|
|
|
|
|
|
|
|
handle.close();
|
|
|
|
}
|
|
|
|
|
2023-07-30 13:55:22 +00:00
|
|
|
private function parseFile(content:String) {
|
2022-03-12 16:18:12 +00:00
|
|
|
var unserializer = new Unserializer(content);
|
|
|
|
this.kvStore = unserializer.unserialize();
|
|
|
|
}
|
|
|
|
|
2023-07-30 13:55:22 +00:00
|
|
|
public inline function set(key:String, value:Dynamic) {
|
|
|
|
this.kvStore.set(key, value);
|
2022-03-12 16:18:12 +00:00
|
|
|
}
|
|
|
|
|
2023-07-30 13:55:22 +00:00
|
|
|
public inline function get<T>(key:String, ?orElse:T = null):Null<T> {
|
2023-05-29 10:49:42 +00:00
|
|
|
return this.kvStore.get(key) ?? orElse;
|
2022-03-12 16:18:12 +00:00
|
|
|
}
|
|
|
|
|
2023-07-30 13:55:22 +00:00
|
|
|
public inline function exists(key:String):Bool {
|
2022-03-12 16:18:12 +00:00
|
|
|
return this.kvStore.exists(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public inline function clear() {
|
|
|
|
this.kvStore.clear();
|
|
|
|
}
|
|
|
|
|
2023-07-30 13:55:22 +00:00
|
|
|
public inline function remove(key:String):Bool {
|
2022-03-12 16:18:12 +00:00
|
|
|
return this.kvStore.remove(key);
|
|
|
|
}
|
|
|
|
|
2023-07-30 13:55:22 +00:00
|
|
|
public inline function keys():Iterator<String> {
|
2022-03-12 16:18:12 +00:00
|
|
|
return this.kvStore.keys();
|
|
|
|
}
|
|
|
|
|
|
|
|
public inline function keyValueIterator():KeyValueIterator<String, Dynamic> {
|
|
|
|
return this.kvStore.keyValueIterator();
|
|
|
|
}
|
|
|
|
}
|