akka/akka-core

Ddata GMap

Open

#27,716 创建于 2019年9月16日

在 GitHub 查看
 (1 评论) (0 反应) (0 负责人)Scala (3,547 fork)batch import
1 - triagedhelp wantedt:distributed-data

仓库指标

Star
 (13,277 star)
PR 合并指标
 (平均合并 8天 19小时) (30 天内合并 10 个 PR)

描述

A Grow-only Map would be particularly useful for implementing products CRDTs, eg, if you had a case class (or like structure) that holds multiple CRDTs (which could themselves be products) that together represent one logical entity, and you want to be able to update them all atomically. It can be implemented currently with an ORMap, though ORMaps have the overhead of all the vectors and tags etc that isn't necessary for the product use case. Plus, for the product use case, you would likely want delta CRDT support on the values, and ORMap doesn't currently support that.

A feature that would pair nicely with this is a marshaller/unmarshaller abstraction, so you could work with a case class directly, rather than having to turn it to/from a GMap manually.

Here's an example such CRDT:

case class ShoppingCart(
  items: PNCounterMap[String],
  checkedOut: Flag,
  deliveryDetails: LWWRegister[DeliveryDetails]
) {

  def withDeliveryDetails(details: DeliveryDetails) = 
    copy(deliveryDetails = deliveryDetails.withValueOf(details))

  def checkout = copy(checkedOut = checkedOut.switchOn)

  def checkout(deliveryDetails: DeliveryDetails) = 
    copy(
      checkedOut = checkedOut.switchOn,
      deliveryDetails = deliveryDetails.withValueOf(details)
    )

  def addItems(productId: String, count: Int) = 
    copy(items = items.incrementBy(productId, count))

  def removeItems(productId: String, count: Int) =
    copy(items = items.decementBy(productId, count))

  def removeAllItems(productId: String) = 
    copy(items = items.remove(productId))
}

贡献者指南