forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GODRIVER-932 Change-Id: I1234c66952cde375a272052f87f9753ab14e7706
- Loading branch information
Showing
78 changed files
with
2,548 additions
and
518 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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,66 @@ | ||
package driver | ||
|
||
import ( | ||
"errors" | ||
|
||
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore" | ||
) | ||
|
||
// this is the amount of reserved buffer space in a message that the | ||
// driver reserves for command overhead. | ||
const reservedCommandBufferBytes = 16 * 10 * 10 * 10 | ||
|
||
// ErrDocumentTooLarge occurs when a document that is larger than the maximum size accepted by a | ||
// server is passed to an insert command. | ||
var ErrDocumentTooLarge = errors.New("an inserted document is too large") | ||
|
||
// Batches contains the necessary information to batch split an operation. This is only used for write | ||
// oeprations. | ||
type Batches struct { | ||
Identifier string | ||
Documents []bsoncore.Document | ||
Current []bsoncore.Document | ||
Ordered *bool | ||
} | ||
|
||
// Valid returns true if Batches contains both an identifier and the length of Documents is greater | ||
// than zero. | ||
func (b *Batches) Valid() bool { return b != nil && b.Identifier != "" && len(b.Documents) > 0 } | ||
|
||
// ClearBatch clears the Current batch. This must be called before AdvanceBatch will advance to the | ||
// next batch. | ||
func (b *Batches) ClearBatch() { b.Current = b.Current[:0] } | ||
|
||
// AdvanceBatch splits the next batch using maxCount and targetBbatchSize. This method will do nothing if | ||
// the current batch has not been cleared. We do this so that when this is called during execute we | ||
// can call it without first needing to check if we already have a batch, which makes the code | ||
// simpler and makes retrying easier. | ||
func (b *Batches) AdvanceBatch(maxCount, targetBatchSize int) error { | ||
if len(b.Current) > 0 { | ||
return nil | ||
} | ||
if targetBatchSize > reservedCommandBufferBytes { | ||
targetBatchSize -= reservedCommandBufferBytes | ||
} | ||
|
||
if maxCount <= 0 { | ||
maxCount = 1 | ||
} | ||
|
||
splitAfter := 0 | ||
size := 1 | ||
for _, doc := range b.Documents { | ||
if len(doc) > targetBatchSize { | ||
return ErrDocumentTooLarge | ||
} | ||
if size+len(doc) > targetBatchSize { | ||
break | ||
} | ||
|
||
size += len(doc) | ||
splitAfter++ | ||
} | ||
|
||
b.Current, b.Documents = b.Documents[:splitAfter], b.Documents[splitAfter:] | ||
return nil | ||
} |
Oops, something went wrong.