Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 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 |
| 8 | |
| 9 | import ( |
| 10 | "github.com/mongodb/mongo-go-driver/bson/bsontype" |
| 11 | "github.com/mongodb/mongo-go-driver/x/bsonx/bsoncore" |
| 12 | ) |
| 13 | |
| 14 | // ReadConcern for replica sets and replica set shards determines which data to return from a query. |
| 15 | type ReadConcern struct { |
| 16 | level string |
| 17 | } |
| 18 | |
| 19 | // Option is an option to provide when creating a ReadConcern. |
| 20 | type Option func(concern *ReadConcern) |
| 21 | |
| 22 | // Level creates an option that sets the level of a ReadConcern. |
| 23 | func Level(level string) Option { |
| 24 | return func(concern *ReadConcern) { |
| 25 | concern.level = level |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | // Local specifies that the query should return the instance’s most recent data. |
| 30 | func Local() *ReadConcern { |
| 31 | return New(Level("local")) |
| 32 | } |
| 33 | |
| 34 | // Majority specifies that the query should return the instance’s most recent data acknowledged as |
| 35 | // having been written to a majority of members in the replica set. |
| 36 | func Majority() *ReadConcern { |
| 37 | return New(Level("majority")) |
| 38 | } |
| 39 | |
| 40 | // Linearizable specifies that the query should return data that reflects all successful writes |
| 41 | // issued with a write concern of "majority" and acknowledged prior to the start of the read operation. |
| 42 | func Linearizable() *ReadConcern { |
| 43 | return New(Level("linearizable")) |
| 44 | } |
| 45 | |
| 46 | // Available specifies that the query should return data from the instance with no guarantee |
| 47 | // that the data has been written to a majority of the replica set members (i.e. may be rolled back). |
| 48 | func Available() *ReadConcern { |
| 49 | return New(Level("available")) |
| 50 | } |
| 51 | |
| 52 | // Snapshot is only available for operations within multi-document transactions. |
| 53 | func Snapshot() *ReadConcern { |
| 54 | return New(Level("snapshot")) |
| 55 | } |
| 56 | |
| 57 | // New constructs a new read concern from the given string. |
| 58 | func New(options ...Option) *ReadConcern { |
| 59 | concern := &ReadConcern{} |
| 60 | |
| 61 | for _, option := range options { |
| 62 | option(concern) |
| 63 | } |
| 64 | |
| 65 | return concern |
| 66 | } |
| 67 | |
| 68 | // MarshalBSONValue implements the bson.ValueMarshaler interface. |
| 69 | func (rc *ReadConcern) MarshalBSONValue() (bsontype.Type, []byte, error) { |
| 70 | var elems []byte |
| 71 | |
| 72 | if len(rc.level) > 0 { |
| 73 | elems = bsoncore.AppendStringElement(elems, "level", rc.level) |
| 74 | } |
| 75 | |
| 76 | return bsontype.EmbeddedDocument, bsoncore.BuildDocument(nil, elems), nil |
| 77 | } |