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

188 lines
4.8 KiB
Java
Raw Normal View History

2022-04-09 02:45:42 +02:00
package it.cavallium.dbengine.database.disk;
2020-12-07 22:15:18 +01:00
import static it.cavallium.dbengine.database.LLUtils.asArray;
import it.cavallium.dbengine.buffers.Buf;
2020-12-07 22:15:18 +01:00
import java.nio.ByteBuffer;
2022-04-09 02:45:42 +02:00
import org.rocksdb.ColumnFamilyHandle;
import org.rocksdb.RocksDBException;
import org.rocksdb.WriteBatch;
import org.rocksdb.WriteOptions;
2020-12-07 22:15:18 +01:00
public class CappedWriteBatch extends WriteBatch {
2020-12-07 22:15:18 +01:00
2021-10-20 01:51:34 +02:00
private final RocksDBColumn db;
2020-12-07 22:15:18 +01:00
private final int cap;
private final WriteOptions writeOptions;
2021-09-01 00:01:56 +02:00
2020-12-07 22:15:18 +01:00
/**
2021-10-20 01:51:34 +02:00
* @param db
2020-12-07 22:15:18 +01:00
* @param cap The limit of operations
*/
2021-10-20 01:51:34 +02:00
public CappedWriteBatch(RocksDBColumn db,
int cap,
int reservedWriteBatchSize,
long maxWriteBatchSize,
WriteOptions writeOptions) {
super(reservedWriteBatchSize);
2020-12-07 22:15:18 +01:00
this.db = db;
this.cap = cap;
this.writeOptions = writeOptions;
this.setMaxBytes(maxWriteBatchSize);
2020-12-07 22:15:18 +01:00
}
2021-03-13 19:01:36 +01:00
private synchronized void flushIfNeeded(boolean force) throws RocksDBException {
if (this.count() >= (force ? 1 : cap)) {
db.write(writeOptions, this.getWriteBatch());
this.clear();
2021-09-01 00:01:56 +02:00
}
2020-12-07 22:15:18 +01:00
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized int count() {
return super.count();
2020-12-07 22:15:18 +01:00
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized void put(byte[] key, byte[] value) throws RocksDBException {
super.put(key, value);
2020-12-07 22:15:18 +01:00
flushIfNeeded(false);
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized void put(ColumnFamilyHandle columnFamilyHandle, byte[] key, byte[] value) throws RocksDBException {
super.put(columnFamilyHandle, key, value);
2020-12-07 22:15:18 +01:00
flushIfNeeded(false);
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized void put(ByteBuffer key, ByteBuffer value) throws RocksDBException {
super.put(key, value);
2020-12-07 22:15:18 +01:00
flushIfNeeded(false);
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized void put(ColumnFamilyHandle columnFamilyHandle, ByteBuffer key, ByteBuffer value) throws RocksDBException {
super.put(columnFamilyHandle, key, value);
flushIfNeeded(false);
}
2021-08-29 23:18:03 +02:00
public synchronized void put(ColumnFamilyHandle columnFamilyHandle,
Buf keyToReceive,
Buf valueToReceive) throws RocksDBException {
super.put(columnFamilyHandle, asArray(keyToReceive), asArray(valueToReceive));
2020-12-07 22:15:18 +01:00
flushIfNeeded(false);
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized void merge(byte[] key, byte[] value) throws RocksDBException {
super.merge(key, value);
2020-12-07 22:15:18 +01:00
flushIfNeeded(false);
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized void merge(ColumnFamilyHandle columnFamilyHandle, byte[] key, byte[] value) throws RocksDBException {
super.merge(columnFamilyHandle, key, value);
2020-12-07 22:15:18 +01:00
flushIfNeeded(false);
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized void delete(byte[] key) throws RocksDBException {
super.delete(key);
2020-12-07 22:15:18 +01:00
flushIfNeeded(false);
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized void delete(ColumnFamilyHandle columnFamilyHandle, byte[] key) throws RocksDBException {
super.delete(columnFamilyHandle, key);
flushIfNeeded(false);
}
public synchronized void delete(ColumnFamilyHandle columnFamilyHandle, Buf keyToDelete) throws RocksDBException {
super.delete(columnFamilyHandle, asArray(keyToDelete));
2020-12-07 22:15:18 +01:00
flushIfNeeded(false);
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized void singleDelete(byte[] key) throws RocksDBException {
super.singleDelete(key);
2020-12-07 22:15:18 +01:00
flushIfNeeded(false);
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized void singleDelete(ColumnFamilyHandle columnFamilyHandle, byte[] key) throws RocksDBException {
super.singleDelete(columnFamilyHandle, key);
2020-12-07 22:15:18 +01:00
flushIfNeeded(false);
}
@Override
2022-03-27 01:22:20 +01:00
public synchronized void delete(ByteBuffer key) throws RocksDBException {
super.delete(key);
2020-12-07 22:15:18 +01:00
flushIfNeeded(false);
}
@Override
2022-03-27 01:22:20 +01:00
public synchronized void delete(ColumnFamilyHandle columnFamilyHandle, ByteBuffer key) throws RocksDBException {
super.delete(columnFamilyHandle, key);
2020-12-07 22:15:18 +01:00
flushIfNeeded(false);
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized void deleteRange(byte[] beginKey, byte[] endKey) throws RocksDBException {
super.deleteRange(beginKey, endKey);
2020-12-07 22:15:18 +01:00
flushIfNeeded(false);
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized void deleteRange(ColumnFamilyHandle columnFamilyHandle, byte[] beginKey, byte[] endKey)
2020-12-07 22:15:18 +01:00
throws RocksDBException {
super.deleteRange(columnFamilyHandle, beginKey, endKey);
2020-12-07 22:15:18 +01:00
flushIfNeeded(false);
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized void putLogData(byte[] blob) throws RocksDBException {
super.putLogData(blob);
2020-12-07 22:15:18 +01:00
flushIfNeeded(false);
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized void clear() {
super.clear();
2020-12-07 22:15:18 +01:00
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized void setSavePoint() {
super.setSavePoint();
2020-12-07 22:15:18 +01:00
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized void rollbackToSavePoint() throws RocksDBException {
super.rollbackToSavePoint();
2020-12-07 22:15:18 +01:00
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized void popSavePoint() throws RocksDBException {
super.popSavePoint();
2020-12-07 22:15:18 +01:00
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized void setMaxBytes(long maxBytes) {
super.setMaxBytes(maxBytes);
2020-12-07 22:15:18 +01:00
}
@Override
2021-03-13 19:01:36 +01:00
public synchronized WriteBatch getWriteBatch() {
2021-05-02 19:18:15 +02:00
return super.getWriteBatch();
2020-12-07 22:15:18 +01:00
}
2021-03-13 19:01:36 +01:00
public synchronized void writeToDbAndClose() throws RocksDBException {
flushIfNeeded(true);
super.close();
2021-05-02 19:18:15 +02:00
}
public void flush() throws RocksDBException {
flushIfNeeded(true);
2020-12-07 22:15:18 +01:00
}
}