CavalliumDBEngine/src/test/java/it/cavallium/dbengine/tests/DbTestUtils.java

177 lines
5.8 KiB
Java
Raw Normal View History

2023-02-22 22:31:36 +01:00
package it.cavallium.dbengine.tests;
2021-08-28 22:42:51 +02:00
import static org.junit.jupiter.api.Assertions.assertEquals;
2023-03-06 12:19:08 +01:00
import it.cavallium.buffer.BufDataInput;
import it.cavallium.buffer.BufDataOutput;
import it.cavallium.dbengine.client.LuceneIndex;
import it.cavallium.dbengine.client.LuceneIndexImpl;
2021-08-28 22:42:51 +02:00
import it.cavallium.dbengine.database.LLDatabaseConnection;
import it.cavallium.dbengine.database.LLDictionary;
import it.cavallium.dbengine.database.LLKeyValueDatabase;
import it.cavallium.dbengine.database.LLLuceneIndex;
import it.cavallium.dbengine.database.UpdateMode;
import it.cavallium.dbengine.database.collections.DatabaseMapDictionary;
import it.cavallium.dbengine.database.collections.DatabaseMapDictionaryDeep;
import it.cavallium.dbengine.database.collections.DatabaseMapDictionaryHashed;
2021-05-03 18:07:18 +02:00
import it.cavallium.dbengine.database.collections.DatabaseStageEntry;
import it.cavallium.dbengine.database.collections.DatabaseStageMap;
import it.cavallium.dbengine.database.collections.SubStageGetterHashMap;
import it.cavallium.dbengine.database.collections.SubStageGetterMap;
2023-02-22 22:31:36 +01:00
import it.cavallium.dbengine.database.serialization.SerializationException;
import it.cavallium.dbengine.database.serialization.Serializer;
import it.cavallium.dbengine.database.serialization.SerializerFixedBinaryLength;
2021-12-18 18:16:56 +01:00
import it.unimi.dsi.fastutil.objects.Object2ObjectSortedMap;
2023-02-22 22:31:36 +01:00
import java.io.IOException;
import java.nio.file.Path;
2023-02-22 22:31:36 +01:00
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
2023-02-22 22:31:36 +01:00
import org.apache.lucene.util.IOSupplier;
2021-05-03 18:07:18 +02:00
import org.jetbrains.annotations.NotNull;
2023-02-22 22:31:36 +01:00
import org.junit.jupiter.api.Assertions;
public class DbTestUtils {
2021-09-06 01:15:13 +02:00
public static final String BIG_STRING = generateBigString();
2021-12-12 23:40:30 +01:00
public static final int MAX_IN_MEMORY_RESULT_ENTRIES = 8192;
2021-09-06 01:15:13 +02:00
private static String generateBigString() {
2021-09-08 00:22:39 +02:00
return "0123456789".repeat(1024);
2021-09-06 01:15:13 +02:00
}
2023-02-22 22:31:36 +01:00
public static boolean isCIMode() {
return System.getProperty("dbengine.ci", "false").equalsIgnoreCase("true");
2021-08-28 22:42:51 +02:00
}
2023-02-22 22:31:36 +01:00
public static <U> U tempDb(TemporaryDbGenerator temporaryDbGenerator,
Function<LLKeyValueDatabase, U> action) throws IOException {
var tempDb = temporaryDbGenerator.openTempDb();
try {
return action.apply(tempDb.db());
} finally {
temporaryDbGenerator.closeTempDb(tempDb);
2021-08-29 01:15:51 +02:00
}
2021-08-28 22:42:51 +02:00
}
2023-02-22 22:31:36 +01:00
public static void runVoid(boolean shouldFail, Runnable consumer) {
if (shouldFail) {
Assertions.assertThrows(Throwable.class, consumer::run);
} else {
Assertions.assertDoesNotThrow(consumer::run);
}
2021-09-10 13:34:26 +02:00
}
2023-02-22 22:31:36 +01:00
public static <X> X run(boolean shouldFail, IOSupplier<X> consumer) {
AtomicReference<X> result = new AtomicReference<>(null);
if (shouldFail) {
Assertions.assertThrows(Throwable.class, consumer::get);
} else {
Assertions.assertDoesNotThrow(() -> result.set(consumer.get()));
}
return result.get();
}
2021-05-02 19:18:15 +02:00
2023-02-22 22:31:36 +01:00
public record TempDb(LLDatabaseConnection connection, LLKeyValueDatabase db,
2022-03-22 19:40:15 +01:00
LLLuceneIndex luceneSingle,
LLLuceneIndex luceneMulti,
SwappableLuceneSearcher swappableLuceneSearcher,
Path path) {}
2021-08-28 22:42:51 +02:00
2023-02-28 23:10:31 +01:00
public static void ensureNoLeaks() {
System.gc();
2021-08-28 22:42:51 +02:00
}
2023-02-22 22:31:36 +01:00
public static LLDictionary tempDictionary(LLKeyValueDatabase database, UpdateMode updateMode) {
return tempDictionary(database, "testmap", updateMode);
}
2023-02-22 22:31:36 +01:00
public static LLDictionary tempDictionary(LLKeyValueDatabase database,
String name,
UpdateMode updateMode) {
return database.getDictionary(name, updateMode);
}
2023-02-22 22:31:36 +01:00
public static LuceneIndex<String, String> tempLuceneIndex(LLLuceneIndex index) {
return new LuceneIndexImpl<>(index, new StringIndicizer());
}
2021-05-03 18:07:18 +02:00
2021-09-06 01:15:13 +02:00
public enum MapType {
2021-05-03 18:07:18 +02:00
MAP,
HASH_MAP
}
public static DatabaseStageMap<String, String, DatabaseStageEntry<String>> tempDatabaseMapDictionaryMap(
LLDictionary dictionary,
2021-09-06 01:15:13 +02:00
MapType mapType,
int keyBytes) {
2021-09-06 01:15:13 +02:00
if (mapType == MapType.MAP) {
2021-05-03 21:41:51 +02:00
return DatabaseMapDictionary.simple(dictionary,
2021-10-19 00:22:05 +02:00
SerializerFixedBinaryLength.utf8(keyBytes),
2022-07-19 23:45:39 +02:00
Serializer.UTF8_SERIALIZER
2021-05-03 21:41:51 +02:00
);
2021-05-03 18:07:18 +02:00
} else {
return DatabaseMapDictionaryHashed.simple(dictionary,
2021-10-19 00:22:05 +02:00
Serializer.UTF8_SERIALIZER,
Serializer.UTF8_SERIALIZER,
2021-05-08 03:09:00 +02:00
s -> (short) s.hashCode(),
2021-05-03 18:07:18 +02:00
new SerializerFixedBinaryLength<>() {
@Override
public int getSerializedBinaryLength() {
2021-05-08 03:09:00 +02:00
return Short.BYTES;
2021-05-03 18:07:18 +02:00
}
@Override
2023-02-22 22:31:36 +01:00
public @NotNull Short deserialize(@NotNull BufDataInput in) throws SerializationException {
return in.readShort();
2021-05-03 18:07:18 +02:00
}
@Override
2023-02-22 22:31:36 +01:00
public void serialize(@NotNull Short deserialized, BufDataOutput out) throws SerializationException {
out.writeShort(deserialized);
2021-05-03 18:07:18 +02:00
}
2022-07-19 23:45:39 +02:00
}
2021-05-03 18:07:18 +02:00
);
}
}
2021-12-18 18:16:56 +01:00
public static DatabaseMapDictionaryDeep<String, Object2ObjectSortedMap<String, String>,
2021-05-08 03:09:00 +02:00
DatabaseMapDictionary<String, String>> tempDatabaseMapDictionaryDeepMap(
LLDictionary dictionary,
int key1Bytes,
int key2Bytes) {
return DatabaseMapDictionaryDeep.deepTail(dictionary,
2021-10-19 00:22:05 +02:00
SerializerFixedBinaryLength.utf8(key1Bytes),
key2Bytes,
2021-10-19 00:22:05 +02:00
new SubStageGetterMap<>(SerializerFixedBinaryLength.utf8(key2Bytes),
Serializer.UTF8_SERIALIZER
2022-07-19 23:45:39 +02:00
)
2021-05-08 03:09:00 +02:00
);
}
2021-12-18 18:16:56 +01:00
public static DatabaseMapDictionaryDeep<String, Object2ObjectSortedMap<String, String>,
2021-05-08 03:09:00 +02:00
DatabaseMapDictionaryHashed<String, String, Integer>> tempDatabaseMapDictionaryDeepMapHashMap(
LLDictionary dictionary,
int key1Bytes) {
return DatabaseMapDictionaryDeep.deepTail(dictionary,
2021-10-19 00:22:05 +02:00
SerializerFixedBinaryLength.utf8(key1Bytes),
2021-05-08 03:09:00 +02:00
Integer.BYTES,
2021-10-19 00:22:05 +02:00
new SubStageGetterHashMap<>(Serializer.UTF8_SERIALIZER,
Serializer.UTF8_SERIALIZER,
2021-05-08 03:09:00 +02:00
String::hashCode,
2023-02-22 22:31:36 +01:00
SerializerFixedBinaryLength.intSerializer()
2022-07-19 23:45:39 +02:00
)
);
}
2022-04-11 16:40:55 +02:00
public static DatabaseMapDictionaryHashed<String, String, Integer> tempDatabaseMapDictionaryHashMap(
LLDictionary dictionary) {
return DatabaseMapDictionaryHashed.simple(dictionary,
2021-10-19 00:22:05 +02:00
Serializer.UTF8_SERIALIZER,
Serializer.UTF8_SERIALIZER,
String::hashCode,
2023-02-22 22:31:36 +01:00
SerializerFixedBinaryLength.intSerializer()
);
}
}