-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add immutable LIRS Cache implementation #155
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
55e7462
Add preliminary implimination of LIRS Cache, based on Clojure's core.…
e6ce1ce
Refactor and fix
d23dddf
Make helper class private
c7f2b8e
Create a CyclicIncrement which allows us not to grow our counter with…
7842ef3
Remove unnecessary parameter in CyclicIncrement
4a25789
Remove unnecessary parameter in CyclicIncrement
c339c9c
Remove private[storehaus], fix construction bug
89f28b6
Add license header
081de8e
Minor tweak
c422848
Make nextSide method a val
99dea48
Move around classes
4df8f1a
Comments for CyclicIncrement
8646317
Specialize the CyclicIncrement
cde9d6a
Get rid of the unecessary reference in the CyclicIncrement to the side
bc66918
Add tests, add Clock type
0252557
remove irrelevant comment
cd000fe
comment cleanup
2db108f
cleanup whitespace
2d4cd0b
Take into account Oscar's comments
52f9b37
Use Successible
a965167
Merge remote-tracking branch 'upstream/develop' into jco/lirscache
036875a
more cleaning
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 35 additions & 0 deletions
35
storehaus-cache/src/main/scala/com/twitter/storehaus/cache/Clock.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,35 @@ | ||
/* | ||
* Copyright 2013 Twitter Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. You may obtain | ||
* a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.twitter.storehaus.cache | ||
|
||
/** | ||
* A clock is anything which provides a succession of values. | ||
*/ | ||
|
||
trait Clock[@specialized(Int, Long) T, +This <: Clock[T, This]] { | ||
def tick: (T, This) | ||
def empty: This | ||
} | ||
|
||
trait IdProvider[@specialized(Int, Long) T] extends Clock[T, IdProvider[T]] { | ||
def cull(oldId: T): IdProvider[T] | ||
} | ||
|
||
case class LongClock(v:Long = 0) extends Clock[Long, LongClock] { | ||
def tick = (v, LongClock(v + 1)) | ||
def empty = LongClock() | ||
} |
140 changes: 140 additions & 0 deletions
140
storehaus-cache/src/main/scala/com/twitter/storehaus/cache/CyclicIncrement.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,140 @@ | ||
/* | ||
* Copyright 2013 Twitter Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. You may obtain | ||
* a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.twitter.storehaus.cache | ||
|
||
import com.twitter.algebird.Successible | ||
|
||
/** | ||
* This is an immutable implementation of a provider of cyclical increments. The motivation | ||
* is that we need ordered identifiers for caches, but we do not want to just increment | ||
* an int as this has a theoretical upper limit (this is functional programming, our stuff | ||
* doesn't crash, right?). This class allows the reuse of numbers, making it highly unlikely | ||
* to run out of identifiers (it is still possible, but in a way that would be unavoidable -- | ||
* if there is a key that stays in the stacks forever, thus never allowing us to reuse the | ||
* increments after that key). This class ensures that when it is asked for a new increment, | ||
* it will be greater than all currently outstanding increments at that time. | ||
* | ||
* @author Jonathan Coveney | ||
*/ | ||
|
||
object CyclicIncrementProvider { | ||
def intIncrementer: CyclicIncrementProvider[Int] = CyclicIncrementProvider(0) | ||
|
||
def apply[@specialized(Int, Long) K: Successible](zero: K): CyclicIncrementProvider[K] = | ||
CyclicIncrementProvider(zero, SideA, 0, zero, 0, zero) | ||
|
||
def apply[@specialized(Int, Long) K: Successible] | ||
(zero: K, | ||
currentSide: Side, | ||
currentSideCount: Int, | ||
maxCurrentSideVal: K, | ||
nextSideCount: Int, | ||
maxNextSideVal: K): CyclicIncrementProvider[K] = | ||
new CyclicIncrementProvider( | ||
zero, currentSide, currentSideCount, maxCurrentSideVal, nextSideCount, maxNextSideVal) | ||
} | ||
|
||
// Algorithm: we start on a side. We hand out values. Once we've handed out at least 1 value, we begin to give | ||
// out values of side.nextSide. Once all of the increments of the previous side have been culled, we now switch. | ||
// side.nextSide becomes the current side. Then we repeat the algorithm. | ||
class CyclicIncrementProvider[@specialized(Int, Long) K: Successible] | ||
(zero: K, | ||
currentSide: Side, | ||
currentSideCount: Int, | ||
maxCurrentSideVal: K, | ||
nextSideCount: Int, | ||
maxNextSideVal: K) extends IdProvider[CyclicIncrement[K]] { | ||
|
||
implicit val ord = implicitly[Successible[K]].ordering | ||
private def next(v: K) = | ||
Successible.next(v).getOrElse(throw new IllegalStateException("Hit maximum value for increment")) | ||
|
||
override def tick = | ||
if (nextSideCount > 0 || currentSideCount > 0) { | ||
// We hand one out of the next time | ||
val nextVal = next(maxNextSideVal) | ||
(currentSide.nextSide.makeCyclicIncrement(nextVal), | ||
CyclicIncrementProvider(zero, currentSide, currentSideCount, maxCurrentSideVal, nextSideCount + 1, nextVal)) | ||
} else { | ||
// We hand out one of the current time | ||
val nextVal = next(maxCurrentSideVal) | ||
(currentSide.makeCyclicIncrement(nextVal), | ||
CyclicIncrementProvider(zero, currentSide, currentSideCount + 1, nextVal, nextSideCount, maxNextSideVal)) | ||
} | ||
|
||
override def cull(cyclicIncrement: CyclicIncrement[K]) = | ||
if (cyclicIncrement.side == currentSide) { | ||
val nextCurrentSidecount = currentSideCount - 1 | ||
if (nextCurrentSidecount == 0) { | ||
CyclicIncrementProvider(zero, currentSide.nextSide, nextSideCount, maxNextSideVal, 0, zero) | ||
} else { | ||
CyclicIncrementProvider( | ||
zero, currentSide, nextCurrentSidecount, maxCurrentSideVal, nextSideCount, maxNextSideVal) | ||
} | ||
} else if (cyclicIncrement.side == currentSide.nextSide) { | ||
CyclicIncrementProvider( | ||
zero, currentSide, currentSideCount, maxCurrentSideVal, nextSideCount - 1, maxNextSideVal) | ||
} else { | ||
throw new IllegalStateException("Shouldn't be culling a value of given type") | ||
} | ||
|
||
override def toString = | ||
("CyclicIncrementProvider: zero:%d currentSide:%s currentSideCount:%d " + | ||
"maxCurrentSideVal:%d nextSideCount:%d maxNextSideVal:%d") | ||
.format(zero, currentSide, currentSideCount, maxCurrentSideVal, nextSideCount, maxNextSideVal) | ||
|
||
override def empty = CyclicIncrementProvider(zero) | ||
} | ||
|
||
object CyclicIncrement { | ||
implicit def ordering[K](implicit ordering: Ordering[K]): Ordering[CyclicIncrement[K]] = Ordering.by { _.value } | ||
} | ||
|
||
sealed trait CyclicIncrement[@specialized(Int, Long) K] { | ||
def value: K | ||
def side: Side | ||
override def toString = side + ":" + value | ||
} | ||
|
||
case class SideACyclicIncrement[@specialized(Int, Long) K](override val value: K) extends CyclicIncrement[K] { | ||
override def side = SideA | ||
} | ||
case class SideBCyclicIncrement[@specialized(Int, Long) K](override val value: K) extends CyclicIncrement[K] { | ||
override def side = SideB | ||
} | ||
case class SideCCyclicIncrement[@specialized(Int, Long) K](override val value: K) extends CyclicIncrement[K] { | ||
override def side = SideC | ||
} | ||
sealed trait Side { | ||
def nextSide: Side | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. successible? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed |
||
def makeCyclicIncrement[@specialized(Int, Long) K](value: K): CyclicIncrement[K] | ||
|
||
override def toString = getClass.getSimpleName | ||
def offset(that:Side) = if (this == that) 0 else if (nextSide == that) -1 else 1 | ||
} | ||
object SideA extends Side { | ||
override def nextSide = SideB | ||
override def makeCyclicIncrement[@specialized(Int, Long) K](value: K) = SideACyclicIncrement(value) | ||
} | ||
object SideB extends Side { | ||
override def nextSide = SideC | ||
override def makeCyclicIncrement[@specialized(Int, Long) K](value: K) = SideBCyclicIncrement(value) | ||
} | ||
object SideC extends Side { | ||
override def nextSide = SideA | ||
override def makeCyclicIncrement[@specialized(Int, Long) K](value: K) = SideCCyclicIncrement(value) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment on the meaning here. (A bit like "free" right? in a malloc sense).