From 05354499ebe923034e009cdf1082513fa4b7aca5 Mon Sep 17 00:00:00 2001 From: noblepaul Date: Tue, 10 Dec 2024 11:53:55 +1100 Subject: [PATCH] added support for primitive arrays --- .../apache/solr/common/util/JavaBinCodec.java | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java b/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java index 77e7186c9de..c390847810a 100644 --- a/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java +++ b/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java @@ -103,6 +103,7 @@ public class JavaBinCodec implements PushWriter { MAP_ENTRY = 19, UUID = 20, // This is reserved to be used only in LogCodec // types that combine tag + length (or other info) in a single byte + PRIMITIVE_ARR = 21, TAG_AND_LEN = (byte) (1 << 5), STR = (byte) (1 << 5), SINT = (byte) (2 << 5), @@ -348,6 +349,8 @@ protected Object readObject(DataInputInputStream dis) throws IOException { return readMapEntry(dis); case MAP_ENTRY_ITER: return readMapIter(dis); + case PRIMITIVE_ARR: + return readPrimitiveArray(dis); } throw new RuntimeException("Unknown type " + tagByte); @@ -438,9 +441,129 @@ public boolean writeKnownType(Object val) throws IOException { writeBoolean(((AtomicBoolean) val).get()); return true; } + if (val instanceof float[]) { + writeFloatArr((float[]) val); + return true; + } + if (val instanceof int[]) { + writeIntArr((int[]) val); + return true; + } + if (val instanceof long[]) { + writeLongArr((long[]) val); + return true; + } + if (val instanceof double[]) { + writeDoubleArr((double[]) val); + return true; + } + if (val instanceof short[]) { + writeShortArr((short[]) val); + return true; + } return false; } + public Object readPrimitiveArray(DataInputInputStream dis) throws IOException { + tagByte = dis.readByte(); + int len = readVInt(dis); + switch (tagByte) { + case FLOAT: + { + float[] v = new float[len]; + for (int i = 0; i < len; i++) { + v[i] = dis.readFloat(); + } + return v; + } + case INT: + { + int[] v = new int[len]; + for (int i = 0; i < len; i++) { + v[i] = dis.readInt(); + } + return v; + } + + case LONG: + { + long[] v = new long[len]; + for (int i = 0; i < len; i++) { + v[i] = dis.readLong(); + } + return v; + } + case DOUBLE: + { + double[] v = new double[len]; + for (int i = 0; i < len; i++) { + v[i] = dis.readDouble(); + } + return v; + } + case SHORT: + { + short[] v = new short[len]; + for (int i = 0; i < len; i++) { + v[i] = dis.readShort(); + } + return v; + } + case BYTE: + { + // it should be possible to serialize byte[] in the new format as well + byte[] v = new byte[len]; + dis.readFully(v); + return v; + } + default: + { + throw new RuntimeException("Invalid type : " + tagByte); + } + } + } + + public void writePrimitiveArrHeader(byte tag, int len) throws IOException { + writeTag(PRIMITIVE_ARR); + writeTag(tag); + writeVInt(len, daos); + } + + public void writeFloatArr(float[] vals) throws IOException { + writePrimitiveArrHeader(FLOAT, vals.length); + for (float f : vals) { + daos.writeFloat(f); + } + } + + public void writeIntArr(int[] vals) throws IOException { + writePrimitiveArrHeader(INT, vals.length); + for (int i : vals) { + daos.writeInt(i); + } + } + + public void writeDoubleArr(double[] vals) throws IOException { + writePrimitiveArrHeader(DOUBLE, vals.length); + for (double d : vals) { + daos.writeDouble(d); + } + } + + public void writeLongArr(long[] vals) throws IOException { + writePrimitiveArrHeader(LONG, vals.length); + for (long l : vals) { + daos.writeLong(l); + } + } + + public void writeShortArr(short[] vals) throws IOException { + writePrimitiveArrHeader(SHORT, vals.length); + for (short l : vals) { + daos.writeShort(l); + } + } + public class BinEntryWriter implements MapWriter.EntryWriter { @Override public MapWriter.EntryWriter put(CharSequence k, Object v) throws IOException {