You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

readconcern.go 2.5 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (C) MongoDB, Inc. 2017-present.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. package readconcern // import "go.mongodb.org/mongo-driver/mongo/readconcern"
  7. import (
  8. "go.mongodb.org/mongo-driver/bson/bsontype"
  9. "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
  10. )
  11. // ReadConcern for replica sets and replica set shards determines which data to return from a query.
  12. type ReadConcern struct {
  13. level string
  14. }
  15. // Option is an option to provide when creating a ReadConcern.
  16. type Option func(concern *ReadConcern)
  17. // Level creates an option that sets the level of a ReadConcern.
  18. func Level(level string) Option {
  19. return func(concern *ReadConcern) {
  20. concern.level = level
  21. }
  22. }
  23. // Local specifies that the query should return the instance’s most recent data.
  24. func Local() *ReadConcern {
  25. return New(Level("local"))
  26. }
  27. // Majority specifies that the query should return the instance’s most recent data acknowledged as
  28. // having been written to a majority of members in the replica set.
  29. func Majority() *ReadConcern {
  30. return New(Level("majority"))
  31. }
  32. // Linearizable specifies that the query should return data that reflects all successful writes
  33. // issued with a write concern of "majority" and acknowledged prior to the start of the read operation.
  34. func Linearizable() *ReadConcern {
  35. return New(Level("linearizable"))
  36. }
  37. // Available specifies that the query should return data from the instance with no guarantee
  38. // that the data has been written to a majority of the replica set members (i.e. may be rolled back).
  39. func Available() *ReadConcern {
  40. return New(Level("available"))
  41. }
  42. // Snapshot is only available for operations within multi-document transactions.
  43. func Snapshot() *ReadConcern {
  44. return New(Level("snapshot"))
  45. }
  46. // New constructs a new read concern from the given string.
  47. func New(options ...Option) *ReadConcern {
  48. concern := &ReadConcern{}
  49. for _, option := range options {
  50. option(concern)
  51. }
  52. return concern
  53. }
  54. // MarshalBSONValue implements the bson.ValueMarshaler interface.
  55. func (rc *ReadConcern) MarshalBSONValue() (bsontype.Type, []byte, error) {
  56. var elems []byte
  57. if len(rc.level) > 0 {
  58. elems = bsoncore.AppendStringElement(elems, "level", rc.level)
  59. }
  60. return bsontype.EmbeddedDocument, bsoncore.BuildDocument(nil, elems), nil
  61. }