Skip to content

Commit 9d5d83c

Browse files
committed
Address PR comments (move logic into Unmarshal only - and don't set channel info for winning rev)
1 parent e3dd646 commit 9d5d83c

File tree

6 files changed

+47
-28
lines changed

6 files changed

+47
-28
lines changed

db/blip_sync_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func (bsc *BlipSyncContext) handleChangesResponse(sender *blip.Sender, response
364364
sentSeqs = append(sentSeqs, seq)
365365
}
366366
} else {
367-
base.DebugfCtx(bsc.loggingCtx, base.KeySync, "Peer didn't want revision %s / %s (seq:%v)", base.UD(docID), revID, seq)
367+
base.DebugfCtx(bsc.loggingCtx, base.KeySync, "Peer didn't want revision %s/%s (seq:%v)", base.UD(docID), revID, seq)
368368
if collectionCtx.sgr2PushAlreadyKnownSeqsCallback != nil {
369369
alreadyKnownSeqs = append(alreadyKnownSeqs, seq)
370370
}

db/crud.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,7 +1770,8 @@ func (col *DatabaseCollectionWithUser) documentUpdateFunc(ctx context.Context, d
17701770
return
17711771
}
17721772

1773-
if len(channelSet) > 0 {
1773+
isWinningRev := doc.CurrentRev == newRevID
1774+
if len(channelSet) > 0 && !isWinningRev {
17741775
doc.History[newRevID].Channels = channelSet
17751776
}
17761777

@@ -1797,7 +1798,7 @@ func (col *DatabaseCollectionWithUser) documentUpdateFunc(ctx context.Context, d
17971798
return
17981799
}
17991800
}
1800-
_, err = doc.updateChannels(ctx, channelSet)
1801+
_, err = doc.updateChannels(ctx, isWinningRev, channelSet)
18011802
if err != nil {
18021803
return
18031804
}
@@ -2011,7 +2012,11 @@ func (db *DatabaseCollectionWithUser) updateAndReturnDoc(ctx context.Context, do
20112012
return nil, "", err
20122013
}
20132014

2014-
revChannels := doc.History[newRevID].Channels
2015+
revChannels, ok := doc.channelsForRev(newRevID)
2016+
if !ok {
2017+
// Should be unreachable, as we've already checked History[newRevID] above ...
2018+
return nil, "", base.RedactErrorf("unable to determine channels for %s/%s", base.UD(docid), newRevID)
2019+
}
20152020
documentRevision := DocumentRevision{
20162021
DocID: docid,
20172022
RevID: newRevID,

db/database.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,9 +1734,11 @@ func (db *DatabaseCollectionWithUser) getResyncedDocument(ctx context.Context, d
17341734
access = nil
17351735
channels = nil
17361736
}
1737-
rev.Channels = channels
17381737

1739-
if rev.ID == doc.CurrentRev {
1738+
isWinningRev := rev.ID == doc.CurrentRev
1739+
if !isWinningRev {
1740+
rev.Channels = channels
1741+
} else {
17401742
if regenerateSequences {
17411743
updatedUnusedSequences, err = db.assignSequence(ctx, 0, doc, unusedSequences)
17421744
if err != nil {
@@ -1745,7 +1747,7 @@ func (db *DatabaseCollectionWithUser) getResyncedDocument(ctx context.Context, d
17451747
forceUpdate = true
17461748
}
17471749

1748-
changedChannels, err := doc.updateChannels(ctx, channels)
1750+
changedChannels, err := doc.updateChannels(ctx, isWinningRev, channels)
17491751
changed = len(doc.Access.updateAccess(ctx, doc, access)) +
17501752
len(doc.RoleAccess.updateAccess(ctx, doc, roles)) +
17511753
len(changedChannels)

db/document.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ func (doc *Document) addToChannelSetHistory(channelName string, historyEntry Cha
973973

974974
// Updates the Channels property of a document object with current & past channels.
975975
// Returns the set of channels that have changed (document joined or left in this revision)
976-
func (doc *Document) updateChannels(ctx context.Context, newChannels base.Set) (changedChannels base.Set, err error) {
976+
func (doc *Document) updateChannels(ctx context.Context, isWinningRev bool, newChannels base.Set) (changedChannels base.Set, err error) {
977977
var changed []string
978978
oldChannels := doc.Channels
979979
if oldChannels == nil {
@@ -1002,7 +1002,9 @@ func (doc *Document) updateChannels(ctx context.Context, newChannels base.Set) (
10021002
doc.updateChannelHistory(channel, doc.Sequence, true)
10031003
}
10041004
}
1005-
doc.currentRevChannels = newChannels
1005+
if isWinningRev {
1006+
doc.SyncData.currentRevChannels = newChannels
1007+
}
10061008
if changed != nil {
10071009
base.InfofCtx(ctx, base.KeyCRUD, "\tDoc %q / %q in channels %q", base.UD(doc.ID), doc.CurrentRev, base.UD(newChannels))
10081010
changedChannels, err = channels.SetFromArray(changed, channels.KeepStar)

db/revtree.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ func (tree RevTree) MarshalJSON() ([]byte, error) {
6363
}
6464
revIndexes := map[string]int{"": -1}
6565

66-
winner, _, _ := tree.winningRevision(context.TODO())
67-
6866
i := 0
6967
for _, info := range tree {
7068
revIndexes[info.ID] = i
@@ -86,7 +84,7 @@ func (tree RevTree) MarshalJSON() ([]byte, error) {
8684
}
8785

8886
// for non-winning leaf revisions we'll store channel information
89-
if winner != info.ID && len(info.Channels) > 0 {
87+
if len(info.Channels) > 0 {
9088
if rep.ChannelsMap == nil {
9189
rep.ChannelsMap = make(map[string]base.Set, 1)
9290
}
@@ -192,10 +190,12 @@ func (tree *RevTree) UnmarshalJSON(inputjson []byte) (err error) {
192190

193191
// we shouldn't be in a situation where we have both channels and channelsMap populated, but we still need to handle the old format
194192
if len(rep.Channels_Old) > 0 {
193+
winner, _, _ := tree.winningRevision(context.TODO())
195194
leaves := tree.Leaves()
196195
for i, channels := range rep.Channels_Old {
197196
info := (*tree)[rep.Revs[i]]
198-
if _, isLeaf := leaves[info.ID]; isLeaf {
197+
// Ensure this leaf is not a winner (channels not stored in revtree for winning revision)
198+
if _, isLeaf := leaves[info.ID]; isLeaf && info.ID != winner {
199199
// set only if we've not already populated from ChannelsMap (we shouldn't ever have both)
200200
if info.Channels != nil {
201201
return fmt.Errorf("RevTree leaf %q had channels set already (from channelsMap)", info.ID)

db/revtree_test.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -229,40 +229,43 @@ func TestRevTreeUnmarshalOldFormat(t *testing.T) {
229229
"bodies": ["{\"foo\":\"bar\"}", "", ""],
230230
"channels": [["ABC", "CBS"], null, ["ABC"]]
231231
}`
232-
tree := testmap.copy()
232+
expected := testmap.copy()
233233

234-
// set desired channels
235-
ri, err := tree.getInfo("3-three")
234+
// winning rev channel information no longer present in revtree,
235+
// expect to drop it at unmarshal time if it's still in the raw JSON
236+
ri, err := expected.getInfo("3-three")
236237
require.NoError(t, err)
237-
ri.Channels = base.SetOf("ABC", "CBS")
238+
ri.Channels = nil
238239

239-
assertRevTreeUnmarshal(t, testJSON, tree)
240+
assertRevTreeUnmarshal(t, testJSON, expected)
240241
}
241242

242-
func TestRevTreeUnmarshalChannelMap(t *testing.T) {
243+
func TestRevTreeUnmarshalOldFormatNonWinningRev(t *testing.T) {
243244
// we moved non-winning leaf revision channel information into a 'channelMap' property instead to handle the case where documents are in conflict.
244245
const testJSON = `{
245246
"revs": ["3-drei", "3-three", "2-two", "1-one"],
246247
"parents": [2, 2, 3, -1],
247248
"bodies": ["{\"foo\":\"buzz\"}", "{\"foo\":\"bar\"}", "", ""],
248249
"channels": [["DE"], ["EN"], null, ["ABC"]]
249250
}`
250-
tree := testmap.copy()
251+
expected := testmap.copy()
251252

252-
// set desired channels
253-
ri, err := tree.getInfo("3-three")
253+
// winning rev channel information no longer present in revtree,
254+
// expect to drop it at unmarshal time if it's still in the raw JSON
255+
ri, err := expected.getInfo("3-three")
254256
require.NoError(t, err)
255-
ri.Channels = base.SetOf("EN")
257+
ri.Channels = nil
256258

257-
err = tree.addRevision("", RevInfo{
259+
// non-winning revisions do retain channel information, so populate this for the expected expected
260+
err = expected.addRevision("", RevInfo{
258261
ID: "3-drei",
259262
Parent: "2-two",
260263
Body: []byte(`{"foo":"buzz"}`),
261264
Channels: base.SetOf("DE"),
262265
})
263266
require.NoError(t, err)
264267

265-
assertRevTreeUnmarshal(t, testJSON, tree)
268+
assertRevTreeUnmarshal(t, testJSON, expected)
266269
}
267270

268271
func TestRevTreeUnmarshal(t *testing.T) {
@@ -390,15 +393,22 @@ func TestRevTreeChannelMapLeafOnly(t *testing.T) {
390393
// └─│ 3-drei (DE) │
391394
// └─────────────┘
392395
tree := branchymap.copy()
393-
err := tree.addRevision(t.Name(), RevInfo{
396+
397+
ri, err := tree.getInfo("3-three")
398+
require.NoError(t, err)
399+
// this wouldn't have been set normally (winning rev),
400+
// but let's force it to ensure we're not storing old revs in ChannelsMap
401+
ri.Channels = base.SetOf("EN")
402+
403+
err = tree.addRevision(t.Name(), RevInfo{
394404
ID: "4-four",
395405
Parent: "3-three",
396-
Channels: base.SetOf("EN-us", "EN-gb"),
406+
Channels: nil, // we don't store channels for winning revs
397407
})
398408
require.NoError(t, err)
399409

400410
// insert channel into tree - we don't store it in the globals because each test requires different channel data.
401-
ri, err := tree.getInfo("3-drei")
411+
ri, err = tree.getInfo("3-drei")
402412
require.NoError(t, err)
403413
ri.Channels = base.SetOf("DE")
404414

0 commit comments

Comments
 (0)