Skip to content

Additional collection methods#32

Open
toptobes wants to merge 5 commits intomainfrom
KG-collection-updates
Open

Additional collection methods#32
toptobes wants to merge 5 commits intomainfrom
KG-collection-updates

Conversation

@toptobes
Copy link
Copy Markdown
Collaborator

No description provided.

@toptobes toptobes marked this pull request as draft April 12, 2026 21:46
@toptobes toptobes marked this pull request as ready for review April 12, 2026 21:46
@toptobes
Copy link
Copy Markdown
Collaborator Author

@DeanPDX My turn to tell you to check this out...

It's not entirely ready for merge (I still want to write some additional tests and figure out a few other things first) but I wouldn't mind a quick look-through.

I tried to keep changes atomic, which each commit handling one specific method/set of methods.

@toptobes toptobes force-pushed the KG-collection-updates branch from 04fe1af to 525144c Compare April 12, 2026 21:53
@toptobes toptobes force-pushed the KG-collection-updates branch from 525144c to 842fbcb Compare April 13, 2026 17:59
Copy link
Copy Markdown
Contributor

@DeanPDX DeanPDX left a comment

Choose a reason for hiding this comment

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

Lookin' good so far! I have some comments. I think biggest thing we need to think about is that errors.Is thing and concurrency questions.

var doc testDoc
if err := c.Decode(&doc); err != nil {
t.Fatalf("Decode failed: %v", err)
t.Fatalf("DecodeID failed: %v", err)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do these say DecodeID when they are calling Decode?

@@ -0,0 +1,195 @@
// Copyright IBM Corp.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure about the file name here. I would contend that "utils" and "helpers" is almost always not idiomatic (though I'm guilty of this with "testutils" package; in that case I couldn't think of a better package name and gave up).


// DecodeID unmarshalls the inserted ID into v.
// v should be a pointer to the appropriate ID type (string, int, ObjectId, UUID, etc.)
func (r *InsertOneResult) DecodeID(v any) error {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When we get generic method receivers we can make this a little easier to consume.

// for cursor.Next(ctx) {
// var doc MyDocument
// if err := cursor.Decode(&doc); err != nil {
// if err := cursor.DecodeID(&doc); err != nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think DecodeID might have been globally replaced too much with find/replace or something.

var allWarnings results.Warnings
errChan := make(chan error, 1)

for w := 0; w < *opts.Concurrency; w++ {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I may have missed it, but, should we guard against concurrency values <= 0?

slice := records.Slice(start, end).Interface()
res, warn, err := runInsertMany(ctx, slice, mkCmd, opts)

if err != nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not 100% sure about this. If we encounter an error, the "first" error wins. But the other goroutines keep spinning away. And that buffer is full after 1 error so the first error wins, right? Here's a go playground link that tests/illustrates this:

https://go.dev/play/p/2gpA76ye97T

(I always have to test my concurrency stuff because it can get complicated quickly)

Is that the desired behavior? It seems like you are trying to differentiate between a critical error (err) and api errors that we batch up and send back. If we hit a critical error, do you think we should cancel the other goroutines? I'm honestly not sure but wanted to just at least think about this with you out loud. :)

}, opts.APIOptions)

b, warnings, execErr := cmd.Execute(ctx)
if execErr != nil && !errors.Is(execErr, &DataAPIError{}) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think errors.Is is almost certainly not doing what you want it to here. I think you are wanting to type-assert that execErr is NOT a DataAPIError, right? Maybe something like this?

var apiErr *DataAPIError
if execErr != nil && !errors.As(execErr, &apiErr) {
    return nil, warnings, execErr
}

Docs: https://pkg.go.dev/errors#example-As

Also - there is a new AsType but I think it's so recent we can't use it yet:

https://pkg.go.dev/errors#example-AsType

}, nil
}

// collectionFindOneAndUpdatePayload is the payload for the findOneAndUpdate command.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh one more thing. Copy/paste error here. A tale as old as time.

return result, nil
}

// collectionFindOneAndUpdatePayload is the payload for the findOneAndUpdate command.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One other copy/paste error.

Projection map[string]any `json:"projection,omitempty"`
}

// FindOneAndUpdate finds a single document matching the filter, applies the update,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One LAST copy/paste thing. Probably should be something along the lines of:

// FindOneAndDelete finds a single document matching the filter and deletes it...

I need to check the docs though to see if there are any special circumstances to think about here. But I bet you know exactly what it does off the top of your head so I'm not unless you want me to! 🤷

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.

2 participants