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

114 lines
2.6 KiB
Java
Raw Normal View History

2021-08-28 22:42:51 +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.Resource;
import io.netty5.util.Send;
2022-03-16 13:47:56 +01:00
import io.netty5.buffer.api.internal.ResourceSupport;
2022-06-30 13:54:55 +02:00
import it.cavallium.dbengine.utils.SimpleResource;
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.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2021-08-29 23:18:03 +02:00
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
2021-08-28 22:42:51 +02:00
2022-06-30 15:06:10 +02:00
public class LLEntry extends SimpleResource implements DiscardingCloseable {
2021-08-28 22:42:51 +02:00
private static final Logger logger = LogManager.getLogger(LLEntry.class);
2021-10-01 19:17:33 +02:00
private Buffer key;
private Buffer value;
private LLEntry(@NotNull Send<Buffer> key, @NotNull Send<Buffer> value) {
this.key = key.receive();
this.value = value.receive();
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) {
this.key = key;
this.value = value;
2021-11-08 16:33:41 +01:00
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
return true;
}
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();
2021-10-01 19:17:33 +02:00
}
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
2022-06-30 13:54:55 +02:00
protected void onClose() {
try {
if (key != null && key.isAccessible()) {
key.close();
}
} catch (Throwable ex) {
logger.error("Failed to close key", ex);
}
try {
if (value != null && value.isAccessible()) {
value.close();
}
} catch (Throwable ex) {
logger.error("Failed to close value", ex);
}
key = null;
value = null;
2021-08-28 22:42:51 +02:00
}
}