Skip to content

Commit 4d19118

Browse files
authored
CBG-4280 consolidate Attachment on document (#7647)
- merge Document.SyncData.Attachments, Document.GlobalSyncData.GlobalAttachments, and Document.DocAttachments into Document._globalSync.Attachments - Document.SyncData.Attachments becomes Document.SyncData.AttachmentsPre4dot0 and is used for unmarshalling, but should be cleared before writing a document - rename GlobalSyncData.GlobalAttachments to GlobalSyncData.Attachments
1 parent 8cc0b0a commit 4d19118

20 files changed

+159
-197
lines changed

db/attachment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (db *DatabaseCollectionWithUser) storeAttachments(ctx context.Context, doc
9898
}
9999
digest := Sha1DigestKey(attachment)
100100
key := MakeAttachmentKey(AttVersion2, doc.ID, digest)
101-
_, updated := doc.SyncData.Attachments[name]
101+
_, updated := doc.Attachments()[name]
102102
newAttachments[key] = updatedAttachment{
103103
body: attachment,
104104
name: name,

db/attachment_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,8 @@ func TestStoreAttachments(t *testing.T) {
647647
assert.NoError(t, err, "Couldn't update document")
648648
assert.NotEmpty(t, revId, "Document revision id should be generated")
649649
require.NotNil(t, doc)
650-
assert.NotEmpty(t, doc.Attachments, "Attachment metadata should be populated")
651-
attachment := doc.Attachments["att1.txt"].(map[string]interface{})
650+
assert.NotEmpty(t, doc.Attachments(), "Attachment metadata should be populated")
651+
attachment := doc.Attachments()["att1.txt"].(map[string]interface{})
652652
assert.Equal(t, "text/plain", attachment["content_type"])
653653
assert.Equal(t, "sha1-crv3IVNxp3JXbP6bizTHt3GB3O0=", attachment["digest"])
654654
assert.Equal(t, 8, attachment["encoded_length"])
@@ -665,8 +665,8 @@ func TestStoreAttachments(t *testing.T) {
665665
assert.NoError(t, err, "Couldn't update document")
666666
assert.NotEmpty(t, revId, "Document revision id should be generated")
667667
require.NotNil(t, doc)
668-
assert.NotEmpty(t, doc.Attachments, "Attachment metadata should be populated")
669-
attachment = doc.Attachments["att1.txt"].(map[string]interface{})
668+
assert.NotEmpty(t, doc.Attachments(), "Attachment metadata should be populated")
669+
attachment = doc.Attachments()["att1.txt"].(map[string]interface{})
670670
assert.Equal(t, "text/plain", attachment["content_type"])
671671
assert.Equal(t, "sha1-crv3IVNxp3JXbP6bizTHt3GB3O0=", attachment["digest"])
672672
assert.Equal(t, 8, attachment["encoded_length"])
@@ -693,7 +693,7 @@ func TestStoreAttachments(t *testing.T) {
693693
"stub": true,
694694
"ver": 2,
695695
},
696-
}, doc.Attachments)
696+
}, doc.Attachments())
697697

698698
// Simulate error scenario for attachment without data; stub is not provided; If the data is
699699
// empty in attachment, the attachment must be a stub that repeats a parent attachment.
@@ -778,7 +778,7 @@ func TestMigrateBodyAttachments(t *testing.T) {
778778
require.NoError(t, err)
779779

780780
// Fetch the raw doc sync data from the bucket to make sure we didn't store pre-2.5 attachments in syncData.
781-
assert.Empty(t, GetRawSyncXattr(t, collection.dataStore, docKey).Attachments)
781+
assert.Empty(t, GetRawSyncXattr(t, collection.dataStore, docKey).AttachmentsPre4dot0)
782782
base.RequireXattrNotFound(t, collection.dataStore, docKey, base.GlobalXattrName)
783783
return db, ctx
784784
}
@@ -809,7 +809,7 @@ func TestMigrateBodyAttachments(t *testing.T) {
809809

810810
// Fetch the raw doc sync data from the bucket to see if this read-only op unintentionally persisted the migrated meta.
811811
syncData := GetRawSyncXattr(t, collection.dataStore, docKey)
812-
assert.Empty(t, syncData.Attachments)
812+
assert.Empty(t, syncData.AttachmentsPre4dot0)
813813
base.RequireXattrNotFound(t, collection.dataStore, docKey, base.GlobalXattrName)
814814
})
815815

