From 55b298678d359433cda63c2fa14a5dbb83ed56a8 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Mon, 8 Apr 2019 17:08:34 +0400 Subject: [PATCH] Added key sorting in StorageBerkeleyDB --- .../influence/helpers/StorageBerkeleyDB.kt | 32 +++++++++++++------ .../helpers/comparators/CompareLong.java | 16 ++++++++++ .../helpers/comparators/CompareNumber640.java | 20 ++++++++++++ 3 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/io/github/chronosx88/influence/helpers/comparators/CompareLong.java create mode 100644 app/src/main/java/io/github/chronosx88/influence/helpers/comparators/CompareNumber640.java diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/StorageBerkeleyDB.kt b/app/src/main/java/io/github/chronosx88/influence/helpers/StorageBerkeleyDB.kt index 6184207..4e1b893 100644 --- a/app/src/main/java/io/github/chronosx88/influence/helpers/StorageBerkeleyDB.kt +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/StorageBerkeleyDB.kt @@ -5,6 +5,8 @@ import com.sleepycat.je.Database import com.sleepycat.je.DatabaseConfig import com.sleepycat.je.Environment import com.sleepycat.je.EnvironmentConfig +import io.github.chronosx88.influence.helpers.comparators.CompareLong +import io.github.chronosx88.influence.helpers.comparators.CompareNumber640 import net.tomp2p.connection.SignatureFactory import net.tomp2p.dht.Storage import net.tomp2p.peers.Number160 @@ -16,7 +18,8 @@ import java.io.File import java.security.PublicKey import java.util.* import java.util.concurrent.ConcurrentHashMap - +import kotlin.collections.ArrayList +import kotlin.collections.HashMap class StorageBerkeleyDB(peerId: Number160, path : File, signatureFactory: SignatureFactory) : Storage { // Core @@ -47,16 +50,25 @@ class StorageBerkeleyDB(peerId: Number160, path : File, signatureFactory: Signat val envConfig = EnvironmentConfig() envConfig.allowCreate = true dbEnvironment = Environment(path, envConfig) - val dbConfig = DatabaseConfig() - dbConfig.allowCreate = true - dataMapDB = dbEnvironment.openDatabase(null, "dataMap_$peerId", dbConfig) - timeoutMapDB = dbEnvironment.openDatabase(null, "timeoutMap_$peerId", dbConfig) - timeoutMapRevDB = dbEnvironment.openDatabase(null, "timeoutMapRev_$peerId", dbConfig) - protectedDomainMapDB = dbEnvironment.openDatabase(null, "protectedDomainMap_$peerId", dbConfig) - protectedEntryMapDB = dbEnvironment.openDatabase(null, "protectedEntryMap_$peerId", dbConfig) - responsibilityMapDB = dbEnvironment.openDatabase(null, "responsibilityMap_$peerId", dbConfig) - responsibilityMapRevDB = dbEnvironment.openDatabase(null, "responsibilityMapRev_$peerId", dbConfig) + val configMap : HashMap = HashMap() + + val compareNumber640 = CompareNumber640() + val compareLong = CompareLong() + configMap["dataMapConfig"] = DatabaseConfig().setBtreeComparator(compareNumber640) + configMap["dataMapConfig"]!!.allowCreate = true + configMap["timeoutMapRevConfig"] = DatabaseConfig().setBtreeComparator(compareLong) + configMap["timeoutMapRevConfig"]!!.allowCreate = true + configMap["other"] = DatabaseConfig() + configMap["other"]!!.allowCreate = true + + dataMapDB = dbEnvironment.openDatabase(null, "dataMap_$peerId", configMap["dataMapConfig"]) + timeoutMapDB = dbEnvironment.openDatabase(null, "timeoutMap_$peerId", configMap["other"]) + timeoutMapRevDB = dbEnvironment.openDatabase(null, "timeoutMapRev_$peerId", configMap["timeoutMapRevConfig"]) + protectedDomainMapDB = dbEnvironment.openDatabase(null, "protectedDomainMap_$peerId", configMap["other"]) + protectedEntryMapDB = dbEnvironment.openDatabase(null, "protectedEntryMap_$peerId", configMap["other"]) + responsibilityMapDB = dbEnvironment.openDatabase(null, "responsibilityMap_$peerId", configMap["other"]) + responsibilityMapRevDB = dbEnvironment.openDatabase(null, "responsibilityMapRev_$peerId", configMap["other"]) storageCheckIntervalMillis = 60 * 1000 diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/comparators/CompareLong.java b/app/src/main/java/io/github/chronosx88/influence/helpers/comparators/CompareLong.java new file mode 100644 index 0000000..f8e74f0 --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/comparators/CompareLong.java @@ -0,0 +1,16 @@ +package io.github.chronosx88.influence.helpers.comparators; + +import java.io.Serializable; +import java.util.Comparator; + +import io.github.chronosx88.influence.helpers.Serializer; + +public class CompareLong implements Comparator, Serializable { + @Override + public int compare(byte[] o1, byte[] o2) { + Serializer serializer = new Serializer<>(); + Long num1 = serializer.deserialize(o1); + Long num2 = serializer.deserialize(o2); + return num1.compareTo(num2); + } +} diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/comparators/CompareNumber640.java b/app/src/main/java/io/github/chronosx88/influence/helpers/comparators/CompareNumber640.java new file mode 100644 index 0000000..35a0b5f --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/comparators/CompareNumber640.java @@ -0,0 +1,20 @@ +package io.github.chronosx88.influence.helpers.comparators; + +import net.tomp2p.peers.Number640; + +import java.io.Serializable; +import java.util.Comparator; + +import io.github.chronosx88.influence.helpers.Serializer; + +public class CompareNumber640 implements Comparator, Serializable { + @Override + public int compare(byte[] o1, byte[] o2) { + Serializer serializer = new Serializer<>(); + Number640 num1 = serializer.deserialize(o1); + Number640 num2 = serializer.deserialize(o2); + return num1.compareTo(num2); + } +} + +