Skip to content

Commit 2c35303

Browse files
committed
Minor changes based on reviews.
1 parent 6c70a3c commit 2c35303

10 files changed

Lines changed: 62 additions & 59 deletions

File tree

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ under the License.
3333

3434
<groupId>org.apache.datasketches</groupId>
3535
<artifactId>datasketches-java</artifactId>
36-
<version>8.0.0-SNAPSHOT</version>
36+
<version>9.0.0-SNAPSHOT</version>
3737
<packaging>jar</packaging>
3838

3939
<name>${project.artifactId}</name>
@@ -190,7 +190,7 @@ under the License.
190190
<configuration>
191191
<rules>
192192
<requireJavaVersion>
193-
<version>[22,)</version> <!-- java.version -->
193+
<version>[24,)</version> <!-- java.version -->
194194
</requireJavaVersion>
195195
<requireMavenVersion>
196196
<version>[${maven.version},4.0.0)</version>

src/main/java/org/apache/datasketches/common/Util.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,19 @@ public static long floorPowerOf2(final long n) {
424424
return Long.highestOneBit(n);
425425
}
426426

427+
/**
428+
* This is a long integer equivalent to <i>Math.ceil(n / (double)(1 << k))</i>
429+
* where: <i>0 &lt; k &le; 6</i> and <i>n</i> is a non-negative long.
430+
* These limits are not checked for speed.
431+
* @param n the input dividend as a positive long greater than zero.
432+
* @param k the input divisor exponent of 2 as a positive integer where 0 &lt; k &le; 6.
433+
* @return the long integer equivalent to <i>Math.ceil(n / 2^k)</i>.
434+
*/
435+
public static long ceilingMultiple2expK(final long n, final int k) {
436+
final long mask = (1L << k) - 1L;
437+
return (n & mask) > 0 ? (n >>> k) + 1 : n >>> k;
438+
}
439+
427440
/**
428441
* Computes the inverse integer power of 2: 1/(2^e) = 2^(-e).
429442
* @param e a positive value between 0 and 1023 inclusive

src/main/java/org/apache/datasketches/filters/bloomfilter/BitArray.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.apache.datasketches.filters.bloomfilter;
2121

2222
import static org.apache.datasketches.common.Util.LS;
23+
import static org.apache.datasketches.common.Util.ceilingMultiple2expK;
2324

2425
import java.lang.foreign.MemorySegment;
2526

@@ -115,7 +116,7 @@ static long getSerializedSizeBytes(final long numBits) {
115116
throw new SketchesArgumentException("Requested number of bits exceeds maximum allowed. "
116117
+ "Requested: " + numBits + ", maximum: " + MAX_BITS);
117118
}
118-
final int numLongs = (int) Math.ceil(numBits / 64.0);
119+
final int numLongs = (int) ceilingMultiple2expK(numBits, 6);//Math.ceil(numBits / 64.0)
119120
return Long.BYTES * (numLongs + 2L);
120121
}
121122

src/main/java/org/apache/datasketches/filters/bloomfilter/BloomFilter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,6 @@ public byte[] toByteArray() {
793793
posSeg.setLong(seed_);
794794

795795
((HeapBitArray) bitArray_).writeToSegmentAsStream(posSeg); //option: posSeg.asSlice()
796-
System.out.println("HERE");
797796
} else {
798797
MemorySegment.copy(wseg_, JAVA_BYTE, 0, bytes, 0, (int)sizeBytes);
799798

@@ -837,7 +836,7 @@ public long[] toLongArray() {
837836
return longs;
838837
}
839838

840-
// Throws an exception with the provided message if the given condition is false
839+
// Throws an exception with the provided message if the given condition is true
841840
private static void checkArgument(final boolean condition, final String message) {
842841
if (condition) { throw new SketchesArgumentException(message); }
843842
}

src/main/java/org/apache/datasketches/filters/bloomfilter/BloomFilterBuilder.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static BloomFilter createByAccuracy(final long maxDistinctItems, final do
128128

129129
/**
130130
* Creates a BloomFilter with given number of bits and number of hash functions,
131-
* using a rnadom base seed for the hash function.
131+
* using a random base seed for the hash function.
132132
*
133133
* @param numBits The size of the BloomFilter, in bits
134134
* @param numHashes The number of hash functions to apply to items
@@ -162,8 +162,7 @@ public static BloomFilter createBySize(final long numBits, final int numHashes,
162162
*/
163163
public static BloomFilter initializeByAccuracy(
164164
final long maxDistinctItems, final double targetFalsePositiveProb, final MemorySegment dstSeg) {
165-
return initializeByAccuracy(maxDistinctItems, targetFalsePositiveProb, ThreadLocalRandom.current().nextLong(),
166-
dstSeg);
165+
return initializeByAccuracy(maxDistinctItems, targetFalsePositiveProb, ThreadLocalRandom.current().nextLong(), dstSeg);
167166
}
168167

