Skip to content

Instantly share code, notes, and snippets.

@matthandlersux
Created October 24, 2013 18:49
Show Gist options
  • Save matthandlersux/7142859 to your computer and use it in GitHub Desktop.
Save matthandlersux/7142859 to your computer and use it in GitHub Desktop.
showing jason my spikes
// file 1
package active_column.driver
import me.prettyprint.cassandra.serializers.ByteBufferSerializer
import me.prettyprint.cassandra.serializers.DoubleSerializer
import me.prettyprint.cassandra.serializers.DynamicCompositeSerializer
import me.prettyprint.cassandra.serializers.IntegerSerializer
import me.prettyprint.cassandra.serializers.LongSerializer
import me.prettyprint.cassandra.serializers.SerializerTypeInferer
import me.prettyprint.cassandra.serializers.StringSerializer
import me.prettyprint.cassandra.serializers.BooleanSerializer
import me.prettyprint.cassandra.serializers.FloatSerializer
import me.prettyprint.cassandra.serializers.AbstractSerializer
import java.lang.{Integer => JInt, Long => JLong, Float => JFloat, Double => JDouble }
import me.prettyprint.hector.api.beans.DynamicComposite
import me.prettyprint.hector.api.ddl.ComparatorType
import java.nio.ByteBuffer
object Serializers {
val BOOLEAN = BooleanSerializer.get()
val DOUBLE = DoubleSerializer.get()
val INT = IntegerSerializer.get()
val STRING = StringSerializer.get()
val FLOAT = FloatSerializer.get()
val LONG = LongSerializer.get()
val BYTE_BUFFER = ByteBufferSerializer.get()
val DYNAMIC_COMPOSITE = DynamicCompositeSerializer.get()
trait Serializable[A] {
type Serialized
def get: AbstractSerializer[A]
def comparator: String
def serialize(s: A): Serialized
def fromSerialized(b: Serialized): Option[A]
}
trait CassandraSerializable[T] extends Serializable[T] {
type Serialized = java.nio.ByteBuffer
def serialize(s: T): Serialized = get.toByteBuffer(s)
def fromSerialized(b: Serialized): Option[T] = try {
Option(get.fromByteBuffer(b))
} catch {
case _: Exception => None
}
}
object CassandraSerializable {
implicit object SerializableString extends CassandraSerializable[String] {
def get = STRING
def comparator = ComparatorType.UTF8TYPE.getClassName()
}
implicit object SerializableInt extends CassandraSerializable[JInt] {
def get = INT
def comparator = ComparatorType.INTEGERTYPE.getClassName()
}
// hector 1.05 doesnt have float/double implemented for some reason
implicit object SerializableDouble extends CassandraSerializable[JDouble] {
def get = DOUBLE
def comparator = "org.apache.cassandra.db.marshal.DoubleType"
}
implicit object SerializableFloat extends CassandraSerializable[JFloat] {
def get = FLOAT
def comparator = "org.apache.cassandra.db.marshal.FloatType"
}
implicit object SerializableLong extends CassandraSerializable[JLong] {
def get = LONG
def comparator = ComparatorType.INTEGERTYPE.getClassName()
}
implicit object SerializableDynamicComposite extends CassandraSerializable[DynamicComposite] {
def get = DYNAMIC_COMPOSITE
def comparator = ComparatorType.INTEGERTYPE.getClassName()
}
}
}
// file 2
package active_column.orm2
import active_column.driver.{Keyspace}
import active_column.driver.Serializers
import active_column.driver.Serializers.Serializable
import java.util.{ UUID => JUUID }
import java.nio.ByteBuffer
import scala.reflect._
trait NameReflection {
val modelName = {
val targetName = this.getClass.getName
targetName.replaceAll("\\$+", ".").replaceAll("\\.\\d\\.",".").replaceAll("\\.\\d+$","").split("\\.").last
}
}
trait UUIDKey {
protected def UUID = JUUID.randomUUID().toString()
}
abstract class ActiveColumn[K : Serializable](val keyspace: Keyspace/* = ActiveColumn.KEYSPACE*/)
extends OrmInstance[K]
with NameReflection {
val id: Option[K] = None
val columnFamilyName: String = modelName + "s"
}
trait OrmInstance[K] extends OrmModel[K]
with OrmSingleAssociation
trait OrmModel[K] extends OrmField {
// state
val id: Option[K]
def key = id
// maintenance
def initialize: Unit = {}
def reset = for ((_, field) <- CachedField.all) field.unset
}
trait OrmField {
import Serializers.Serializable
trait FieldValue[T] {
var value: Option[T] = None
var dirty = true
var replacing = false
var time = System.currentTimeMillis
def set(t: T) = {
if (! (value map (_ == t) getOrElse false)) dirtyOperation
value = Some(t)
}
def unset = {
if (value.isEmpty) dirtyOperation
value = None
}
private def dirtyOperation = {
if (! dirty) replacing = true
dirty = true
time = System.currentTimeMillis
}
}
// fields
class CachedField[T : Serializable](name: String) extends FieldValue[T] {
CachedField.add(name, this)
}
object CachedField {
implicit def toOpt[T](f: FieldValue[T]): Option[T] = f.value
var all = Map[String, FieldValue[_]]()
def add[T](name: String, field: FieldValue[T])(implicit s: Serializable[T]): Unit =
all = all + (name -> field)
}
}
abstract class OrmCompanion[T, Model <: OrmModel[T]](implicit tag: ClassTag[Model]) {
private lazy val thisClass = tag.runtimeClass.asInstanceOf[Class[Model]]
def makeInstance: Model = thisClass.newInstance
def find(key: T): Option[Model] = Some(makeInstance)
}
trait OrmCassandraPersistence extends OrmField { self: OrmModel[_] =>
import Serializers.CassandraSerializable
class PersistedField[T : Serializable](name: String) extends CachedField[T](name) {
PersistedField.add(name, this)
}
object PersistedField {
implicit def toOpt[T](f: FieldValue[T]): Option[T] = f.value
var getters = Map[String, () => Option[ByteBuffer]]()
var setters = Map[String, ByteBuffer => Unit]()
def add[T](name: String, field: FieldValue[T])(implicit s: Serializable[T]): Unit = {
val toSerialized = () => field map { v => s.serialize(v) }
val fromSerialized = (serialized: ByteBuffer) => s.fromSerialized(serialized) foreach { v => field set v }
getters = getters + (name -> toSerialized)
setters = setters + (name -> fromSerialized)
}
}
}
trait OrmSingleAssociation extends OrmField { self: OrmModel[_] =>
import Serializers.Serializable
// def showAll = Field.all
class BelongsToField[T : Serializable, Model <: OrmModel[T]](name: String, val companion: OrmCompanion[T, Model]) extends FieldValue[Model] {
val keyField = new CachedField[T](name)
override def set(m: Model) = {
if (m.key.isEmpty)
throw new Exception("No support for setting non-persisted models")
else {
super.set(m)
m.key map keyField.set
}
}
override def unset = {
keyField.unset
super.unset
}
}
object BelongsToField {
implicit def toOpt[T, Model <: OrmModel[T]](f: BelongsToField[T, Model]): Option[Model] = {
f.value orElse (f.keyField flatMap {key => f.companion.find(key) })
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment