CavalliumDBEngine/src/main/java/it/cavallium/dbengine/lucene/BytesRefCodec.java

25 lines
655 B
Java
Raw Normal View History

2021-10-15 22:03:53 +02:00
package it.cavallium.dbengine.lucene;
2022-03-16 13:47:56 +01:00
import io.netty5.buffer.ByteBuf;
2021-10-15 22:03:53 +02:00
import java.util.function.Function;
import org.apache.lucene.util.BytesRef;
public class BytesRefCodec implements LMDBCodec<BytesRef> {
@Override
public ByteBuf serialize(Function<Integer, ByteBuf> allocator, BytesRef data) {
var buf = allocator.apply(data.length + Integer.BYTES);
buf.writeInt(data.length);
buf.writeBytes(data.bytes, data.offset, data.length);
return buf;
}
@Override
public BytesRef deserialize(ByteBuf b) {
var length = b.readInt();
var bytes = new byte[length];
b.readBytes(bytes, 0, length);
return new BytesRef(bytes, 0, length);
}
}