Skip to content

Commit

Permalink
CBL-6642: Invalid version string error when inserting merged version …
Browse files Browse the repository at this point in the history
…pushed by active replicator (#2212)

For version vector, the merged versions if exists will appear in the history property of the rev message. The VersionVector class used for parsing the history expects the current version to be together with its merged version in the cv, mv, mv; format.
  • Loading branch information
jianminzhao authored Jan 17, 2025
1 parent e07892a commit 6d3748c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
7 changes: 5 additions & 2 deletions Replicator/Pusher+Revs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,11 @@ namespace litecore::repl {
// Include the document history, but skip the current revision 'cause it's redundant
alloc_slice history = request->historyString(doc);
alloc_slice effectiveRevID = currentReplacementRevID ? currentReplacementRevID : currentRevID;
if ( history.hasPrefix(effectiveRevID) && history.size > effectiveRevID.size )
msg["history"_sl] = history.from(effectiveRevID.size + 1);
if ( history.hasPrefix(effectiveRevID) && history.size > effectiveRevID.size ) {
slice historyTruncated = history.from(effectiveRevID.size + 1);
while ( historyTruncated.hasPrefix(' ') ) historyTruncated = historyTruncated.from(1);
msg["history"_sl] = historyTruncated;
}

bool sendLegacyAttachments =
(request->legacyAttachments && (revisionFlags & kRevHasAttachments) && !_db->disableBlobSupport());
Expand Down
25 changes: 23 additions & 2 deletions Replicator/ReplicatorTypes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ namespace litecore::repl {
historyBuf.reset();
deltaSrc.reset();
deltaSrcRevID.reset();
_curVersAlloc.reset();
}

void RevToInsert::trim() {
Expand All @@ -108,8 +109,28 @@ namespace litecore::repl {
vector<C4String> RevToInsert::history() {
vector<C4String> history;
history.reserve(10);
history.push_back(revID);
for ( const void *pos = historyBuf.buf, *end = historyBuf.end(); pos < end; ) {

// For version vector, the merged versions if exists will appear
// in the history property of the rev message. The VersionVector class
// used for parsing the history expects the current version to be
// together with its merged version in the cv, mv, mv; format.
// This code below will check for the merge versions from the history
// and put them together with the current version (revID).

const void* pos = historyBuf.buf;
auto sc = historyBuf.findByte(';'); // RevTree will not have ';'.
if ( sc ) {
while ( pos < sc && *(char*)pos == ' ' ) pos = (char*)pos + 1;
_curVersAlloc = revID;
_curVersAlloc.append(", ");
_curVersAlloc.append(slice(pos, sc + 1));
history.push_back(_curVersAlloc);
pos = (char*)sc + 1;
} else {
history.push_back(revID);
}

for ( const void* end = historyBuf.end(); pos < end; ) {
while ( pos < end && *(char*)pos == ' ' ) pos = (char*)pos + 1;
auto comma = slice(pos, end).findByteOrEnd(',');
history.push_back(slice(pos, comma));
Expand Down
2 changes: 2 additions & 0 deletions Replicator/ReplicatorTypes.hh
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ namespace litecore::repl {

protected:
~RevToInsert() override;

alloc_slice _curVersAlloc; // storage used by history();
};

/** Metadata of a blob to download. */
Expand Down

0 comments on commit 6d3748c

Please sign in to comment.