2021-10-20 01:51:34 +02:00
|
|
|
package it.cavallium.dbengine.database.disk;
|
|
|
|
|
|
|
|
import static it.cavallium.dbengine.database.LLUtils.MARKER_ROCKSDB;
|
|
|
|
|
2021-10-30 11:13:46 +02:00
|
|
|
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,
|
2022-03-30 15:15:53 +02:00
|
|
|
String dbName,
|
2021-10-30 11:13:46 +02:00
|
|
|
ColumnFamilyHandle cfh, MeterRegistry meterRegistry) {
|
2022-03-30 15:15:53 +02:00
|
|
|
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
|
2022-03-30 15:15:53 +02:00
|
|
|
public @NotNull UpdateAtomicResult updateAtomicImpl(@NotNull ReadOptions readOptions,
|
2021-10-20 01:51:34 +02:00
|
|
|
@NotNull WriteOptions writeOptions,
|
2022-04-01 01:30:56 +02:00
|
|
|
Buffer key,
|
|
|
|
BinarySerializationFunction updater,
|
2022-03-30 15:15:53 +02:00
|
|
|
UpdateAtomicResultMode returnMode) throws IOException {
|
2022-04-01 01:30:56 +02:00
|
|
|
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,
|
2022-04-01 01:30:56 +02:00
|
|
|
"Updating {}. previous data: {}, updated data: {}",
|
2022-03-02 18:33:58 +01:00
|
|
|
LLUtils.toStringSafe(key),
|
2022-04-01 01:30:56 +02:00
|
|
|
LLUtils.toStringSafe(prevData),
|
|
|
|
LLUtils.toStringSafe(newData)
|
2022-03-02 18:33:58 +01:00
|
|
|
);
|
2021-10-20 01:51:34 +02:00
|
|
|
}
|
2022-04-01 01:30:56 +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
|
|
|
}
|
2022-04-01 01:30:56 +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,
|
2022-04-01 01:30:56 +02:00
|
|
|
"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
|
|
|
}
|
2022-04-01 01:30:56 +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;
|
2022-04-01 01:30:56 +02:00
|
|
|
} finally {
|
|
|
|
if (dataToPut != newData) {
|
|
|
|
dataToPut.close();
|
2021-10-20 01:51:34 +02:00
|
|
|
}
|
2022-04-01 01:30:56 +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
|
|
|
}
|
2022-04-01 01:30:56 +02:00
|
|
|
if (newData != null) {
|
|
|
|
newData.close();
|
2021-10-20 01:51:34 +02:00
|
|
|
}
|
2022-04-01 01:30:56 +02:00
|
|
|
yield RESULT_NOTHING;
|
2021-10-20 01:51:34 +02:00
|
|
|
}
|
2022-04-01 01:30:56 +02:00
|
|
|
case CURRENT -> {
|
|
|
|
if (prevData != null) {
|
|
|
|
prevData.close();
|
2021-10-20 01:51:34 +02:00
|
|
|
}
|
2022-04-01 01:30:56 +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
|
|
|
}
|
2022-04-01 01:30:56 +02:00
|
|
|
yield new UpdateAtomicResultPrevious(prevData != null ? prevData.send() : null);
|
2021-10-20 01:51:34 +02:00
|
|
|
}
|
2022-04-01 01:30:56 +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
|
|
|
}
|
|
|
|
}
|
2022-04-01 01:30:56 +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;
|
|
|
|
}
|
|
|
|
}
|