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

101 lines
2.4 KiB
Java
Raw Normal View History

2021-08-29 23:18:03 +02:00
package it.cavallium.dbengine.database;
2022-03-16 13:47:56 +01:00
import io.netty5.buffer.api.Buffer;
import io.netty5.buffer.api.Drop;
import io.netty5.buffer.api.Owned;
2022-07-15 02:44:50 +02:00
import io.netty5.util.Send;
2022-03-16 13:47:56 +01:00
import io.netty5.buffer.api.internal.ResourceSupport;
2022-07-19 23:45:39 +02:00
import it.cavallium.dbengine.utils.SimpleResource;
2021-08-29 23:18:03 +02:00
import java.util.StringJoiner;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2021-08-29 23:18:03 +02:00
import org.jetbrains.annotations.Nullable;
2022-07-21 01:07:16 +02:00
public class LLDelta extends SimpleResource implements DiscardingCloseable {
2021-10-01 19:17:33 +02:00
2021-08-29 23:18:03 +02:00
@Nullable
2022-07-19 23:45:39 +02:00
private final Buffer previous;
2021-08-29 23:18:03 +02:00
@Nullable
2022-07-19 23:45:39 +02:00
private final Buffer current;
2021-08-29 23:18:03 +02:00
2022-07-19 23:45:39 +02:00
private LLDelta(@Nullable Buffer previous, @Nullable Buffer current) {
super();
this.previous = previous != null ? previous.makeReadOnly() : null;
this.current = current != null ? current.makeReadOnly() : null;
2021-08-29 23:18:03 +02:00
}
2022-07-19 23:45:39 +02:00
@Override
protected void ensureOpen() {
super.ensureOpen();
2021-08-29 23:18:03 +02:00
assert previous == null || previous.isAccessible();
assert current == null || current.isAccessible();
2022-07-19 23:45:39 +02:00
}
@Override
protected void onClose() {
if (previous != null && previous.isAccessible()) {
previous.close();
}
if (current != null && current.isAccessible()) {
current.close();
}
2021-08-29 23:18:03 +02:00
}
public static LLDelta of(Buffer previous, Buffer current) {
2021-10-17 19:52:43 +02:00
assert (previous == null && current == null) || (previous != current);
2022-07-19 23:45:39 +02:00
return new LLDelta(previous, current);
2021-08-29 23:18:03 +02:00
}
public Send<Buffer> previous() {
2022-07-19 23:45:39 +02:00
ensureOpen();
2021-08-29 23:18:03 +02:00
return previous != null ? previous.copy().send() : null;
}
public Send<Buffer> current() {
2022-07-19 23:45:39 +02:00
ensureOpen();
2021-08-29 23:18:03 +02:00
return current != null ? current.copy().send() : null;
}
public Buffer currentUnsafe() {
2022-07-19 23:45:39 +02:00
ensureOpen();
return current;
}
public Buffer previousUnsafe() {
2022-07-19 23:45:39 +02:00
ensureOpen();
return previous;
}
2021-08-29 23:18:03 +02:00
public boolean isModified() {
return !LLUtils.equals(previous, current);
}
@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();
}
}