Optimize serializer

This commit is contained in:
Andrea Cavalli 2023-04-17 23:31:16 +02:00
parent 97df3bf725
commit cce49a50ee
5 changed files with 63 additions and 22 deletions

12
pom.xml
View File

@ -346,7 +346,7 @@
</dependency>
<dependency>
<groupId>io.soabase.record-builder</groupId>
<artifactId>record-builder-processor</artifactId>
<artifactId>record-builder-core</artifactId>
<version>36</version>
<scope>provided</scope>
</dependency>
@ -386,6 +386,16 @@
<version>3.8.1</version>
<configuration>
<release>17</release>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>io.soabase.record-builder</groupId>
<artifactId>record-builder-processor</artifactId>
<version>33</version>
</annotationProcessorPath>
</annotationProcessorPaths>
<annotationProcessors>
<annotationProcessor>io.soabase.recordbuilder.processor.RecordBuilderProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
</plugin>
<plugin>

View File

@ -18,6 +18,7 @@ import java.lang.invoke.MethodType;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HexFormat;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -90,6 +91,7 @@ public class LLUtils {
private static final Consumer<Object> NULL_CONSUMER = ignored -> {};
private static final Buf BUF_TRUE = Buf.wrap(new byte[] {(byte) 1});
private static final Buf BUF_FALSE = Buf.wrap(new byte[] {(byte) 0});
private static final HexFormat HEX_FORMAT = HexFormat.of().withUpperCase();
static {
for (int i1 = 0; i1 < 256; i1++) {
@ -302,6 +304,14 @@ public class LLUtils {
}
}
public static String toStringSafe(@Nullable Buf key, int iLimit) {
if (key != null) {
return toString(key, iLimit);
} else {
return "(released)";
}
}
public static String toStringSafe(@Nullable LLRange range) {
if (range != null) {
return toString(range);
@ -334,15 +344,26 @@ public class LLUtils {
}
}
public static String toString(@Nullable Buf key, int iLimit) {
if (key != null) {
return toString(key.asArray(), iLimit);
} else {
return "null";
}
}
public static String toString(byte @Nullable[] key) {
return toString(key, 1024);
}
public static String toString(byte @Nullable[] key, int iLimit) {
if (key == null) {
return "null";
} else {
int startIndex = 0;
int iMax = key.length - 1;
int iLimit = 128;
if (iMax <= -1) {
return "[]";
return "\"\"";
} else {
StringBuilder arraySB = new StringBuilder();
StringBuilder asciiSB = new StringBuilder();
@ -370,7 +391,11 @@ public class LLUtils {
if (isAscii) {
return asciiSB.insert(0, "\"").append("\"").toString();
} else {
return arraySB.append(']').toString();
if (i >= iLimit) {
return arraySB.append(']').toString();
} else {
return HEX_FORMAT.formatHex(key);
}
}
}
@ -381,6 +406,10 @@ public class LLUtils {
}
}
public static byte[] parseHex(String hex) {
return HEX_FORMAT.parseHex(hex);
}
public static boolean equals(Buf a, Buf b) {
if (a == null && b == null) {
return true;

View File

@ -109,13 +109,14 @@ public final class OptimisticRocksDBColumn extends AbstractRocksDBColumn<Optimis
} else {
prevData = null;
}
Buf prevDataToSendToUpdater;
if (prevData != null) {
prevDataToSendToUpdater = prevData.copy();
} else {
prevDataToSendToUpdater = null;
prevData.freeze();
}
try {
newData = updater.apply(prevData);
} catch (Exception ex) {
throw new DBException("Failed to update key " + LLUtils.toStringSafe(key) + ". The previous value was:\n" + LLUtils.toStringSafe(prevData, 8192), ex);
}
newData = updater.apply(prevDataToSendToUpdater);
var newDataArray = newData == null ? null : LLUtils.asArray(newData);
if (logger.isTraceEnabled()) {
logger.trace(MARKER_ROCKSDB,
@ -197,3 +198,4 @@ public final class OptimisticRocksDBColumn extends AbstractRocksDBColumn<Optimis
return true;
}
}

View File

@ -80,14 +80,14 @@ public final class PessimisticRocksDBColumn extends AbstractRocksDBColumn<Transa
} else {
readValueNotFoundWithoutBloomBufferSize.record(0);
}
Buf prevDataToSendToUpdater;
if (prevData != null) {
prevDataToSendToUpdater = prevData.copy();
} else {
prevDataToSendToUpdater = null;
prevData.freeze();
}
try {
newData = updater.apply(prevData);
} catch (Exception ex) {
throw new DBException("Failed to update key " + LLUtils.toStringSafe(key) + ". The previous value was:\n" + LLUtils.toStringSafe(prevData, 8192), ex);
}
newData = updater.apply(prevDataToSendToUpdater);
var newDataArray = newData == null ? null : LLUtils.asArray(newData);
if (logger.isTraceEnabled()) {
logger.trace(MARKER_ROCKSDB,

View File

@ -49,6 +49,7 @@ public final class StandardRocksDBColumn extends AbstractRocksDBColumn<RocksDB>
long initNanoTime = System.nanoTime();
try {
@Nullable Buf prevData = this.get(readOptions, key);
@Nullable Buf newData;
if (logger.isTraceEnabled()) {
logger.trace(MARKER_ROCKSDB,
"Reading {}: {} (before update)",
@ -57,15 +58,14 @@ public final class StandardRocksDBColumn extends AbstractRocksDBColumn<RocksDB>
);
}
Buf prevDataToSendToUpdater;
if (prevData != null) {
prevDataToSendToUpdater = prevData.copy();
} else {
prevDataToSendToUpdater = null;
prevData.freeze();
}
try {
newData = updater.apply(prevData);
} catch (Exception ex) {
throw new DBException("Failed to update key " + LLUtils.toStringSafe(key) + ". The previous value was:\n" + LLUtils.toStringSafe(prevData, 8192), ex);
}
@Nullable Buf newData;
newData = updater.apply(prevDataToSendToUpdater);
boolean changed;
if (logger.isTraceEnabled()) {
logger.trace(MARKER_ROCKSDB,