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-08-03 09:03:51 +02:00
|
|
|
import java.io.{File, Flushable, Closeable}
|
2009-07-23 20:01:37 +02:00
|
|
|
|
|
|
|
|
import kernel.util.Logging
|
|
|
|
|
import serialization.{Serializer, Serializable, SerializationProtocol}
|
2009-04-19 10:58:20 +02:00
|
|
|
|
|
|
|
|
import org.apache.cassandra.config.DatabaseDescriptor
|
|
|
|
|
import org.apache.cassandra.service._
|
|
|
|
|
|
2009-08-03 09:03:51 +02:00
|
|
|
//import org.apache.thrift.server.TThreadPoolServer
|
2009-07-03 17:15:36 +02:00
|
|
|
import org.apache.thrift.TProcessorFactory
|
2009-08-03 09:03:51 +02:00
|
|
|
import org.apache.thrift.transport._
|
|
|
|
|
import org.apache.thrift._
|
|
|
|
|
import org.apache.thrift.transport._
|
|
|
|
|
import org.apache.thrift.protocol._
|
2009-07-03 17:15:36 +02:00
|
|
|
|
2009-04-19 10:58:20 +02:00
|
|
|
/**
|
2009-06-11 13:47:07 +02:00
|
|
|
* NOTE: requires command line options:
|
|
|
|
|
* <br/>
|
|
|
|
|
* <code>-Dcassandra -Dstorage-config=config/ -Dpidfile=akka.pid</code>
|
|
|
|
|
* <p/>
|
2009-04-19 10:58:20 +02:00
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
2009-07-23 20:01:37 +02:00
|
|
|
object CassandraStorage extends Logging {
|
2009-04-27 19:55:57 +02:00
|
|
|
val TABLE_NAME = "akka"
|
2009-06-11 13:47:07 +02:00
|
|
|
val MAP_COLUMN_FAMILY = "map"
|
|
|
|
|
val VECTOR_COLUMN_FAMILY = "vector"
|
|
|
|
|
val REF_COLUMN_FAMILY = "ref:item"
|
2009-08-02 11:47:00 +02:00
|
|
|
|
2009-07-03 17:15:36 +02:00
|
|
|
val IS_ASCENDING = true
|
2009-06-10 20:04:33 +02:00
|
|
|
|
2009-08-03 09:03:51 +02:00
|
|
|
import kernel.Kernel.config
|
|
|
|
|
|
|
|
|
|
val CASSANDRA_SERVER_HOSTNAME = config.getString("akka.storage.cassandra.hostname", "localhost")
|
|
|
|
|
val CASSANDRA_SERVER_PORT = config.getInt("akka.storage.cassandra.port", 9160)
|
|
|
|
|
val BLOCKING_CALL = if (config.getBool("akka.storage.cassandra.blocking", true)) 0
|
|
|
|
|
else 1
|
2009-08-02 16:14:12 +02:00
|
|
|
|
|
|
|
|
@volatile private[this] var isRunning = false
|
2009-08-03 09:03:51 +02:00
|
|
|
private[this] val protocol: Protocol = {
|
|
|
|
|
config.getString("akka.storage.cassandra.storage-format", "binary") match {
|
|
|
|
|
case "binary" => Protocol.Binary
|
|
|
|
|
case "json" => Protocol.JSON
|
|
|
|
|
case "simple-json" => Protocol.SimpleJSON
|
|
|
|
|
case unknown => throw new UnsupportedOperationException("Unknown storage serialization protocol [" + unknown + "]")
|
2009-08-02 16:14:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-08-03 09:03:51 +02:00
|
|
|
private[this] var sessions: Option[CassandraSessionPool[_]] = None
|
|
|
|
|
|
2009-08-02 16:14:12 +02:00
|
|
|
def start = synchronized {
|
|
|
|
|
if (!isRunning) {
|
|
|
|
|
try {
|
2009-08-03 09:03:51 +02:00
|
|
|
sessions = Some(new CassandraSessionPool(StackPool(SocketProvider(CASSANDRA_SERVER_HOSTNAME, CASSANDRA_SERVER_PORT)), protocol))
|
2009-08-02 16:14:12 +02:00
|
|
|
log.info("Cassandra persistent storage has started up successfully");
|
|
|
|
|
} catch {
|
|
|
|
|
case e =>
|
|
|
|
|
log.error("Could not start up Cassandra persistent storage")
|
|
|
|
|
throw e
|
|
|
|
|
}
|
|
|
|
|
isRunning
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-08-03 09:03:51 +02:00
|
|
|
|
|
|
|
|
def stop = synchronized {
|
|
|
|
|
if (isRunning && sessions.isDefined) sessions.get.close
|
2009-08-02 16:14:12 +02:00
|
|
|
}
|
2009-08-03 09:03:51 +02:00
|
|
|
|
|
|
|
|
//implicit def strToBytes(s: String) = s.getBytes("UTF-8")
|
2009-08-02 16:14:12 +02:00
|
|
|
|
2009-08-03 09:03:51 +02:00
|
|
|
/*
|
|
|
|
|
def insertRefStorageFor(name: String, element: AnyRef) = sessions.withSession { session => {
|
|
|
|
|
val user_id = "1"
|
|
|
|
|
session ++| ("users", user_id, "base_attributes:name", "Lord Foo Bar", false)
|
|
|
|
|
session ++| ("users", user_id, "base_attributes:age", "24", false)
|
|
|
|
|
for( i <- session / ("users", user_id, "base_attributes", None, None).toList) println(i)
|
|
|
|
|
}}
|
|
|
|
|
*/
|
2009-08-02 16:14:12 +02:00
|
|
|
// ===============================================================
|
|
|
|
|
// For Ref
|
|
|
|
|
// ===============================================================
|
|
|
|
|
|
2009-08-03 09:03:51 +02:00
|
|
|
def insertRefStorageFor(name: String, element: String) = if (sessions.isDefined) {
|
|
|
|
|
sessions.get.withSession {
|
|
|
|
|
_ ++| (
|
|
|
|
|
TABLE_NAME,
|
|
|
|
|
name,
|
|
|
|
|
REF_COLUMN_FAMILY,
|
|
|
|
|
element,
|
|
|
|
|
System.currentTimeMillis,
|
|
|
|
|
BLOCKING_CALL)
|
|
|
|
|
}
|
|
|
|
|
} else throw new IllegalStateException("CassandraStorage is not started")
|
2009-08-02 16:14:12 +02:00
|
|
|
|
2009-08-03 09:03:51 +02:00
|
|
|
def getRefStorageFor(name: String): Option[String] = if (sessions.isDefined) {
|
2009-08-02 16:14:12 +02:00
|
|
|
try {
|
2009-08-03 09:03:51 +02:00
|
|
|
val column = sessions.get.withSession { _ | (TABLE_NAME, name, REF_COLUMN_FAMILY) }
|
|
|
|
|
Some(column.value)
|
2009-08-02 16:14:12 +02:00
|
|
|
} catch {
|
|
|
|
|
case e =>
|
|
|
|
|
e.printStackTrace
|
|
|
|
|
None
|
|
|
|
|
}
|
2009-08-03 09:03:51 +02:00
|
|
|
} else throw new IllegalStateException("CassandraStorage is not started")
|
2009-08-02 16:14:12 +02:00
|
|
|
|
|
|
|
|
// ===============================================================
|
|
|
|
|
// For Vector
|
|
|
|
|
// ===============================================================
|
|
|
|
|
|
2009-08-03 09:03:51 +02:00
|
|
|
def insertVectorStorageEntryFor(name: String, element: String) = if (sessions.isDefined) {
|
|
|
|
|
sessions.get.withSession {
|
|
|
|
|
_ ++| (
|
|
|
|
|
TABLE_NAME,
|
|
|
|
|
name,
|
|
|
|
|
VECTOR_COLUMN_FAMILY + ":" + getVectorStorageSizeFor(name),
|
|
|
|
|
element,
|
|
|
|
|
System.currentTimeMillis,
|
|
|
|
|
BLOCKING_CALL)
|
|
|
|
|
}
|
|
|
|
|
} else throw new IllegalStateException("CassandraStorage is not started")
|
2009-08-02 16:14:12 +02:00
|
|
|
|
2009-08-03 09:03:51 +02:00
|
|
|
def getVectorStorageEntryFor(name: String, index: Int): String = if (sessions.isDefined) {
|
2009-08-02 16:14:12 +02:00
|
|
|
try {
|
2009-08-03 09:03:51 +02:00
|
|
|
val column = sessions.get.withSession { _ | (TABLE_NAME, name, VECTOR_COLUMN_FAMILY + ":" + index) }
|
|
|
|
|
column.value
|
2009-08-02 16:14:12 +02:00
|
|
|
} catch {
|
|
|
|
|
case e =>
|
|
|
|
|
e.printStackTrace
|
2009-08-03 09:03:51 +02:00
|
|
|
throw new NoSuchElementException(e.getMessage)
|
2009-08-02 16:14:12 +02:00
|
|
|
}
|
2009-08-03 09:03:51 +02:00
|
|
|
} else throw new IllegalStateException("CassandraStorage is not started")
|
2009-08-02 16:14:12 +02:00
|
|
|
|
2009-08-03 09:03:51 +02:00
|
|
|
def getVectorStorageRangeFor(name: String, start: Int, count: Int): List[String] = if (sessions.isDefined) {
|
|
|
|
|
sessions.get.withSession { _ / (TABLE_NAME, name, VECTOR_COLUMN_FAMILY, IS_ASCENDING, count) }.map(_.value)
|
|
|
|
|
} else throw new IllegalStateException("CassandraStorage is not started")
|
2009-08-02 16:14:12 +02:00
|
|
|
|
2009-08-03 09:03:51 +02:00
|
|
|
def getVectorStorageSizeFor(name: String): Int = if (sessions.isDefined) {
|
|
|
|
|
sessions.get.withSession { _ |# (TABLE_NAME, name, VECTOR_COLUMN_FAMILY) }
|
|
|
|
|
} else throw new IllegalStateException("CassandraStorage is not started")
|
2009-08-02 16:14:12 +02:00
|
|
|
|
|
|
|
|
// ===============================================================
|
|
|
|
|
// For Map
|
|
|
|
|
// ===============================================================
|
|
|
|
|
|
2009-08-03 09:03:51 +02:00
|
|
|
def insertMapStorageEntryFor(name: String, key: String, value: String) = if (sessions.isDefined) {
|
|
|
|
|
sessions.get.withSession {
|
|
|
|
|
_ ++| (
|
|
|
|
|
TABLE_NAME,
|
|
|
|
|
name,
|
|
|
|
|
MAP_COLUMN_FAMILY + ":" + key,
|
|
|
|
|
value,
|
|
|
|
|
System.currentTimeMillis,
|
|
|
|
|
BLOCKING_CALL)
|
|
|
|
|
}
|
|
|
|
|
} else throw new IllegalStateException("CassandraStorage is not started")
|
2009-08-02 16:14:12 +02:00
|
|
|
|
2009-08-03 09:03:51 +02:00
|
|
|
def insertMapStorageEntriesFor(name: String, entries: List[Tuple2[String, String]]) = if (sessions.isDefined) {
|
2009-08-02 16:14:12 +02:00
|
|
|
import java.util.{Map, HashMap, List, ArrayList}
|
|
|
|
|
val columns: Map[String, List[column_t]] = new HashMap
|
|
|
|
|
for (entry <- entries) {
|
|
|
|
|
val cls: List[column_t] = new ArrayList
|
2009-08-03 09:03:51 +02:00
|
|
|
cls.add(new column_t(entry._1, entry._2, System.currentTimeMillis))
|
2009-08-02 16:14:12 +02:00
|
|
|
columns.put(MAP_COLUMN_FAMILY, cls)
|
|
|
|
|
}
|
2009-08-03 09:03:51 +02:00
|
|
|
sessions.get.withSession {
|
|
|
|
|
_ ++| (
|
|
|
|
|
new batch_mutation_t(
|
|
|
|
|
TABLE_NAME,
|
|
|
|
|
name,
|
|
|
|
|
columns),
|
|
|
|
|
BLOCKING_CALL)
|
|
|
|
|
}
|
|
|
|
|
} else throw new IllegalStateException("CassandraStorage is not started")
|
2009-08-02 16:14:12 +02:00
|
|
|
|
2009-08-03 09:03:51 +02:00
|
|
|
def getMapStorageEntryFor(name: String, key: String): Option[String] = if (sessions.isDefined) {
|
2009-08-02 16:14:12 +02:00
|
|
|
try {
|
2009-08-03 09:03:51 +02:00
|
|
|
val column = sessions.get.withSession { _ | (TABLE_NAME, name, MAP_COLUMN_FAMILY + ":" + key) }
|
|
|
|
|
Some(column.value)
|
2009-08-02 16:14:12 +02:00
|
|
|
} catch {
|
|
|
|
|
case e =>
|
|
|
|
|
e.printStackTrace
|
|
|
|
|
None
|
|
|
|
|
}
|
2009-08-03 09:03:51 +02:00
|
|
|
} else throw new IllegalStateException("CassandraStorage is not started")
|
2009-08-02 16:14:12 +02:00
|
|
|
|
2009-08-03 09:03:51 +02:00
|
|
|
/*
|
|
|
|
|
def getMapStorageFor(name: String): List[Tuple2[String, String]] = if (sessions.isDefined) {
|
2009-08-02 16:14:12 +02:00
|
|
|
val columns = server.get_columns_since(TABLE_NAME, name, MAP_COLUMN_FAMILY, -1)
|
|
|
|
|
.toArray.toList.asInstanceOf[List[org.apache.cassandra.service.column_t]]
|
|
|
|
|
for {
|
|
|
|
|
column <- columns
|
2009-08-03 09:03:51 +02:00
|
|
|
col = (column.columnName, column.value)
|
2009-08-02 16:14:12 +02:00
|
|
|
} yield col
|
2009-08-03 09:03:51 +02:00
|
|
|
} else throw new IllegalStateException("CassandraStorage is not started")
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
def getMapStorageSizeFor(name: String): Int = if (sessions.isDefined) {
|
|
|
|
|
sessions.get.withSession { _ |# (TABLE_NAME, name, MAP_COLUMN_FAMILY) }
|
|
|
|
|
} else throw new IllegalStateException("CassandraStorage is not started")
|
|
|
|
|
|
|
|
|
|
def removeMapStorageFor(name: String) = if (sessions.isDefined) {
|
|
|
|
|
sessions.get.withSession { _ -- (TABLE_NAME, name, MAP_COLUMN_FAMILY, System.currentTimeMillis, BLOCKING_CALL) }
|
|
|
|
|
} else throw new IllegalStateException("CassandraStorage is not started")
|
|
|
|
|
|
|
|
|
|
def getMapStorageRangeFor(name: String, start: Int, count: Int): List[Tuple2[String, String]] = if (sessions.isDefined) {
|
|
|
|
|
sessions.get.withSession { _ / (TABLE_NAME, name, MAP_COLUMN_FAMILY, IS_ASCENDING, count) }.toArray.toList.asInstanceOf[List[Tuple2[String, String]]]
|
|
|
|
|
} else throw new IllegalStateException("CassandraStorage is not started")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trait CassandraSession extends Closeable with Flushable {
|
|
|
|
|
import scala.collection.jcl.Conversions._
|
|
|
|
|
import org.scala_tools.javautils.Imports._
|
|
|
|
|
|
|
|
|
|
private implicit def null2Option[T](t: T): Option[T] = if(t != null) Some(t) else None
|
|
|
|
|
|
|
|
|
|
protected val client: Cassandra.Client
|
|
|
|
|
|
|
|
|
|
val obtainedAt: Long
|
|
|
|
|
|
|
|
|
|
def /(tableName: String, key: String, columnParent: String, start: Option[Int],end: Option[Int]): List[column_t] =
|
|
|
|
|
client.get_slice(tableName, key, columnParent, start.getOrElse(-1),end.getOrElse(-1)).toList
|
2009-08-02 16:14:12 +02:00
|
|
|
|
2009-08-03 09:03:51 +02:00
|
|
|
def /(tableName: String, key: String, columnParent: String, colNames: List[String]): List[column_t] =
|
|
|
|
|
client.get_slice_by_names(tableName, key, columnParent, colNames.asJava ).toList
|
2009-08-02 16:14:12 +02:00
|
|
|
|
2009-08-03 09:03:51 +02:00
|
|
|
def |(tableName: String, key: String, colPath: String): Option[column_t] =
|
|
|
|
|
client.get_column(tableName, key, colPath)
|
|
|
|
|
|
|
|
|
|
def |#(tableName: String, key: String, columnParent: String): Int =
|
|
|
|
|
client.get_column_count(tableName, key, columnParent)
|
|
|
|
|
|
|
|
|
|
def ++|(tableName: String, key: String, columnPath: String, cellData: Array[Byte], timestamp: Long, block: Int) =
|
|
|
|
|
client.insert(tableName, key, columnPath, cellData,timestamp,block)
|
|
|
|
|
|
|
|
|
|
def ++|(tableName: String, key: String, columnPath: String, cellData: Array[Byte], block: Int) =
|
|
|
|
|
client.insert(tableName,key,columnPath,cellData,obtainedAt,block)
|
|
|
|
|
|
|
|
|
|
def ++|(batch: batch_mutation_t, block: Int) =
|
|
|
|
|
client.batch_insert(batch, block)
|
|
|
|
|
|
|
|
|
|
def --(tableName: String, key: String, columnPathOrParent: String, timestamp: Long, block: Int) =
|
|
|
|
|
client.remove(tableName, key, columnPathOrParent, timestamp, block)
|
|
|
|
|
|
|
|
|
|
def --(tableName: String, key: String, columnPathOrParent: String, block: Int) =
|
|
|
|
|
client.remove(tableName, key, columnPathOrParent, obtainedAt, block)
|
|
|
|
|
|
|
|
|
|
def /@(tableName: String, key: String, columnParent: String, timestamp: Long): List[column_t] =
|
|
|
|
|
client.get_columns_since(tableName, key, columnParent, timestamp).toList
|
|
|
|
|
|
|
|
|
|
def /^(tableName: String, key: String, columnFamily: String, start: Option[Int], end: Option[Int], count: Int ): List[superColumn_t] =
|
|
|
|
|
client.get_slice_super(tableName, key,columnFamily, start.getOrElse(-1), end.getOrElse(-1)).toList //TODO upgrade thrift interface to support count
|
2009-08-02 16:14:12 +02:00
|
|
|
|
2009-08-03 09:03:51 +02:00
|
|
|
def /^(tableName: String, key: String, columnFamily: String, superColNames: List[String]): List[superColumn_t] =
|
|
|
|
|
client.get_slice_super_by_names(tableName, key, columnFamily, superColNames.asJava).toList
|
|
|
|
|
|
|
|
|
|
def |^(tableName: String, key: String, superColumnPath: String): Option[superColumn_t] =
|
|
|
|
|
client.get_superColumn(tableName,key,superColumnPath)
|
|
|
|
|
|
|
|
|
|
def ++|^ (batch: batch_mutation_super_t, block: Int) =
|
|
|
|
|
client.batch_insert_superColumn(batch, block)
|
|
|
|
|
|
|
|
|
|
def keys(tableName: String, startsWith: String, stopsAt: String, maxResults: Option[Int]): List[String] =
|
|
|
|
|
client.get_key_range(tableName, startsWith, stopsAt, maxResults.getOrElse(-1)).toList
|
|
|
|
|
|
|
|
|
|
def property(name: String): String = client.getStringProperty(name)
|
|
|
|
|
def properties(name: String): List[String] = client.getStringListProperty(name).toList
|
|
|
|
|
def describeTable(tableName: String) = client.describeTable(tableName)
|
|
|
|
|
|
|
|
|
|
def ?(query: String) = client.executeQuery(query)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CassandraSessionPool[T <: TTransport](transportPool: Pool[T], inputProtocol: Protocol, outputProtocol: Protocol) extends Closeable {
|
|
|
|
|
def this(transportPool: Pool[T], ioProtocol: Protocol) = this(transportPool,ioProtocol,ioProtocol)
|
|
|
|
|
|
|
|
|
|
def newSession: CassandraSession = {
|
|
|
|
|
val t = transportPool.borrowObject
|
|
|
|
|
val c = new Cassandra.Client(inputProtocol(t),outputProtocol(t))
|
|
|
|
|
new CassandraSession {
|
|
|
|
|
val client = c
|
|
|
|
|
val obtainedAt = System.currentTimeMillis
|
|
|
|
|
def flush = t.flush
|
|
|
|
|
def close = transportPool.returnObject(t)
|
|
|
|
|
}
|
2009-08-02 16:14:12 +02:00
|
|
|
}
|
2009-08-03 09:03:51 +02:00
|
|
|
|
|
|
|
|
def withSession[R](body: CassandraSession => R) = {
|
|
|
|
|
val session = newSession
|
|
|
|
|
try {
|
|
|
|
|
val result = body(session)
|
|
|
|
|
session.flush
|
|
|
|
|
result
|
|
|
|
|
} finally {
|
|
|
|
|
session.close
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def close = transportPool.close
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sealed abstract class Protocol(val factory: TProtocolFactory) {
|
|
|
|
|
def apply(transport: TTransport) = factory.getProtocol(transport)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object Protocol {
|
|
|
|
|
object Binary extends Protocol(new TBinaryProtocol.Factory)
|
|
|
|
|
object SimpleJSON extends Protocol(new TSimpleJSONProtocol.Factory)
|
|
|
|
|
object JSON extends Protocol(new TJSONProtocol.Factory)
|
2009-08-02 16:14:12 +02:00
|
|
|
}
|
|
|
|
|
|
2009-04-19 10:58:20 +02:00
|
|
|
/**
|
2009-06-11 13:47:07 +02:00
|
|
|
* NOTE: requires command line options:
|
|
|
|
|
* <br/>
|
|
|
|
|
* <code>-Dcassandra -Dstorage-config=config/ -Dpidfile=akka.pid</code>
|
|
|
|
|
* <p/>
|
2009-04-19 10:58:20 +02:00
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
2009-08-03 09:03:51 +02:00
|
|
|
*
|
|
|
|
|
object EmbeddedCassandraStorage extends Logging {
|
2009-04-27 19:55:57 +02:00
|
|
|
val TABLE_NAME = "akka"
|
2009-06-11 13:47:07 +02:00
|
|
|
val MAP_COLUMN_FAMILY = "map"
|
|
|
|
|
val VECTOR_COLUMN_FAMILY = "vector"
|
|
|
|
|
val REF_COLUMN_FAMILY = "ref:item"
|
2009-08-02 11:47:00 +02:00
|
|
|
|
2009-07-03 17:15:36 +02:00
|
|
|
val IS_ASCENDING = true
|
2009-06-10 20:04:33 +02:00
|
|
|
|
2009-07-03 17:15:36 +02:00
|
|
|
val RUN_THRIFT_SERVICE = kernel.Kernel.config.getBool("akka.storage.cassandra.thrift-server.service", false)
|
2009-07-12 23:08:17 +02:00
|
|
|
val BLOCKING_CALL = {
|
|
|
|
|
if (kernel.Kernel.config.getBool("akka.storage.cassandra.blocking", true)) 0
|
|
|
|
|
else 1
|
|
|
|
|
}
|
2009-07-04 06:38:47 +02:00
|
|
|
|
|
|
|
|
@volatile private[this] var isRunning = false
|
2009-07-03 17:15:36 +02:00
|
|
|
private[this] val serializer: Serializer = {
|
2009-07-23 20:01:37 +02:00
|
|
|
kernel.Kernel.config.getString("akka.storage.cassandra.storage-format", "java") match {
|
|
|
|
|
case "scala-json" => Serializer.ScalaJSON
|
|
|
|
|
case "java-json" => Serializer.JavaJSON
|
2009-07-28 17:14:54 +02:00
|
|
|
case "protobuf" => Serializer.Protobuf
|
2009-07-23 20:01:37 +02:00
|
|
|
case "java" => Serializer.Java
|
2009-07-28 17:14:54 +02:00
|
|
|
case "sbinary" => throw new UnsupportedOperationException("SBinary serialization protocol is not yet supported for storage")
|
|
|
|
|
case "avro" => throw new UnsupportedOperationException("Avro serialization protocol is not yet supported for storage")
|
2009-07-24 00:41:42 +02:00
|
|
|
case unknown => throw new UnsupportedOperationException("Unknown storage serialization protocol [" + unknown + "]")
|
2009-07-03 17:15:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
2009-08-02 11:47:00 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
// TODO: is this server thread-safe or needed to be wrapped up in an actor?
|
2009-05-13 19:28:55 +02:00
|
|
|
private[this] val server = classOf[CassandraServer].newInstance.asInstanceOf[CassandraServer]
|
2009-07-03 17:15:36 +02:00
|
|
|
|
|
|
|
|
private[this] var thriftServer: CassandraThriftServer = _
|
2009-05-01 13:25:43 +02:00
|
|
|
|
2009-07-04 06:38:47 +02:00
|
|
|
def start = synchronized {
|
|
|
|
|
if (!isRunning) {
|
|
|
|
|
try {
|
|
|
|
|
server.start
|
2009-07-06 23:45:15 +02:00
|
|
|
log.info("Cassandra persistent storage has started up successfully");
|
2009-07-04 06:38:47 +02:00
|
|
|
} catch {
|
|
|
|
|
case e =>
|
2009-07-06 23:45:15 +02:00
|
|
|
log.error("Could not start up Cassandra persistent storage")
|
2009-07-04 06:38:47 +02:00
|
|
|
throw e
|
|
|
|
|
}
|
|
|
|
|
if (RUN_THRIFT_SERVICE) {
|
|
|
|
|
thriftServer = new CassandraThriftServer(server)
|
|
|
|
|
thriftServer.start
|
|
|
|
|
}
|
|
|
|
|
isRunning
|
2009-07-03 17:15:36 +02:00
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
}
|
|
|
|
|
|
2009-07-04 06:38:47 +02:00
|
|
|
def stop = if (isRunning) {
|
2009-07-03 17:15:36 +02:00
|
|
|
//server.storageService.shutdown
|
|
|
|
|
if (RUN_THRIFT_SERVICE) thriftServer.stop
|
|
|
|
|
}
|
2009-04-27 19:55:57 +02:00
|
|
|
|
2009-06-10 20:04:33 +02:00
|
|
|
// ===============================================================
|
|
|
|
|
// For Ref
|
|
|
|
|
// ===============================================================
|
|
|
|
|
|
2009-06-11 13:47:07 +02:00
|
|
|
def insertRefStorageFor(name: String, element: AnyRef) = {
|
|
|
|
|
server.insert(
|
|
|
|
|
TABLE_NAME,
|
|
|
|
|
name,
|
|
|
|
|
REF_COLUMN_FAMILY,
|
2009-08-03 09:03:51 +02:00
|
|
|
element,
|
2009-06-11 13:47:07 +02:00
|
|
|
System.currentTimeMillis,
|
2009-07-03 17:15:36 +02:00
|
|
|
BLOCKING_CALL)
|
2009-06-11 13:47:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def getRefStorageFor(name: String): Option[AnyRef] = {
|
|
|
|
|
try {
|
|
|
|
|
val column = server.get_column(TABLE_NAME, name, REF_COLUMN_FAMILY)
|
2009-07-12 23:08:17 +02:00
|
|
|
Some(serializer.in(column.value, None))
|
2009-06-11 13:47:07 +02:00
|
|
|
} catch {
|
|
|
|
|
case e =>
|
|
|
|
|
e.printStackTrace
|
2009-07-12 23:08:17 +02:00
|
|
|
None
|
2009-06-11 13:47:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-10 20:04:33 +02:00
|
|
|
// ===============================================================
|
|
|
|
|
// For Vector
|
|
|
|
|
// ===============================================================
|
|
|
|
|
|
|
|
|
|
def insertVectorStorageEntryFor(name: String, element: AnyRef) = {
|
|
|
|
|
server.insert(
|
|
|
|
|
TABLE_NAME,
|
2009-06-11 13:47:07 +02:00
|
|
|
name,
|
|
|
|
|
VECTOR_COLUMN_FAMILY + ":" + getVectorStorageSizeFor(name),
|
2009-08-03 09:03:51 +02:00
|
|
|
element,
|
2009-06-10 20:04:33 +02:00
|
|
|
System.currentTimeMillis,
|
2009-07-03 17:15:36 +02:00
|
|
|
BLOCKING_CALL)
|
2009-06-10 20:04:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def getVectorStorageEntryFor(name: String, index: Int): AnyRef = {
|
|
|
|
|
try {
|
2009-06-11 13:47:07 +02:00
|
|
|
val column = server.get_column(TABLE_NAME, name, VECTOR_COLUMN_FAMILY + ":" + index)
|
2009-07-12 23:08:17 +02:00
|
|
|
serializer.in(column.value, None)
|
2009-06-10 20:04:33 +02:00
|
|
|
} catch {
|
2009-06-25 13:07:58 +02:00
|
|
|
case e =>
|
|
|
|
|
e.printStackTrace
|
|
|
|
|
throw new Predef.NoSuchElementException(e.getMessage)
|
2009-06-10 20:04:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def getVectorStorageRangeFor(name: String, start: Int, count: Int): List[AnyRef] =
|
2009-07-03 17:15:36 +02:00
|
|
|
server.get_slice(TABLE_NAME, name, VECTOR_COLUMN_FAMILY, IS_ASCENDING, count)
|
2009-06-10 20:04:33 +02:00
|
|
|
.toArray.toList.asInstanceOf[List[Tuple2[String, AnyRef]]].map(tuple => tuple._2)
|
|
|
|
|
|
|
|
|
|
def getVectorStorageSizeFor(name: String): Int =
|
2009-06-11 13:47:07 +02:00
|
|
|
server.get_column_count(TABLE_NAME, name, VECTOR_COLUMN_FAMILY)
|
2009-06-10 20:04:33 +02:00
|
|
|
|
|
|
|
|
// ===============================================================
|
|
|
|
|
// For Map
|
|
|
|
|
// ===============================================================
|
|
|
|
|
|
|
|
|
|
def insertMapStorageEntryFor(name: String, key: String, value: AnyRef) = {
|
2009-04-27 19:55:57 +02:00
|
|
|
server.insert(
|
|
|
|
|
TABLE_NAME,
|
2009-06-11 13:47:07 +02:00
|
|
|
name,
|
|
|
|
|
MAP_COLUMN_FAMILY + ":" + key,
|
2009-06-10 20:04:33 +02:00
|
|
|
serializer.out(value),
|
2009-05-13 19:28:55 +02:00
|
|
|
System.currentTimeMillis,
|
2009-07-03 17:15:36 +02:00
|
|
|
BLOCKING_CALL)
|
2009-04-27 19:55:57 +02:00
|
|
|
}
|
|
|
|
|
|
2009-06-10 20:04:33 +02:00
|
|
|
def insertMapStorageEntriesFor(name: String, entries: List[Tuple2[String, AnyRef]]) = {
|
2009-04-27 19:55:57 +02:00
|
|
|
import java.util.{Map, HashMap, List, ArrayList}
|
|
|
|
|
val columns: Map[String, List[column_t]] = new HashMap
|
|
|
|
|
for (entry <- entries) {
|
|
|
|
|
val cls: List[column_t] = new ArrayList
|
2009-05-01 13:25:43 +02:00
|
|
|
cls.add(new column_t(entry._1, serializer.out(entry._2), System.currentTimeMillis))
|
2009-06-11 13:47:07 +02:00
|
|
|
columns.put(MAP_COLUMN_FAMILY, cls)
|
2009-04-27 19:55:57 +02:00
|
|
|
}
|
2009-05-13 19:28:55 +02:00
|
|
|
server.batch_insert(new batch_mutation_t(
|
2009-04-27 19:55:57 +02:00
|
|
|
TABLE_NAME,
|
2009-06-11 13:47:07 +02:00
|
|
|
name,
|
2009-05-13 19:28:55 +02:00
|
|
|
columns),
|
2009-07-03 17:15:36 +02:00
|
|
|
BLOCKING_CALL)
|
2009-04-27 19:55:57 +02:00
|
|
|
}
|
|
|
|
|
|
2009-06-10 20:04:33 +02:00
|
|
|
def getMapStorageEntryFor(name: String, key: AnyRef): Option[AnyRef] = {
|
2009-04-27 19:55:57 +02:00
|
|
|
try {
|
2009-06-11 13:47:07 +02:00
|
|
|
val column = server.get_column(TABLE_NAME, name, MAP_COLUMN_FAMILY + ":" + key)
|
2009-07-12 23:08:17 +02:00
|
|
|
Some(serializer.in(column.value, None))
|
2009-06-25 13:07:58 +02:00
|
|
|
} catch {
|
|
|
|
|
case e =>
|
|
|
|
|
e.printStackTrace
|
|
|
|
|
None
|
|
|
|
|
}
|
2009-04-27 19:55:57 +02:00
|
|
|
}
|
|
|
|
|
|
2009-06-10 20:04:33 +02:00
|
|
|
def getMapStorageFor(name: String): List[Tuple2[String, AnyRef]] = {
|
2009-06-11 13:47:07 +02:00
|
|
|
val columns = server.get_columns_since(TABLE_NAME, name, MAP_COLUMN_FAMILY, -1)
|
2009-04-27 19:55:57 +02:00
|
|
|
.toArray.toList.asInstanceOf[List[org.apache.cassandra.service.column_t]]
|
|
|
|
|
for {
|
|
|
|
|
column <- columns
|
2009-07-12 23:08:17 +02:00
|
|
|
col = (column.columnName, serializer.in(column.value, None))
|
2009-04-27 19:55:57 +02:00
|
|
|
} yield col
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-10 20:04:33 +02:00
|
|
|
def getMapStorageSizeFor(name: String): Int =
|
2009-06-11 13:47:07 +02:00
|
|
|
server.get_column_count(TABLE_NAME, name, MAP_COLUMN_FAMILY)
|
2009-04-27 19:55:57 +02:00
|
|
|
|
2009-06-10 20:04:33 +02:00
|
|
|
def removeMapStorageFor(name: String) =
|
2009-07-03 17:15:36 +02:00
|
|
|
server.remove(TABLE_NAME, name, MAP_COLUMN_FAMILY, System.currentTimeMillis, BLOCKING_CALL)
|
2009-04-27 19:55:57 +02:00
|
|
|
|
2009-07-03 17:15:36 +02:00
|
|
|
def getMapStorageRangeFor(name: String, start: Int, count: Int): List[Tuple2[String, AnyRef]] = {
|
|
|
|
|
server.get_slice(TABLE_NAME, name, MAP_COLUMN_FAMILY, IS_ASCENDING, count)
|
|
|
|
|
.toArray.toList.asInstanceOf[List[Tuple2[String, AnyRef]]]
|
|
|
|
|
}
|
2009-04-27 19:55:57 +02:00
|
|
|
}
|
|
|
|
|
|
2009-08-03 09:03:51 +02:00
|
|
|
|
2009-07-03 17:15:36 +02:00
|
|
|
class CassandraThriftServer(server: CassandraServer) extends Logging {
|
|
|
|
|
case object Start
|
|
|
|
|
case object Stop
|
2009-04-27 19:55:57 +02:00
|
|
|
|
|
|
|
|
private[this] val serverEngine: TThreadPoolServer = try {
|
2009-07-03 17:15:36 +02:00
|
|
|
val pidFile = kernel.Kernel.config.getString("akka.storage.cassandra.thrift-server.pidfile", "akka.pid")
|
2009-04-19 10:58:20 +02:00
|
|
|
if (pidFile != null) new File(pidFile).deleteOnExit();
|
|
|
|
|
val listenPort = DatabaseDescriptor.getThriftPort
|
|
|
|
|
|
|
|
|
|
val processor = new Cassandra.Processor(server)
|
|
|
|
|
val tServerSocket = new TServerSocket(listenPort)
|
|
|
|
|
val tProtocolFactory = new TBinaryProtocol.Factory
|
|
|
|
|
|
|
|
|
|
val options = new TThreadPoolServer.Options
|
|
|
|
|
options.minWorkerThreads = 64
|
|
|
|
|
new TThreadPoolServer(new TProcessorFactory(processor),
|
|
|
|
|
tServerSocket,
|
|
|
|
|
new TTransportFactory,
|
|
|
|
|
new TTransportFactory,
|
|
|
|
|
tProtocolFactory,
|
|
|
|
|
tProtocolFactory,
|
|
|
|
|
options)
|
|
|
|
|
} catch {
|
|
|
|
|
case e =>
|
2009-07-06 23:45:15 +02:00
|
|
|
log.error("Could not start up Cassandra thrift service")
|
2009-04-19 10:58:20 +02:00
|
|
|
throw e
|
|
|
|
|
}
|
2009-07-03 17:15:36 +02:00
|
|
|
|
|
|
|
|
import scala.actors.Actor._
|
2009-04-27 19:55:57 +02:00
|
|
|
private[this] val serverDaemon = actor {
|
|
|
|
|
receive {
|
2009-07-03 17:15:36 +02:00
|
|
|
case Start =>
|
2009-04-27 19:55:57 +02:00
|
|
|
serverEngine.serve
|
2009-07-06 23:45:15 +02:00
|
|
|
log.info("Cassandra thrift service has starting up successfully")
|
|
|
|
|
case Stop =>
|
2009-07-03 17:15:36 +02:00
|
|
|
log.info("Cassandra thrift service is shutting down...")
|
2009-04-27 19:55:57 +02:00
|
|
|
serverEngine.stop
|
2009-04-19 10:58:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
2009-04-27 19:55:57 +02:00
|
|
|
|
2009-07-03 17:15:36 +02:00
|
|
|
def start = serverDaemon ! Start
|
|
|
|
|
def stop = serverDaemon ! Stop
|
|
|
|
|
}
|
2009-08-03 09:03:51 +02:00
|
|
|
*/
|