Skip to content

Commit 142cd38

Browse files
committed
Fix BitmapFrequencyCounter serialization
1 parent 035db87 commit 142cd38

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

solr/core/src/java/org/apache/solr/search/facet/BitmapFrequencyCounter.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.apache.solr.search.facet;
22

3+
import java.util.ArrayList;
34
import java.util.HashMap;
5+
import java.util.List;
46
import java.util.Map;
57

68
import org.apache.solr.common.util.SimpleOrderedMap;
@@ -84,7 +86,7 @@ public void add(int value) {
8486
public SimpleOrderedMap<Object> serialize() {
8587
SimpleOrderedMap<Object> serialized = new SimpleOrderedMap<>();
8688

87-
byte[][] serializedBitmaps = new byte[bitmaps.length][];
89+
List<byte[]> serializedBitmaps = new ArrayList<>(bitmaps.length);
8890

8991
int i = 0;
9092
while (i < bitmaps.length) {
@@ -94,7 +96,7 @@ public SimpleOrderedMap<Object> serialize() {
9496
}
9597

9698
bitmap.runOptimize();
97-
serializedBitmaps[i] = BitmapUtil.bitmapToBytes(bitmap);
99+
serializedBitmaps.add(BitmapUtil.bitmapToBytes(bitmap));
98100

99101
i++;
100102
}
@@ -119,10 +121,10 @@ public SimpleOrderedMap<Object> serialize() {
119121
* @param serialized The serialized data
120122
*/
121123
public void deserialize(SimpleOrderedMap<Object> serialized) {
122-
byte[][] serializedBitmaps = (byte[][]) serialized.get("bitmaps");
124+
List<byte[]> serializedBitmaps = (List<byte[]>) serialized.get("bitmaps");
123125
if (serializedBitmaps != null) {
124-
for (int i = 0; i < bitmaps.length; i++) {
125-
bitmaps[i] = BitmapUtil.bytesToBitmap(serializedBitmaps[i]);
126+
for (int i = 0; i < serializedBitmaps.size(); i++) {
127+
bitmaps[i] = BitmapUtil.bytesToBitmap(serializedBitmaps.get(i));
126128
}
127129
}
128130

solr/core/src/test/org/apache/solr/search/facet/BitmapFrequencyCounterTest.java

+57
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package org.apache.solr.search.facet;
22

3+
import java.io.ByteArrayInputStream;
4+
import java.io.ByteArrayOutputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
38
import org.apache.lucene.util.LuceneTestCase;
9+
import org.apache.solr.common.util.JavaBinCodec;
10+
import org.apache.solr.common.util.SimpleOrderedMap;
411
import org.junit.Test;
512

613
public class BitmapFrequencyCounterTest extends LuceneTestCase {
@@ -293,4 +300,54 @@ public void givenSize4_whenMergingValues() {
293300
assertEquals(decoded[14], 0);
294301
assertEquals(decoded[15], 1);
295302
}
303+
304+
@Test
305+
public void testSerialization() throws IOException {
306+
BitmapFrequencyCounter x = new BitmapFrequencyCounter(2);
307+
308+
x.add(101);
309+
310+
x.add(102);
311+
x.add(102);
312+
x.add(202);
313+
x.add(202);
314+
315+
x.add(103);
316+
x.add(103);
317+
x.add(103);
318+
x.add(203);
319+
x.add(203);
320+
x.add(203);
321+
x.add(303);
322+
x.add(303);
323+
x.add(303);
324+
325+
JavaBinCodec codec = new JavaBinCodec();
326+
327+
ByteArrayOutputStream out = new ByteArrayOutputStream();
328+
codec.marshal(x.serialize(), out);
329+
330+
InputStream in = new ByteArrayInputStream(out.toByteArray());
331+
BitmapFrequencyCounter y = new BitmapFrequencyCounter(2);
332+
y.deserialize((SimpleOrderedMap<Object>) codec.unmarshal(in));
333+
334+
assertEquals(y.getBitmaps().length, 2);
335+
336+
assertTrue(y.getBitmaps()[0].contains(101));
337+
assertFalse(y.getBitmaps()[1].contains(101));
338+
339+
assertFalse(y.getBitmaps()[0].contains(102));
340+
assertTrue(y.getBitmaps()[1].contains(102));
341+
assertFalse(y.getBitmaps()[0].contains(202));
342+
assertTrue(y.getBitmaps()[1].contains(202));
343+
344+
assertTrue(y.getBitmaps()[0].contains(103));
345+
assertTrue(y.getBitmaps()[1].contains(103));
346+
assertTrue(y.getBitmaps()[0].contains(203));
347+
assertTrue(y.getBitmaps()[1].contains(203));
348+
assertTrue(y.getBitmaps()[0].contains(303));
349+
assertTrue(y.getBitmaps()[1].contains(303));
350+
351+
assertTrue(y.getOverflow().isEmpty());
352+
}
296353
}

0 commit comments

Comments
 (0)