Skip to content

Commit

Permalink
added boolean[] support and testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
noblepaul committed Dec 18, 2024
1 parent 0535449 commit 1e30707
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
41 changes: 31 additions & 10 deletions solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -441,24 +441,28 @@ public boolean writeKnownType(Object val) throws IOException {
writeBoolean(((AtomicBoolean) val).get());
return true;
}
if (val instanceof float[]) {
writeFloatArr((float[]) val);
if (val instanceof float[] ff) {
writeFloatArr(ff);
return true;
}
if (val instanceof int[]) {
writeIntArr((int[]) val);
if (val instanceof int[] ii) {
writeIntArr(ii);
return true;
}
if (val instanceof long[]) {
writeLongArr((long[]) val);
if (val instanceof long[] ll) {
writeLongArr(ll);
return true;
}
if (val instanceof double[]) {
writeDoubleArr((double[]) val);
if (val instanceof double[] dd) {
writeDoubleArr(dd);
return true;
}
if (val instanceof short[]) {
writeShortArr((short[]) val);
if (val instanceof short[] ss) {
writeShortArr(ss);
return true;
}
if (val instanceof boolean[] bb) {
writeBoolArr(bb);
return true;
}
return false;
Expand Down Expand Up @@ -509,6 +513,16 @@ public Object readPrimitiveArray(DataInputInputStream dis) throws IOException {
}
return v;
}
case BOOL_TRUE:
case BOOL_FALSE:
{
boolean[] v = new boolean[len];
for (int i = 0; i < len; i++) {
byte b = dis.readByte();
v[i] = b == BOOL_FALSE ? false : true;
}
return v;
}
case BYTE:
{
// it should be possible to serialize byte[] in the new format as well
Expand Down Expand Up @@ -557,6 +571,13 @@ public void writeLongArr(long[] vals) throws IOException {
}
}

public void writeBoolArr(boolean[] vals) throws IOException {
writePrimitiveArrHeader(BOOL_TRUE, vals.length);
for (boolean b : vals) {
writeBoolean(b);
}
}

public void writeShortArr(short[] vals) throws IOException {
writePrimitiveArrHeader(SHORT, vals.length);
for (short l : vals) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ public static SolrDocument generateSolrDocumentWithChildDocs() {
return parentDocument;
}

@Test
public void testPrimitiveArrays() throws Exception {
List<Object> types = new ArrayList<>();

types.add(new float[] {1.0678f, 4.094565f, 0.000456f});
types.add(new double[] {1.0678d, 4.094565d, 0.000456d});
types.add(new int[] {145543, 4546354, 9789857});
types.add(new long[] {145543L, 4546354L, 9789857L});
types.add(new short[] {43, 454, 857});
types.add(new boolean[] {true, true, false});

compareObjects((List<?>) getObject(getBytes(types)), types);
}

private List<Object> generateAllDataTypes() {
List<Object> types = new ArrayList<>();

Expand Down Expand Up @@ -223,6 +237,23 @@ private void compareObjects(List<?> unmarshalledObj, List<?> matchObj) {
} else if (unmarshalledObj.get(i) instanceof SolrInputField
&& matchObj.get(i) instanceof SolrInputField) {
assertTrue(assertSolrInputFieldEquals(unmarshalledObj.get(i), matchObj.get(i)));
} else if (unmarshalledObj.get(i) instanceof float[] a
&& matchObj.get(i) instanceof float[] e) {
assertArrayEquals(e, a, 0.000000f);
} else if (unmarshalledObj.get(i) instanceof double[] a
&& matchObj.get(i) instanceof double[] e) {
assertArrayEquals(e, a, 0.000000d);
} else if (unmarshalledObj.get(i) instanceof long[] a
&& matchObj.get(i) instanceof long[] e) {
assertArrayEquals(e, a);
} else if (unmarshalledObj.get(i) instanceof int[] a && matchObj.get(i) instanceof int[] e) {
assertArrayEquals(e, a);
} else if (unmarshalledObj.get(i) instanceof short[] a
&& matchObj.get(i) instanceof short[] e) {
assertArrayEquals(e, a);
} else if (unmarshalledObj.get(i) instanceof boolean[] a
&& matchObj.get(i) instanceof boolean[] e) {
assertArrayEquals(e, a);
} else {
assertEquals(unmarshalledObj.get(i), matchObj.get(i));
}
Expand Down

0 comments on commit 1e30707

Please sign in to comment.