Skip to content

Commit

Permalink
register function returns Unit
Browse files Browse the repository at this point in the history
  • Loading branch information
simerplaha committed Oct 7, 2019
1 parent 5c81825 commit 2a6c7a8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 86 deletions.
145 changes: 75 additions & 70 deletions swaydb-stress/src/test/scala/swaydb/simulation/SimulationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -186,23 +186,24 @@ sealed trait SimulationSpec extends WordSpec with TestBase with LazyLogging {
val (productId, (product, deadline)) = randomCreatedProducts.head
val updatedProduct = product.copy(name = product.name + "-" + s"updated_${System.nanoTime()}")

def createFunction() =
new Function.GetKey[Long, Domain] with Functions {
override def apply(key: Long, deadline: Option[Deadline]): Apply.Map[Domain] =
if (key == productId)
Apply.Update(updatedProduct)
else
Apply.Nothing

override def id: Slice[Byte] =
Slice.writeInt(functionIDs.incrementAndGet())
}

if (randomBoolean())
db.update(productId, updatedProduct).get
else {
val functionId =
db.registerFunction(
new Function.GetKey[Long, Domain] with Functions {
override def apply(key: Long, deadline: Option[Deadline]): Apply.Map[Domain] =
if (key == productId)
Apply.Update(updatedProduct)
else
Apply.Nothing

override def id: Slice[Byte] =
Slice.writeInt(functionIDs.incrementAndGet())
}
)
db.applyFunction(productId, functionId).get
val function = createFunction()
db.registerFunction(function)
db.applyFunction(productId, function).get
}

state.products.put(productId, (updatedProduct, deadline))
Expand All @@ -218,21 +219,21 @@ sealed trait SimulationSpec extends WordSpec with TestBase with LazyLogging {
val (productId, (product, deadline)) = randomCreatedProducts.head
val newDeadline = deadline.map(_ - 1.second) getOrElse 1.hour.fromNow

def createFunction() =
new Function.GetKey[Long, Domain] with Functions {
override def apply(key: Long, deadline: Option[Deadline]): Apply.Map[Domain] =
Apply.Expire(newDeadline)

override def id: Slice[Byte] =
Slice.writeInt(functionIDs.incrementAndGet())
}

if (randomBoolean())
db.expire(productId, newDeadline).get
else {
val functionId =
db.registerFunction(
new Function.GetKey[Long, Domain] with Functions {
override def apply(key: Long, deadline: Option[Deadline]): Apply.Map[Domain] =
Apply.Expire(newDeadline)

override def id: Slice[Byte] =
Slice.writeInt(functionIDs.incrementAndGet())
}
)

db.applyFunction(productId, functionId).get
val function = createFunction()
db.registerFunction(function)
db.applyFunction(productId, function).get
}

state.products.put(productId, (product, Some(newDeadline)))
Expand Down Expand Up @@ -320,20 +321,21 @@ sealed trait SimulationSpec extends WordSpec with TestBase with LazyLogging {
//state.updateRangeCount indicates the number of times UpdateRang is invoked on the key-value.
val updatedProduct = Product(s"update_range_${System.nanoTime()}")

def createFunction() =
new Function.GetKey[Long, Domain] with Functions {
override def apply(key: Long, deadline: Option[Deadline]): Apply.Map[Domain] =
Apply.Update(updatedProduct)

override def id: Slice[Byte] =
Slice.writeInt(functionIDs.incrementAndGet())
}

if (randomBoolean())
db.update(from, to, updatedProduct).get
else {
val functionId =
db.registerFunction(
new Function.GetKey[Long, Domain] with Functions {
override def apply(key: Long, deadline: Option[Deadline]): Apply.Map[Domain] =
Apply.Update(updatedProduct)

override def id: Slice[Byte] =
Slice.writeInt(functionIDs.incrementAndGet())
}
)
db.applyFunction(from, to, functionId).get
val function = createFunction()
db.registerFunction(function)
db.applyFunction(from, to, function).get
}

(from to to) foreach {
Expand Down Expand Up @@ -361,20 +363,21 @@ sealed trait SimulationSpec extends WordSpec with TestBase with LazyLogging {
else
1.hour.fromNow

def createFunction() =
new Function.GetKey[Long, Domain] with Functions {
override def apply(key: Long, deadline: Option[Deadline]): Apply.Map[Domain] =
Apply.Expire(newDeadline)

override def id: Slice[Byte] =
Slice.writeInt(functionIDs.incrementAndGet())
}

if (randomBoolean())
db.expire(from, to, newDeadline).get
else {
val functionId =
db.registerFunction(
new Function.GetKey[Long, Domain] with Functions {
override def apply(key: Long, deadline: Option[Deadline]): Apply.Map[Domain] =
Apply.Expire(newDeadline)

override def id: Slice[Byte] =
Slice.writeInt(functionIDs.incrementAndGet())
}
)
db.applyFunction(from, to, functionId).get
val function = createFunction()
db.registerFunction(function)
db.applyFunction(from, to, function).get
}

(from to to) foreach {
Expand All @@ -395,20 +398,21 @@ sealed trait SimulationSpec extends WordSpec with TestBase with LazyLogging {
//delete random single product
val (productToRemoveId, productToRemove) = randomCreatedProducts.head

def createFunction() =
new Function.GetKey[Long, Domain] with Functions {
override def apply(key: Long, deadline: Option[Deadline]): Apply.Map[Domain] =
Apply.Remove

override def id: Slice[Byte] =
Slice.writeInt(functionIDs.incrementAndGet())
}

if (randomBoolean())
db.remove(productToRemoveId).get
else {
val functionId =
db.registerFunction(
new Function.GetKey[Long, Domain] with Functions {
override def apply(key: Long, deadline: Option[Deadline]): Apply.Map[Domain] =
Apply.Remove

override def id: Slice[Byte] =
Slice.writeInt(functionIDs.incrementAndGet())
}
)
db.applyFunction(productToRemoveId, functionId).get
val function = createFunction()
db.registerFunction(function)
db.applyFunction(productToRemoveId, function).get
}

state.products remove productToRemoveId
Expand Down Expand Up @@ -447,20 +451,21 @@ sealed trait SimulationSpec extends WordSpec with TestBase with LazyLogging {
System.exit(0)
}

def createFunction() =
new Function.GetKey[Long, Domain] with Functions {
override def apply(key: Long, deadline: Option[Deadline]): Apply.Map[Domain] =
Apply.Remove

override def id: Slice[Byte] =
Slice.writeInt(functionIDs.incrementAndGet())
}

if (randomBoolean())
db.remove(from, to).get
else {
val functionId =
db.registerFunction(
new Function.GetKey[Long, Domain] with Functions {
override def apply(key: Long, deadline: Option[Deadline]): Apply.Map[Domain] =
Apply.Remove

override def id: Slice[Byte] =
Slice.writeInt(functionIDs.incrementAndGet())
}
)
db.applyFunction(from, to, functionId).get
val function = createFunction()
db.registerFunction(function)
db.applyFunction(from, to, function).get
}

(from to to) foreach {
Expand Down
5 changes: 1 addition & 4 deletions swaydb/src/main/scala/swaydb/Map.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ case class Map[K, V, F, T[_]](private[swaydb] val core: Core[T],
def clear(): T[IO.Done] =
tag.point(core.clear(core.readStates.get()))

def registerFunction[R <: F with swaydb.Function[K, V]](function: R): R = {
def registerFunction(function: F with swaydb.Function[K, V]): Unit =
(function: swaydb.Function[K, V]) match {
case function: swaydb.Function.GetValue[V] =>
core.registerFunction(function.id, SwayDB.toCoreFunction(function))
Expand All @@ -150,9 +150,6 @@ case class Map[K, V, F, T[_]](private[swaydb] val core: Core[T],
core.registerFunction(function.id, SwayDB.toCoreFunction(function))
}

function
}

def applyFunction(key: K, function: F with swaydb.Function[K, V]): T[IO.Done] =
tag.point(core.function(key, function.id))

Expand Down
4 changes: 1 addition & 3 deletions swaydb/src/main/scala/swaydb/Set.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,8 @@ case class Set[A, F, T[_]](private val core: Core[T],
def clear(): T[IO.Done] =
tag.point(core.clear(core.readStates.get()))

def registerFunction[R <: F with swaydb.Function.GetKey[A, Nothing]](function: R): R = {
def registerFunction(function: F with swaydb.Function.GetKey[A, Nothing]): Unit =
core.registerFunction(function.id, SwayDB.toCoreFunction(function))
function
}

def applyFunction(from: A, to: A, function: F with swaydb.Function.GetKey[A, Nothing]): T[IO.Done] =
tag.point(core.function(from, to, function.id))
Expand Down
15 changes: 6 additions & 9 deletions swaydb/src/test/scala/swaydb/api/SwayDBFunctionSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,13 @@ sealed trait SwayDBFunctionSpec extends TestBase {
"perform concurrent atomic updates to a single key" in {

val db = newDB()
db.registerFunction(Key.IncrementValue)

db.put(Key.Id(1), 0).get

val incrementValue = db.registerFunction(Key.IncrementValue)

(1 to 1000).par foreach {
_ =>
db.applyFunction(Key.Id(1), incrementValue).get
db.applyFunction(Key.Id(1), Key.IncrementValue).get
}

db.get(Key.Id(1)).get should contain(1000)
Expand All @@ -124,19 +123,18 @@ sealed trait SwayDBFunctionSpec extends TestBase {
"perform concurrent atomic updates to multiple keys" in {

val db = newDB()
db.registerFunction(Key.IncrementValue)

(1 to 1000) foreach {
i =>
db.put(Key.Id(i), 0).get
}

val functionId = db.registerFunction(Key.IncrementValue)

(1 to 100).par foreach {
_ =>
(1 to 1000).par foreach {
i =>
db.applyFunction(Key.Id(i), functionId).get
db.applyFunction(Key.Id(i), Key.IncrementValue).get
}
}

Expand All @@ -151,19 +149,18 @@ sealed trait SwayDBFunctionSpec extends TestBase {
"Nothing should not update data" in {

val db = newDB()
db.registerFunction(Key.DoNothing)

(1 to 1000) foreach {
i =>
db.put(Key.Id(i), 0).get
}

val functionId = db.registerFunction(Key.DoNothing)

(1 to 100).par foreach {
_ =>
(1 to 1000).par foreach {
i =>
db.applyFunction(Key.Id(i), functionId).get
db.applyFunction(Key.Id(i), Key.DoNothing).get
}
}

Expand Down

0 comments on commit 2a6c7a8

Please sign in to comment.