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

254 lines
5.8 KiB
Java
Raw Normal View History

2020-12-07 22:15:18 +01:00
package it.cavallium.dbengine.database;
2022-02-22 02:10:36 +01:00
import com.google.common.primitives.Floats;
2020-12-07 22:15:18 +01:00
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
2022-02-22 02:10:36 +01:00
import java.nio.Buffer;
2020-12-07 22:15:18 +01:00
import java.nio.ByteBuffer;
2022-02-22 02:10:36 +01:00
import java.nio.FloatBuffer;
2020-12-07 22:15:18 +01:00
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Objects;
import java.util.StringJoiner;
import org.apache.lucene.document.Field;
2022-02-25 15:46:32 +01:00
import org.apache.lucene.document.LongPoint;
2022-02-23 00:49:29 +01:00
import org.apache.lucene.index.VectorSimilarityFunction;
2022-02-25 15:46:32 +01:00
import org.apache.lucene.util.BytesRef;
2020-12-07 22:15:18 +01:00
public class LLItem {
private final LLType type;
private final String name;
2022-02-22 02:10:36 +01:00
private final Object data;
2020-12-07 22:15:18 +01:00
2022-02-22 02:10:36 +01:00
public LLItem(LLType type, String name, ByteBuffer data) {
2020-12-07 22:15:18 +01:00
this.type = type;
this.name = name;
this.data = data;
}
2022-02-25 15:46:32 +01:00
public LLItem(LLType type, String name, BytesRef data) {
this.type = type;
this.name = name;
this.data = data;
}
2022-02-23 00:49:29 +01:00
public LLItem(LLType type, String name, KnnFieldData data) {
this.type = type;
this.name = name;
this.data = data;
}
2020-12-07 22:15:18 +01:00
private LLItem(LLType type, String name, String data) {
this.type = type;
this.name = name;
2022-02-22 02:10:36 +01:00
this.data = data;
2020-12-07 22:15:18 +01:00
}
private LLItem(LLType type, String name, int data) {
this.type = type;
this.name = name;
2022-02-22 02:10:36 +01:00
this.data = data;
2020-12-07 22:15:18 +01:00
}
private LLItem(LLType type, String name, float data) {
this.type = type;
this.name = name;
2022-02-22 02:10:36 +01:00
this.data = data;
2020-12-07 22:15:18 +01:00
}
private LLItem(LLType type, String name, long data) {
this.type = type;
this.name = name;
2022-02-22 02:10:36 +01:00
this.data = data;
2020-12-07 22:15:18 +01:00
}
2022-02-06 19:29:23 +01:00
private LLItem(LLType type, String name, int... data) {
this.type = type;
this.name = name;
2022-02-22 02:10:36 +01:00
this.data = data;
}
private LLItem(LLType type, String name, float... data) {
this.type = type;
this.name = name;
this.data = data;
}
private LLItem(LLType type, String name, double... data) {
this.type = type;
this.name = name;
this.data = data;
2022-02-06 19:29:23 +01:00
}
private LLItem(LLType type, String name, long... data) {
this.type = type;
this.name = name;
2022-02-22 02:10:36 +01:00
this.data = data;
2022-02-06 19:29:23 +01:00
}
2020-12-07 22:15:18 +01:00
public static LLItem newIntPoint(String name, int data) {
return new LLItem(LLType.IntPoint, name, data);
}
2022-02-06 19:29:23 +01:00
public static LLItem newIntPointND(String name, int... data) {
return new LLItem(LLType.IntPointND, name, data);
}
2020-12-07 22:15:18 +01:00
public static LLItem newLongPoint(String name, long data) {
return new LLItem(LLType.LongPoint, name, data);
}
2022-02-22 02:10:36 +01:00
public static LLItem newFloatPoint(String name, float data) {
return new LLItem(LLType.FloatPoint, name, data);
}
public static LLItem newDoublePoint(String name, double data) {
return new LLItem(LLType.DoublePoint, name, data);
}
2022-02-06 19:29:23 +01:00
public static LLItem newLongPointND(String name, long... data) {
return new LLItem(LLType.LongPointND, name, data);
}
2022-02-22 02:10:36 +01:00
public static LLItem newFloatPointND(String name, float... data) {
return new LLItem(LLType.FloatPointND, name, data);
2021-11-19 19:03:31 +01:00
}
2022-02-22 02:10:36 +01:00
public static LLItem newDoublePointND(String name, double... data) {
return new LLItem(LLType.DoublePointND, name, data);
}
public static LLItem newLongStoredField(String name, long data) {
return new LLItem(LLType.LongStoredField, name, data);
2020-12-07 22:15:18 +01:00
}
2022-02-25 15:46:32 +01:00
public static LLItem newLongStoredFieldND(String name, long... data) {
BytesRef packed = LongPoint.pack(data);
return new LLItem(LLType.BytesStoredField, name, packed);
}
2020-12-07 22:15:18 +01:00
public static LLItem newTextField(String name, String data, Field.Store store) {
if (store == Field.Store.YES) {
return new LLItem(LLType.TextFieldStored, name, data);
} else {
return new LLItem(LLType.TextField, name, data);
}
}
public static LLItem newStringField(String name, String data, Field.Store store) {
if (store == Field.Store.YES) {
return new LLItem(LLType.StringFieldStored, name, data);
} else {
return new LLItem(LLType.StringField, name, data);
}
}
2022-03-15 11:46:00 +01:00
public static LLItem newStringField(String name, BytesRef bytesRef, Field.Store store) {
if (store == Field.Store.YES) {
return new LLItem(LLType.StringFieldStored, name, bytesRef);
} else {
return new LLItem(LLType.StringField, name, bytesRef);
}
}
2020-12-07 22:15:18 +01:00
public static LLItem newSortedNumericDocValuesField(String name, long data) {
return new LLItem(LLType.SortedNumericDocValuesField, name, data);
}
2021-11-20 16:09:00 +01:00
public static LLItem newNumericDocValuesField(String name, long data) {
return new LLItem(LLType.NumericDocValuesField, name, data);
}
2022-02-23 00:49:29 +01:00
public static LLItem newKnnField(String name, KnnFieldData knnFieldData) {
return new LLItem(LLType.NumericDocValuesField, name, knnFieldData);
}
2020-12-07 22:15:18 +01:00
public String getName() {
return name;
}
public LLType getType() {
return type;
}
2022-02-22 02:10:36 +01:00
public Object getData() {
2020-12-07 22:15:18 +01:00
return data;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
2022-02-22 02:10:36 +01:00
2020-12-07 22:15:18 +01:00
LLItem llItem = (LLItem) o;
2022-02-22 02:10:36 +01:00
if (type != llItem.type) {
return false;
}
return Objects.equals(name, llItem.name);
2020-12-07 22:15:18 +01:00
}
@Override
public int hashCode() {
2022-02-22 02:10:36 +01:00
int result = type != null ? type.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
2020-12-07 22:15:18 +01:00
return result;
}
@Override
public String toString() {
2022-02-22 02:10:36 +01:00
return new StringJoiner(", ", LLItem.class.getSimpleName() + "[", "]")
2020-12-07 22:15:18 +01:00
.add("type=" + type)
2022-02-22 02:10:36 +01:00
.add("name='" + name + "'")
.add("data=" + data)
.toString();
}
public int intData() {
return (int) data;
}
public int[] intArrayData() {
return (int[]) data;
}
public long longData() {
return (long) data;
}
public long[] longArrayData() {
return (long[]) data;
}
public float floatData() {
return (float) data;
}
public float[] floatArrayData() {
return (float[]) data;
}
public double doubleData() {
return (double) data;
}
public double[] doubleArrayData() {
return (double[]) data;
2020-12-07 22:15:18 +01:00
}
2022-02-23 00:49:29 +01:00
public KnnFieldData knnFieldData() {
return (KnnFieldData) data;
}
2020-12-07 22:15:18 +01:00
public String stringValue() {
2022-02-22 02:10:36 +01:00
return (String) data;
2020-12-07 22:15:18 +01:00
}
2022-02-23 00:49:29 +01:00
public record KnnFieldData(float[] data, VectorSimilarityFunction vectorSimilarityFunction) {}
2020-12-07 22:15:18 +01:00
}