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

231 lines
5.1 KiB
Java
Raw Normal View History

2021-01-30 00:24:55 +01: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-01-30 00:24:55 +01:00
import java.util.StringJoiner;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2021-09-24 01:59:56 +02:00
import org.jetbrains.annotations.Nullable;
2021-01-30 00:24:55 +01:00
2021-03-13 19:01:36 +01:00
/**
2022-03-24 21:14:17 +01:00
* Range of data, from min (inclusive), to max (exclusive)
2021-03-13 19:01:36 +01:00
*/
2022-07-19 23:45:39 +02:00
public class LLRange extends SimpleResource {
2021-10-01 19:17:33 +02:00
2021-11-08 16:33:41 +01:00
private static final LLRange RANGE_ALL = new LLRange((Buffer) null, (Buffer) null, (Buffer) null);
2021-09-24 01:59:56 +02:00
@Nullable
2021-08-29 23:18:03 +02:00
private Buffer min;
2021-09-24 01:59:56 +02:00
@Nullable
2021-08-29 23:18:03 +02:00
private Buffer max;
2021-09-24 01:59:56 +02:00
@Nullable
2021-08-29 23:18:03 +02:00
private Buffer single;
2021-10-01 19:17:33 +02:00
private LLRange(Send<Buffer> min, Send<Buffer> max, Send<Buffer> single) {
2022-07-19 23:45:39 +02:00
super();
2021-08-29 23:18:03 +02:00
assert single == null || (min == null && max == null);
this.min = min != null ? min.receive().makeReadOnly() : null;
this.max = max != null ? max.receive().makeReadOnly() : null;
this.single = single != null ? single.receive().makeReadOnly() : null;
}
2021-11-08 16:33:41 +01:00
private LLRange(Buffer min, Buffer max, Buffer single) {
2022-07-19 23:45:39 +02:00
super();
2021-11-08 16:33:41 +01:00
assert single == null || (min == null && max == null);
this.min = min != null ? min.makeReadOnly() : null;
this.max = max != null ? max.makeReadOnly() : null;
this.single = single != null ? single.makeReadOnly() : null;
}
2021-01-30 00:24:55 +01:00
public static LLRange all() {
2021-08-29 23:18:03 +02:00
return RANGE_ALL.copy();
2021-01-30 00:24:55 +01:00
}
2021-08-29 23:18:03 +02:00
public static LLRange from(Send<Buffer> min) {
2021-10-01 19:17:33 +02:00
return new LLRange(min, null, null);
2021-01-30 00:24:55 +01:00
}
2021-08-29 23:18:03 +02:00
public static LLRange to(Send<Buffer> max) {
2021-10-01 19:17:33 +02:00
return new LLRange(null, max, null);
2021-01-30 00:24:55 +01:00
}
2021-08-29 23:18:03 +02:00
public static LLRange single(Send<Buffer> single) {
2021-10-01 19:17:33 +02:00
return new LLRange(null, null, single);
}
2022-01-22 23:21:40 +01:00
public static LLRange singleUnsafe(Buffer single) {
return new LLRange(null, null, single);
}
2021-08-29 23:18:03 +02:00
public static LLRange of(Send<Buffer> min, Send<Buffer> max) {
2021-10-01 19:17:33 +02:00
return new LLRange(min, max, null);
2021-11-08 16:33:41 +01:00
}
public static LLRange ofUnsafe(Buffer min, Buffer max) {
return new LLRange(min, max, null);
}
2021-01-30 00:24:55 +01:00
public boolean isAll() {
2022-07-19 23:45:39 +02:00
ensureOpen();
2021-08-29 23:18:03 +02:00
return min == null && max == null && single == null;
2021-01-30 00:24:55 +01:00
}
public boolean isSingle() {
2022-07-19 23:45:39 +02:00
ensureOpen();
2021-08-29 23:18:03 +02:00
return single != null;
2021-01-30 00:24:55 +01:00
}
public boolean hasMin() {
2022-07-19 23:45:39 +02:00
ensureOpen();
2021-08-29 23:18:03 +02:00
return min != null || single != null;
}
public Send<Buffer> getMin() {
2022-07-19 23:45:39 +02:00
ensureOpen();
2021-08-29 23:18:03 +02:00
if (min != null) {
2022-03-24 21:14:17 +01:00
// todo: use a read-only copy
2021-08-29 23:18:03 +02:00
return min.copy().send();
} else if (single != null) {
2022-03-24 21:14:17 +01:00
// todo: use a read-only copy
2021-08-29 23:18:03 +02:00
return single.copy().send();
} else {
return null;
}
2021-01-30 00:24:55 +01:00
}
2021-08-29 23:18:03 +02:00
public Buffer getMinUnsafe() {
2022-07-19 23:45:39 +02:00
ensureOpen();
2021-08-29 23:18:03 +02:00
if (min != null) {
return min;
} else if (single != null) {
return single;
} else {
return null;
}
2021-01-30 00:24:55 +01:00
}
2022-05-20 10:44:00 +02:00
public Buffer getMinCopy() {
2022-07-19 23:45:39 +02:00
ensureOpen();
2022-05-20 10:44:00 +02:00
if (min != null) {
return min.copy();
} else if (single != null) {
return single.copy();
} else {
return null;
}
}
2021-01-30 00:24:55 +01:00
public boolean hasMax() {
2022-07-19 23:45:39 +02:00
ensureOpen();
2021-08-29 23:18:03 +02:00
return max != null || single != null;
}
public Send<Buffer> getMax() {
2022-07-19 23:45:39 +02:00
ensureOpen();
2021-08-29 23:18:03 +02:00
if (max != null) {
2022-03-24 21:14:17 +01:00
// todo: use a read-only copy
2021-08-29 23:18:03 +02:00
return max.copy().send();
} else if (single != null) {
2022-03-24 21:14:17 +01:00
// todo: use a read-only copy
2021-08-29 23:18:03 +02:00
return single.copy().send();
} else {
return null;
}
}
public Buffer getMaxUnsafe() {
2022-07-19 23:45:39 +02:00
ensureOpen();
2021-08-29 23:18:03 +02:00
if (max != null) {
return max;
} else if (single != null) {
return single;
} else {
2022-05-20 10:44:00 +02:00
return null;
}
}
public Buffer getMaxCopy() {
2022-07-19 23:45:39 +02:00
ensureOpen();
2022-05-20 10:44:00 +02:00
if (max != null) {
return max.copy();
} else if (single != null) {
return single.copy();
} else {
2021-08-29 23:18:03 +02:00
return null;
}
2021-01-30 00:24:55 +01:00
}
2021-08-29 23:18:03 +02:00
public Send<Buffer> getSingle() {
2022-07-19 23:45:39 +02:00
ensureOpen();
2021-08-29 23:18:03 +02:00
assert isSingle();
2022-03-24 21:14:17 +01:00
// todo: use a read-only copy
2021-08-29 23:18:03 +02:00
return single != null ? single.copy().send() : null;
2021-01-30 00:24:55 +01:00
}
2021-08-29 23:18:03 +02:00
public Buffer getSingleUnsafe() {
2022-07-19 23:45:39 +02:00
ensureOpen();
2021-01-30 00:24:55 +01:00
assert isSingle();
2021-08-29 23:18:03 +02:00
return single;
2021-01-30 00:24:55 +01:00
}
2022-07-19 23:45:39 +02:00
@Override
protected void ensureOpen() {
super.ensureOpen();
assert min == null || min.isAccessible() : "Range min not owned";
assert max == null || max.isAccessible() : "Range max not owned";
assert single == null || single.isAccessible() : "Range single not owned";
}
@Override
protected void onClose() {
if (min != null && min.isAccessible()) {
min.close();
}
if (max != null && max.isAccessible()) {
max.close();
}
if (single != null && single.isAccessible()) {
single.close();
2021-06-19 13:27:58 +02:00
}
}
2021-01-30 00:24:55 +01:00
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LLRange llRange = (LLRange) o;
return LLUtils.equals(min, llRange.min) && LLUtils.equals(max, llRange.max);
2021-01-30 00:24:55 +01:00
}
@Override
public int hashCode() {
int result = LLUtils.hashCode(min);
result = 31 * result + LLUtils.hashCode(max);
2021-01-30 00:24:55 +01:00
return result;
}
@Override
public String toString() {
return new StringJoiner(", ", LLRange.class.getSimpleName() + "[", "]")
.add("min=" + LLUtils.toString(min))
.add("max=" + LLUtils.toString(max))
2021-01-30 00:24:55 +01:00
.toString();
}
2021-08-29 23:18:03 +02:00
public LLRange copy() {
2022-07-19 23:45:39 +02:00
ensureOpen();
2022-03-24 21:14:17 +01:00
// todo: use a read-only copy
2021-08-29 23:18:03 +02:00
return new LLRange(min != null ? min.copy().send() : null,
max != null ? max.copy().send() : null,
2021-10-01 19:17:33 +02:00
single != null ? single.copy().send(): null
2021-08-29 23:18:03 +02:00
);
}
2021-01-30 00:24:55 +01:00
}