2009-04-19 10:58:20 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009 Scalable Solutions.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-06-21 14:08:43 +02:00
|
|
|
package se.scalablesolutions.akka.kernel.state
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-07-12 23:08:17 +02:00
|
|
|
import kernel.stm.TransactionManagement
|
|
|
|
|
import akka.collection._
|
2009-06-22 13:13:58 +02:00
|
|
|
|
2009-07-12 23:08:17 +02:00
|
|
|
import org.codehaus.aspectwerkz.proxy.Uuid
|
2009-06-22 13:13:58 +02:00
|
|
|
|
2009-07-12 23:08:17 +02:00
|
|
|
import scala.collection.mutable.{ArrayBuffer, HashMap}
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-06-22 14:12:09 +02:00
|
|
|
sealed abstract class TransactionalStateConfig
|
|
|
|
|
abstract class PersistentStorageConfig extends TransactionalStateConfig
|
2009-08-03 09:03:51 +02:00
|
|
|
case class CassandraStorageConfig extends PersistentStorageConfig
|
2009-06-24 15:12:47 +02:00
|
|
|
case class TerracottaStorageConfig extends PersistentStorageConfig
|
|
|
|
|
case class TokyoCabinetStorageConfig extends PersistentStorageConfig
|
2009-06-22 14:12:09 +02:00
|
|
|
|
2009-06-29 15:01:20 +02:00
|
|
|
/**
|
|
|
|
|
* Scala API.
|
|
|
|
|
* <p/>
|
|
|
|
|
* Example Scala usage:
|
|
|
|
|
* <pre>
|
2009-08-03 09:03:51 +02:00
|
|
|
* val myMap = TransactionalState.newPersistentMap(CassandraStorageConfig)
|
2009-06-29 15:01:20 +02:00
|
|
|
* </pre>
|
|
|
|
|
*/
|
2009-06-22 14:12:09 +02:00
|
|
|
object TransactionalState extends TransactionalState
|
|
|
|
|
|
2009-06-29 15:01:20 +02:00
|
|
|
/**
|
|
|
|
|
* Java API.
|
|
|
|
|
* <p/>
|
|
|
|
|
* Example Java usage:
|
|
|
|
|
* <pre>
|
|
|
|
|
* TransactionalState state = new TransactionalState();
|
2009-08-03 09:03:51 +02:00
|
|
|
* TransactionalMap myMap = state.newPersistentMap(new CassandraStorageConfig());
|
2009-06-29 15:01:20 +02:00
|
|
|
* </pre>
|
|
|
|
|
*/
|
|
|
|
|
class TransactionalState {
|
|
|
|
|
def newPersistentMap(config: PersistentStorageConfig): TransactionalMap[String, AnyRef] = config match {
|
2009-08-03 09:03:51 +02:00
|
|
|
case CassandraStorageConfig() => new CassandraPersistentTransactionalMap
|
2009-06-29 15:01:20 +02:00
|
|
|
case TerracottaStorageConfig() => throw new UnsupportedOperationException
|
|
|
|
|
case TokyoCabinetStorageConfig() => throw new UnsupportedOperationException
|
2009-06-22 14:12:09 +02:00
|
|
|
}
|
|
|
|
|
|
2009-06-29 15:01:20 +02:00
|
|
|
def newPersistentVector(config: PersistentStorageConfig): TransactionalVector[AnyRef] = config match {
|
2009-08-03 09:03:51 +02:00
|
|
|
case CassandraStorageConfig() => new CassandraPersistentTransactionalVector
|
2009-06-29 15:01:20 +02:00
|
|
|
case TerracottaStorageConfig() => throw new UnsupportedOperationException
|
|
|
|
|
case TokyoCabinetStorageConfig() => throw new UnsupportedOperationException
|
2009-06-22 14:12:09 +02:00
|
|
|
}
|
|
|
|
|
|
2009-06-29 15:01:20 +02:00
|
|
|
def newPersistentRef(config: PersistentStorageConfig): TransactionalRef[AnyRef] = config match {
|
2009-08-03 09:03:51 +02:00
|
|
|
case CassandraStorageConfig() => new CassandraPersistentTransactionalRef
|
2009-06-29 15:01:20 +02:00
|
|
|
case TerracottaStorageConfig() => throw new UnsupportedOperationException
|
|
|
|
|
case TokyoCabinetStorageConfig() => throw new UnsupportedOperationException
|
2009-06-22 14:12:09 +02:00
|
|
|
}
|
2009-06-29 15:01:20 +02:00
|
|
|
|
|
|
|
|
def newInMemoryMap[K, V]: TransactionalMap[K, V] = new InMemoryTransactionalMap[K, V]
|
|
|
|
|
|
|
|
|
|
def newInMemoryVector[T]: TransactionalVector[T] = new InMemoryTransactionalVector[T]
|
|
|
|
|
|
|
|
|
|
def newInMemoryRef[T]: TransactionalRef[T] = new TransactionalRef[T]
|
2009-06-22 14:12:09 +02:00
|
|
|
}
|
|
|
|
|
|
2009-05-13 19:28:55 +02:00
|
|
|
/**
|
2009-06-22 14:12:09 +02:00
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
2009-05-13 19:28:55 +02:00
|
|
|
*/
|
2009-06-25 23:47:30 +02:00
|
|
|
@serializable
|
2009-04-19 10:58:20 +02:00
|
|
|
trait Transactional {
|
2009-06-29 23:38:10 +02:00
|
|
|
// FIXME: won't work across the cluster
|
2009-06-10 20:04:33 +02:00
|
|
|
val uuid = Uuid.newUuid.toString
|
2009-06-22 13:13:58 +02:00
|
|
|
|
2009-04-19 10:58:20 +02:00
|
|
|
private[kernel] def begin
|
|
|
|
|
private[kernel] def commit
|
|
|
|
|
private[kernel] def rollback
|
2009-06-22 13:13:58 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
protected def verifyTransaction = {
|
|
|
|
|
val cflowTx = TransactionManagement.threadBoundTx.get
|
|
|
|
|
if (!cflowTx.isDefined) {
|
|
|
|
|
throw new IllegalStateException("Can't access transactional reference outside the scope of a transaction [" + this + "]")
|
|
|
|
|
} else {
|
|
|
|
|
cflowTx.get.register(this)
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
}
|
|
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
/**
|
|
|
|
|
* Base trait for all state implementations (persistent or in-memory).
|
2009-06-29 15:01:20 +02:00
|
|
|
*
|
2009-07-04 06:38:47 +02:00
|
|
|
* FIXME: Create Java versions using pcollections
|
2009-06-29 15:01:20 +02:00
|
|
|
*
|
2009-04-27 20:06:48 +02:00
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
2009-04-27 19:55:57 +02:00
|
|
|
*/
|
2009-05-13 19:28:55 +02:00
|
|
|
trait TransactionalMap[K, V] extends Transactional with scala.collection.mutable.Map[K, V] {
|
2009-07-12 23:08:17 +02:00
|
|
|
override def hashCode: Int = System.identityHashCode(this);
|
|
|
|
|
override def equals(other: Any): Boolean = false
|
2009-06-22 13:13:58 +02:00
|
|
|
def remove(key: K)
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-19 10:58:20 +02:00
|
|
|
/**
|
|
|
|
|
* Not thread-safe, but should only be using from within an Actor, e.g. one single thread at a time.
|
2009-05-13 19:28:55 +02:00
|
|
|
*
|
2009-04-27 20:06:48 +02:00
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
2009-04-19 10:58:20 +02:00
|
|
|
*/
|
2009-05-13 19:28:55 +02:00
|
|
|
class InMemoryTransactionalMap[K, V] extends TransactionalMap[K, V] {
|
|
|
|
|
protected[kernel] var state = new HashTrie[K, V]
|
|
|
|
|
protected[kernel] var snapshot = state
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-05-13 19:28:55 +02:00
|
|
|
// ---- For Transactional ----
|
|
|
|
|
override def begin = snapshot = state
|
|
|
|
|
override def commit = snapshot = state
|
|
|
|
|
override def rollback = state = snapshot
|
2009-06-29 15:01:20 +02:00
|
|
|
|
2009-05-13 19:28:55 +02:00
|
|
|
// ---- Overriding scala.collection.mutable.Map behavior ----
|
2009-06-29 23:38:10 +02:00
|
|
|
override def contains(key: K): Boolean = {
|
|
|
|
|
verifyTransaction
|
|
|
|
|
state.contains(key)
|
|
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
override def clear = {
|
|
|
|
|
verifyTransaction
|
|
|
|
|
state = new HashTrie[K, V]
|
|
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
override def size: Int = {
|
|
|
|
|
verifyTransaction
|
|
|
|
|
state.size
|
|
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-05-13 19:28:55 +02:00
|
|
|
// ---- For scala.collection.mutable.Map ----
|
2009-06-29 23:38:10 +02:00
|
|
|
override def remove(key: K) = {
|
|
|
|
|
verifyTransaction
|
|
|
|
|
state = state - key
|
|
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
override def elements: Iterator[(K, V)] = {
|
|
|
|
|
// verifyTransaction
|
|
|
|
|
state.elements
|
|
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
override def get(key: K): Option[V] = {
|
|
|
|
|
verifyTransaction
|
|
|
|
|
state.get(key)
|
|
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-05-13 19:28:55 +02:00
|
|
|
override def put(key: K, value: V): Option[V] = {
|
2009-06-29 23:38:10 +02:00
|
|
|
verifyTransaction
|
2009-05-13 19:28:55 +02:00
|
|
|
val oldValue = state.get(key)
|
|
|
|
|
state = state.update(key, value)
|
|
|
|
|
oldValue
|
2009-04-19 10:58:20 +02:00
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
override def -=(key: K) = {
|
|
|
|
|
verifyTransaction
|
|
|
|
|
remove(key)
|
|
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
override def update(key: K, value: V) = {
|
|
|
|
|
verifyTransaction
|
|
|
|
|
put(key, value)
|
|
|
|
|
}
|
2009-04-27 19:55:57 +02:00
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
/**
|
2009-06-10 20:04:33 +02:00
|
|
|
* Base class for all persistent transactional map implementations should extend.
|
2009-05-13 19:28:55 +02:00
|
|
|
* Implements a Unit of Work, records changes into a change set.
|
2009-06-29 15:01:20 +02:00
|
|
|
*
|
2009-04-27 19:55:57 +02:00
|
|
|
* Not thread-safe, but should only be using from within an Actor, e.g. one single thread at a time.
|
2009-06-29 15:01:20 +02:00
|
|
|
*
|
2009-04-27 20:06:48 +02:00
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
2009-04-27 19:55:57 +02:00
|
|
|
*/
|
2009-05-13 19:28:55 +02:00
|
|
|
abstract class PersistentTransactionalMap[K, V] extends TransactionalMap[K, V] {
|
2009-06-10 20:04:33 +02:00
|
|
|
|
|
|
|
|
// FIXME: need to handle remove in another changeSet
|
2009-05-13 19:28:55 +02:00
|
|
|
protected[kernel] val changeSet = new HashMap[K, V]
|
2009-06-29 15:01:20 +02:00
|
|
|
|
2009-05-13 19:28:55 +02:00
|
|
|
def getRange(start: Int, count: Int)
|
|
|
|
|
|
|
|
|
|
// ---- For Transactional ----
|
2009-07-04 06:38:47 +02:00
|
|
|
override def begin = {}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-07-04 06:38:47 +02:00
|
|
|
override def rollback = changeSet.clear
|
|
|
|
|
|
2009-05-13 19:28:55 +02:00
|
|
|
// ---- For scala.collection.mutable.Map ----
|
|
|
|
|
override def put(key: K, value: V): Option[V] = {
|
2009-06-29 23:38:10 +02:00
|
|
|
verifyTransaction
|
2009-05-13 19:28:55 +02:00
|
|
|
changeSet += key -> value
|
|
|
|
|
None // always return None to speed up writes (else need to go to DB to get
|
|
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
override def remove(key: K) = {
|
|
|
|
|
verifyTransaction
|
|
|
|
|
changeSet -= key
|
|
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-05-13 19:28:55 +02:00
|
|
|
override def -=(key: K) = remove(key)
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-05-13 19:28:55 +02:00
|
|
|
override def update(key: K, value: V) = put(key, value)
|
2009-04-27 19:55:57 +02:00
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
/**
|
2009-07-03 17:15:36 +02:00
|
|
|
* Implements a persistent transactional map based on the Cassandra distributed P2P key-value storage.
|
2009-06-10 20:04:33 +02:00
|
|
|
*
|
2009-04-27 20:06:48 +02:00
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
2009-04-27 19:55:57 +02:00
|
|
|
*/
|
2009-06-11 13:47:07 +02:00
|
|
|
class CassandraPersistentTransactionalMap extends PersistentTransactionalMap[String, AnyRef] {
|
2009-05-13 19:28:55 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
override def getRange(start: Int, count: Int) = {
|
|
|
|
|
verifyTransaction
|
2009-07-12 23:08:17 +02:00
|
|
|
try {
|
2009-08-03 09:03:51 +02:00
|
|
|
CassandraStorage.getMapStorageRangeFor(uuid, start, count)
|
2009-07-12 23:08:17 +02:00
|
|
|
} catch {
|
|
|
|
|
case e: Exception => Nil
|
|
|
|
|
}
|
2009-06-29 23:38:10 +02:00
|
|
|
}
|
2009-05-13 19:28:55 +02:00
|
|
|
|
|
|
|
|
// ---- For Transactional ----
|
2009-04-27 19:55:57 +02:00
|
|
|
override def commit = {
|
2009-08-03 09:03:51 +02:00
|
|
|
CassandraStorage.insertMapStorageEntriesFor(uuid, changeSet.toList)
|
2009-07-04 06:38:47 +02:00
|
|
|
changeSet.clear
|
2009-04-27 19:55:57 +02:00
|
|
|
}
|
2009-06-10 20:04:33 +02:00
|
|
|
|
2009-05-13 19:28:55 +02:00
|
|
|
// ---- Overriding scala.collection.mutable.Map behavior ----
|
2009-06-29 23:38:10 +02:00
|
|
|
override def clear = {
|
|
|
|
|
verifyTransaction
|
2009-07-12 23:08:17 +02:00
|
|
|
try {
|
2009-08-03 09:03:51 +02:00
|
|
|
CassandraStorage.removeMapStorageFor(uuid)
|
2009-07-12 23:08:17 +02:00
|
|
|
} catch {
|
|
|
|
|
case e: Exception => {}
|
|
|
|
|
}
|
2009-06-29 23:38:10 +02:00
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
override def contains(key: String): Boolean = {
|
2009-07-12 23:08:17 +02:00
|
|
|
try {
|
|
|
|
|
verifyTransaction
|
2009-08-03 09:03:51 +02:00
|
|
|
CassandraStorage.getMapStorageEntryFor(uuid, key).isDefined
|
2009-07-12 23:08:17 +02:00
|
|
|
} catch {
|
|
|
|
|
case e: Exception => false
|
|
|
|
|
}
|
2009-06-29 23:38:10 +02:00
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
override def size: Int = {
|
|
|
|
|
verifyTransaction
|
2009-07-12 23:08:17 +02:00
|
|
|
try {
|
2009-08-03 09:03:51 +02:00
|
|
|
CassandraStorage.getMapStorageSizeFor(uuid)
|
2009-07-12 23:08:17 +02:00
|
|
|
} catch {
|
|
|
|
|
case e: Exception => 0
|
|
|
|
|
}
|
2009-06-29 23:38:10 +02:00
|
|
|
}
|
2009-06-10 20:04:33 +02:00
|
|
|
|
2009-05-13 19:28:55 +02:00
|
|
|
// ---- For scala.collection.mutable.Map ----
|
2009-06-29 17:33:38 +02:00
|
|
|
override def get(key: String): Option[AnyRef] = {
|
2009-06-29 23:38:10 +02:00
|
|
|
verifyTransaction
|
2009-07-12 23:08:17 +02:00
|
|
|
// if (changeSet.contains(key)) changeSet.get(key)
|
|
|
|
|
// else {
|
|
|
|
|
val result = try {
|
2009-08-03 09:03:51 +02:00
|
|
|
CassandraStorage.getMapStorageEntryFor(uuid, key)
|
2009-07-12 23:08:17 +02:00
|
|
|
} catch {
|
|
|
|
|
case e: Exception => None
|
|
|
|
|
}
|
|
|
|
|
result
|
|
|
|
|
//}
|
2009-06-29 17:33:38 +02:00
|
|
|
}
|
|
|
|
|
|
2009-05-13 19:28:55 +02:00
|
|
|
override def elements: Iterator[Tuple2[String, AnyRef]] = {
|
2009-06-29 23:38:10 +02:00
|
|
|
//verifyTransaction
|
2009-05-01 13:25:43 +02:00
|
|
|
new Iterator[Tuple2[String, AnyRef]] {
|
2009-07-12 23:08:17 +02:00
|
|
|
private val originalList: List[Tuple2[String, AnyRef]] = try {
|
2009-08-03 09:03:51 +02:00
|
|
|
CassandraStorage.getMapStorageFor(uuid)
|
2009-07-12 23:08:17 +02:00
|
|
|
} catch {
|
|
|
|
|
case e: Throwable => Nil
|
|
|
|
|
}
|
2009-04-27 19:55:57 +02:00
|
|
|
private var elements = originalList.reverse
|
2009-05-01 13:25:43 +02:00
|
|
|
override def next: Tuple2[String, AnyRef]= synchronized {
|
2009-04-27 19:55:57 +02:00
|
|
|
val element = elements.head
|
|
|
|
|
elements = elements.tail
|
|
|
|
|
element
|
2009-06-10 20:04:33 +02:00
|
|
|
}
|
2009-04-27 19:55:57 +02:00
|
|
|
override def hasNext: Boolean = synchronized { !elements.isEmpty }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
/**
|
|
|
|
|
* Base for all transactional vector implementations.
|
2009-06-10 20:04:33 +02:00
|
|
|
*
|
2009-04-27 20:06:48 +02:00
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
2009-04-27 19:55:57 +02:00
|
|
|
*/
|
2009-05-13 19:28:55 +02:00
|
|
|
abstract class TransactionalVector[T] extends Transactional with RandomAccessSeq[T] {
|
2009-07-12 23:08:17 +02:00
|
|
|
override def hashCode: Int = System.identityHashCode(this);
|
|
|
|
|
override def equals(other: Any): Boolean = false
|
|
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
def add(elem: T)
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
def get(index: Int): T
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-10 20:04:33 +02:00
|
|
|
def getRange(start: Int, count: Int): List[T]
|
2009-04-19 10:58:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2009-04-27 19:55:57 +02:00
|
|
|
* Implements an in-memory transactional vector.
|
2009-06-29 15:01:20 +02:00
|
|
|
*
|
2009-04-19 10:58:20 +02:00
|
|
|
* Not thread-safe, but should only be using from within an Actor, e.g. one single thread at a time.
|
2009-06-29 15:01:20 +02:00
|
|
|
*
|
2009-04-27 20:06:48 +02:00
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
2009-04-19 10:58:20 +02:00
|
|
|
*/
|
2009-04-27 19:55:57 +02:00
|
|
|
class InMemoryTransactionalVector[T] extends TransactionalVector[T] {
|
|
|
|
|
private[kernel] var state: Vector[T] = EmptyVector
|
|
|
|
|
private[kernel] var snapshot = state
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
def add(elem: T) = {
|
|
|
|
|
verifyTransaction
|
|
|
|
|
state = state + elem
|
|
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
def get(index: Int): T = {
|
|
|
|
|
verifyTransaction
|
|
|
|
|
state(index)
|
|
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
def getRange(start: Int, count: Int): List[T] = {
|
|
|
|
|
verifyTransaction
|
|
|
|
|
state.slice(start, count).toList.asInstanceOf[List[T]]
|
|
|
|
|
}
|
2009-05-13 19:28:55 +02:00
|
|
|
|
|
|
|
|
// ---- For Transactional ----
|
2009-04-27 19:55:57 +02:00
|
|
|
override def begin = snapshot = state
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
override def commit = snapshot = state
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
override def rollback = state = snapshot
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-05-13 19:28:55 +02:00
|
|
|
// ---- For Seq ----
|
2009-06-29 23:38:10 +02:00
|
|
|
def length: Int = {
|
|
|
|
|
verifyTransaction
|
|
|
|
|
state.length
|
|
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
def apply(index: Int): T = {
|
|
|
|
|
verifyTransaction
|
|
|
|
|
state(index)
|
|
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
override def elements: Iterator[T] = {
|
|
|
|
|
//verifyTransaction
|
|
|
|
|
state.elements
|
|
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
override def toList: List[T] = {
|
|
|
|
|
verifyTransaction
|
|
|
|
|
state.toList
|
|
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
}
|
|
|
|
|
|
2009-06-10 20:04:33 +02:00
|
|
|
/**
|
|
|
|
|
* Base class for all persistent transactional vector implementations should extend.
|
|
|
|
|
* Implements a Unit of Work, records changes into a change set.
|
|
|
|
|
*
|
|
|
|
|
* Not thread-safe, but should only be using from within an Actor, e.g. one single thread at a time.
|
|
|
|
|
*
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
|
|
|
|
abstract class PersistentTransactionalVector[T] extends TransactionalVector[T] {
|
|
|
|
|
|
|
|
|
|
// FIXME: need to handle remove in another changeSet
|
2009-07-12 23:08:17 +02:00
|
|
|
protected[kernel] val changeSet = new ArrayBuffer[T]
|
2009-06-10 20:04:33 +02:00
|
|
|
|
|
|
|
|
// ---- For Transactional ----
|
2009-07-04 06:38:47 +02:00
|
|
|
override def begin = {}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
|
|
|
|
override def rollback = changeSet.clear
|
2009-06-10 20:04:33 +02:00
|
|
|
|
|
|
|
|
// ---- For TransactionalVector ----
|
2009-06-29 23:38:10 +02:00
|
|
|
override def add(value: T) = {
|
|
|
|
|
verifyTransaction
|
2009-07-12 23:08:17 +02:00
|
|
|
changeSet += value
|
2009-06-29 23:38:10 +02:00
|
|
|
}
|
2009-06-10 20:04:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implements a persistent transactional vector based on the Cassandra distributed P2P key-value storage.
|
|
|
|
|
*
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
2009-06-11 13:47:07 +02:00
|
|
|
class CassandraPersistentTransactionalVector extends PersistentTransactionalVector[AnyRef] {
|
2009-06-10 20:04:33 +02:00
|
|
|
|
|
|
|
|
// ---- For TransactionalVector ----
|
2009-06-29 23:38:10 +02:00
|
|
|
override def get(index: Int): AnyRef = {
|
|
|
|
|
verifyTransaction
|
2009-07-12 23:08:17 +02:00
|
|
|
if (changeSet.size > index) changeSet(index)
|
2009-08-03 09:03:51 +02:00
|
|
|
else CassandraStorage.getVectorStorageEntryFor(uuid, index)
|
2009-06-29 23:38:10 +02:00
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
override def getRange(start: Int, count: Int): List[AnyRef] = {
|
|
|
|
|
verifyTransaction
|
2009-08-03 09:03:51 +02:00
|
|
|
CassandraStorage.getVectorStorageRangeFor(uuid, start, count)
|
2009-06-29 23:38:10 +02:00
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
override def length: Int = {
|
|
|
|
|
verifyTransaction
|
2009-08-03 09:03:51 +02:00
|
|
|
CassandraStorage.getVectorStorageSizeFor(uuid)
|
2009-06-29 23:38:10 +02:00
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-10 20:04:33 +02:00
|
|
|
override def apply(index: Int): AnyRef = get(index)
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-10 20:04:33 +02:00
|
|
|
override def first: AnyRef = get(0)
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 15:01:20 +02:00
|
|
|
override def last: AnyRef = {
|
2009-06-29 23:38:10 +02:00
|
|
|
verifyTransaction
|
2009-06-29 15:01:20 +02:00
|
|
|
val l = length
|
|
|
|
|
if (l == 0) throw new NoSuchElementException("Vector is empty")
|
|
|
|
|
get(length - 1)
|
|
|
|
|
}
|
2009-06-10 20:04:33 +02:00
|
|
|
|
|
|
|
|
// ---- For Transactional ----
|
|
|
|
|
override def commit = {
|
|
|
|
|
// FIXME: should use batch function once the bug is resolved
|
2009-08-03 09:03:51 +02:00
|
|
|
for (element <- changeSet) CassandraStorage.insertVectorStorageEntryFor(uuid, element)
|
2009-07-12 23:08:17 +02:00
|
|
|
changeSet.clear
|
2009-06-10 20:04:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
/**
|
|
|
|
|
* Implements a transactional reference.
|
2009-06-29 15:01:20 +02:00
|
|
|
*
|
2009-04-27 19:55:57 +02:00
|
|
|
* Not thread-safe, but should only be using from within an Actor, e.g. one single thread at a time.
|
2009-06-29 15:01:20 +02:00
|
|
|
*
|
2009-04-27 20:06:48 +02:00
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
2009-04-27 19:55:57 +02:00
|
|
|
*/
|
|
|
|
|
class TransactionalRef[T] extends Transactional {
|
|
|
|
|
private[kernel] var ref: Option[T] = None
|
|
|
|
|
private[kernel] var snapshot: Option[T] = None
|
|
|
|
|
|
|
|
|
|
override def begin = if (ref.isDefined) snapshot = Some(ref.get)
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
override def commit = if (ref.isDefined) snapshot = Some(ref.get)
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
override def rollback = if (snapshot.isDefined) ref = Some(snapshot.get)
|
|
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
def swap(elem: T) = {
|
|
|
|
|
verifyTransaction
|
|
|
|
|
ref = Some(elem)
|
|
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
def get: Option[T] = {
|
|
|
|
|
verifyTransaction
|
|
|
|
|
ref
|
|
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
def getOrElse(default: => T): T = {
|
|
|
|
|
verifyTransaction
|
|
|
|
|
ref.getOrElse(default)
|
|
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
def isDefined: Boolean = {
|
|
|
|
|
verifyTransaction
|
|
|
|
|
ref.isDefined
|
|
|
|
|
}
|
2009-04-27 19:55:57 +02:00
|
|
|
}
|
2009-06-11 13:47:07 +02:00
|
|
|
|
|
|
|
|
class CassandraPersistentTransactionalRef extends TransactionalRef[AnyRef] {
|
2009-07-04 06:38:47 +02:00
|
|
|
override def commit = if (ref.isDefined) {
|
2009-08-03 09:03:51 +02:00
|
|
|
CassandraStorage.insertRefStorageFor(uuid, ref.get)
|
2009-07-04 06:38:47 +02:00
|
|
|
ref = None
|
|
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-07-04 06:38:47 +02:00
|
|
|
override def rollback = ref = None
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
override def get: Option[AnyRef] = {
|
|
|
|
|
verifyTransaction
|
2009-08-03 09:03:51 +02:00
|
|
|
CassandraStorage.getRefStorageFor(uuid)
|
2009-06-29 23:38:10 +02:00
|
|
|
}
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-11 13:47:07 +02:00
|
|
|
override def isDefined: Boolean = get.isDefined
|
2009-07-12 23:08:17 +02:00
|
|
|
|
2009-06-11 13:47:07 +02:00
|
|
|
override def getOrElse(default: => AnyRef): AnyRef = {
|
|
|
|
|
val ref = get
|
|
|
|
|
if (ref.isDefined) ref
|
|
|
|
|
else default
|
|
|
|
|
}
|
2009-08-02 16:14:12 +02:00
|
|
|
}
|