blob: e04bd6fe6979910d68d850f34800b2a8d401af10 [file] [log] [blame]
Don Newton379ae252019-04-01 12:17:06 -04001// 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
7package command
8
9import (
10 "context"
11
12 "github.com/mongodb/mongo-go-driver/bson"
13 "github.com/mongodb/mongo-go-driver/x/bsonx"
14 "github.com/mongodb/mongo-go-driver/x/mongo/driver/session"
15 "github.com/mongodb/mongo-go-driver/x/network/description"
16 "github.com/mongodb/mongo-go-driver/x/network/result"
17 "github.com/mongodb/mongo-go-driver/x/network/wiremessage"
18)
19
20// CommitTransaction represents the commitTransaction() command
21type CommitTransaction struct {
22 Session *session.Client
23 err error
24 result result.TransactionResult
25}
26
27// Encode will encode this command into a wiremessage for the given server description.
28func (ct *CommitTransaction) Encode(desc description.SelectedServer) (wiremessage.WireMessage, error) {
29 cmd := ct.encode(desc)
30 return cmd.Encode(desc)
31}
32
33func (ct *CommitTransaction) encode(desc description.SelectedServer) *Write {
34 cmd := bsonx.Doc{{"commitTransaction", bsonx.Int32(1)}}
35 return &Write{
36 DB: "admin",
37 Command: cmd,
38 Session: ct.Session,
39 WriteConcern: ct.Session.CurrentWc,
40 }
41}
42
43// Decode will decode the wire message using the provided server description. Errors during decoding are deferred until
44// either the Result or Err methods are called.
45func (ct *CommitTransaction) Decode(desc description.SelectedServer, wm wiremessage.WireMessage) *CommitTransaction {
46 rdr, err := (&Write{}).Decode(desc, wm).Result()
47 if err != nil {
48 ct.err = err
49 return ct
50 }
51
52 return ct.decode(desc, rdr)
53}
54
55func (ct *CommitTransaction) decode(desc description.SelectedServer, rdr bson.Raw) *CommitTransaction {
56 ct.err = bson.Unmarshal(rdr, &ct.result)
57 if ct.err == nil && ct.result.WriteConcernError != nil {
58 ct.err = Error{
59 Code: int32(ct.result.WriteConcernError.Code),
60 Message: ct.result.WriteConcernError.ErrMsg,
61 }
62 }
63 return ct
64}
65
66// Result returns the result of a decoded wire message and server description.
67func (ct *CommitTransaction) Result() (result.TransactionResult, error) {
68 if ct.err != nil {
69 return result.TransactionResult{}, ct.err
70 }
71
72 return ct.result, nil
73}
74
75// Err returns the error set on this command
76func (ct *CommitTransaction) Err() error {
77 return ct.err
78}
79
80// RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter
81func (ct *CommitTransaction) RoundTrip(ctx context.Context, desc description.SelectedServer, rw wiremessage.ReadWriter) (result.TransactionResult, error) {
82 cmd := ct.encode(desc)
83 rdr, err := cmd.RoundTrip(ctx, desc, rw)
84 if err != nil {
85 return result.TransactionResult{}, err
86 }
87
88 return ct.decode(desc, rdr).Result()
89}