Skip to content

Commit

Permalink
Work in progress - HLV compare implemented but depends on CBG-4257
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcfraser committed Dec 21, 2024
1 parent 61b0628 commit d4a7237
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
26 changes: 26 additions & 0 deletions db/hybrid_logical_vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,32 @@ func NewHybridLogicalVector() *HybridLogicalVector {
}
}

func (hlv *HybridLogicalVector) Equals(other *HybridLogicalVector) bool {
if hlv.SourceID != other.SourceID {
return false
}
if hlv.Version != other.Version {
return false
}
if len(hlv.PreviousVersions) != len(other.PreviousVersions) {
return false
}
for k, v := range hlv.PreviousVersions {
if other.PreviousVersions[k] != v {
return false
}
}
if len(hlv.MergeVersions) != len(other.MergeVersions) {
return false
}
for k, v := range hlv.MergeVersions {
if other.MergeVersions[k] != v {
return false
}
}
return true
}

// GetCurrentVersion returns the current version from the HLV in memory.
func (hlv *HybridLogicalVector) GetCurrentVersion() (string, uint64) {
return hlv.SourceID, hlv.Version
Expand Down
5 changes: 5 additions & 0 deletions topologytest/couchbase_lite_mock_peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package topologytest
import (
"context"
"fmt"
"log"
"testing"

sgbucket "github.com/couchbase/sg-bucket"
Expand Down Expand Up @@ -135,6 +136,10 @@ func (p *CouchbaseLiteMockPeer) WaitForDocVersion(dsName sgbucket.DataStoreName,
if !assert.NotNil(c, actual, "Could not find docID:%+v on %p\nVersion %#v", docID, p, expected) {
return
}
if !actual.IsHLVEqual(expected) {
log.Printf("not equal")
}
assert.True(c, actual.IsHLVEqual(expected), "Actual HLV does not match expected on %s for peer %s. Expected: %+v, Actual: %+v", docID, p, expected.HLV, actual.HLV)
assert.Equal(c, expected.CV(c), actual.CV(c), "Could not find matching CV on %s for peer %s (sourceID:%s)\nexpected: %#v\nactual: %#v\n body: %+v\n", docID, p, p.SourceID(), expected, actual, string(data))

}, totalWaitTime, pollInterval)
Expand Down
2 changes: 1 addition & 1 deletion topologytest/couchbase_server_peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (p *CouchbaseServerPeer) waitForDocVersion(dsName sgbucket.DataStoreName, d
}
// have to use p.tb instead of c because of the assert.CollectT doesn't implement TB
version = getDocVersion(docID, p, cas, xattrs)

assert.True(c, version.IsHLVEqual(expected), "Actual HLV does not match expected on %s for peer %s. Expected: %+v, Actual: %+v", docID, p, expected, version)
assert.Equal(c, expected.CV(c), version.CV(c), "Could not find matching CV on %s for peer %s\nexpected: %#v\nactual: %#v\n body: %#v\n", docID, p, expected, version, string(docBytes))

}, totalWaitTime, pollInterval)
Expand Down
21 changes: 21 additions & 0 deletions topologytest/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ func (v DocMetadata) CV(t require.TestingT) db.Version {
return db.Version{}
}

func (v DocMetadata) IsHLVEqual(other DocMetadata) bool {
if v.ImplicitHLV != nil {
return other.hlvEquals(v.ImplicitHLV)
} else if v.HLV != nil {
return other.hlvEquals(v.HLV)
} else {
return other.ImplicitHLV == nil && other.HLV == nil
}
}

func (v DocMetadata) hlvEquals(hlv *db.HybridLogicalVector) bool {
if v.ImplicitHLV != nil {
return v.ImplicitHLV.Equals(hlv)
} else if v.HLV != nil {
return v.HLV.Equals(hlv)
} else {
return hlv == nil
}

}

// DocMetadataFromDocument returns a DocVersion from the given document.
func DocMetadataFromDocument(doc *db.Document) DocMetadata {
return DocMetadata{
Expand Down

0 comments on commit d4a7237

Please sign in to comment.