Are there any examples of implementing the counter operation within phantom-dsl?
Have checked:
http://outworkers.com/blog/post/a-series-on-cassandra-part-3-advanced-features
https://github.com/outworkers/phantom/wiki/Counter-columns
Kinda looking for a phantom-dsl version of this info:
https://github.com/Netflix/astyanax/wiki/Working-with-counter-columns
The following is a partial implementation. It has risen two questions:
I am not sure how to take values from within the application and implement the increment counter operation in a counter column within a counter table.
How to update Rows within tables relating to the same entry where the tables have different number of rows and keys.
In thiagos example the two tables; 'songs' & 'songs_by_artist' both have the same rows but with different partitions (primary keys / clustering columns)
I am not sure how in phantom-dsl one would update rows relating to the same entries, such as with "records" & "record_transaction_counts" tables below.
e.g.
RecordTransactionCounts.{hash, time} relates to Records.{hash, time}
case class Record(hash: String,
size: Int,
time: Long,
difficulty: Float)
sealed class RecordsModel extends CassandraTable[RecordsModel, Record] {
override def fromRow(row: Row): Record = {
Record(
hash(row),
size(row),
time(row),
difficulty(row)
)
}
object hash extends StringColumn(this) with PartitionKey[String]
object size extends IntColumn(this)
object time extends LongColumn(this)
object difficulty extends FloatColumn(this)
}
abstract class ConcreteRecordsModel extends RecordsModel with RootConnector {
override val tableName = "records"
def insertNew(block: Record): Future[ResultSet] = insertNewRecord(block).future()
def insertNewRecord(r: Record) = {
insert
.value(_.hash, r.hash)
.value(_.size, r.size)
.value(_.time, r.time)
.value(_.difficulty, r.difficulty)
}
}
case class RecordTransactionCounts(hash: String, time: Long, num_transactions: Long )
class RecordTransactionCountsModel extends CassandraTable[RecordTransactionCountsModel, RecordTransactionCounts] {
override def tableName: String = "record_transaction_counts"
object hash extends StringColumn(this) with PartitionKey[String]
object time extends LongColumn(this) with ClusteringOrder[Long]
object num_transactions extends CounterColumn(this)
override def fromRow(r: Row): RecordTransactionCounts = {
RecordTransactionCounts(
hash(r),
time(r),
num_transactions(r)
)
}
}
abstract class ConcreteRecordTransactionCountsModel extends TransactionCountsModel with RootConnector {
def createTable(): Future[ResultSet] = {
create.ifNotExists().future()
}
def store(count: RecordTransactionCounts): Future[ResultSet] = {
insert
.value(_.hash, count.hash)
.value(_.time, count.time)
.value(_.num_transactions, count.num_transactions)
.future()
}
def getCount(hash: String): Future[Option[Long]] = {
select(_.count).where(_.hash eqs hash).one()
}
}
class Database(val keyspace: KeySpaceDef) extends DatabaseImpl(keyspace) {
def insertRecordTransactionCounts(tc: RecordTransactionCounts) = {
Batch.logged
.add(ChainDatabase.tc.store(tc))
.future()
}
object tc extends ConcreteRecordTransactionCountsModel with keyspace.Connector
}
object ChainDatabase extends Database(Config.keySpaceDefinition)