169168
/**

src/main/java/org/apache/datasketches/filters/bloomfilter/DirectBitArray.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static java.lang.foreign.ValueLayout.JAVA_BYTE;
2323
import static java.lang.foreign.ValueLayout.JAVA_INT_UNALIGNED;
2424
import static java.lang.foreign.ValueLayout.JAVA_LONG_UNALIGNED;
25+
import static org.apache.datasketches.common.Util.ceilingMultiple2expK;
2526
import static org.apache.datasketches.common.Util.clear;
2627
import static org.apache.datasketches.common.Util.setBits;
2728

@@ -54,12 +55,12 @@ static DirectBitArray initialize(final long numBits, final MemorySegment wseg) {
5455
throw new SketchesArgumentException("Maximum size of a single filter is " + MAX_BITS + " + bits. "
5556
+ "Requested: " + numBits);
5657
}
57-
58-
final int arrayLength = (int) Math.ceil(numBits / 64.0); // we know it'll fit in an int based on above checks
58+
// we know it'll fit in an int based on above checks
59+
final int arrayLength = (int) ceilingMultiple2expK(numBits, 6);//Math.ceil(numBits / 64.0);
5960
final long requiredBytes = (2L + arrayLength) * Long.BYTES;
6061
if (wseg.byteSize() < requiredBytes) {
6162
throw new SketchesArgumentException("Provided MemorySegment too small for requested array length. "
62-
+ "Requited: " + requiredBytes + ", provided capcity: " + wseg.byteSize());
63+
+ "Required: " + requiredBytes + ", provided capacity: " + wseg.byteSize());
6364
}
6465

6566
return new DirectBitArray(arrayLength, wseg);
@@ -107,7 +108,7 @@ protected boolean isDirty() {
107108
}
108109

109110
@Override
110-
boolean getBit(final long index) {
111+
boolean getBit(final long index) { //index a bit in an array of bytes
111112
return (wseg_.get(JAVA_BYTE, DATA_OFFSET + ((int) index >>> 3)) & (1 << (index & 0x7))) != 0;
112113
}
113114

@@ -128,15 +129,15 @@ void reset() {
128129
}
129130

130131
@Override
131-
void setBit(final long index) {
132+
void setBit(final long index) { //index a bit in an array of bytes
132133
final long segmentOffset = DATA_OFFSET + ((int) index >>> 3);
133134
final byte val = wseg_.get(JAVA_BYTE, segmentOffset);
134135
setBits(wseg_, segmentOffset, (byte) (val | (1 << (index & 0x07))));
135136
setNumBitsSet(-1); // mark dirty
136137
}
137138

138139
@Override
139-
boolean getAndSetBit(final long index) {
140+
boolean getAndSetBit(final long index) { //index a bit in an array of bytes
140141
final long segmentOffset = DATA_OFFSET + ((int) index >>> 3);
141142
final byte mask = (byte) (1 << (index & 0x07));
142143
final byte val = wseg_.get(JAVA_BYTE, segmentOffset);

src/main/java/org/apache/datasketches/filters/bloomfilter/DirectBitArrayR.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected DirectBitArrayR(final int dataLength, final long storedNumBitsSet, fin
5656

5757
// assumes we have a slice with only the portion of the MemorySegment the BitArray cares about
5858
static DirectBitArrayR wrap(final MemorySegment seg, final boolean isEmpty) {
59-
final int arrayLength = seg.get(JAVA_INT_UNALIGNED, 0);
59+
final int arrayLength = seg.get(JAVA_INT_UNALIGNED, 0L);
6060
final long storedNumBitsSet = isEmpty ? 0L : seg.get(JAVA_LONG_UNALIGNED, NUM_BITS_OFFSET);
6161

6262
if (arrayLength < 0) {
@@ -66,7 +66,7 @@ static DirectBitArrayR wrap(final MemorySegment seg, final boolean isEmpty) {
6666
// required capacity is arrayLength plus room for
6767
// arrayLength (in longs) and numBitsSet
6868
if ((storedNumBitsSet != 0) && (seg.byteSize() < (arrayLength + 2))) {
69-
throw new SketchesArgumentException("MemorySegment capacity insufficient for Bloom Filter. Needed: "
69+
throw new SketchesArgumentException("MemorySegment capacity is insufficient for Bloom Filter. Needs: "
7070
+ (arrayLength + 2) + " , found: " + seg.byteSize());
7171
}
7272
return new DirectBitArrayR(arrayLength, storedNumBitsSet, seg);
@@ -94,7 +94,7 @@ int getArrayLength() {
9494
}
9595

9696
@Override
97-
boolean getBit(final long index) {
97+
boolean getBit(final long index) { //index into an array of bytes
9898
if (isEmpty()) { return false; }
9999
return (wseg_.get(JAVA_BYTE, DATA_OFFSET + ((int) index >>> 3)) & (1 << (index & 0x7))) != 0;
100100
}

src/main/java/org/apache/datasketches/filters/bloomfilter/HeapBitArray.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919

2020
package org.apache.datasketches.filters.bloomfilter;
2121

22+
import static org.apache.datasketches.common.Util.ceilingMultiple2expK;
23+
2224
import java.lang.foreign.MemorySegment;
2325
import java.util.Arrays;
2426

25-
import org.apache.datasketches.common.positional.PositionalSegment;
2627
import org.apache.datasketches.common.SketchesArgumentException;
28+
import org.apache.datasketches.common.positional.PositionalSegment;
2729

2830
/**
2931
* This class holds an array of bits suitable for use in a Bloom Filter
@@ -45,7 +47,7 @@ final class HeapBitArray extends BitArray {
4547
throw new SketchesArgumentException("Number of bits may not exceed " + MAX_BITS + ". Found: " + numBits);
4648
}
4749

48-
final int numLongs = (int) Math.ceil(numBits / 64.0);
50+
final int numLongs = (int) ceilingMultiple2expK(numBits, 6);//Math.ceil(numBits / 64.0)
4951
numBitsSet_ = 0;
5052
isDirty_ = false;
5153
data_ = new long[numLongs];
@@ -102,21 +104,21 @@ public boolean isOffHeap() {
102104
@Override
103105
public boolean isSameResource(final MemorySegment that) { return false; }
104106

105-
// queries a single bit in the array
107+
// queries a single bit in the array of longs
106108
@Override
107109
boolean getBit(final long index) {
108110
return ((data_[(int) index >>> 6] & (1L << index)) != 0);
109111
}
110112

111-
// sets a single bit in the array without querying, meaning the method
112-
// cannot properly track the number of bits set so set isDirty = true
113+
// sets a single bit in the array of longs without querying, meaning the method
114+
// cannot properly track the number of bits set, so set isDirty = true
113115
@Override
114116
void setBit(final long index) {
115117
data_[(int) index >>> 6] |= 1L << index;
116118
isDirty_ = true;
117119
}
118120

119-
// returns existing value of bit
121+
// returns existing value of bit in an array of longs
120122
@Override
121123
boolean getAndSetBit(final long index) {
122124
final int offset = (int) index >>> 6;

src/test/java/org/apache/datasketches/common/UtilTest.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,18 @@
2020
package org.apache.datasketches.common;
2121

2222
import static java.lang.Math.pow;
23+
import static org.apache.datasketches.common.TestUtil.cppPath;
24+
import static org.apache.datasketches.common.TestUtil.javaPath;
2325
import static org.apache.datasketches.common.Util.bytesToInt;
2426
import static org.apache.datasketches.common.Util.bytesToLong;
2527
import static org.apache.datasketches.common.Util.bytesToString;
26-
import static org.apache.datasketches.common.Util.ceilingPowerOf2;
28+
import static org.apache.datasketches.common.Util.ceilingMultiple2expK;
2729
import static org.apache.datasketches.common.Util.ceilingPowerBaseOfDouble;
30+
import static org.apache.datasketches.common.Util.ceilingPowerOf2;
2831
import static org.apache.datasketches.common.Util.characterPad;
2932
import static org.apache.datasketches.common.Util.checkBounds;
30-
import static org.apache.datasketches.common.Util.checkIfPowerOf2;
3133
import static org.apache.datasketches.common.Util.checkIfMultipleOf8AndGT0;
34+
import static org.apache.datasketches.common.Util.checkIfPowerOf2;
3235
import static org.apache.datasketches.common.Util.checkProbability;
3336
import static org.apache.datasketches.common.Util.convertToLongArray;
3437
import static org.apache.datasketches.common.Util.exactLog2OfInt;
@@ -39,9 +42,9 @@
3942
import static org.apache.datasketches.common.Util.invPow2;
4043
import static org.apache.datasketches.common.Util.isEven;
4144
import static org.apache.datasketches.common.Util.isLessThanUnsigned;
42-
import static org.apache.datasketches.common.Util.isPowerOf2;
4345
import static org.apache.datasketches.common.Util.isMultipleOf8AndGT0;
4446
import static org.apache.datasketches.common.Util.isOdd;
47+
import static org.apache.datasketches.common.Util.isPowerOf2;
4548
import static org.apache.datasketches.common.Util.longToBytes;
4649
import static org.apache.datasketches.common.Util.milliSecToString;
4750
import static org.apache.datasketches.common.Util.nanoSecToString;
@@ -51,8 +54,6 @@
5154
import static org.apache.datasketches.common.Util.pwr2SeriesNext;
5255
import static org.apache.datasketches.common.Util.pwr2SeriesPrev;
5356
import static org.apache.datasketches.common.Util.zeroPad;
54-
import static org.apache.datasketches.common.TestUtil.cppPath;
55-
import static org.apache.datasketches.common.TestUtil.javaPath;
5657
import static org.testng.Assert.assertEquals;
5758
import static org.testng.Assert.assertFalse;
5859
import static org.testng.Assert.assertNotNull;
@@ -78,8 +79,6 @@ public void numTrailingOnes() {
7879
final int numL1s = numberOfLeadingOnes(v);
7980
assertEquals(Long.numberOfTrailingZeros(~v), numT1s);
8081
assertEquals(Long.numberOfLeadingZeros(~v), numL1s);
81-
//println(zeroPad(Long.toBinaryString(v),64) + ", " + numL1s + ", " + numT1s);
82-
continue;
8382
}
8483
}
8584

@@ -270,7 +269,7 @@ public void checkEvenOdd() {
270269

271270
@Test
272271
public void checkBytesToInt() {
273-
final byte[] arr = new byte[] {4, 3, 2, 1};
272+
final byte[] arr = {4, 3, 2, 1};
274273
final int result = 4 + (3 << 8) + (2 << 16) + (1 << 24);
275274
Assert.assertEquals(bytesToInt(arr), result);
276275
final byte[] arr2 = intToBytes(result, new byte[4]);
@@ -279,7 +278,7 @@ public void checkBytesToInt() {
279278

280279
@Test
281280
public void checkBytesToLong() {
282-
final byte[] arr = new byte[] {8, 7, 6, 5, 4, 3, 2, 1};
281+
final byte[] arr = {8, 7, 6, 5, 4, 3, 2, 1};
283282
final long result = 8L + (7L << 8) + (6L << 16) + (5L << 24)
284283
+ (4L << 32) + (3L << 40) + (2L << 48) + (1L << 56);
285284
Assert.assertEquals(bytesToLong(arr), result);
@@ -312,7 +311,7 @@ public void checkNsecToString() {
312311

313312
@Test
314313
public void checkMsecToString() {
315-
final long nS = 60L * 60L * 1000L + 60L * 1000L + 1000L + 1L;
314+
final long nS = (60L * 60L * 1000L) + (60L * 1000L) + 1000L + 1L;
316315
final String result = milliSecToString(nS);
317316
final String expected = "1:01:01.001";
318317
Assert.assertEquals(result, expected);
@@ -413,7 +412,7 @@ public void checkExactLog2OfIntWithArg() {
413412

414413
@Test
415414
static void checkConvertToLongArray() {
416-
byte[] arr = {1,2,3,4,5,6,7,8,9,10,11,12};
415+
final byte[] arr = {1,2,3,4,5,6,7,8,9,10,11,12};
417416

418417
long[] out = convertToLongArray(arr, false);
419418
String s = zeroPad(Long.toHexString(out[0]), 16);
@@ -429,8 +428,12 @@ static void checkConvertToLongArray() {
429428
}
430429

431430
@Test
432-
public void printlnTest() {
433-
println("PRINTING: " + this.getClass().getName());
431+
static void checkCeilingMultiple2expK() {
432+
final long n = (1 << 36) - 1L;
433+
final int k = 6;
434+
final long v = ceilingMultiple2expK(n, k);
435+
final long v2 = (long)Math.ceil(n / (double)(1 << k));
436+
assertEquals(v, v2);
434437
}
435438

436439
@Test
@@ -439,7 +442,12 @@ public void checkDirCreation() {
439442
assertNotNull(cppPath);
440443
}
441444

442-
private static boolean enablePrinting = false;
445+
@Test
446+
public void printlnTest() {
447+
println("PRINTING: " + this.getClass().getName());
448+
}
449+
450+
private static boolean enablePrinting = true;
443451

444452
static void println(final Object o) {
445453
if (enablePrinting) {
@@ -452,8 +460,8 @@ static void println(final Object o) {
452460
* @param o value to print
453461
*/
454462
static void print(final Object o) {
455-
if (enablePrinting && o != null) {
456-
System.out.print(o.toString());
463+
if (enablePrinting && (o != null)) {
464+
//System.out.print(o.toString());
457465
}
458466
}
459467

src/test/java/org/apache/datasketches/common/positional/package-info.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)