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

136 lines
3.2 KiB
Java
Raw Normal View History

2020-12-07 22:15:18 +01:00
package it.cavallium.dbengine.database;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Objects;
import java.util.StringJoiner;
import org.apache.lucene.document.Field;
public class LLItem {
private final LLType type;
private final String name;
private final byte[] data;
2021-04-03 19:09:06 +02:00
public LLItem(LLType type, String name, byte[] data) {
2020-12-07 22:15:18 +01:00
this.type = type;
this.name = name;
this.data = data;
}
private LLItem(LLType type, String name, String data) {
this.type = type;
this.name = name;
this.data = data.getBytes(StandardCharsets.UTF_8);
}
private LLItem(LLType type, String name, int data) {
this.type = type;
this.name = name;
this.data = Ints.toByteArray(data);
}
private LLItem(LLType type, String name, float data) {
this.type = type;
this.name = name;
2021-04-03 19:09:06 +02:00
this.data = ByteBuffer.allocate(4).putFloat(data).array();
2020-12-07 22:15:18 +01:00
}
private LLItem(LLType type, String name, long data) {
this.type = type;
this.name = name;
this.data = Longs.toByteArray(data);
}
public static LLItem newIntPoint(String name, int data) {
return new LLItem(LLType.IntPoint, name, data);
}
public static LLItem newLongPoint(String name, long data) {
return new LLItem(LLType.LongPoint, name, data);
}
2021-11-19 19:03:31 +01:00
public static LLItem newLongStoredField(String name, long data) {
return new LLItem(LLType.LongStoredField, name, data);
}
2020-12-07 22:15:18 +01:00
public static LLItem newFloatPoint(String name, float data) {
return new LLItem(LLType.FloatPoint, name, data);
}
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);
}
}
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);
}
2020-12-07 22:15:18 +01:00
public String getName() {
return name;
}
public LLType getType() {
return type;
}
public byte[] getData() {
return data;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LLItem llItem = (LLItem) o;
return type == llItem.type &&
Objects.equals(name, llItem.name) &&
2021-04-03 19:09:06 +02:00
Arrays.equals(data, llItem.data);
2020-12-07 22:15:18 +01:00
}
@Override
public int hashCode() {
int result = Objects.hash(type, name);
result = 31 * result + Arrays.hashCode(data);
return result;
}
@Override
public String toString() {
var sj = new StringJoiner(", ", "[", "]")
.add("type=" + type)
.add("name='" + name + "'");
if (data != null && data.length > 0) {
2021-04-03 19:09:06 +02:00
sj.add("data=" + new String(data));
2020-12-07 22:15:18 +01:00
}
2021-04-03 19:09:06 +02:00
return sj.toString();
2020-12-07 22:15:18 +01:00
}
public String stringValue() {
return new String(data, StandardCharsets.UTF_8);
}
}