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 mongo |
| 8 | |
| 9 | import ( |
| 10 | "fmt" |
| 11 | |
| 12 | "github.com/mongodb/mongo-go-driver/bson" |
| 13 | "github.com/mongodb/mongo-go-driver/x/network/result" |
| 14 | ) |
| 15 | |
| 16 | // BulkWriteResult holds the result of a bulk write operation. |
| 17 | type BulkWriteResult struct { |
| 18 | InsertedCount int64 |
| 19 | MatchedCount int64 |
| 20 | ModifiedCount int64 |
| 21 | DeletedCount int64 |
| 22 | UpsertedCount int64 |
| 23 | UpsertedIDs map[int64]interface{} |
| 24 | } |
| 25 | |
| 26 | // InsertOneResult is a result of an InsertOne operation. |
| 27 | // |
| 28 | // InsertedID will be a Go type that corresponds to a BSON type. |
| 29 | type InsertOneResult struct { |
| 30 | // The identifier that was inserted. |
| 31 | InsertedID interface{} |
| 32 | } |
| 33 | |
| 34 | // InsertManyResult is a result of an InsertMany operation. |
| 35 | type InsertManyResult struct { |
| 36 | // Maps the indexes of inserted documents to their _id fields. |
| 37 | InsertedIDs []interface{} |
| 38 | } |
| 39 | |
| 40 | // DeleteResult is a result of an DeleteOne operation. |
| 41 | type DeleteResult struct { |
| 42 | // The number of documents that were deleted. |
| 43 | DeletedCount int64 `bson:"n"` |
| 44 | } |
| 45 | |
| 46 | // ListDatabasesResult is a result of a ListDatabases operation. Each specification |
| 47 | // is a description of the datbases on the server. |
| 48 | type ListDatabasesResult struct { |
| 49 | Databases []DatabaseSpecification |
| 50 | TotalSize int64 |
| 51 | } |
| 52 | |
| 53 | func (ldr ListDatabasesResult) fromResult(res result.ListDatabases) ListDatabasesResult { |
| 54 | ldr.Databases = make([]DatabaseSpecification, 0, len(res.Databases)) |
| 55 | for _, spec := range res.Databases { |
| 56 | ldr.Databases = append( |
| 57 | ldr.Databases, |
| 58 | DatabaseSpecification{Name: spec.Name, SizeOnDisk: spec.SizeOnDisk, Empty: spec.Empty}, |
| 59 | ) |
| 60 | } |
| 61 | ldr.TotalSize = res.TotalSize |
| 62 | return ldr |
| 63 | } |
| 64 | |
| 65 | // DatabaseSpecification is the information for a single database returned |
| 66 | // from a ListDatabases operation. |
| 67 | type DatabaseSpecification struct { |
| 68 | Name string |
| 69 | SizeOnDisk int64 |
| 70 | Empty bool |
| 71 | } |
| 72 | |
| 73 | // UpdateResult is a result of an update operation. |
| 74 | // |
| 75 | // UpsertedID will be a Go type that corresponds to a BSON type. |
| 76 | type UpdateResult struct { |
| 77 | // The number of documents that matched the filter. |
| 78 | MatchedCount int64 |
| 79 | // The number of documents that were modified. |
| 80 | ModifiedCount int64 |
| 81 | // The number of documents that were upserted. |
| 82 | UpsertedCount int64 |
| 83 | // The identifier of the inserted document if an upsert took place. |
| 84 | UpsertedID interface{} |
| 85 | } |
| 86 | |
| 87 | // UnmarshalBSON implements the bson.Unmarshaler interface. |
| 88 | func (result *UpdateResult) UnmarshalBSON(b []byte) error { |
| 89 | elems, err := bson.Raw(b).Elements() |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | |
| 94 | for _, elem := range elems { |
| 95 | switch elem.Key() { |
| 96 | case "n": |
| 97 | switch elem.Value().Type { |
| 98 | case bson.TypeInt32: |
| 99 | result.MatchedCount = int64(elem.Value().Int32()) |
| 100 | case bson.TypeInt64: |
| 101 | result.MatchedCount = elem.Value().Int64() |
| 102 | default: |
| 103 | return fmt.Errorf("Received invalid type for n, should be Int32 or Int64, received %s", elem.Value().Type) |
| 104 | } |
| 105 | case "nModified": |
| 106 | switch elem.Value().Type { |
| 107 | case bson.TypeInt32: |
| 108 | result.ModifiedCount = int64(elem.Value().Int32()) |
| 109 | case bson.TypeInt64: |
| 110 | result.ModifiedCount = elem.Value().Int64() |
| 111 | default: |
| 112 | return fmt.Errorf("Received invalid type for nModified, should be Int32 or Int64, received %s", elem.Value().Type) |
| 113 | } |
| 114 | case "upserted": |
| 115 | switch elem.Value().Type { |
| 116 | case bson.TypeArray: |
| 117 | e, err := elem.Value().Array().IndexErr(0) |
| 118 | if err != nil { |
| 119 | break |
| 120 | } |
| 121 | if e.Value().Type != bson.TypeEmbeddedDocument { |
| 122 | break |
| 123 | } |
| 124 | var d struct { |
| 125 | ID interface{} `bson:"_id"` |
| 126 | } |
| 127 | err = bson.Unmarshal(e.Value().Document(), &d) |
| 128 | if err != nil { |
| 129 | return err |
| 130 | } |
| 131 | result.UpsertedID = d.ID |
| 132 | default: |
| 133 | return fmt.Errorf("Received invalid type for upserted, should be Array, received %s", elem.Value().Type) |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return nil |
| 139 | } |