github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/mongo/readconcern/readconcern.go (about) 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 7 // Package readconcern defines read concerns for MongoDB operations. 8 // 9 // For more information about MongoDB read concerns, see 10 // https://www.mongodb.com/docs/manual/reference/read-concern/ 11 package readconcern // import "go.mongodb.org/mongo-driver/mongo/readconcern" 12 13 import ( 14 "errors" 15 16 "go.mongodb.org/mongo-driver/bson/bsontype" 17 "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" 18 ) 19 20 // A ReadConcern defines a MongoDB read concern, which allows you to control the consistency and 21 // isolation properties of the data read from replica sets and replica set shards. 22 // 23 // For more information about MongoDB read concerns, see 24 // https://www.mongodb.com/docs/manual/reference/read-concern/ 25 type ReadConcern struct { 26 Level string 27 } 28 29 // Option is an option to provide when creating a ReadConcern. 30 // 31 // Deprecated: Use the ReadConcern literal declaration instead. For example: 32 // 33 // &readconcern.ReadConcern{Level: "local"} 34 type Option func(concern *ReadConcern) 35 36 // Level creates an option that sets the level of a ReadConcern. 37 // 38 // Deprecated: Use the ReadConcern literal declaration instead. For example: 39 // 40 // &readconcern.ReadConcern{Level: "local"} 41 func Level(level string) Option { 42 return func(concern *ReadConcern) { 43 concern.Level = level 44 } 45 } 46 47 // Local returns a ReadConcern that requests data from the instance with no guarantee that the data 48 // has been written to a majority of the replica set members (i.e. may be rolled back). 49 // 50 // For more information about read concern "local", see 51 // https://www.mongodb.com/docs/manual/reference/read-concern-local/ 52 func Local() *ReadConcern { 53 return New(Level("local")) 54 } 55 56 // Majority returns a ReadConcern that requests data that has been acknowledged by a majority of the 57 // replica set members (i.e. the documents read are durable and guaranteed not to roll back). 58 // 59 // For more information about read concern "majority", see 60 // https://www.mongodb.com/docs/manual/reference/read-concern-majority/ 61 func Majority() *ReadConcern { 62 return New(Level("majority")) 63 } 64 65 // Linearizable returns a ReadConcern that requests data that reflects all successful 66 // majority-acknowledged writes that completed prior to the start of the read operation. 67 // 68 // For more information about read concern "linearizable", see 69 // https://www.mongodb.com/docs/manual/reference/read-concern-linearizable/ 70 func Linearizable() *ReadConcern { 71 return New(Level("linearizable")) 72 } 73 74 // Available returns a ReadConcern that requests data from an instance with no guarantee that the 75 // data has been written to a majority of the replica set members (i.e. may be rolled back). 76 // 77 // For more information about read concern "available", see 78 // https://www.mongodb.com/docs/manual/reference/read-concern-available/ 79 func Available() *ReadConcern { 80 return New(Level("available")) 81 } 82 83 // Snapshot returns a ReadConcern that requests majority-committed data as it appears across shards 84 // from a specific single point in time in the recent past. 85 // 86 // For more information about read concern "snapshot", see 87 // https://www.mongodb.com/docs/manual/reference/read-concern-snapshot/ 88 func Snapshot() *ReadConcern { 89 return New(Level("snapshot")) 90 } 91 92 // New constructs a new read concern from the given string. 93 // 94 // Deprecated: Use the ReadConcern literal declaration instead. For example: 95 // 96 // &readconcern.ReadConcern{Level: "local"} 97 func New(options ...Option) *ReadConcern { 98 concern := &ReadConcern{} 99 100 for _, option := range options { 101 option(concern) 102 } 103 104 return concern 105 } 106 107 // MarshalBSONValue implements the bson.ValueMarshaler interface. 108 // 109 // Deprecated: Marshaling a ReadConcern to BSON will not be supported in Go Driver 2.0. 110 func (rc *ReadConcern) MarshalBSONValue() (bsontype.Type, []byte, error) { 111 if rc == nil { 112 return 0, nil, errors.New("cannot marshal nil ReadConcern") 113 } 114 115 var elems []byte 116 117 if len(rc.Level) > 0 { 118 elems = bsoncore.AppendStringElement(elems, "level", rc.Level) 119 } 120 121 return bsontype.EmbeddedDocument, bsoncore.BuildDocument(nil, elems), nil 122 } 123 124 // GetLevel returns the read concern level. 125 // 126 // Deprecated: Use the ReadConcern.Level field instead. 127 func (rc *ReadConcern) GetLevel() string { 128 return rc.Level 129 }