Skip to content

Commit

Permalink
the-qa-companyGH-573 make code more robust to various failure scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
hmottestad committed Jan 15, 2025
1 parent f02c363 commit 5981d20
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class Bitmap375Big extends Bitmap64Big {

private static final Logger logger = LoggerFactory.getLogger(Bitmap375Big.class);

private static final boolean oldBinarySearch;
public static final boolean oldBinarySearch;

static {
// check if the system property "useOldBinarySeearch" is set to true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.the_qa_company.qendpoint.core.util.disk;

import com.the_qa_company.qendpoint.core.compact.bitmap.Bitmap375Big;
import com.the_qa_company.qendpoint.core.util.BitUtil;

public abstract class AbstractLongArray implements LongArray {
Expand All @@ -26,7 +27,7 @@ public abstract class AbstractLongArray implements LongArray {
private final long[] estimatedLocationMin = new long[ESTIMATED_LOCATION_ARRAY_SIZE];
private final long[] estimatedLocation = new long[ESTIMATED_LOCATION_ARRAY_SIZE];

private long estimatedLocationBucketSize;
private long estimatedLocationBucketSize = 1;

long maxValue = 1;

Expand All @@ -38,6 +39,7 @@ public long getEstimatedLocationArrayBucketSize() {
void updateEstimatedLocationArrayBucketSize() {
// this.estimatedLocationBucketSize = maxValue/ESTIMATED_LOCATION_ARRAY_SIZE+1;
this.estimatedLocationBucketSize = ((1L << BitUtil.log2(maxValue)) - 1) / ESTIMATED_LOCATION_ARRAY_SIZE + 1;
assert this.estimatedLocationBucketSize > 0;
}

@Override
Expand All @@ -57,17 +59,32 @@ public long[] getEstimatedLocationArrayMax() {

@Override
public void recalculateEstimatedValueLocation() {
if (Bitmap375Big.oldBinarySearch) {
return;
}

updateEstimatedLocationArrayBucketSize();
long estimatedLocationBucketSize = getEstimatedLocationArrayBucketSize();
long len = length();

for (long i = 0; i < len; i++) {
long val = get(i);
if (val == 0) {
// val shouldn't be zero, since this represents a value that
// does not exist
continue;
}

int index = (int) (val / estimatedLocationBucketSize);
int index = getEstimatedLocationIndex(val);
if (index >= estimatedLocation.length || index >= estimatedLocationMax.length
|| index >= estimatedLocationMin.length) {
logger.warn("Index out of bounds for " + getClass().getSimpleName()
+ " when recalculateEstimatedValueLocation for value " + val + " and estimatedBucketSize "
+ estimatedLocationBucketSize + " and index " + index + " and estimatedLocation.length "
+ estimatedLocation.length + " and estimatedLocationMax.length " + estimatedLocationMax.length
+ " and estimatedLocationMin.length " + estimatedLocationMin.length);
continue;
}
estimatedLocationMax[index] = Math.max(estimatedLocationMax[index], i);
if (estimatedLocationMin[index] == 0) {
estimatedLocationMin[index] = i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,12 @@ default long getEstimatedLocationArrayBucketSize() {
}

default long getEstimatedLocationLowerBound(long val) {
int index = (int) (val / getEstimatedLocationArrayBucketSize());
if (index - 1 >= 0) {
long t = getEstimatedLocationArrayMax()[index - 1];
int index = getEstimatedLocationIndex(val);
// move to the index below to get the estimated lower bound
index -= 1;
long[] estimatedLocationArrayMax = getEstimatedLocationArrayMax();
if (index >= 0 && index < estimatedLocationArrayMax.length) {
long t = estimatedLocationArrayMax[index];
if (t > 0) {
return t;
}
Expand All @@ -254,10 +257,12 @@ default long getEstimatedLocationLowerBound(long val) {
}

default long getEstimatedLocationUpperBound(long val) {
int index = (int) (val / getEstimatedLocationArrayBucketSize());
int index = getEstimatedLocationIndex(val);
// move to the index above to get the upper bound
index += 1;
long[] estimatedLocationMin = getEstimatedLocationArrayMin();
if (index + 1 < estimatedLocationMin.length) {
long t = estimatedLocationMin[index + 1];
if (index < estimatedLocationMin.length && index >= 0) {
long t = estimatedLocationMin[index];
if (t > 0) {
return Math.min(length(), t);
}
Expand All @@ -267,10 +272,10 @@ default long getEstimatedLocationUpperBound(long val) {
}

default long getEstimatedLocation(long val, long min, long max) {
int index = (int) (val / getEstimatedLocationArrayBucketSize());
int index = getEstimatedLocationIndex(val);
var estimatedLocation = getEstimatedLocationArray();

if (index >= estimatedLocation.length) {
if (index >= estimatedLocation.length || index < 0) {
return (min + max) / 2;
}
long t = estimatedLocation[index];
Expand All @@ -281,15 +286,23 @@ default long getEstimatedLocation(long val, long min, long max) {
}
}

default int getEstimatedLocationIndex(long val) {
long estimatedLocationArrayBucketSize = getEstimatedLocationArrayBucketSize();
if (estimatedLocationArrayBucketSize <= 0) {
return Integer.MIN_VALUE;
}
return (int) (val / getEstimatedLocationArrayBucketSize());
}

default void recalculateEstimatedValueLocation() {
logger.info("Class {} does not support recalculateEstimatedValueLocation()",
this.getClass().getCanonicalName());
}

default void updateEstimatedValueLocation(long val, long min) {
int index = (int) (val / getEstimatedLocationArrayBucketSize());
int index = getEstimatedLocationIndex(val);
long[] estimatedLocation = getEstimatedLocationArray();
if (index >= estimatedLocation.length) {
if (index >= estimatedLocation.length || index < 0) {
return;
}
estimatedLocation[index] = min;
Expand Down

0 comments on commit 5981d20

Please sign in to comment.