@@ -839,7 +839,7 @@ func TestMigrateBodyAttachments(t *testing.T) {
839839

840840
// Fetch the raw doc sync data from the bucket to see if this read-only op unintentionally persisted the migrated meta.
841841
syncData := GetRawSyncXattr(t, collection.dataStore, docKey)
842-
assert.Empty(t, syncData.Attachments)
842+
assert.Empty(t, syncData.AttachmentsPre4dot0)
843843
base.RequireXattrNotFound(t, collection.dataStore, docKey, base.GlobalXattrName)
844844
})
845845

@@ -881,7 +881,7 @@ func TestMigrateBodyAttachments(t *testing.T) {
881881
require.NotContains(t, body1, BodyAttachments)
882882

883883
// Fetch the raw doc sync data from the bucket to make sure we actually moved attachments on write.
884-
require.Empty(t, GetRawSyncXattr(t, collection.dataStore, docKey).Attachments)
884+
require.Empty(t, GetRawSyncXattr(t, collection.dataStore, docKey).AttachmentsPre4dot0)
885885
require.Equal(t, AttachmentMap{
886886
"hello.txt": {
887887
Digest: "sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0=",
@@ -906,7 +906,7 @@ func TestMigrateBodyAttachments(t *testing.T) {
906906

907907
// Fetch the raw doc sync data from the bucket to see if this read-only op unintentionally persisted the migrated meta.
908908
syncData := GetRawSyncXattr(t, collection.dataStore, docKey)
909-
require.Empty(t, syncData.Attachments)
909+
require.Empty(t, syncData.AttachmentsPre4dot0)
910910
base.RequireXattrNotFound(t, collection.dataStore, docKey, base.GlobalXattrName)
911911

912912
byeTxtData, err := base64.StdEncoding.DecodeString("Z29vZGJ5ZSBjcnVlbCB3b3JsZA==")
@@ -946,7 +946,7 @@ func TestMigrateBodyAttachments(t *testing.T) {
946946
require.NotContains(t, body1, BodyAttachments)
947947

948948
// Fetch the raw doc sync data from the bucket to make sure we actually moved attachments on write.
949-
require.Empty(t, GetRawSyncXattr(t, collection.dataStore, docKey).Attachments)
949+
require.Empty(t, GetRawSyncXattr(t, collection.dataStore, docKey).AttachmentsPre4dot0)
950950
require.Equal(t, AttachmentMap{
951951
"hello.txt": {
952952
Digest: "sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0=",
@@ -1066,7 +1066,7 @@ func TestMigrateBodyAttachmentsMerge(t *testing.T) {
10661066
"revpos": float64(1),
10671067
"stub": true,
10681068
},
1069-
}, GetRawSyncXattr(t, collection.dataStore, docKey).Attachments)
1069+
}, GetRawSyncXattr(t, collection.dataStore, docKey).AttachmentsPre4dot0)
10701070
base.RequireXattrNotFound(t, collection.dataStore, docKey, base.GlobalXattrName)
10711071

10721072
rev, err := collection.GetRev(ctx, docKey, "3-a", true, nil)
@@ -1102,7 +1102,7 @@ func TestMigrateBodyAttachmentsMerge(t *testing.T) {
11021102
"revpos": float64(1),
11031103
"stub": true,
11041104
},
1105-
}, GetRawSyncXattr(t, collection.dataStore, docKey).Attachments)
1105+
}, GetRawSyncXattr(t, collection.dataStore, docKey).AttachmentsPre4dot0)
11061106
base.RequireXattrNotFound(t, collection.dataStore, docKey, base.GlobalXattrName)
11071107
}
11081108

