Skip to content

CBG-5326: Function to handle document channel history compaction#8218

Open
RIT3shSapata wants to merge 3 commits intomainfrom
CBG-5326
Open

CBG-5326: Function to handle document channel history compaction#8218
RIT3shSapata wants to merge 3 commits intomainfrom
CBG-5326

Conversation

@RIT3shSapata
Copy link
Copy Markdown
Contributor

@RIT3shSapata RIT3shSapata commented Apr 30, 2026

CBG-5326

Describe your PR here...

  • Added a function to compact the channel history of a given document based on sequence number
  • Added a high level test coverage for the same

Pre-review checklist

  • Removed debug logging (fmt.Print, log.Print, ...)
  • Logging sensitive data? Make sure it's tagged (e.g. base.UD(docID), base.MD(dbName))
  • Updated relevant information in the API specifications (such as endpoint descriptions, schemas, ...) in docs/api

Dependencies (if applicable)

  • Link upstream PRs
  • Update Go module dependencies when merged

Integration Tests

@RIT3shSapata RIT3shSapata self-assigned this Apr 30, 2026
Copilot AI review requested due to automatic review settings April 30, 2026 13:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new DatabaseCollection helper to compact per-document channel assignment history up to a given sequence, with a REST-level test validating the basic behavior.

Changes:

  • Introduces CompactDocChannelHistory(ctx, docid, seq) to prune ChannelSetHistory and persist the updated _sync (and _mou) xattrs.
  • Adds an API test that creates/removes/re-adds channels to generate history, then compacts and validates the result.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 8 comments.

File Description
db/crud.go Adds the CompactDocChannelHistory implementation and persists updated sync metadata via xattr update.
rest/api_test.go Adds a high-level test covering history generation and compaction behavior.

Comment thread db/crud.go
Comment thread rest/api_test.go
Comment thread rest/api_test.go Outdated
Comment thread rest/api_test.go Outdated
Comment thread rest/api_test.go Outdated
Comment thread db/crud.go Outdated
Comment thread db/crud.go Outdated
Comment thread db/crud.go
Copy link
Copy Markdown
Collaborator

@adamcfraser adamcfraser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left one comment - have a look at that as well as the copilot comments and assign back once you've updated the PR.

Comment thread db/crud.go
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

Comment thread rest/api_test.go
Comment on lines +4060 to +4074
// Compact at seq 3
err = collection.CompactDocChannelHistory(ctx, "doc4", 2)
require.NoError(t, err)

syncDataAfter, err := collection.GetDocSyncData(ctx, "doc4")
require.NoError(t, err)

// Verify: only entries with End > 3 or End == 0 (still active) should remain
for _, entry := range syncDataAfter.ChannelSet {
assert.True(t, entry.End == 0 || entry.End > uint64(3))
}

// Verify only channels with seq > 3 should remain
for _, entry := range syncDataAfter.Channels {
assert.True(t, entry.Seq > uint64(3))
Comment thread rest/api_test.go
Comment on lines +4113 to +4122
syncDataBefore, err := collection.GetDocSyncData(ctx, "doc7")
require.NoError(t, err)
err = collection.CompactDocChannelHistory(ctx, "doc7", 2)
require.NoError(t, err)

syncData, err := collection.GetDocSyncData(ctx, "doc7")
require.NoError(t, err)
// Should still have active channels
assert.Less(t, len(syncData.ChannelSet), len(syncDataBefore.ChannelSet))
assert.Less(t, len(syncData.Channels), len(syncDataBefore.Channels))
Comment thread db/crud.go
Comment on lines +219 to +230
// CompactDocChannelHistory removes channel history entries that ended at or before the given sequence number.
// This is used to truncate stale channel assignment history to reduce storage overhead.
func (c *DatabaseCollection) CompactDocChannelHistory(ctx context.Context, docid string, seq uint64) (err error) {
key := realDocID(docid)
if key == "" {
return base.HTTPErrorf(400, "Invalid doc ID")
}

_, xattrs, cas, err := c.dataStore.GetWithXattrs(ctx, key, c.syncGlobalSyncMouRevSeqNoAndUserXattrKeys())
if err != nil {
return
}
Comment thread db/crud.go
Comment on lines +227 to +233
_, xattrs, cas, err := c.dataStore.GetWithXattrs(ctx, key, c.syncGlobalSyncMouRevSeqNoAndUserXattrKeys())
if err != nil {
return
}

doc, err := c.unmarshalDocumentWithXattrs(ctx, docid, nil, xattrs, cas, DocUnmarshalSync)
if err != nil {
Comment thread db/crud.go
Comment on lines +288 to +292
updatedXattr := map[string][]byte{
base.SyncXattrName: rawSyncXattr,
base.MouXattrName: rawMouXattr,
}
_, err = c.dataStore.UpdateXattrs(ctx, key, 0, cas, updatedXattr, opts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants