Skip to content
Permalink
Browse files
Fixes problems found when running latest version of SpotBugs.
  • Loading branch information
leerho committed Jan 11, 2022
1 parent 98489ec commit 9b860f75de65c7db6cc0b607073e1cbb94201dfc
Show file tree
Hide file tree
Showing 21 changed files with 236 additions and 54 deletions.
@@ -92,12 +92,23 @@ See the pom.xml file for test dependencies.

Building and running tests using JDK 8 should not be a problem.

However, with JDK 9+, and Eclipse version up to and including 4.22.0 (2021-12), Eclipse fails to translate the required JPMS JVM arguments specified in the POM into the *.classpath* file, causing illegal reflection access errors.
However, with JDK 9+, and Eclipse versions up to and including 4.22.0 (2021-12), Eclipse fails to translate the required JPMS JVM arguments specified in the POM compiler or surefire plugins into the *.classpath* file, causing illegal reflection access errors
[eclipse-m2e/m2e-core Bug 543631](https://github.com/eclipse-m2e/m2e-core/issues/129).

There are two ways to fix this:

#### Manually update *.classpath* file:
Open the *.classpath* file in a text editor and insert the following *classpathentry* element (this assumes JDK11, change to suit) then *refresh*.:
#### Method 1: Manually update *.classpath* file:
Open the *.classpath* file in a text editor and find the following *classpathentry* element (this assumes JDK11, change to suit):

```
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
```
Then edit it as follows:

```
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
@@ -109,8 +120,9 @@ Open the *.classpath* file in a text editor and insert the following *classpathe
</attributes>
</classpathentry>
```
Finally, *refresh*.

#### Manually update *Module Dependencies*
#### Method 2: Manually update *Module Dependencies*

In Eclipse, open the project *Properties / Java Build Path / Module Dependencies ...*

@@ -33,6 +33,7 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;

/**
* Common utility functions.
@@ -846,13 +847,15 @@ public static boolean isLessThanUnsigned(final long n1, final long n2) {
* @return the absolute path of the given resource file's shortName.
*/
public static String getResourcePath(final String shortFileName) {
Objects.requireNonNull(shortFileName, "input parameter " + shortFileName + " cannot be null.");
try {
final URL url = Util.class.getClassLoader().getResource(shortFileName);
Objects.requireNonNull(url, "resource " + shortFileName + " could not be acquired.");
final URI uri = url.toURI();
//decodes any special characters
final String path = uri.isAbsolute() ? Paths.get(uri).toAbsolutePath().toString() : uri.getPath();
return path;
} catch (final NullPointerException | URISyntaxException e) {
} catch (final URISyntaxException e) {
throw new SketchesArgumentException("Cannot find resource: " + shortFileName + LS + e);
}
}
@@ -82,6 +82,23 @@ public FdtSketch(final double threshold, final double rse) {
super(computeLgK(threshold, rse));
}

/**
* Copy Constructor
* @param sketch the sketch to copy
*/
public FdtSketch(final FdtSketch sketch) {
super(sketch);
}

/**
* @return a deep copy of this sketch
*/
@SuppressWarnings("unchecked")
@Override
public FdtSketch copy() {
return new FdtSketch(this);
}

/**
* Update the sketch with the given string array tuple.
* @param tuple the given string array tuple.
@@ -58,7 +58,7 @@ public class PostProcessor {
* @param sep the separator character
*/
public PostProcessor(final FdtSketch sketch, final Group group, final char sep) {
this.sketch = sketch;
this.sketch = sketch.copy();
this.sep = sep;
final int numEntries = sketch.getRetainedEntries();
mapArrSize = ceilingPowerOf2((int)(numEntries / 0.75));
@@ -42,6 +42,7 @@
import static org.apache.datasketches.quantiles.Util.computeBitPattern;

import org.apache.datasketches.Family;
import org.apache.datasketches.SketchesArgumentException;
import org.apache.datasketches.memory.MemoryRequestServer;
import org.apache.datasketches.memory.WritableMemory;

@@ -250,6 +251,11 @@ private WritableMemory growCombinedMemBuffer(final int itemSpaceNeeded) {
assert needBytes > memBytes;

memReqSvr = (memReqSvr == null) ? mem_.getMemoryRequestServer() : memReqSvr;
if (memReqSvr == null) {
throw new SketchesArgumentException(
"A reequest for more memory has been denied, "
+ "or a default MemoryRequestServer has not been provided. Must abort. ");
}

final WritableMemory newMem = memReqSvr.request(mem_, needBytes);

@@ -785,6 +785,10 @@ T getItem(final int idx) {
return weights_.get(idx);
}

boolean isMarksValid() {
return marks_ != null;
}

// package-private: Relies on ArrayList for bounds checking and assumes caller knows how to
// handle a null from the middle of the list.
boolean getMark(final int idx) { return marks_.get(idx); }
@@ -78,7 +78,6 @@ public void setA(final Sketch skA) {

//process A
hashArr_ = getHashArrA(skA);
hashArr_ = (hashArr_ == null) ? new long[0] : hashArr_;
empty_ = false;
thetaLong_ = skA.getThetaLong();
curCount_ = hashArr_.length;
@@ -94,7 +93,6 @@ public void notB(final Sketch skB) {

//process B
hashArr_ = getResultHashArr(thetaLong_, curCount_, hashArr_, skB);
hashArr_ = (hashArr_ == null) ? new long[0] : hashArr_;
curCount_ = hashArr_.length;
empty_ = curCount_ == 0 && thetaLong_ == Long.MAX_VALUE;
}
@@ -135,7 +133,7 @@ public CompactSketch aNotB(final Sketch skA, final Sketch skB, final boolean dst

//process A
final long[] hashArrA = getHashArrA(skA);
final int countA = (hashArrA == null) ? 0 : hashArrA.length;
final int countA = hashArrA.length;


//process B
@@ -134,6 +134,24 @@ private enum Flags { IS_BIG_ENDIAN, IS_IN_SAMPLING_MODE, IS_EMPTY, HAS_ENTRIES,
setRebuildThreshold();
}

/**
* Copy constructor
* @param sketch the QuickSelectSketch to be copied.
*/
QuickSelectSketch(final QuickSelectSketch<S> sketch) {
nomEntries_ = sketch.nomEntries_;
lgCurrentCapacity_ = sketch.lgCurrentCapacity_;
lgResizeFactor_ = sketch.lgResizeFactor_;
count_ = sketch.count_;
samplingProbability_ = sketch.samplingProbability_;
rebuildThreshold_ = sketch.rebuildThreshold_;
thetaLong_ = sketch.thetaLong_;
empty_ = sketch.empty_;
summaryFactory_ = sketch.summaryFactory_;
hashTable_ = sketch.hashTable_.clone();
summaryTable_ = Util.copySummaryArray(sketch.summaryTable_);
}

/**
* This is to create an instance of a QuickSelectSketch given a serialized form
* @param mem Memory object with serialized QukckSelectSketch
@@ -205,6 +223,13 @@ private enum Flags { IS_BIG_ENDIAN, IS_IN_SAMPLING_MODE, IS_EMPTY, HAS_ENTRIES,
setRebuildThreshold();
}

/**
* @return a deep copy of this sketch
*/
QuickSelectSketch<S> copy() {
return new QuickSelectSketch<S>(this);
}

long[] getHashTable() {
return hashTable_;
}
@@ -75,6 +75,23 @@ public UpdatableSketch(final Memory srcMem, final SummaryDeserializer<S> deseria
super(srcMem, deserializer, summaryFactory);
}

/**
* Copy Constructor
* @param sketch the sketch to copy
*/
public UpdatableSketch(final UpdatableSketch<U, S> sketch) {
super(sketch);
}

/**
* @return a deep copy of this sketch
*/
@SuppressWarnings("unchecked")
@Override
public UpdatableSketch<U,S> copy() {
return new UpdatableSketch<U,S>(this);
}

/**
* Updates this sketch with a long key and U value.
* The value is passed to update() method of the Summary object associated with the key
@@ -137,12 +137,20 @@ public static long stringArrHash(final String[] strArray) {
return hashCharArr(s.toCharArray(), 0, s.length(), PRIME);
}

/**
* Will copy compact summary arrays as well as hashed summary tables (with nulls).
* @param <S> type of summary
* @param summaryArr the given summary array or table
* @return the copy
*/
@SuppressWarnings("unchecked")
public static <S extends Summary> S[] copySummaryArray(final S[] summaryArr) {
final int len = summaryArr.length;
final S[] tmpSummaryArr = newSummaryArray(summaryArr, len);
for (int i = 0; i < len; i++) {
tmpSummaryArr[i] = (S) summaryArr[i].copy();
final S summary = summaryArr[i];
if (summary == null) { continue; }
tmpSummaryArr[i] = (S) summary.copy();
}
return tmpSummaryArr;
}
@@ -73,6 +73,23 @@ public ArrayOfStringsSketch(final Memory mem) {
super(mem, new ArrayOfStringsSummaryDeserializer(), new ArrayOfStringsSummaryFactory());
}

/**
* Copy Constructor
* @param sketch the sketch to copy
*/
public ArrayOfStringsSketch(final ArrayOfStringsSketch sketch) {
super(sketch);
}

/**
* @return a deep copy of this sketch
*/
@SuppressWarnings("unchecked")
@Override
public ArrayOfStringsSketch copy() {
return new ArrayOfStringsSketch(this);
}

/**
* Updates the sketch with String arrays for both key and value.
* @param strArrKey the given String array key
@@ -89,7 +89,7 @@ public void numTrailingOnes() {
public void checkBoundsTest() {
Util.checkBounds(999, 2, 1000);
}

@Test
public void checkIsPowerOf2() {
Assert.assertEquals(isPowerOf2(0), false);
@@ -396,7 +396,7 @@ public void resourcefileExists() {
assertTrue(file.exists());
}

@Test(expectedExceptions = SketchesArgumentException.class)
@Test(expectedExceptions = NullPointerException.class)
public void resourceFileNotFound() {
final String shortFileName = "cpc-empty.sk";
getResourceFile(shortFileName + "123");
@@ -409,7 +409,7 @@ public void resourceBytesCorrect() {
assertTrue(bytes.length == 8);
}

@Test(expectedExceptions = SketchesArgumentException.class)
@Test(expectedExceptions = NullPointerException.class)
public void resourceBytesFileNotFound() {
final String shortFileName = "cpc-empty.sk";
getResourceBytes(shortFileName + "123");
@@ -151,6 +151,17 @@ public void checkEstimatingPostProcessing() {
}
}

@Test
public void checkCopyCtor() {
final int lgK = 14;
final FdtSketch sk = new FdtSketch(lgK);

final String[] nodesArr = {"abc", "def" };
sk.update(nodesArr);
assertEquals(sk.getRetainedEntries(), 1);
final FdtSketch sk2 = sk.copy();
assertEquals(sk2.getRetainedEntries(), 1);
}

@Test
public void printlnTest() {
@@ -152,15 +152,17 @@ public void directSketchShouldMoveOntoHeapEventually() {

@Test
public void directSketchShouldMoveOntoHeapEventually2() {
try (WritableHandle wdh = WritableMemory.allocateDirect(50)) {
int i = 0;
try (WritableHandle wdh =
WritableMemory.allocateDirect(50, ByteOrder.LITTLE_ENDIAN, new DefaultMemoryRequestServer())) {
WritableMemory mem = wdh.getWritable();
UpdateDoublesSketch sketch = DoublesSketch.builder().build(mem);
Assert.assertTrue(sketch.isSameResource(mem));
for (int i = 0; i < 1000; i++) {
try {
for (; i < 1000; i++) {
if (sketch.isSameResource(mem)) {
sketch.update(i);
if (sketch.isSameResource(mem)) { continue; }
} catch (NullPointerException e) {
} else {
System.out.println("MOVED at i = " + i);
break;
}
}
@@ -23,6 +23,7 @@
import static org.apache.datasketches.sampling.PreambleUtil.PREAMBLE_LONGS_BYTE;
import static org.apache.datasketches.sampling.PreambleUtil.SER_VER_BYTE;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
@@ -515,17 +516,10 @@ public void checkReset() {
for (int i = 0; i < (2 * k); ++i) {
sketch.update("a", 100.0 + i);
}

assertEquals(sketch.getN(), 2 * k);
assertEquals(sketch.getHRegionCount(), 0);
assertEquals(sketch.getRRegionCount(), k);
try {
sketch.getMark(0);
fail();
} catch (final NullPointerException e) {
// expected
}

assertFalse(sketch.isMarksValid());
sketch.reset();
assertEquals(sketch.getN(), 0);
assertEquals(sketch.getHRegionCount(), 0);

0 comments on commit 9b860f7

Please sign in to comment.