有没有在 phantom-dsl 中实现计数器操作的例子?
已检查:
http://outworkers.com/blog/post/a-series-on-cassandra-part-3-advanced-features
https://github.com/outworkers/phantom/wiki/Counter-columns
有点寻找此信息的 phantom-dsl 版本:
https://github.com/Netflix/astyanax/wiki/Working-with-counter-columns
下面是部分实现。它提出了两个问题:
我不确定如何从应用程序中获取值并在计数器表的计数器列中实现增量计数器操作。
如何更新与同一条目相关的表中的行,其中表具有不同的行数和键数。
在thiagos 示例中,这两个表;'songs' 和 'songs_by_artist' 都具有相同的行但具有不同的分区(主键/集群列)
我不确定在 phantom-dsl 中如何更新与相同条目相关的行,例如下面的“records”和“record_transaction_counts”表。
例如
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)