Skip to content

Commit

Permalink
[SPARK-18200][GRAPHX] Support zero as an initial capacity in OpenHashSet
Browse files Browse the repository at this point in the history
## What changes were proposed in this pull request?

[SPARK-18200](https://issues.apache.org/jira/browse/SPARK-18200) reports Apache Spark 2.x raises `java.lang.IllegalArgumentException: requirement failed: Invalid initial capacity` while running `triangleCount`. The root cause is that `VertexSet`, a type alias of `OpenHashSet`, does not allow zero as a initial size. This PR loosens the restriction to allow zero.

## How was this patch tested?

Pass the Jenkins test with a new test case in `OpenHashSetSuite`.

Author: Dongjoon Hyun <[email protected]>

Closes #15741 from dongjoon-hyun/SPARK-18200.

(cherry picked from commit d24e736)
Signed-off-by: Reynold Xin <[email protected]>
  • Loading branch information
dongjoon-hyun authored and rxin committed Nov 3, 2016
1 parent 3253ae7 commit dae1581
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class OpenHashSet[@specialized(Long, Int) T: ClassTag](

require(initialCapacity <= OpenHashSet.MAX_CAPACITY,
s"Can't make capacity bigger than ${OpenHashSet.MAX_CAPACITY} elements")
require(initialCapacity >= 1, "Invalid initial capacity")
require(initialCapacity >= 0, "Invalid initial capacity")
require(loadFactor < 1.0, "Load factor must be less than 1.0")
require(loadFactor > 0.0, "Load factor must be greater than 0.0")

Expand Down Expand Up @@ -271,8 +271,12 @@ class OpenHashSet[@specialized(Long, Int) T: ClassTag](
private def hashcode(h: Int): Int = Hashing.murmur3_32().hashInt(h).asInt()

private def nextPowerOf2(n: Int): Int = {
val highBit = Integer.highestOneBit(n)
if (highBit == n) n else highBit << 1
if (n == 0) {
2
} else {
val highBit = Integer.highestOneBit(n)
if (highBit == n) n else highBit << 1
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ class OpenHashMapSuite extends SparkFunSuite with Matchers {
intercept[IllegalArgumentException] {
new OpenHashMap[String, Int](-1)
}
intercept[IllegalArgumentException] {
new OpenHashMap[String, String](0)
}
}

test("primitive value") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,9 @@ class OpenHashSetSuite extends SparkFunSuite with Matchers {
assert(set.size === 1000)
assert(set.capacity > 1000)
}

test("SPARK-18200 Support zero as an initial set size") {
val set = new OpenHashSet[Long](0)
assert(set.size === 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ class PrimitiveKeyOpenHashMapSuite extends SparkFunSuite with Matchers {
intercept[IllegalArgumentException] {
new PrimitiveKeyOpenHashMap[Int, Int](-1)
}
intercept[IllegalArgumentException] {
new PrimitiveKeyOpenHashMap[Int, Int](0)
}
}

test("basic operations") {
Expand Down

0 comments on commit dae1581

Please sign in to comment.