forked from twitter-archive/snowflake
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** Copyright 2010 Twitter, Inc.*/ | ||
package com.twitter.collections | ||
|
||
import scala.collection.mutable.LinkedHashSet | ||
|
||
/** | ||
* A size-limited HashSet. Eviction is FIFO | ||
*/ | ||
class FIFOHashSet[T](sizeLimit: Int) extends LinkedHashSet[T] { | ||
|
||
def this() { | ||
this(Integer.MAX_VALUE) | ||
} | ||
|
||
override def add(t: T): Boolean = { | ||
if (size >= sizeLimit) { | ||
trim | ||
} | ||
super.add(t) | ||
} | ||
|
||
protected def trim() { | ||
val iter = elements | ||
while (size >= sizeLimit) { | ||
remove(iter.next) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/test/scala/com/twitter/collections/FIFOHashSetSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.twitter.collections | ||
import org.specs._ | ||
|
||
class FIFOHashSetSpec extends Specification { | ||
"FIFOHashSet" should { | ||
"store and retrieve an item" in { | ||
val set = new FIFOHashSet[Int]() | ||
set.add(1) must be_==(true) | ||
set.contains(1) must be_==(true) | ||
} | ||
|
||
"not grow over the size limit" in { | ||
val set = new FIFOHashSet[Int](1) | ||
set.add(1) must be_==(true) | ||
set.add(2) must be_==(true) | ||
set.size must be_==(1) | ||
} | ||
|
||
"evict the oldest item" in { | ||
val set = new FIFOHashSet[Int](10) | ||
for (i <- 1 to 10) { | ||
set.add(i) | ||
} | ||
for (i <- 11 to 100) { | ||
set.add(i) | ||
set.size must be_==(10) | ||
set.contains(i-10) must be_==(false) | ||
for (j <- i-9 to i) { | ||
set.contains(j) must be_==(true) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,5 +18,4 @@ class ReporterSpec extends Specification { | |
true | ||
} | ||
} | ||
|
||
} |