Skip to content

Commit

Permalink
Correct "bugs" found with FindBugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Rhodes committed Jul 27, 2016
1 parent 98c5f87 commit e4b9410
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
37 changes: 20 additions & 17 deletions src/main/java/com/yahoo/sketches/memory/UnsafeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package com.yahoo.sketches.memory;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import com.yahoo.sketches.SketchesException;

Expand Down Expand Up @@ -70,18 +71,25 @@ final class UnsafeUtil {
static final long UNSAFE_COPY_THRESHOLD = 1L << 20; //2^20

static {
try {
//should work across JVMs, e.g., with Android:
Constructor<Unsafe> unsafeConstructor = Unsafe.class.getDeclaredConstructor();
unsafeConstructor.setAccessible(true);
unsafe = unsafeConstructor.newInstance();

// Alternative, but may not work across different JVMs.
// Field field = Unsafe.class.getDeclaredField("theUnsafe");
// field.setAccessible(true);
// unsafe = (Unsafe) field.get(null);

//4 on 32-bits systems, 8 on 64-bit systems. Not an indicator of compressed ref (Oop)
try {
//should work across JVMs, e.g., with Android:
Constructor<Unsafe> unsafeConstructor = Unsafe.class.getDeclaredConstructor();
unsafeConstructor.setAccessible(true);
unsafe = unsafeConstructor.newInstance();

// Alternative, but may not work across different JVMs.
// Field field = Unsafe.class.getDeclaredField("theUnsafe");
// field.setAccessible(true);
// unsafe = (Unsafe) field.get(null);

} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
throw new SketchesException("Unable to acquire Unsafe. ", e);
}

//4 on 32-bits systems and 64-bit systems < 32GB, otherwise 8.
//This alone is not an indicator of compressed ref (Oop)
ADDRESS_SIZE = unsafe.addressSize();

ARRAY_BOOLEAN_BASE_OFFSET = unsafe.arrayBaseOffset(boolean[].class);
Expand All @@ -103,11 +111,6 @@ final class UnsafeUtil {
ARRAY_FLOAT_INDEX_SCALE = unsafe.arrayIndexScale(float[].class);
ARRAY_DOUBLE_INDEX_SCALE = unsafe.arrayIndexScale(double[].class);
ARRAY_OBJECT_INDEX_SCALE = unsafe.arrayIndexScale(Object[].class);

}
catch (Exception e) {
throw new SketchesException("Unable to acquire Unsafe. ", e);
}
}

private UnsafeUtil() {}
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/com/yahoo/sketches/theta/SetOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,8 @@ static final int computeMinLgArrLongsFromCount(final int count) {
*/
static boolean isValidSetOpID(int id) {
Family family = Family.idToFamily(id);
boolean ret;
switch (family) {
case UNION :
case INTERSECTION :
case A_NOT_B : ret = true; break;
default: ret = false; break;
}
boolean ret = ((family == Family.UNION) || (family == Family.INTERSECTION)
|| (family == Family.A_NOT_B));
return ret;
}
}
18 changes: 12 additions & 6 deletions src/test/java/com/yahoo/sketches/memory/MemoryMappedFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.CharArrayReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import org.testng.annotations.Test;

Expand All @@ -25,7 +27,7 @@ public class MemoryMappedFileTest {
@Test(expectedExceptions = RuntimeException.class)
public void testMapException() throws Exception {
File dummy = createFile("dummy.txt", "");
Memory mmf = new MemoryMappedFile(dummy, 0, dummy.length()); //zero length
new MemoryMappedFile(dummy, 0, dummy.length()); //zero length
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -154,7 +156,7 @@ public void testForce() throws Exception {

// add content
String cor = "Correcting spelling mistakes";
byte[] b = cor.getBytes();
byte[] b = cor.getBytes(UTF_8);
mmf.putByteArray(0, b, 0, b.length);

mmf.force();
Expand Down Expand Up @@ -193,13 +195,17 @@ public void checkPassThrough() {
mem.freeMemory();
}

@SuppressWarnings("resource")
private static File createFile(String fileName, String text) throws FileNotFoundException {
File file = new File(fileName);
file.deleteOnExit();
PrintWriter writer = new PrintWriter(file);
writer.print(text);
writer.close();
PrintWriter writer;
try {
writer = new PrintWriter(file, UTF_8.name());
writer.print(text);
writer.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return file;
}

Expand Down

0 comments on commit e4b9410

Please sign in to comment.