问题描述
是否有在phantom-dsl中实现计数器操作的示例?
Are there any examples of implementing the counter operation within phantom-dsl?
已检查:
Kinda正在寻找此信息的幻影单反版本:
Kinda looking for a phantom-dsl version of this info:
以下是部分实现。它提出了两个问题:
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.
在这两个表; 歌曲和 songs_by_artist都具有相同的行,但具有不同的分区(主键/群集列)
In thiagos example the two tables; 'songs' & 'songs_by_artist' both have the same rows but with different partitions (primary keys / clustering columns)
我不确定在phantom-dsl中如何更新与相同的条目,例如带有记录&下面的 record_transaction_counts表。
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.
例如
RecordTransactionCounts。{hash,time}与记录有关。{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)
推荐答案
如Thiago所建议,则可以使用 + =
或-=
运算符减少计数器的值。您也可以分别使用递增
或递减
方法来实现相同的目的。
As Thiago suggested, you can use the +=
or alternatively the -=
operator to decrement the value of a counter. You can also use the increment
or decrement
methods respectively to achieve the same thing.
def increment(count: RecordTransactionCounts): Future[ResultSet] = {
update
.where(_.hash eqs count.hash)
.and(_.time eqs count.time)
.modify(_.num_transactions += count.num_transactions)
.future()
}
// or
def increment(count: RecordTransactionCounts): Future[ResultSet] = {
update
.where(_.hash eqs count.hash)
.and(_.time eqs count.time)
.modify(_.num_transactions increment count.num_transactions)
.future()
}
要减少,只需将行替换为:
To decrement, simply replace the lines with:
...
.modify(_.num_transactions -= count.num_transactions)
// or
.modify(_.num_transactions decrement count.num_transactions)
在您过度依赖柜台之前,y ou还应该让Google找出其他人遇到的问题。
Before you rely too much on counters, you should also Google a bit to find out what problems other people have encountered.
这篇关于如何用phantom-dsl增加Cassandra计数器列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!