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 21 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,129 @@
/*
* 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: Ordering: Successible](zero: K): CyclicIncrementProvider[K] =
Copy link
Collaborator

Choose a reason for hiding this comment

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

doesn't Successible have an ordering? You could weaken this to Successible and just get the ordering from there, right?

CyclicIncrementProvider(zero, SideA, 0, zero, 0, zero)

def apply[@specialized(Int, Long) K: Ordering: Successible]
(zero: K,
currentSide: Side,
currentSideCount: Int,
maxCurrentSideVal: K,
nextSideCount: Int,
maxNextSideVal: K): CyclicIncrementProvider[K] =
new CyclicIncrementProvider[K](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: Ordering: Successible]
(zero: K,
currentSide: Side,
currentSideCount: Int,
maxCurrentSideVal: K,
nextSideCount: Int,
maxNextSideVal: K) extends IdProvider[CyclicIncrement[K]] {
def tick: (CyclicIncrement[K], CyclicIncrementProvider[K]) =
if (nextSideCount > 0 || currentSideCount > 0) {
// We hand one out of the next time
val nextVal =
Successible.next(maxNextSideVal)
.getOrElse(throw new IllegalStateException("Hit maximum value for increment"))
(currentSide.nextSide.makeCyclicIncrement(nextVal),
CyclicIncrementProvider[K](zero, currentSide, currentSideCount, maxCurrentSideVal, nextSideCount+1, nextVal))
} else {
// We hand out one of the current time
val nextVal =
Successible.next(maxCurrentSideVal)
.getOrElse(throw new IllegalStateException("Hit maximum value for increment"))
(currentSide.makeCyclicIncrement(nextVal),
CyclicIncrementProvider[K](zero, currentSide, currentSideCount+1, nextVal, nextSideCount, maxNextSideVal))
}

def cull(cyclicIncrement: CyclicIncrement[K]): CyclicIncrementProvider[K] =
if (cyclicIncrement.side == currentSide) {
val nextCurrentSidecount = currentSideCount - 1
if (nextCurrentSidecount == 0) {
CyclicIncrementProvider[K](zero, currentSide.nextSide, nextSideCount, maxNextSideVal, 0, zero)
} else {
CyclicIncrementProvider[K](zero, currentSide, nextCurrentSidecount, maxCurrentSideVal, nextSideCount, maxNextSideVal)
}
} else if (cyclicIncrement.side == currentSide.nextSide) {
CyclicIncrementProvider[K](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)

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 ordering(implicit ord: Ordering[K]): Ordering[K] = ord
Copy link
Collaborator

Choose a reason for hiding this comment

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

don't think this should have an implicit param. You never need this method.

def side: Side
override def toString = side + ":" + value
}

case class SideACyclicIncrement[@specialized(Int, Long) K: Ordering](override val value: K) extends CyclicIncrement[K] { def side = SideA }
case class SideBCyclicIncrement[@specialized(Int, Long) K: Ordering](override val value: K) extends CyclicIncrement[K] { def side = SideB }
case class SideCCyclicIncrement[@specialized(Int, Long) K: Ordering](override val value: K) extends CyclicIncrement[K] { 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: Ordering](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 {
def nextSide = SideB
def makeCyclicIncrement[@specialized(Int, Long) K: Ordering](value: K) = SideACyclicIncrement[K](value)
}
object SideB extends Side {
def nextSide = SideC
def makeCyclicIncrement[@specialized(Int, Long) K: Ordering](value: K) = SideBCyclicIncrement[K](value)
}
object SideC extends Side {
def nextSide = SideA
def makeCyclicIncrement[@specialized(Int, Long) K: Ordering](value: K) = SideCCyclicIncrement[K](value)
}
Loading