From 1bd1cb87de15a0f61646eb0d05204824eaf00203 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Sat, 19 Jun 2021 16:26:54 +0200 Subject: [PATCH] Fix compatibility --- .../cavallium/dbengine/database/Column.java | 36 +------------ .../database/LLDatabaseConnection.java | 7 ++- .../dbengine/database/LLKeyValueDatabase.java | 8 +-- .../disk/LLLocalDatabaseConnection.java | 3 ++ .../disk/LLLocalKeyValueDatabase.java | 53 ++++++++++++++----- .../it/cavallium/dbengine/DbTestUtils.java | 2 +- .../cavallium/dbengine/OldDatabaseTests.java | 2 +- 7 files changed, 55 insertions(+), 56 deletions(-) diff --git a/src/main/java/it/cavallium/dbengine/database/Column.java b/src/main/java/it/cavallium/dbengine/database/Column.java index bcf013a..bf0fba9 100644 --- a/src/main/java/it/cavallium/dbengine/database/Column.java +++ b/src/main/java/it/cavallium/dbengine/database/Column.java @@ -4,13 +4,7 @@ import java.nio.charset.StandardCharsets; import java.util.Objects; import java.util.StringJoiner; -public class Column { - - private final String name; - - private Column(String name) { - this.name = name; - } +public record Column(String name) { public static Column dictionary(String name) { return new Column("hash_map_" + name); @@ -28,32 +22,4 @@ public class Column { public static String toString(byte[] name) { return new String(name, StandardCharsets.US_ASCII); } - - public String getName() { - return name; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof Column)) { - return false; - } - Column column = (Column) o; - return Objects.equals(name, column.name); - } - - @Override - public int hashCode() { - return Objects.hash(name); - } - - @Override - public String toString() { - return new StringJoiner(", ", Column.class.getSimpleName() + "[", "]") - .add("name='" + name + "'") - .toString(); - } } diff --git a/src/main/java/it/cavallium/dbengine/database/LLDatabaseConnection.java b/src/main/java/it/cavallium/dbengine/database/LLDatabaseConnection.java index 5897dac..17ae61c 100644 --- a/src/main/java/it/cavallium/dbengine/database/LLDatabaseConnection.java +++ b/src/main/java/it/cavallium/dbengine/database/LLDatabaseConnection.java @@ -7,6 +7,7 @@ import it.cavallium.dbengine.lucene.analyzer.TextFieldsAnalyzer; import it.cavallium.dbengine.lucene.analyzer.TextFieldsSimilarity; import java.time.Duration; import java.util.List; +import java.util.Map; import reactor.core.publisher.Mono; @SuppressWarnings("UnusedReturnValue") @@ -16,7 +17,11 @@ public interface LLDatabaseConnection { Mono connect(); - Mono getDatabase(String name, List columns, boolean lowMemory, boolean inMemory); + Mono getDatabase(String name, + List columns, + Map extraFlags, + boolean lowMemory, + boolean inMemory); Mono getLuceneIndex(String name, int instancesCount, diff --git a/src/main/java/it/cavallium/dbengine/database/LLKeyValueDatabase.java b/src/main/java/it/cavallium/dbengine/database/LLKeyValueDatabase.java index d078b06..3e65d24 100644 --- a/src/main/java/it/cavallium/dbengine/database/LLKeyValueDatabase.java +++ b/src/main/java/it/cavallium/dbengine/database/LLKeyValueDatabase.java @@ -16,16 +16,16 @@ public interface LLKeyValueDatabase extends LLSnapshottable, LLKeyValueDatabaseS @Deprecated default Mono getDeprecatedSet(String name, UpdateMode updateMode) { - return getDictionary(Column.deprecatedSet(name).getName().getBytes(StandardCharsets.US_ASCII), updateMode); + return getDictionary(Column.deprecatedSet(name).name().getBytes(StandardCharsets.US_ASCII), updateMode); } default Mono getDictionary(String name, UpdateMode updateMode) { - return getDictionary(Column.dictionary(name).getName().getBytes(StandardCharsets.US_ASCII), updateMode); + return getDictionary(Column.dictionary(name).name().getBytes(StandardCharsets.US_ASCII), updateMode); } default Mono getInteger(String singletonListName, String name, int defaultValue) { return this - .getSingleton(Column.special(singletonListName).getName().getBytes(StandardCharsets.US_ASCII), + .getSingleton(Column.special(singletonListName).name().getBytes(StandardCharsets.US_ASCII), name.getBytes(StandardCharsets.US_ASCII), Ints.toByteArray(defaultValue) ) @@ -34,7 +34,7 @@ public interface LLKeyValueDatabase extends LLSnapshottable, LLKeyValueDatabaseS default Mono getLong(String singletonListName, String name, long defaultValue) { return this - .getSingleton(Column.special(singletonListName).getName().getBytes(StandardCharsets.US_ASCII), + .getSingleton(Column.special(singletonListName).name().getBytes(StandardCharsets.US_ASCII), name.getBytes(StandardCharsets.US_ASCII), Longs.toByteArray(defaultValue) ) diff --git a/src/main/java/it/cavallium/dbengine/database/disk/LLLocalDatabaseConnection.java b/src/main/java/it/cavallium/dbengine/database/disk/LLLocalDatabaseConnection.java index a35710c..090bd36 100644 --- a/src/main/java/it/cavallium/dbengine/database/disk/LLLocalDatabaseConnection.java +++ b/src/main/java/it/cavallium/dbengine/database/disk/LLLocalDatabaseConnection.java @@ -14,6 +14,7 @@ import java.nio.file.Path; import java.time.Duration; import java.util.LinkedList; import java.util.List; +import java.util.Map; import reactor.core.publisher.Mono; import reactor.core.scheduler.Schedulers; @@ -53,6 +54,7 @@ public class LLLocalDatabaseConnection implements LLDatabaseConnection { @Override public Mono getDatabase(String name, List columns, + Map extraFlags, boolean lowMemory, boolean inMemory) { return Mono @@ -62,6 +64,7 @@ public class LLLocalDatabaseConnection implements LLDatabaseConnection { basePath.resolve("database_" + name), columns, new LinkedList<>(), + extraFlags, crashIfWalError, lowMemory, inMemory diff --git a/src/main/java/it/cavallium/dbengine/database/disk/LLLocalKeyValueDatabase.java b/src/main/java/it/cavallium/dbengine/database/disk/LLLocalKeyValueDatabase.java index 93abc9f..0d1c7c1 100644 --- a/src/main/java/it/cavallium/dbengine/database/disk/LLLocalKeyValueDatabase.java +++ b/src/main/java/it/cavallium/dbengine/database/disk/LLLocalKeyValueDatabase.java @@ -20,6 +20,7 @@ import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.NoSuchElementException; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; @@ -66,6 +67,7 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase { private final Path dbPath; private final boolean inMemory; private final String name; + private final boolean enableColumnsBug; private RocksDB db; private final Map handles; private final ConcurrentHashMap snapshotsHandles = new ConcurrentHashMap<>(); @@ -76,6 +78,7 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase { Path path, List columns, List handles, + Map extraFlags, boolean crashIfWalError, boolean lowMemory, boolean inMemory) throws IOException { @@ -87,7 +90,7 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase { .add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); for (Column column : columns) { descriptors - .add(new ColumnFamilyDescriptor(column.getName().getBytes(StandardCharsets.US_ASCII))); + .add(new ColumnFamilyDescriptor(column.name().getBytes(StandardCharsets.US_ASCII))); } // Get databases directory path @@ -105,6 +108,7 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase { 60, true ); + this.enableColumnsBug = "true".equals(extraFlags.getOrDefault("enableColumnBug", "false")); createIfNotExists(descriptors, options, inMemory, this.dbPath, dbPathString); @@ -119,8 +123,19 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase { ); createInMemoryColumns(descriptors, inMemory, handles); this.handles = new HashMap<>(); - for (int i = 0; i < columns.size(); i++) { - this.handles.put(columns.get(i), handles.get(i)); + if (enableColumnsBug) { + for (int i = 0; i < columns.size(); i++) { + this.handles.put(columns.get(i), handles.get(i)); + } + } else { + handles: for (ColumnFamilyHandle handle : handles) { + for (Column column : columns) { + if (Arrays.equals(column.name().getBytes(StandardCharsets.US_ASCII), handle.getName())) { + this.handles.put(column, handle); + continue handles; + } + } + } } // compactDb(db, handles); @@ -398,7 +413,7 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase { public Mono getSingleton(byte[] singletonListColumnName, byte[] name, byte[] defaultValue) { return Mono .fromCallable(() -> new LLLocalSingleton(db, - handles.get(Column.special(Column.toString(singletonListColumnName))), + getCfh(singletonListColumnName), (snapshot) -> snapshotsHandles.get(snapshot.getSequenceNumber()), LLLocalKeyValueDatabase.this.name, name, @@ -412,19 +427,29 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase { @Override public Mono getDictionary(byte[] columnName, UpdateMode updateMode) { return Mono - .fromCallable(() -> new LLLocalDictionary( - allocator, - db, - handles.get(Column.special(Column.toString(columnName))), - name, - Column.toString(columnName), - dbScheduler, - (snapshot) -> snapshotsHandles.get(snapshot.getSequenceNumber()), - updateMode - )) + .fromCallable(() -> { + return new LLLocalDictionary( + allocator, + db, + getCfh(columnName), + name, + Column.toString(columnName), + dbScheduler, + (snapshot) -> snapshotsHandles.get(snapshot.getSequenceNumber()), + updateMode + ); + }) .subscribeOn(dbScheduler); } + private ColumnFamilyHandle getCfh(byte[] columnName) throws RocksDBException { + ColumnFamilyHandle cfh = handles.get(Column.special(Column.toString(columnName))); + if (!enableColumnsBug) { + assert Arrays.equals(cfh.getName(), columnName); + } + return cfh; + } + @Override public Mono getProperty(String propertyName) { return Mono.fromCallable(() -> db.getAggregatedLongProperty(propertyName)) diff --git a/src/test/java/it/cavallium/dbengine/DbTestUtils.java b/src/test/java/it/cavallium/dbengine/DbTestUtils.java index 154719f..3715a27 100644 --- a/src/test/java/it/cavallium/dbengine/DbTestUtils.java +++ b/src/test/java/it/cavallium/dbengine/DbTestUtils.java @@ -57,7 +57,7 @@ public class DbTestUtils { .then(new LLLocalDatabaseConnection(DbTestUtils.ALLOCATOR, wrkspcPath, true).connect()) .flatMap(conn -> conn.getDatabase("testdb", List.of(Column.dictionary("testmap"), Column.special("ints"), Column.special("longs")), - false, true + Map.of(), false, true )), action, db -> db.close().then(Mono.fromCallable(() -> { diff --git a/src/test/java/it/cavallium/dbengine/OldDatabaseTests.java b/src/test/java/it/cavallium/dbengine/OldDatabaseTests.java index 36125ca..ce4873e 100644 --- a/src/test/java/it/cavallium/dbengine/OldDatabaseTests.java +++ b/src/test/java/it/cavallium/dbengine/OldDatabaseTests.java @@ -132,7 +132,7 @@ public class OldDatabaseTests { }) .subscribeOn(Schedulers.boundedElastic()) .then(new LLLocalDatabaseConnection(PooledByteBufAllocator.DEFAULT, wrkspcPath, true).connect()) - .flatMap(conn -> conn.getDatabase("testdb", List.of(Column.dictionary("testmap")), false, true)); + .flatMap(conn -> conn.getDatabase("testdb", List.of(Column.dictionary("testmap")), Map.of(), false, true)); } private static final ByteBuf DUMMY_VALUE;