-
Notifications
You must be signed in to change notification settings - Fork 14
Improve support for docdb #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| package mongo | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "cmp" | ||
| "encoding/binary" | ||
| "encoding/hex" | ||
| "fmt" | ||
| "sort" | ||
|
|
||
| "context" | ||
|
|
||
| "go.mongodb.org/mongo-driver/bson" | ||
| "go.mongodb.org/mongo-driver/bson/bsontype" | ||
| "go.mongodb.org/mongo-driver/mongo" | ||
| "golang.org/x/sync/errgroup" | ||
| ) | ||
|
|
||
| var supportedIDTypes = map[bsontype.Type]bool{ | ||
| bson.TypeObjectID: true, | ||
| bson.TypeString: true, | ||
| bson.TypeInt32: true, | ||
| bson.TypeInt64: true, | ||
| bson.TypeBinary: true, | ||
| } | ||
|
|
||
| func compareBSONRawValues(a, b bson.RawValue) int { | ||
| switch a.Type { | ||
| case bson.TypeObjectID: | ||
| return bytes.Compare(a.Value, b.Value) | ||
| case bson.TypeString: | ||
| return bytes.Compare(a.Value[4:len(a.Value)-1], b.Value[4:len(b.Value)-1]) | ||
| case bson.TypeInt32: | ||
| ai := int32(binary.LittleEndian.Uint32(a.Value)) | ||
| bi := int32(binary.LittleEndian.Uint32(b.Value)) | ||
| return cmp.Compare(ai, bi) | ||
| case bson.TypeInt64: | ||
| ai := int64(binary.LittleEndian.Uint64(a.Value)) | ||
| bi := int64(binary.LittleEndian.Uint64(b.Value)) | ||
| return cmp.Compare(ai, bi) | ||
| case bson.TypeBinary: | ||
| return bytes.Compare(a.Value[5:], b.Value[5:]) | ||
| } | ||
| panic("compareBSONRawValues called with unsupported type") | ||
| } | ||
|
|
||
| func (c *conn) sampleIDs(ctx context.Context, col *mongo.Collection, numSamples int64) ([]bson.RawValue, error) { | ||
| if c.flavor != FlavorDocumentDB { | ||
| res, err := col.Aggregate(ctx, mongo.Pipeline{ | ||
| {{"$sample", bson.D{{"size", numSamples}}}}, | ||
| {{"$project", bson.D{{"_id", 1}}}}, | ||
| {{"$sort", bson.D{{"_id", 1}}}}, | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error getting %v samples: %w", numSamples, err) | ||
| } | ||
| defer res.Close(ctx) | ||
| var ids []bson.RawValue | ||
| for res.Next(ctx) { | ||
| ids = append(ids, res.Current.Lookup("_id")) | ||
| } | ||
| if res.Err() != nil { | ||
| return nil, fmt.Errorf("err iterating through samples: %w", res.Err()) | ||
| } | ||
| return ids, nil | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| type sampleResult struct { | ||
| id bson.RawValue | ||
| key string | ||
| } | ||
|
|
||
| results := make([]sampleResult, numSamples) | ||
| eg, ctx := errgroup.WithContext(ctx) | ||
| for i := int64(0); i < numSamples; i++ { | ||
| i := i | ||
| eg.Go(func() error { | ||
| res, err := col.Aggregate(ctx, mongo.Pipeline{ | ||
| {{"$sample", bson.D{{"size", 1}}}}, | ||
| {{"$project", bson.D{{"_id", 1}}}}, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("error getting sample %v: %w", i, err) | ||
| } | ||
| defer res.Close(ctx) | ||
| if res.Next(ctx) { | ||
| id := res.Current.Lookup("_id") | ||
| results[i] = sampleResult{id: id, key: hex.EncodeToString(id.Value)} | ||
| } | ||
| return nil | ||
| }) | ||
| } | ||
| if err := eg.Wait(); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| seen := map[string]struct{}{} | ||
| var ids []bson.RawValue | ||
| for _, r := range results { | ||
| if r.key == "" { | ||
| continue | ||
| } | ||
| if _, dup := seen[r.key]; !dup { | ||
| seen[r.key] = struct{}{} | ||
| ids = append(ids, r.id) | ||
| } | ||
| } | ||
|
|
||
| if len(ids) > 0 { | ||
| t := ids[0].Type | ||
| for _, id := range ids[1:] { | ||
| if id.Type != t { | ||
| return nil, fmt.Errorf("mixed _id types not supported for DocumentDB sampling") | ||
| } | ||
| } | ||
| if !supportedIDTypes[t] { | ||
| return nil, fmt.Errorf("unsupported _id type for DocumentDB sampling: %v", t) | ||
| } | ||
| sort.Slice(ids, func(i, j int) bool { | ||
| return compareBSONRawValues(ids[i], ids[j]) < 0 | ||
| }) | ||
| } | ||
|
|
||
| return ids, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: adiom-data/dsync
Length of output: 5660
Add empty cursor guard to
changeStreamOptsfor consistency.The function currently passes cursor to
SetResumeAfter/SetStartAfterwithout checking if it's empty. The codebase already uses this pattern inDecodeCursor(line 379), and proto definitions document cursor as optional ("if present"). Add the guard before setting resume options:Proposed fix
func (c *conn) changeStreamOpts(cursor []byte) *moptions.ChangeStreamOptions { opts := moptions.ChangeStream() + if len(cursor) == 0 { + return opts + } if c.flavor == FlavorDocumentDB { opts.SetResumeAfter(bson.Raw(cursor)) } else { opts.SetStartAfter(bson.Raw(cursor)) } return opts }📝 Committable suggestion
🤖 Prompt for AI Agents