Skip to content

Commit

Permalink
Encode ids in mvreg
Browse files Browse the repository at this point in the history
Peer-base uses Buffers for ids, which were getting serialized in
strange ways so the mvreg CRDT wasn't converging when syncing.

See: peer-base/peer-base#287

This fix encodes the ids to strings, using a similar method as what
is used in the rga type.
  • Loading branch information
jimpick committed May 28, 2019
1 parent 02d8dc9 commit 9a5f527
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/mvreg.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const DotSet = require('./dot-set')
const { encode } = require('delta-crdts-msgpack-codec')

module.exports = {
initial () { return new DotSet() },
Expand All @@ -14,9 +15,10 @@ module.exports = {
},
mutators: {
write (id, s, value) {
const encodedId = encode(id).toString('base64')
return s.join(
s.removeAll(),
s.add(id, value))
s.add(encodedId, value))
}
}
}
10 changes: 10 additions & 0 deletions test/mvreg.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,15 @@ describe('mvreg', () => {
it('and the second also converges', () => {
expect(Array.from(replica2.value()).sort()).to.deep.equal(['hello', 'world'])
})

it('binary ids also converge', () => {
const replicaA = MVReg(Buffer.from('idA'))
const deltaA = replicaA.write('a')
const replicaB = MVReg(Buffer.from('idB'))
replicaB.apply(deltaA)
const deltaB = replicaB.write('b')
replicaA.apply(deltaB)
expect(Array.from(replicaA.value()).sort()).to.deep.equal(['b'])
})
})
})

0 comments on commit 9a5f527

Please sign in to comment.