Browse Source

Pull request #2841: KERNEL-8448 提供RoaringBitmap兼容实现

Merge in CORE/base-third from ~LOY/base-third:feature/10.0 to feature/10.0

* commit '61f0da25748ac54f436a9211687a105ce350d350':
  修改信息
  修复bug
  KERNEL-8448 提供RoaringBitmap兼容实现
feature/10.0
loy 3 years ago
parent
commit
90f2636232
  1. 1
      base-third-project/base-third-step1/pom.xml
  2. 3
      build.third_step1-jdk11.gradle
  3. 3
      build.third_step1.gradle
  4. 18
      fine-roaringbitmap/pom.xml
  5. 194
      fine-roaringbitmap/src/main/java/com/fr/third/bitmap/roaringbitmap/RoaringBitmap.java

1
base-third-project/base-third-step1/pom.xml

@ -37,6 +37,7 @@
<module>../../fine-lucene</module>
<module>../../fine-lz4</module>
<module>../../fine-org-dom4j</module>
<module>../../fine-roaringbitmap</module>
<module>../../fine-sense4</module>
<module>../../fine-deprecated-utils</module>
<module>../../fine-third-default</module>

3
build.third_step1-jdk11.gradle

@ -54,6 +54,7 @@ sourceSets{
"${srcDir}/fine-lucene/src/main/java",
"${srcDir}/fine-lz4/src/main/java",
"${srcDir}/fine-org-dom4j/src/main/java",
"${srcDir}/fine-roaringbitmap/src/main/java",
"${srcDir}/fine-sense4/src/main/java",
"${srcDir}/fine-third-default/fine-mail/src/main/java",
"${srcDir}/fine-third-default/fine-sun-misc/src/main/java",
@ -113,6 +114,8 @@ def resourceDirs = [
"${srcDir}/fine-lz4/src/main/resources",
"${srcDir}/fine-org-dom4j/src/main/java",
"${srcDir}/fine-org-dom4j/src/main/resources",
"${srcDir}/fine-roaringbitmap/src/main/java",
"${srcDir}/fine-roaringbitmap/src/main/resources",
"${srcDir}/fine-sense4/src/main/java",
"${srcDir}/fine-sense4/src/main/resources",
"${srcDir}/fine-third-default/fine-mail/src/main/java",

3
build.third_step1.gradle

@ -55,6 +55,7 @@ sourceSets{
"${srcDir}/fine-lucene/src/main/java",
"${srcDir}/fine-lz4/src/main/java",
"${srcDir}/fine-org-dom4j/src/main/java",
"${srcDir}/fine-roaringbitmap/src/main/java",
"${srcDir}/fine-sense4/src/main/java",
"${srcDir}/fine-third-default/fine-mail/src/main/java",
"${srcDir}/fine-third-default/fine-sun-misc/src/main/java",
@ -156,6 +157,8 @@ task copyFiles(type:Copy,dependsOn:'compileJava'){
with dataContent.call("${srcDir}/fine-lz4/src/main/resources")
with dataContent.call("${srcDir}/fine-org-dom4j/src/main/java")
with dataContent.call("${srcDir}/fine-org-dom4j/src/main/resources")
with dataContent.call("${srcDir}/fine-roaringbitmap/src/main/java")
with dataContent.call("${srcDir}/fine-roaringbitmap/src/main/resources")
with dataContent.call("${srcDir}/fine-sense4/src/main/java")
with dataContent.call("${srcDir}/fine-sense4/src/main/resources")
with dataContent.call("${srcDir}/fine-third-default/fine-sun-misc/src/main/java")

18
fine-roaringbitmap/pom.xml

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.fr.third</groupId>
<artifactId>step1</artifactId>
<version>${revision}</version>
<relativePath>../base-third-project/base-third-step1</relativePath>
</parent>
<artifactId>fine-roaringbitmap</artifactId>
<version>${revision}</version>
</project>

194
fine-roaringbitmap/src/main/java/com/fr/third/bitmap/roaringbitmap/RoaringBitmap.java

@ -0,0 +1,194 @@
package com.fr.third.bitmap.roaringbitmap;
import java.util.Iterator;
/**
* Created by loy on 2021/7/13.
*
* <p>兼容新引擎插件中用到的这个类后续会删除
*/
@Deprecated
public class RoaringBitmap {
private byte[] value;
private int length;
private int cap;
public RoaringBitmap() {
this(8192);
}
public RoaringBitmap(int cap) {
this.cap = cap;
this.value = new byte[(cap + 7) / 8];
}
public void add(int n) {
checkIndex(n);
ensureCap(n);
int idx0 = n / 8;
int idx1 = n % 8;
if ((value[idx0] & (1 << idx1)) == 0) {
length++;
value[idx0] |= (1 << idx1);
}
}
public void remove(int n) {
checkIndex(n);
if (n >= value.length * 8) {
return;
}
int idx0 = n / 8;
int idx1 = n % 8;
if ((value[idx0] & (1 << idx1)) != 0) {
length--;
value[idx0] &= ~(1 << idx1);
}
}
public boolean contains(int n) {
checkIndex(n);
if (n >= value.length * 8) {
return false;
}
int idx0 = n / 8;
int idx1 = n % 8;
return (value[idx0] & (1 << idx1)) != 0;
}
public void flip(long rangeStart, long rangeEnd) {
int sIdx0 = (int) ((rangeStart + 7) / 8);
int sIdx1 = (int) (rangeStart % 8);
int eIdx0 = (int) (rangeEnd / 8);
int eIdx1 = (int) (rangeEnd % 8);
int lenDiff = 0;
if (sIdx1 > 0) {
byte b = value[sIdx0 - 1];
byte b2 = (byte) (~(b & 0xff) >> sIdx1 << sIdx1 + ((b & 0xff) << (32 - sIdx1) >> (32 - sIdx1)));
value[sIdx0 - 1] = b2;
lenDiff += countInByte(b2) - countInByte(b);
}
for (int i = sIdx0; i < eIdx0; i++) {
value[i] = (byte) ~(value[i]);
lenDiff += countInByte(value[i]) * 2 - 8;
}
if (eIdx1 > 0) {
byte b = value[eIdx0];
byte b2 = (byte) (((b & 0xff) >> eIdx1 << eIdx1) + ~(b & 0xff) << (32 - eIdx1) >> (32 - eIdx1));
value[eIdx0] = b2;
lenDiff += countInByte(b2) - countInByte(b);
}
length += lenDiff;
}
public int getCardinality() {
return length;
}
@Override
public RoaringBitmap clone() {
RoaringBitmap bm = new RoaringBitmap(cap);
bm.length = length;
bm.value = new byte[value.length];
System.arraycopy(value, 0, bm.value, 0, value.length);
return bm;
}
public static RoaringBitmap and(RoaringBitmap bm1, RoaringBitmap bm2) {
int len = Integer.min(bm1.value.length, bm2.value.length);
RoaringBitmap bm = new RoaringBitmap(Integer.max(bm1.cap, bm2.cap));
bm.value = new byte[len];
for (int i = 0; i < len; i++) {
byte b = (byte) (bm1.value[i] & bm2.value[i]);
if (b == 0) {
continue;
}
bm.value[i] = b;
bm.length += countInByte(b);
}
return bm;
}
public static int andCardinality(RoaringBitmap bm1, RoaringBitmap bm2) {
int len = Integer.min(bm1.value.length, bm2.value.length);
int count = 0;
for (int i = 0; i < len; i++) {
byte b = (byte) (bm1.value[i] & bm2.value[i]);
if (b == 0) {
continue;
}
count += countInByte(b);
}
return count;
}
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
private int idx0 = 0;
private int idx1 = 0;
@Override
public boolean hasNext() {
while (idx0 < value.length) {
if (value[idx0] != 0) {
byte b = value[idx0];
while (idx1 < 8) {
if ((b & (1 << idx1)) != 0) {
return true;
}
idx1++;
}
}
idx0++;
idx1 = 0;
}
return false;
}
@Override
public Integer next() {
if (hasNext()) {
int v = idx0 * 8 + idx1;
idx1++;
if (idx1 >= 8) {
idx1 = 0;
idx0++;
}
return v;
}
return null;
}
};
}
private void ensureCap(int n) {
int idx = (n + 7) / 8;
int len = value.length;
while (len < idx) {
len <<= 1;
}
if (len > value.length) {
byte[] value2 = new byte[len];
System.arraycopy(value, 0, value2, 0, value.length);
value = value2;
}
}
private void checkIndex(int n) {
if (n < 0) {
throw new ArrayIndexOutOfBoundsException();
}
}
private static int countInByte(byte b) {
int count = 0;
while (b != 0) {
if ((b & 0x01) == 0x01) {
count++;
}
b = (byte) ((b & 0xff) >> 1);
}
return count;
}
}
Loading…
Cancel
Save