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

165 lines
3.7 KiB
Java
Raw Normal View History

2021-08-28 22:42:51 +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;
2021-11-08 16:33:41 +01:00
import io.net5.buffer.api.Resource;
2021-09-17 16:56:28 +02:00
import io.net5.buffer.api.Send;
2021-10-17 17:15:57 +02:00
import io.net5.buffer.api.internal.ResourceSupport;
2021-10-01 19:17:33 +02:00
import java.util.Objects;
2021-08-29 23:18:03 +02:00
import java.util.StringJoiner;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
2021-10-01 19:17:33 +02:00
import org.warp.commonutils.log.Logger;
import org.warp.commonutils.log.LoggerFactory;
2021-08-28 22:42:51 +02:00
2021-10-17 17:15:57 +02:00
public class LLEntry extends ResourceSupport<LLEntry, LLEntry> {
2021-08-28 22:42:51 +02:00
2021-10-01 19:17:33 +02:00
private static final Logger logger = LoggerFactory.getLogger(LLEntry.class);
private static final Drop<LLEntry> DROP = new Drop<>() {
@Override
public void drop(LLEntry obj) {
try {
if (obj.key != null) {
obj.key.close();
}
} catch (Throwable ex) {
logger.error("Failed to close key", ex);
}
try {
if (obj.value != null) {
obj.value.close();
}
} catch (Throwable ex) {
logger.error("Failed to close value", ex);
}
}
@Override
public Drop<LLEntry> fork() {
return this;
}
@Override
public void attach(LLEntry obj) {
}
};
@Nullable
private Buffer key;
@Nullable
private Buffer value;
private LLEntry(@NotNull Send<Buffer> key, @NotNull Send<Buffer> value) {
super(DROP);
2021-08-29 23:18:03 +02:00
this.key = key.receive().makeReadOnly();
this.value = value.receive().makeReadOnly();
2021-09-02 17:15:40 +02:00
assert isAllAccessible();
2021-08-29 23:18:03 +02:00
}
2021-11-08 16:33:41 +01:00
private LLEntry(@NotNull Buffer key, @NotNull Buffer value) {
super(DROP);
this.key = key.makeReadOnly();
this.value = value.makeReadOnly();
assert isAllAccessible();
}
2021-08-28 22:42:51 +02:00
2021-08-29 23:18:03 +02:00
private boolean isAllAccessible() {
2021-10-01 19:17:33 +02:00
assert key != null && key.isAccessible();
assert value != null && value.isAccessible();
2021-08-29 23:18:03 +02:00
assert this.isAccessible();
assert this.isOwned();
return true;
}
2021-08-28 22:42:51 +02:00
public static LLEntry of(@NotNull Send<Buffer> key, @NotNull Send<Buffer> value) {
2021-10-01 19:17:33 +02:00
return new LLEntry(key, value);
2021-08-29 23:18:03 +02:00
}
2021-08-28 22:42:51 +02:00
2021-11-08 16:33:41 +01:00
public static LLEntry of(@NotNull Buffer key, @NotNull Buffer value) {
return new LLEntry(key, value);
}
2021-08-29 23:18:03 +02:00
public Send<Buffer> getKey() {
ensureOwned();
2021-10-01 19:17:33 +02:00
return Objects.requireNonNull(key).copy().send();
2021-08-28 22:42:51 +02:00
}
2021-08-29 23:18:03 +02:00
public Buffer getKeyUnsafe() {
2021-08-28 22:42:51 +02:00
return key;
}
2021-08-29 23:18:03 +02:00
public Send<Buffer> getValue() {
ensureOwned();
2021-10-01 19:17:33 +02:00
return Objects.requireNonNull(value).copy().send();
2021-08-29 23:18:03 +02:00
}
public Buffer getValueUnsafe() {
2021-08-28 22:42:51 +02:00
return value;
}
2021-08-29 23:18:03 +02:00
private void ensureOwned() {
assert isAllAccessible();
if (!isOwned()) {
if (!isAccessible()) {
throw this.createResourceClosedException();
} else {
throw new IllegalStateException("Resource not owned");
}
2021-08-28 22:42:51 +02:00
}
}
2021-10-01 19:17:33 +02:00
@Override
protected void makeInaccessible() {
this.key = null;
this.value = null;
}
2021-08-29 23:18:03 +02:00
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
2021-08-28 22:42:51 +02:00
}
2021-08-29 23:18:03 +02:00
if (o == null || getClass() != o.getClass()) {
return false;
2021-08-28 22:42:51 +02:00
}
2021-08-29 23:18:03 +02:00
LLEntry LLEntry = (LLEntry) o;
return LLUtils.equals(key, LLEntry.key) && LLUtils.equals(value, LLEntry.value);
}
@Override
public int hashCode() {
int result = LLUtils.hashCode(key);
result = 31 * result + LLUtils.hashCode(value);
return result;
}
@Override
public String toString() {
return new StringJoiner(", ", LLEntry.class.getSimpleName() + "[", "]")
.add("key=" + LLUtils.toString(key))
.add("value=" + LLUtils.toString(value))
.toString();
2021-08-28 22:42:51 +02:00
}
2021-08-29 23:18:03 +02:00
@Override
protected RuntimeException createResourceClosedException() {
return new IllegalStateException("Closed");
2021-08-28 22:42:51 +02:00
}
@Override
2021-08-29 23:18:03 +02:00
protected Owned<LLEntry> prepareSend() {
Send<Buffer> keySend;
Send<Buffer> valueSend;
2021-10-01 19:17:33 +02:00
keySend = Objects.requireNonNull(this.key).send();
valueSend = Objects.requireNonNull(this.value).send();
return drop -> {
var instance = new LLEntry(keySend, valueSend);
drop.attach(instance);
return instance;
};
2021-08-28 22:42:51 +02:00
}
}