| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package mongodocstore |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"fmt" |
| 20 |
"io" |
| 21 |
"strings" |
| 22 |
|
| 23 |
"go.mongodb.org/mongo-driver/bson" |
| 24 |
"go.mongodb.org/mongo-driver/mongo" |
| 25 |
"go.mongodb.org/mongo-driver/mongo/options" |
| 26 |
"gocloud.dev/docstore/driver" |
| 27 |
) |
| 28 |
|
| 29 |
func (c *collection) RunGetQuery(ctx context.Context, q *driver.Query) (driver.DocumentIterator, error) { |
| 30 |
opts := options.Find() |
| 31 |
if len(q.FieldPaths) > 0 { |
| 32 |
opts.Projection = c.projectionDoc(q.FieldPaths) |
| 33 |
} |
| 34 |
if q.Offset > 0 { |
| 35 |
offset := int64(q.Offset) |
| 36 |
opts.Skip = &offset |
| 37 |
} |
| 38 |
if q.Limit > 0 { |
| 39 |
lim := int64(q.Limit) |
| 40 |
opts.Limit = &lim |
| 41 |
} |
| 42 |
if q.OrderByField != "" { |
| 43 |
f := q.OrderByField |
| 44 |
if c.opts.LowercaseFields { |
| 45 |
f = strings.ToLower(f) |
| 46 |
} |
| 47 |
var dir int |
| 48 |
if q.OrderAscending { |
| 49 |
dir = 1 |
| 50 |
} else { |
| 51 |
dir = -1 |
| 52 |
} |
| 53 |
opts.Sort = bson.D{{Key: f, Value: dir}} |
| 54 |
} |
| 55 |
|
| 56 |
filter := bson.D{} // must be a zero-length slice, not nil |
| 57 |
for _, f := range q.Filters { |
| 58 |
bf, err := c.filterToBSON(f) |
| 59 |
if err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
filter = append(filter, bf) |
| 63 |
} |
| 64 |
if q.BeforeQuery != nil { |
| 65 |
if err := q.BeforeQuery(driver.AsFunc(opts)); err != nil { |
| 66 |
return nil, err |
| 67 |
} |
| 68 |
} |
| 69 |
cursor, err := c.coll.Find(ctx, filter, opts) |
| 70 |
if err != nil { |
| 71 |
return nil, err |
| 72 |
} |
| 73 |
return &docIterator{cursor: cursor, idField: c.idField, ctx: ctx, lowercaseFields: c.opts.LowercaseFields}, nil |
| 74 |
} |
| 75 |
|
| 76 |
var mongoQueryOps = map[string]string{ |
| 77 |
driver.EqualOp: "$eq", |
| 78 |
">": "$gt", |
| 79 |
">=": "$gte", |
| 80 |
"<": "$lt", |
| 81 |
"<=": "$lte", |
| 82 |
"in": "$in", |
| 83 |
"not-in": "$nin", |
| 84 |
} |
| 85 |
|
| 86 |
// filtersToBSON converts a []driver.Filter to the MongoDB equivalent, expressed |
| 87 |
// as a bson.D (list of key-value pairs). |
| 88 |
func (c *collection) filtersToBSON(fs []driver.Filter) (bson.D, error) { |
| 89 |
filter := bson.D{} // must be a zero-length slice, not nil |
| 90 |
for _, f := range fs { |
| 91 |
bf, err := c.filterToBSON(f) |
| 92 |
if err != nil { |
| 93 |
return nil, err |
| 94 |
} |
| 95 |
filter = append(filter, bf) |
| 96 |
} |
| 97 |
return filter, nil |
| 98 |
} |
| 99 |
|
| 100 |
// filterToBSON converts a driver.Filter to the MongoDB equivalent, expressed |
| 101 |
// as a bson.E (key-value pair). |
| 102 |
// The MongoDB document corresponding to "field op value" is |
| 103 |
// |
| 104 |
// {field: {mop: value}} |
| 105 |
// |
| 106 |
// where mop is the mongo version of op (see the mongoQueryOps map above). |
| 107 |
func (c *collection) filterToBSON(f driver.Filter) (bson.E, error) { |
| 108 |
key := c.toMongoFieldPath(f.FieldPath) |
| 109 |
if c.idField != "" && key == c.idField { |
| 110 |
key = mongoIDField |
| 111 |
} |
| 112 |
val, err := encodeValue(f.Value) |
| 113 |
if err != nil { |
| 114 |
return bson.E{}, err |
| 115 |
} |
| 116 |
op := mongoQueryOps[f.Op] |
| 117 |
if op == "" { |
| 118 |
return bson.E{}, fmt.Errorf("no mongo operator for %q", f.Op) |
| 119 |
} |
| 120 |
return bson.E{Key: key, Value: bson.D{{Key: op, Value: val}}}, nil |
| 121 |
} |
| 122 |
|
| 123 |
type docIterator struct { |
| 124 |
cursor *mongo.Cursor |
| 125 |
idField string |
| 126 |
ctx context.Context // remember for Stop |
| 127 |
lowercaseFields bool |
| 128 |
} |
| 129 |
|
| 130 |
func (it *docIterator) Next(ctx context.Context, doc driver.Document) error { |
| 131 |
m, err := it.nextMap(ctx) |
| 132 |
if err != nil { |
| 133 |
return err |
| 134 |
} |
| 135 |
return decodeDoc(m, doc, it.idField, it.lowercaseFields) |
| 136 |
} |
| 137 |
|
| 138 |
func (it *docIterator) nextMap(ctx context.Context) (map[string]interface{}, error) { |
| 139 |
if !it.cursor.Next(ctx) { |
| 140 |
if it.cursor.Err() != nil { |
| 141 |
return nil, it.cursor.Err() |
| 142 |
} |
| 143 |
return nil, io.EOF |
| 144 |
} |
| 145 |
var m map[string]interface{} |
| 146 |
if err := it.cursor.Decode(&m); err != nil { |
| 147 |
return nil, fmt.Errorf("cursor.Decode: %v", err) |
| 148 |
} |
| 149 |
return m, nil |
| 150 |
} |
| 151 |
|
| 152 |
func (it *docIterator) Stop() { |
| 153 |
// Ignore error on Close. |
| 154 |
_ = it.cursor.Close(it.ctx) |
| 155 |
} |
| 156 |
|
| 157 |
func (it *docIterator) As(i interface{}) bool { |
| 158 |
p, ok := i.(**mongo.Cursor) |
| 159 |
if !ok { |
| 160 |
return false |
| 161 |
} |
| 162 |
*p = it.cursor |
| 163 |
return true |
| 164 |
} |
| 165 |
|
| 166 |
func (c *collection) QueryPlan(q *driver.Query) (string, error) { |
| 167 |
return "unknown", nil |
| 168 |
} |
| 169 |
|
| 170 |
func (c *collection) RunDeleteQuery(ctx context.Context, q *driver.Query) error { |
| 171 |
filter, err := c.filtersToBSON(q.Filters) |
| 172 |
if err != nil { |
| 173 |
return err |
| 174 |
} |
| 175 |
if q.BeforeQuery != nil { |
| 176 |
if err := q.BeforeQuery(driver.AsFunc(filter)); err != nil { |
| 177 |
return err |
| 178 |
} |
| 179 |
} |
| 180 |
_, err = c.coll.DeleteMany(ctx, filter) |
| 181 |
return err |
| 182 |
} |
| 183 |
|
| 184 |
func (c *collection) RunUpdateQuery(ctx context.Context, q *driver.Query, mods []driver.Mod) error { |
| 185 |
filter, err := c.filtersToBSON(q.Filters) |
| 186 |
if err != nil { |
| 187 |
return err |
| 188 |
} |
| 189 |
// TODO(#2458): provide an option for the user to choose whether or not to |
| 190 |
// update the revision. |
| 191 |
updateDoc, _, err := c.newUpdateDoc(mods, !c.opts.NoWriteQueryUpdateRevisions) |
| 192 |
if err != nil { |
| 193 |
return err |
| 194 |
} |
| 195 |
if q.BeforeQuery != nil { |
| 196 |
if err := q.BeforeQuery(driver.AsFunc(filter)); err != nil { |
| 197 |
return err |
| 198 |
} |
| 199 |
} |
| 200 |
_, err = c.coll.UpdateMany(ctx, filter, updateDoc) |
| 201 |
return err |
| 202 |
} |