From 8aacb7115291dee7d3f7d677c6a061d6af3718cc Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Mon, 8 Apr 2019 17:09:03 +0400 Subject: [PATCH] Added key sorting in StorageBerkeleyDB --- .../chronosx88/dhtBootstrap/CompareLong.java | 14 ++++++++++ .../dhtBootstrap/CompareNumber640.java | 16 ++++++++++++ .../dhtBootstrap/StorageBerkeleyDB.kt | 26 ++++++++++++------- 3 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 src/main/java/io/github/chronosx88/dhtBootstrap/CompareLong.java create mode 100644 src/main/java/io/github/chronosx88/dhtBootstrap/CompareNumber640.java diff --git a/src/main/java/io/github/chronosx88/dhtBootstrap/CompareLong.java b/src/main/java/io/github/chronosx88/dhtBootstrap/CompareLong.java new file mode 100644 index 0000000..159f1d4 --- /dev/null +++ b/src/main/java/io/github/chronosx88/dhtBootstrap/CompareLong.java @@ -0,0 +1,14 @@ +package io.github.chronosx88.dhtBootstrap; + +import java.io.Serializable; +import java.util.Comparator; + +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); + } +} \ No newline at end of file diff --git a/src/main/java/io/github/chronosx88/dhtBootstrap/CompareNumber640.java b/src/main/java/io/github/chronosx88/dhtBootstrap/CompareNumber640.java new file mode 100644 index 0000000..7a5599d --- /dev/null +++ b/src/main/java/io/github/chronosx88/dhtBootstrap/CompareNumber640.java @@ -0,0 +1,16 @@ +package io.github.chronosx88.dhtBootstrap; + +import net.tomp2p.peers.Number640; + +import java.io.Serializable; +import java.util.Comparator; + +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); + } +} diff --git a/src/main/kotlin/io/github/chronosx88/dhtBootstrap/StorageBerkeleyDB.kt b/src/main/kotlin/io/github/chronosx88/dhtBootstrap/StorageBerkeleyDB.kt index ff0e8a2..d4aa361 100644 --- a/src/main/kotlin/io/github/chronosx88/dhtBootstrap/StorageBerkeleyDB.kt +++ b/src/main/kotlin/io/github/chronosx88/dhtBootstrap/StorageBerkeleyDB.kt @@ -47,16 +47,24 @@ class StorageBerkeleyDB(peerId: Number160, path : File, signatureFactory: Signat val envConfig = EnvironmentConfig() envConfig.allowCreate = true dbEnvironment = Environment(path, envConfig) - val dbConfig = DatabaseConfig() - dbConfig.allowCreate = true + val configMap : HashMap = HashMap() - 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 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