Skip to content

Commit

Permalink
Add new tests for WriteUpdateWithXattrs,WriteTombstoneWithXattrs (#6816)
Browse files Browse the repository at this point in the history
  • Loading branch information
torcolvin authored May 13, 2024
1 parent b8e9251 commit f771d8c
Show file tree
Hide file tree
Showing 4 changed files with 1,068 additions and 19 deletions.
19 changes: 1 addition & 18 deletions base/bucket_gocb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1988,30 +1988,13 @@ EKTcWGekdmdDPsHloRNtsiCa697B2O9IFA==
func createTombstonedDoc(t *testing.T, dataStore sgbucket.DataStore, key, xattrName string) {

// Create document with XATTR

val := make(map[string]interface{})
val["body_field"] = "1234"

xattrVal := make(map[string]interface{})
xattrVal["seq"] = 123
xattrVal["rev"] = "1-1234"

ctx := TestCtx(t)
// Create w/ doc and XATTR
cas := uint64(0)
cas, err := dataStore.WriteWithXattrs(ctx, key, 0, cas, MustJSONMarshal(t, val), map[string][]byte{xattrName: MustJSONMarshal(t, xattrVal)}, nil)
require.NoError(t, err)

// Create tombstone revision which deletes doc body but preserves XATTR
_, mutateErr := dataStore.WriteTombstoneWithXattrs(ctx, key, 0, cas, nil, true, nil)
/*
flags := gocb.SubdocDocFlagAccessDeleted
_, mutateErr := dataStore.dataStore.MutateInEx(key, flags, gocb.Cas(cas), uint32(0)).
UpsertEx(xattrName, xattrVal, gocb.SubdocFlagXattr). // Update the xattr
UpsertEx(SyncXattrName+".cas", "${Mutation.CAS}", gocb.SubdocFlagXattr|gocb.SubdocFlagUseMacros). // Stamp the cas on the xattr
RemoveEx("", gocb.SubdocFlagNone). // Delete the document body
Execute()
*/
_, mutateErr := dataStore.WriteTombstoneWithXattrs(ctx, key, 0, 0, map[string][]byte{xattrName: MustJSONMarshal(t, xattrVal)}, false, nil)
require.NoError(t, mutateErr)

// Verify delete of body and XATTR
Expand Down
7 changes: 6 additions & 1 deletion base/collection_xattr_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,14 @@ func (c *Collection) WriteWithXattrs(ctx context.Context, k string, exp uint32,
return cas, err
}

// CAS-safe update of a document's xattr (only). Deletes the document body if deleteBody is true.
// WriteTombstoneWithXattrs will create a a tombtonse with xattrs. This is a cas safe operation. If deleteBody is true, will create a tombstone from an existing live document.
// Caveats:
// - Calling deleteBody=false on an alive document with valid cas will not create a tombstone, but be equivalent to calling UpdateXattrs
func (c *Collection) WriteTombstoneWithXattrs(ctx context.Context, k string, exp uint32, cas uint64, xattrs map[string][]byte, deleteBody bool, opts *sgbucket.MutateInOptions) (casOut uint64, err error) {

if len(xattrs) == 0 {
return 0, fmt.Errorf("WriteTombstoneWithXattrs called with empty xattrs")
}
requiresBodyRemoval := false
worker := func() (shouldRetry bool, err error, value uint64) {

Expand Down
Loading

0 comments on commit f771d8c

Please sign in to comment.