@@ -31,19 +31,18 @@ const errorInterrupted int32 = 11601
31
31
const errorCappedPositionLost int32 = 136
32
32
const errorCursorKilled int32 = 237
33
33
34
- // ErrMissingResumeToken indicates that a change stream notification from the server did not
35
- // contain a resume token.
34
+ // ErrMissingResumeToken indicates that a change stream notification from the server did not contain a resume token.
36
35
var ErrMissingResumeToken = errors .New ("cannot provide resume functionality when the resume token is missing" )
37
36
38
- // ErrNilCursor indicates that the cursor for the change stream is nil.
37
+ // ErrNilCursor indicates that the underlying cursor for the change stream is nil.
39
38
var ErrNilCursor = errors .New ("cursor is nil" )
40
39
41
- // ChangeStream instances iterate a stream of change documents. Each document can be decoded via the
42
- // Decode method. Resume tokens should be retrieved via the ResumeToken method and can be stored to
43
- // resume the change stream at a specific point in time.
44
- //
45
- // A typical usage of the ChangeStream type would be:
40
+ // ChangeStream is used to iterate over a stream of events. Each event can be decoded into a Go type via the Decode
41
+ // method or accessed as raw BSON via the Current field. For more information about change streams, see
42
+ // https://docs.mongodb.com/manual/changeStreams/.
46
43
type ChangeStream struct {
44
+ // Current is the BSON bytes of the current event. This property is only valid until the next call to Next or
45
+ // TryNext. If continued access is required, a copy must be made.
47
46
Current bson.Raw
48
47
49
48
aggregate * operation.Aggregate
@@ -405,15 +404,16 @@ func (cs *ChangeStream) replaceOptions(ctx context.Context, wireVersion *descrip
405
404
cs .options .SetStartAtOperationTime (nil )
406
405
}
407
406
408
- // ID returns the cursor ID for this change stream.
407
+ // ID returns the ID for this change stream, or 0 if the cursor has been closed or exhausted .
409
408
func (cs * ChangeStream ) ID () int64 {
410
409
if cs .cursor == nil {
411
410
return 0
412
411
}
413
412
return cs .cursor .ID ()
414
413
}
415
414
416
- // Decode will decode the current document into val.
415
+ // Decode will unmarshal the current event document into val and return any errors from the unmarshalling process
416
+ // without any modification. If val is nil or is a typed nil, an error will be returned.
417
417
func (cs * ChangeStream ) Decode (val interface {}) error {
418
418
if cs .cursor == nil {
419
419
return ErrNilCursor
@@ -422,7 +422,7 @@ func (cs *ChangeStream) Decode(val interface{}) error {
422
422
return bson .UnmarshalWithRegistry (cs .registry , cs .Current , val )
423
423
}
424
424
425
- // Err returns the current error.
425
+ // Err returns the last error seen by the change stream, or nil if no errors has occurred .
426
426
func (cs * ChangeStream ) Err () error {
427
427
if cs .err != nil {
428
428
return replaceErrors (cs .err )
@@ -434,7 +434,8 @@ func (cs *ChangeStream) Err() error {
434
434
return replaceErrors (cs .cursor .Err ())
435
435
}
436
436
437
- // Close closes this cursor.
437
+ // Close closes this change stream and the underlying cursor. Next and TryNext must not be called after Close has been
438
+ // called. Close is idempotent. After the first call, any subsequent calls will not change the state.
438
439
func (cs * ChangeStream ) Close (ctx context.Context ) error {
439
440
if ctx == nil {
440
441
ctx = context .Background ()
@@ -451,26 +452,34 @@ func (cs *ChangeStream) Close(ctx context.Context) error {
451
452
return cs .Err ()
452
453
}
453
454
454
- // ResumeToken returns the last cached resume token for this change stream.
455
+ // ResumeToken returns the last cached resume token for this change stream, or nil if a resume token has not been
456
+ // stored.
455
457
func (cs * ChangeStream ) ResumeToken () bson.Raw {
456
458
return cs .resumeToken
457
459
}
458
460
459
- // Next gets the next result from this change stream. Returns true if there were no errors and the next
460
- // result is available for decoding. Next blocks until an event is available for decoding or ctx expires.
461
- // If the given context expires during execution, the stream's error will be set and the change stream may be in an
462
- // invalid state and should be re-created. If Next returns false, it must not be called again.
461
+ // Next gets the next event for this change stream. It returns true if there were no errors and the next event document
462
+ // is available.
463
+ //
464
+ // Next blocks until an event is available, an error occurs, or ctx expires. If ctx expires, the error
465
+ // will be set to ctx.Err(). In an error case, Next will return false.
466
+ //
467
+ // If Next returns false, subsequent calls will also return false.
463
468
func (cs * ChangeStream ) Next (ctx context.Context ) bool {
464
469
return cs .next (ctx , false )
465
470
}
466
471
467
- // TryNext attempts to get the next result from this change stream. It returns true if there were no errors and the next
468
- // result is available for decoding. It returns false if the change stream was closed by the server, there was an
469
- // error getting more results from the server, the server returned an empty batch of events, or the given context expires.
470
- // If an error occurred or the stream was closed (can be checked with cs.Err() != nil || cs.ID() == 0), TryNext must
471
- // not be called again. If the given context expires during execution, the stream's error will be set and the change
472
- // stream may be in an invalid state and should be re-created.
473
- // Added in version 1.2.0.
472
+ // TryNext attempts to get the next event for this change stream. It returns true if there were no errors and the next
473
+ // event document is available.
474
+ //
475
+ // TryNext returns false if the change stream is closed by the server, an error occurs when getting changes from the
476
+ // server, the next change is not yet available, or ctx expires. If ctx expires, the error will be set to ctx.Err().
477
+ //
478
+ // If TryNext returns false and an error occurred or the change stream was closed
479
+ // (i.e. cs.Err() != nil || cs.ID() == 0), subsequent attempts will also return false. Otherwise, it is safe to call
480
+ // TryNext again until a change is available.
481
+ //
482
+ // This method requires driver version >= 1.2.0.
474
483
func (cs * ChangeStream ) TryNext (ctx context.Context ) bool {
475
484
return cs .next (ctx , true )
476
485
}
@@ -549,11 +558,11 @@ func (cs *ChangeStream) emptyBatch() bool {
549
558
return cs .cursor .Batch ().Empty ()
550
559
}
551
560
552
- // StreamType represents the type of a change stream .
561
+ // StreamType represents the cluster type against which a ChangeStream was created .
553
562
type StreamType uint8
554
563
555
564
// These constants represent valid change stream types. A change stream can be initialized over a collection, all
556
- // collections in a database, or over a whole client .
565
+ // collections in a database, or over a cluster .
557
566
const (
558
567
CollectionStream StreamType = iota
559
568
DatabaseStream
0 commit comments