db/background_mgr_attachment_migration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (a *AttachmentMigrationManager) Run(ctx context.Context, options map[string
138138
return false
139139
}
140140

141-
if syncData == nil || syncData.Attachments == nil {
141+
if syncData == nil || syncData.AttachmentsPre4dot0 == nil {
142142
// no attachments to migrate
143143
return true
144144
}

db/background_mgr_attachment_migration_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestAttachmentMigrationTaskMixMigratedAndNonMigratedDocs(t *testing.T) {
3636
key := fmt.Sprintf("%s_%d", t.Name(), i)
3737
_, doc, err := collection.Put(ctx, key, docBody)
3838
require.NoError(t, err)
39-
assert.NotNil(t, doc.SyncData.Attachments)
39+
assert.NotNil(t, doc.Attachments())
4040
}
4141

4242
// Move some subset of the documents attachment metadata from global sync to sync data
@@ -95,7 +95,7 @@ func TestAttachmentMigrationManagerResumeStoppedMigration(t *testing.T) {
9595
key := fmt.Sprintf("%s_%d", t.Name(), i)
9696
_, doc, err := collection.Put(ctx, key, docBody)
9797
require.NoError(t, err)
98-
require.NotNil(t, doc.SyncData.Attachments)
98+
require.NotNil(t, doc.Attachments())
9999
}
100100
attachMigrationMgr := NewAttachmentMigrationManager(db.DatabaseContext)
101101
require.NotNil(t, attachMigrationMgr)
@@ -206,14 +206,14 @@ func TestMigrationManagerDocWithSyncAndGlobalAttachmentMetadata(t *testing.T) {
206206
var syncData SyncData
207207
require.NoError(t, base.JSONUnmarshal(xattrs[base.SyncXattrName], &syncData))
208208
// define some attachment meta on sync data
209-
syncData.Attachments = AttachmentsMeta{}
209+
syncData.AttachmentsPre4dot0 = AttachmentsMeta{}
210210
att := map[string]interface{}{
211211
"stub": true,
212212
"digest": "sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0=",
213213
"length": 11,
214214
"revpos": 1,
215215
}
216-
syncData.Attachments["someAtt.txt"] = att
216+
syncData.AttachmentsPre4dot0["someAtt.txt"] = att
217217

218218
updateXattrs := map[string][]byte{
219219
base.SyncXattrName: base.MustJSONMarshal(t, syncData),
@@ -254,5 +254,5 @@ func TestMigrationManagerDocWithSyncAndGlobalAttachmentMetadata(t *testing.T) {
254254
Revpos: 1,
255255
},
256256
}, GetRawGlobalSyncAttachments(t, collection.dataStore, key))
257-
require.Empty(t, GetRawSyncXattr(t, collection.dataStore, key).Attachments)
257+
require.Empty(t, GetRawSyncXattr(t, collection.dataStore, key).AttachmentsPre4dot0)
258258
}

db/blip_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ func (bh *blipHandler) processRev(rq *blip.Message, stats *processRevStats) (err
12401240
currentDigests = make(map[string]string, len(bodyAtts))
12411241
for name, value := range bodyAtts {
12421242
// Check if we have this attachment name already, if we do, continue check
1243-
currentAttachment, ok := currentBucketDoc.Attachments[name]
1243+
currentAttachment, ok := currentBucketDoc.Attachments()[name]
12441244
if !ok {
12451245
// If we don't have this attachment already, ensure incoming revpos is greater than minRevPos, otherwise
12461246
// update to ensure it's fetched and uploaded
@@ -1301,7 +1301,7 @@ func (bh *blipHandler) processRev(rq *blip.Message, stats *processRevStats) (err
13011301
return err
13021302
}
13031303

1304-
newDoc.DocAttachments = GetBodyAttachments(body)
1304+
newDoc.SetAttachments(GetBodyAttachments(body))
13051305
delete(body, BodyAttachments)
13061306
newDoc.UpdateBody(body)
13071307
}

0 commit comments

Comments
 (0)