Skip to content
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

GODRIVER-3049 Replace bsoncore.DocumentSequence with a bson type-agnostic analogue #1492

Merged
merged 11 commits into from
Jan 13, 2024

Conversation

prestonvasquez
Copy link
Collaborator

@prestonvasquez prestonvasquez commented Dec 9, 2023

GODRIVER-3049

Summary

The goal of bsoncore.Iterator is to abstract the arithmetic (i.e. constant time) retrieval of elements of a BSON array. The iterator does this by maintaining the “current” position of which element is “next-to-be-retrieved” in a type called List, which would likely be loaded from the cursor.nextBatch on a server response. This solution is more simple than it's predecessor and is more powerful, in that the List data is not restricted to documents.

Blank diagram (1)

Background & Motivation

The DocumentSequence pattern was introduced in GODRIVER-800 to avoid repetitively copying bytes into a document sequence.

The 800 pattern assumes an enum-like type for categorizing data as either a sequence: i.e. {x:1}{x:2}{x:3}, or a BSON array: {1:{x:1},2:{x:2},3:{x:3}}. 3049 proposes we simplify this pattern using bsoncore.Array, which will handle both cases.

@prestonvasquez prestonvasquez requested a review from a team as a code owner December 9, 2023 00:24
@prestonvasquez prestonvasquez requested review from matthewdale and removed request for a team December 9, 2023 00:24
@prestonvasquez prestonvasquez marked this pull request as draft December 9, 2023 00:24
Copy link
Contributor

mongodb-drivers-pr-bot bot commented Dec 9, 2023

API Change Report

./x/bsonx/bsoncore

incompatible changes

ArrayStyle: removed
DocumentSequence: removed
DocumentSequenceStyle: removed
ErrCorruptedDocument: removed
ErrInvalidDocumentSequenceStyle: removed
ErrNonDocument: removed
SequenceStyle: removed

compatible changes

Iterator: added

./x/mongo/driver

incompatible changes

##(*BatchCursor).Batch: changed from func() *./x/bsonx/bsoncore.DocumentSequence to func() *./x/bsonx/bsoncore.Iterator
##CursorResponse.FirstBatch: changed from *./x/bsonx/bsoncore.DocumentSequence to *./x/bsonx/bsoncore.Iterator
NewBatchCursorFromDocuments: removed

compatible changes

NewBatchCursorFromList: added

@prestonvasquez prestonvasquez marked this pull request as ready for review December 11, 2023 21:34
@blink1073
Copy link
Member

@prestonvasquez this has a conflict now.

@prestonvasquez prestonvasquez added the priority-3-low Low Priority PR for Review label Dec 18, 2023
docs := make([]Document, 0, len(vals))
for _, v := range vals {
if v.Type != bsontype.EmbeddedDocument {
continue
Copy link
Collaborator

Choose a reason for hiding this comment

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

The method documentation says this will return an error, but it actually ignores non-document values. We should return an error here instead.

Comment on lines 108 to 112
if len(iter.List[iter.pos:]) == 1 && iter.List[iter.pos] == 0x00 {
return nil, io.EOF // At the end of the document
}

elem, _, ok := ReadElement(iter.List[iter.pos:])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Optional: Consider clarifying the logic here by storing the remaining data as a variable.

Suggested change
if len(iter.List[iter.pos:]) == 1 && iter.List[iter.pos] == 0x00 {
return nil, io.EOF // At the end of the document
}
elem, _, ok := ReadElement(iter.List[iter.pos:])
rem := iter.List[iter.pos:]
if len(rem) == 1 && rem[0] == 0x00 {
return nil, io.EOF // At the end of the document
}
elem, _, ok := ReadElement(rem)


// ErrCorruptedDocument is returned when a full document couldn't be read from
// the sequence.
var ErrCorruptedDocument = errors.New("invalid DocumentSequence: corrupted document")
Copy link
Collaborator

Choose a reason for hiding this comment

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

This error variable isn't used outside of this package and doesn't need to be exported.

Comment on lines 20 to 26
// ErrNonDocument is returned when a DocumentSequence contains a non-document
// BSON value.
var ErrNonDocument = errors.New("invalid DocumentSequence: a non-document value was found in sequence")

// ErrInvalidDocumentSequenceStyle is returned when an unknown
// DocumentSequenceStyle is set on a DocumentSequence.
var ErrInvalidDocumentSequenceStyle = errors.New("invalid DocumentSequenceStyle")
Copy link
Collaborator

Choose a reason for hiding this comment

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

These error variables are unused and should be removed.

matthewdale
matthewdale previously approved these changes Jan 10, 2024
Copy link
Collaborator

@matthewdale matthewdale left a comment

Choose a reason for hiding this comment

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

Looks good! 👍

qingyang-hu
qingyang-hu previously approved these changes Jan 11, 2024
Copy link
Collaborator

@qingyang-hu qingyang-hu left a comment

Choose a reason for hiding this comment

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

👍

qingyang-hu
qingyang-hu previously approved these changes Jan 12, 2024
@prestonvasquez prestonvasquez merged commit 12a97f7 into mongodb:master Jan 13, 2024
34 of 40 checks passed
@prestonvasquez prestonvasquez deleted the GODRIVER-3049 branch January 13, 2024 00:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority-3-low Low Priority PR for Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants