go-libp2p-pubsub-router icon indicating copy to clipboard operation
go-libp2p-pubsub-router copied to clipboard

PubsubValueStore should handle arbitrary merge functions

Open aschmahmann opened this issue 5 years ago • 0 comments

Currently the PubsubValueStore assumes Pubsub is distributing a single Key-Value pair which is updating by utilizing a best function that is passed in via the Validator interface.

This looks like:

bestVal, updated := best(currentVal, receivedVal)
if updated {
   data[key]=bestVal
   Publish(bestVal)
}

Instead it would be better to use an interface that allows for:

updatedState, delta := merge(currentState, receivedState)
if delta != nil {
   data[key]=updatedState
   Publish(delta)
}

Given that the PubsubValueStore assumes Get and Put of full data records (as opposed to deltas) it's probably fine to assume that the types of the state and deltas are the same. However, if we wanted we could also add a diff function that takes the states and computes a delta.

Some example merge functions:

  • Best (as we have now)
  • Union (if the record type is a set take the union of the set)
  • BestN (do a union, but only keep the best N records)

Having this (and landing #33) would make an implementation of the suggestion at https://github.com/libp2p/go-libp2p/issues/694#issuecomment-517339808 fairly straightforward.

aschmahmann avatar Aug 12 '19 16:08 aschmahmann