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 command |
| 8 | |
| 9 | import ( |
| 10 | "context" |
| 11 | |
| 12 | "github.com/mongodb/mongo-go-driver/bson" |
| 13 | "github.com/mongodb/mongo-go-driver/mongo/writeconcern" |
| 14 | "github.com/mongodb/mongo-go-driver/x/bsonx" |
| 15 | "github.com/mongodb/mongo-go-driver/x/mongo/driver/session" |
| 16 | "github.com/mongodb/mongo-go-driver/x/network/description" |
| 17 | "github.com/mongodb/mongo-go-driver/x/network/result" |
| 18 | "github.com/mongodb/mongo-go-driver/x/network/wiremessage" |
| 19 | ) |
| 20 | |
| 21 | // Delete represents the delete command. |
| 22 | // |
| 23 | // The delete command executes a delete with a given set of delete documents |
| 24 | // and options. |
| 25 | type Delete struct { |
| 26 | ContinueOnError bool |
| 27 | NS Namespace |
| 28 | Deletes []bsonx.Doc |
| 29 | Opts []bsonx.Elem |
| 30 | WriteConcern *writeconcern.WriteConcern |
| 31 | Clock *session.ClusterClock |
| 32 | Session *session.Client |
| 33 | |
| 34 | batches []*WriteBatch |
| 35 | result result.Delete |
| 36 | err error |
| 37 | } |
| 38 | |
| 39 | // Encode will encode this command into a wire message for the given server description. |
| 40 | func (d *Delete) Encode(desc description.SelectedServer) ([]wiremessage.WireMessage, error) { |
| 41 | err := d.encode(desc) |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | |
| 46 | return batchesToWireMessage(d.batches, desc) |
| 47 | } |
| 48 | |
| 49 | func (d *Delete) encode(desc description.SelectedServer) error { |
| 50 | batches, err := splitBatches(d.Deletes, int(desc.MaxBatchCount), int(desc.MaxDocumentSize)) |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | |
| 55 | for _, docs := range batches { |
| 56 | cmd, err := d.encodeBatch(docs, desc) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | |
| 61 | d.batches = append(d.batches, cmd) |
| 62 | } |
| 63 | |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | func (d *Delete) encodeBatch(docs []bsonx.Doc, desc description.SelectedServer) (*WriteBatch, error) { |
| 68 | copyDocs := make([]bsonx.Doc, 0, len(docs)) |
| 69 | for _, doc := range docs { |
| 70 | copyDocs = append(copyDocs, doc.Copy()) |
| 71 | } |
| 72 | |
| 73 | var options []bsonx.Elem |
| 74 | for _, opt := range d.Opts { |
| 75 | if opt.Key == "collation" { |
| 76 | for idx := range copyDocs { |
| 77 | copyDocs[idx] = append(copyDocs[idx], opt) |
| 78 | } |
| 79 | } else { |
| 80 | options = append(options, opt) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | command, err := encodeBatch(copyDocs, options, DeleteCommand, d.NS.Collection) |
| 85 | if err != nil { |
| 86 | return nil, err |
| 87 | } |
| 88 | |
| 89 | return &WriteBatch{ |
| 90 | &Write{ |
| 91 | Clock: d.Clock, |
| 92 | DB: d.NS.DB, |
| 93 | Command: command, |
| 94 | WriteConcern: d.WriteConcern, |
| 95 | Session: d.Session, |
| 96 | }, |
| 97 | len(docs), |
| 98 | }, nil |
| 99 | } |
| 100 | |
| 101 | // Decode will decode the wire message using the provided server description. Errors during decoding |
| 102 | // are deferred until either the Result or Err methods are called. |
| 103 | func (d *Delete) Decode(desc description.SelectedServer, wm wiremessage.WireMessage) *Delete { |
| 104 | rdr, err := (&Write{}).Decode(desc, wm).Result() |
| 105 | if err != nil { |
| 106 | d.err = err |
| 107 | return d |
| 108 | } |
| 109 | |
| 110 | return d.decode(desc, rdr) |
| 111 | } |
| 112 | |
| 113 | func (d *Delete) decode(desc description.SelectedServer, rdr bson.Raw) *Delete { |
| 114 | d.err = bson.Unmarshal(rdr, &d.result) |
| 115 | return d |
| 116 | } |
| 117 | |
| 118 | // Result returns the result of a decoded wire message and server description. |
| 119 | func (d *Delete) Result() (result.Delete, error) { |
| 120 | if d.err != nil { |
| 121 | return result.Delete{}, d.err |
| 122 | } |
| 123 | return d.result, nil |
| 124 | } |
| 125 | |
| 126 | // Err returns the error set on this command. |
| 127 | func (d *Delete) Err() error { return d.err } |
| 128 | |
| 129 | // RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter. |
| 130 | func (d *Delete) RoundTrip(ctx context.Context, desc description.SelectedServer, rw wiremessage.ReadWriter) (result.Delete, error) { |
| 131 | if d.batches == nil { |
| 132 | if err := d.encode(desc); err != nil { |
| 133 | return result.Delete{}, err |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | r, batches, err := roundTripBatches( |
| 138 | ctx, desc, rw, |
| 139 | d.batches, |
| 140 | d.ContinueOnError, |
| 141 | d.Session, |
| 142 | DeleteCommand, |
| 143 | ) |
| 144 | |
| 145 | if batches != nil { |
| 146 | d.batches = batches |
| 147 | } |
| 148 | |
| 149 | if err != nil { |
| 150 | return result.Delete{}, err |
| 151 | } |
| 152 | |
| 153 | return r.(result.Delete), nil |
| 154 | } |