CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/disk/StandardRocksDBColumn.java

151 lines
4.7 KiB
Java
Raw Normal View History

2021-10-20 01:51:34 +02:00
package it.cavallium.dbengine.database.disk;
import static it.cavallium.dbengine.database.LLUtils.MARKER_ROCKSDB;
import io.micrometer.core.instrument.MeterRegistry;
2022-03-16 13:47:56 +01:00
import io.netty5.buffer.api.Buffer;
import io.netty5.buffer.api.BufferAllocator;
import io.netty5.buffer.api.Send;
2021-10-20 01:51:34 +02:00
import it.cavallium.dbengine.database.LLDelta;
import it.cavallium.dbengine.database.LLUtils;
import it.cavallium.dbengine.database.serialization.SerializationFunction;
2022-03-02 12:34:30 +01:00
import it.cavallium.dbengine.rpc.current.data.DatabaseOptions;
2021-10-20 01:51:34 +02:00
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.rocksdb.ColumnFamilyHandle;
import org.rocksdb.ReadOptions;
import org.rocksdb.RocksDB;
import org.rocksdb.RocksDBException;
import org.rocksdb.Transaction;
import org.rocksdb.WriteOptions;
public final class StandardRocksDBColumn extends AbstractRocksDBColumn<RocksDB> {
public StandardRocksDBColumn(RocksDB db,
DatabaseOptions databaseOptions,
BufferAllocator alloc,
String dbName,
ColumnFamilyHandle cfh, MeterRegistry meterRegistry) {
super(db, databaseOptions, alloc, dbName, cfh, meterRegistry);
2021-10-20 01:51:34 +02:00
}
@Override
protected boolean commitOptimistically(Transaction tx) {
throw new UnsupportedOperationException("Transactions not supported");
}
@Override
protected Transaction beginTransaction(@NotNull WriteOptions writeOptions) {
throw new UnsupportedOperationException("Transactions not supported");
}
@Override
public @NotNull UpdateAtomicResult updateAtomicImpl(@NotNull ReadOptions readOptions,
2021-10-20 01:51:34 +02:00
@NotNull WriteOptions writeOptions,
Buffer key,
BinarySerializationFunction updater,
UpdateAtomicResultMode returnMode) throws IOException {
long initNanoTime = System.nanoTime();
try {
@Nullable Buffer prevData = this.get(readOptions, key);
try (prevData) {
if (logger.isTraceEnabled()) {
logger.trace(MARKER_ROCKSDB,
"Reading {}: {} (before update)",
LLUtils.toStringSafe(key),
LLUtils.toStringSafe(prevData)
);
}
Buffer prevDataToSendToUpdater;
if (prevData != null) {
prevDataToSendToUpdater = prevData.copy().makeReadOnly();
} else {
prevDataToSendToUpdater = null;
}
@Nullable Buffer newData = applyUpdateAndCloseIfNecessary(updater, prevDataToSendToUpdater);
try (newData) {
boolean changed;
assert newData == null || newData.isAccessible();
2022-03-02 18:33:58 +01:00
if (logger.isTraceEnabled()) {
logger.trace(MARKER_ROCKSDB,
"Updating {}. previous data: {}, updated data: {}",
2022-03-02 18:33:58 +01:00
LLUtils.toStringSafe(key),
LLUtils.toStringSafe(prevData),
LLUtils.toStringSafe(newData)
2022-03-02 18:33:58 +01:00
);
2021-10-20 01:51:34 +02:00
}
if (prevData != null && newData == null) {
if (logger.isTraceEnabled()) {
logger.trace(MARKER_ROCKSDB, "Deleting {} (after update)", LLUtils.toStringSafe(key));
2021-10-20 01:51:34 +02:00
}
this.delete(writeOptions, key);
changed = true;
} else if (newData != null && (prevData == null || !LLUtils.equals(prevData, newData))) {
2021-10-20 01:51:34 +02:00
if (logger.isTraceEnabled()) {
2021-12-29 00:31:35 +01:00
logger.trace(MARKER_ROCKSDB,
"Writing {}: {} (after update)",
2022-03-02 18:33:58 +01:00
LLUtils.toStringSafe(key),
LLUtils.toStringSafe(newData)
);
2021-10-20 01:51:34 +02:00
}
Buffer dataToPut;
if (returnMode == UpdateAtomicResultMode.CURRENT) {
dataToPut = newData.copy();
} else {
dataToPut = newData;
}
try {
this.put(writeOptions, key, dataToPut);
2021-10-20 01:51:34 +02:00
changed = true;
} finally {
if (dataToPut != newData) {
dataToPut.close();
2021-10-20 01:51:34 +02:00
}
}
} else {
changed = false;
}
recordAtomicUpdateTime(changed, prevData != null, newData != null, initNanoTime);
return switch (returnMode) {
case NOTHING -> {
if (prevData != null) {
prevData.close();
2021-10-20 01:51:34 +02:00
}
if (newData != null) {
newData.close();
2021-10-20 01:51:34 +02:00
}
yield RESULT_NOTHING;
2021-10-20 01:51:34 +02:00
}
case CURRENT -> {
if (prevData != null) {
prevData.close();
2021-10-20 01:51:34 +02:00
}
yield new UpdateAtomicResultCurrent(newData != null ? newData.send() : null);
}
case PREVIOUS -> {
if (newData != null) {
newData.close();
2021-12-29 00:31:35 +01:00
}
yield new UpdateAtomicResultPrevious(prevData != null ? prevData.send() : null);
2021-10-20 01:51:34 +02:00
}
case BINARY_CHANGED -> new UpdateAtomicResultBinaryChanged(changed);
case DELTA -> new UpdateAtomicResultDelta(LLDelta
.of(prevData != null ? prevData.send() : null, newData != null ? newData.send() : null)
.send());
};
2021-10-20 01:51:34 +02:00
}
}
} catch (Throwable ex) {
throw new IOException("Failed to update key " + LLUtils.toStringSafe(key), ex);
2021-10-20 01:51:34 +02:00
}
}
@Override
public boolean supportsTransactions() {
return false;
}
}