CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/LLDelta.java

128 lines
3.4 KiB
Java
Raw Normal View History

2021-08-29 23:18:03 +02:00
package it.cavallium.dbengine.database;
2021-09-17 16:56:28 +02:00
import io.net5.buffer.api.Buffer;
import io.net5.buffer.api.Drop;
import io.net5.buffer.api.Owned;
import io.net5.buffer.api.Send;
import io.net5.buffer.api.internal.ResourceSupport;
2021-08-29 23:18:03 +02:00
import java.util.StringJoiner;
import org.jetbrains.annotations.Nullable;
2021-09-23 20:57:28 +02:00
public class LLDelta extends LiveResourceSupport<LLDelta, LLDelta> {
2021-08-29 23:18:03 +02:00
@Nullable
private final Buffer previous;
@Nullable
private final Buffer current;
private LLDelta(@Nullable Send<Buffer> previous, @Nullable Send<Buffer> current, Drop<LLDelta> drop) {
super(new LLDelta.CloseOnDrop(drop));
assert isAllAccessible();
this.previous = previous != null ? previous.receive().makeReadOnly() : null;
this.current = current != null ? current.receive().makeReadOnly() : null;
}
private boolean isAllAccessible() {
assert previous == null || previous.isAccessible();
assert current == null || current.isAccessible();
assert this.isAccessible();
assert this.isOwned();
return true;
}
public static LLDelta of(Send<Buffer> min, Send<Buffer> max) {
2021-09-24 01:59:56 +02:00
assert (min == null && max == null) || (min != max);
2021-08-29 23:18:03 +02:00
return new LLDelta(min, max, d -> {});
}
public Send<Buffer> previous() {
ensureOwned();
return previous != null ? previous.copy().send() : null;
}
public Send<Buffer> current() {
ensureOwned();
return current != null ? current.copy().send() : null;
}
public boolean isModified() {
return !LLUtils.equals(previous, current);
}
private void ensureOwned() {
assert isAllAccessible();
if (!isOwned()) {
if (!isAccessible()) {
throw this.createResourceClosedException();
} else {
throw new IllegalStateException("Resource not owned");
}
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LLDelta LLDelta = (LLDelta) o;
return LLUtils.equals(previous, LLDelta.previous) && LLUtils.equals(current, LLDelta.current);
}
@Override
public int hashCode() {
int result = LLUtils.hashCode(previous);
result = 31 * result + LLUtils.hashCode(current);
return result;
}
@Override
public String toString() {
return new StringJoiner(", ", LLDelta.class.getSimpleName() + "[", "]")
2021-09-24 01:59:56 +02:00
.add("min=" + LLUtils.toStringSafe(previous))
.add("max=" + LLUtils.toStringSafe(current))
2021-08-29 23:18:03 +02:00
.toString();
}
public LLDelta copy() {
ensureOwned();
2021-09-24 01:59:56 +02:00
var prevCopy = previous != null ? previous.copy().send() : null;
Send<Buffer> curCopy = current != null ? current.copy().send() : null;
return new LLDelta(prevCopy, curCopy, d -> {});
2021-08-29 23:18:03 +02:00
}
@Override
protected RuntimeException createResourceClosedException() {
return new IllegalStateException("Closed");
}
@Override
protected Owned<LLDelta> prepareSend() {
2021-09-24 01:59:56 +02:00
Send<Buffer> minSend = this.previous != null ? this.previous.send() : null;
Send<Buffer> maxSend = this.current != null ? this.current.send() : null;
2021-08-29 23:18:03 +02:00
return drop -> new LLDelta(minSend, maxSend, drop);
}
private static class CloseOnDrop implements Drop<LLDelta> {
private final Drop<LLDelta> delegate;
public CloseOnDrop(Drop<LLDelta> drop) {
2021-09-24 01:59:56 +02:00
if (drop instanceof CloseOnDrop closeOnDrop) {
this.delegate = closeOnDrop.delegate;
} else {
this.delegate = drop;
}
2021-08-29 23:18:03 +02:00
}
@Override
public void drop(LLDelta obj) {
2021-09-24 01:59:56 +02:00
if (obj.previous != null) obj.previous.close();
if (obj.current != null) obj.current.close();
2021-08-29 23:18:03 +02:00
delegate.drop(obj);
}
}
}