From b5df73ebf4598d8d2eff3daf7102a37adaa3abd4 Mon Sep 17 00:00:00 2001 From: Richard Startin Date: Fri, 21 Dec 2018 22:13:36 +0000 Subject: [PATCH 1/2] roaring batch iterator wrapper --- pom.xml | 4 +- .../bitmap/BatchIteratorWrapperIterator.java | 85 +++++++++++++++++++ .../bitmap/WrappedImmutableRoaringBitmap.java | 2 +- .../bitmap/WrappedRoaringBitmap.java | 2 +- 4 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 processing/src/main/java/org/apache/druid/collections/bitmap/BatchIteratorWrapperIterator.java diff --git a/pom.xml b/pom.xml index ca2321486f5a..515cb05dd204 100644 --- a/pom.xml +++ b/pom.xml @@ -680,7 +680,7 @@ org.roaringbitmap RoaringBitmap - 0.5.18 + 0.7.35 org.ow2.asm @@ -979,7 +979,7 @@ org.codehaus.mojo animal-sniffer-maven-plugin - 1.15 + 1.17 check-java-api diff --git a/processing/src/main/java/org/apache/druid/collections/bitmap/BatchIteratorWrapperIterator.java b/processing/src/main/java/org/apache/druid/collections/bitmap/BatchIteratorWrapperIterator.java new file mode 100644 index 000000000000..3f82793b44fd --- /dev/null +++ b/processing/src/main/java/org/apache/druid/collections/bitmap/BatchIteratorWrapperIterator.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.collections.bitmap; + +import org.roaringbitmap.BatchIterator; +import org.roaringbitmap.IntIterator; + +/** + * Wraps a batch iterator so it can be used as if it is an IntIterator (at a performance cost) + */ +public class BatchIteratorWrapperIterator implements IntIterator +{ + private int i; + private int mark; + private int[] buffer; + private BatchIterator delegate; + + private BatchIteratorWrapperIterator(BatchIterator delegate, int i, int mark, int[] buffer) + { + this.delegate = delegate; + this.i = i; + this.mark = mark; + this.buffer = buffer; + } + + /** + * Wraps the batch iterator. + * @param delegate the batch iterator to do the actual iteration + */ + BatchIteratorWrapperIterator(BatchIterator delegate, int batchSize) + { + this(delegate, 0, -1, new int[batchSize]); + } + + @Override + public boolean hasNext() + { + if (i < mark) { + return true; + } + if (!delegate.hasNext() || (mark = delegate.nextBatch(buffer)) == 0) { + return false; + } + i = 0; + return true; + } + + @Override + public int next() + { + return buffer[i++]; + } + + @Override + public IntIterator clone() + { + try { + BatchIteratorWrapperIterator it = (BatchIteratorWrapperIterator) super.clone(); + it.delegate = delegate.clone(); + it.buffer = buffer.clone(); + return it; + } + catch (CloneNotSupportedException e) { + // won't happen + throw new IllegalStateException(); + } + } +} diff --git a/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedImmutableRoaringBitmap.java b/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedImmutableRoaringBitmap.java index 50946ca87ddc..56a613db9fc2 100644 --- a/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedImmutableRoaringBitmap.java +++ b/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedImmutableRoaringBitmap.java @@ -76,7 +76,7 @@ public String toString() @Override public IntIterator iterator() { - return bitmap.getIntIterator(); + return new BatchIteratorWrapperIterator(bitmap.getBatchIterator(), 128); } @Override diff --git a/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedRoaringBitmap.java b/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedRoaringBitmap.java index bf20138c7187..333791c0eaae 100644 --- a/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedRoaringBitmap.java +++ b/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedRoaringBitmap.java @@ -192,7 +192,7 @@ public void remove(int entry) @Override public IntIterator iterator() { - return bitmap.getIntIterator(); + return new BatchIteratorWrapperIterator(bitmap.getBatchIterator(), 128); } @Override From 41400f50b43f1f87444ee08bb47175ec58b2f6f8 Mon Sep 17 00:00:00 2001 From: Richard Startin Date: Sat, 22 Dec 2018 15:02:41 +0000 Subject: [PATCH 2/2] bump roaringbitmap, use IntIterator bridge method --- pom.xml | 2 +- .../bitmap/BatchIteratorWrapperIterator.java | 85 ------------------- .../bitmap/WrappedImmutableRoaringBitmap.java | 2 +- .../bitmap/WrappedRoaringBitmap.java | 2 +- 4 files changed, 3 insertions(+), 88 deletions(-) delete mode 100644 processing/src/main/java/org/apache/druid/collections/bitmap/BatchIteratorWrapperIterator.java diff --git a/pom.xml b/pom.xml index 8a5468f5a859..928fd58eba87 100644 --- a/pom.xml +++ b/pom.xml @@ -681,7 +681,7 @@ org.roaringbitmap RoaringBitmap - 0.7.35 + 0.7.36 org.ow2.asm diff --git a/processing/src/main/java/org/apache/druid/collections/bitmap/BatchIteratorWrapperIterator.java b/processing/src/main/java/org/apache/druid/collections/bitmap/BatchIteratorWrapperIterator.java deleted file mode 100644 index 3f82793b44fd..000000000000 --- a/processing/src/main/java/org/apache/druid/collections/bitmap/BatchIteratorWrapperIterator.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.druid.collections.bitmap; - -import org.roaringbitmap.BatchIterator; -import org.roaringbitmap.IntIterator; - -/** - * Wraps a batch iterator so it can be used as if it is an IntIterator (at a performance cost) - */ -public class BatchIteratorWrapperIterator implements IntIterator -{ - private int i; - private int mark; - private int[] buffer; - private BatchIterator delegate; - - private BatchIteratorWrapperIterator(BatchIterator delegate, int i, int mark, int[] buffer) - { - this.delegate = delegate; - this.i = i; - this.mark = mark; - this.buffer = buffer; - } - - /** - * Wraps the batch iterator. - * @param delegate the batch iterator to do the actual iteration - */ - BatchIteratorWrapperIterator(BatchIterator delegate, int batchSize) - { - this(delegate, 0, -1, new int[batchSize]); - } - - @Override - public boolean hasNext() - { - if (i < mark) { - return true; - } - if (!delegate.hasNext() || (mark = delegate.nextBatch(buffer)) == 0) { - return false; - } - i = 0; - return true; - } - - @Override - public int next() - { - return buffer[i++]; - } - - @Override - public IntIterator clone() - { - try { - BatchIteratorWrapperIterator it = (BatchIteratorWrapperIterator) super.clone(); - it.delegate = delegate.clone(); - it.buffer = buffer.clone(); - return it; - } - catch (CloneNotSupportedException e) { - // won't happen - throw new IllegalStateException(); - } - } -} diff --git a/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedImmutableRoaringBitmap.java b/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedImmutableRoaringBitmap.java index 56a613db9fc2..6ad72e67cac9 100644 --- a/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedImmutableRoaringBitmap.java +++ b/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedImmutableRoaringBitmap.java @@ -76,7 +76,7 @@ public String toString() @Override public IntIterator iterator() { - return new BatchIteratorWrapperIterator(bitmap.getBatchIterator(), 128); + return bitmap.getBatchIterator().asIntIterator(new int[128]); } @Override diff --git a/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedRoaringBitmap.java b/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedRoaringBitmap.java index 333791c0eaae..32e33ac2e1d7 100644 --- a/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedRoaringBitmap.java +++ b/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedRoaringBitmap.java @@ -192,7 +192,7 @@ public void remove(int entry) @Override public IntIterator iterator() { - return new BatchIteratorWrapperIterator(bitmap.getBatchIterator(), 128); + return bitmap.getBatchIterator().asIntIterator(new int[128]); } @Override