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

43 lines
978 B
Java
Raw Normal View History

2022-05-22 16:48:08 +02:00
package it.cavallium.dbengine.database.disk;
2022-06-30 15:06:10 +02:00
import it.cavallium.dbengine.database.DiscardingCloseable;
2022-05-22 16:48:08 +02:00
import it.cavallium.dbengine.database.SafeCloseable;
import java.util.ArrayList;
import org.rocksdb.AbstractImmutableNativeReference;
2022-06-30 15:06:10 +02:00
public final class RocksDBRefs implements DiscardingCloseable {
2022-05-22 16:48:08 +02:00
private final ArrayList<AbstractImmutableNativeReference> list = new ArrayList<>();
private boolean closed;
public RocksDBRefs() {
}
public RocksDBRefs(Iterable<? extends AbstractImmutableNativeReference> it) {
it.forEach(list::add);
}
public synchronized void track(AbstractImmutableNativeReference ref) {
if (closed) {
throw new IllegalStateException("Closed");
}
list.add(ref);
}
@Override
public synchronized void close() {
if (!closed) {
closed = true;
for (int i = list.size() - 1; i >= 0; i--) {
var ref = list.get(i);
if (ref.isOwningHandle()) {
ref.close();
}
}
list.clear();
list.trimToSize();
}
}
}