Skip to content
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 22 commits into from
Dec 16, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ object StorehausBuild extends Build {
).dependsOn(storehausTesting % "test->test")
}

lazy val storehausCache = module("cache")
lazy val storehausCache = module("cache").settings(
libraryDependencies += "com.twitter" %% "algebird-core" % algebirdVersion
)

lazy val storehausCore = module("core").settings(
libraryDependencies ++= Seq(
Expand Down
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]
Copy link
Collaborator

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).

}

case class LongClock(v:Long = 0) extends Clock[Long, LongClock] {
def tick = (v, LongClock(v + 1))
def empty = LongClock()
}
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

successible?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
}
Loading