Skip to content

Commit

Permalink
Have Update() and Remove() return ChangeInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
kahuang committed Oct 30, 2018
1 parent eeefdec commit 6ae1285
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 39 deletions.
2 changes: 1 addition & 1 deletion gridfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (gfs *GridFS) Find(query interface{}) *Query {

// RemoveId deletes the file with the provided id from the GridFS.
func (gfs *GridFS) RemoveId(id interface{}) error {
err := gfs.Files.Remove(bson.M{"_id": id})
_, err := gfs.Files.Remove(bson.M{"_id": id})
if err != nil {
return err
}
Expand Down
32 changes: 19 additions & 13 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ func (db *Database) UpsertUser(user *User) error {
}
}
users := db.C("system.users")
err = users.Update(bson.D{{Name: "user", Value: user.Username}}, bson.D{{Name: "$unset", Value: unset}, {Name: "$set", Value: set}})
_, err = users.Update(bson.D{{Name: "user", Value: user.Username}}, bson.D{{Name: "$unset", Value: unset}, {Name: "$set", Value: set}})
if err == ErrNotFound {
set = append(set, bson.DocElem{Name: "user", Value: user.Username})
if user.Roles == nil && user.OtherDBRoles == nil {
Expand Down Expand Up @@ -1394,7 +1394,8 @@ func (db *Database) RemoveUser(user string) error {
err := db.Run(bson.D{{Name: "dropUser", Value: user}}, nil)
if isNoCmd(err) {
users := db.C("system.users")
return users.Remove(bson.M{"user": user})
_, err := users.Remove(bson.M{"user": user})
return err
}
if isNotFound(err) {
return ErrNotFound
Expand Down Expand Up @@ -2912,7 +2913,6 @@ func (p *Pipe) SetMaxTime(d time.Duration) *Pipe {
return p
}


// Collation allows to specify language-specific rules for string comparison,
// such as rules for lettercase and accent marks.
// When specifying collation, the locale field is mandatory; all other collation
Expand Down Expand Up @@ -3013,7 +3013,7 @@ func (c *Collection) Insert(docs ...interface{}) error {
// http://www.mongodb.org/display/DOCS/Updating
// http://www.mongodb.org/display/DOCS/Atomic+Operations
//
func (c *Collection) Update(selector interface{}, update interface{}) error {
func (c *Collection) Update(selector interface{}, update interface{}) (info *ChangeInfo, err error) {
if selector == nil {
selector = bson.D{}
}
Expand All @@ -3024,17 +3024,20 @@ func (c *Collection) Update(selector interface{}, update interface{}) error {
}
lerr, err := c.writeOp(&op, true)
if err == nil && lerr != nil && !lerr.UpdatedExisting {
return ErrNotFound
return &ChangeInfo{}, ErrNotFound
}
return err
if err == nil && lerr != nil {
info = &ChangeInfo{Updated: lerr.modified, Matched: lerr.N}
}
return info, err
}

// UpdateId is a convenience helper equivalent to:
//
// err := collection.Update(bson.M{"_id": id}, update)
// changeInfo, err := collection.Update(bson.M{"_id": id}, update)
//
// See the Update method for more details.
func (c *Collection) UpdateId(id interface{}, update interface{}) error {
func (c *Collection) UpdateId(id interface{}, update interface{}) (*ChangeInfo, error) {
return c.Update(bson.D{{Name: "_id", Value: id}}, update)
}

Expand Down Expand Up @@ -3143,23 +3146,26 @@ func (c *Collection) UpsertId(id interface{}, update interface{}) (info *ChangeI
//
// http://www.mongodb.org/display/DOCS/Removing
//
func (c *Collection) Remove(selector interface{}) error {
func (c *Collection) Remove(selector interface{}) (info *ChangeInfo, err error) {
if selector == nil {
selector = bson.D{}
}
lerr, err := c.writeOp(&deleteOp{c.FullName, selector, 1, 1}, true)
if err == nil && lerr != nil && lerr.N == 0 {
return ErrNotFound
return &ChangeInfo{}, ErrNotFound
}
return err
if err == nil && lerr != nil {
info = &ChangeInfo{Updated: lerr.modified, Matched: lerr.N}
}
return info, err
}

// RemoveId is a convenience helper equivalent to:
//
// err := collection.Remove(bson.M{"_id": id})
// changeInfo, err := collection.Remove(bson.M{"_id": id})
//
// See the Remove method for more details.
func (c *Collection) RemoveId(id interface{}) error {
func (c *Collection) RemoveId(id interface{}) (*ChangeInfo, error) {
return c.Remove(bson.D{{Name: "_id", Value: id}})
}

Expand Down
32 changes: 16 additions & 16 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,18 +644,18 @@ func (s *S) TestUpdate(c *C) {
}

// No changes is a no-op and shouldn't return an error.
err = coll.Update(M{"k": 42}, M{"$set": M{"n": 42}})
_, err = coll.Update(M{"k": 42}, M{"$set": M{"n": 42}})
c.Assert(err, IsNil)

err = coll.Update(M{"k": 42}, M{"$inc": M{"n": 1}})
_, err = coll.Update(M{"k": 42}, M{"$inc": M{"n": 1}})
c.Assert(err, IsNil)

result := make(M)
err = coll.Find(M{"k": 42}).One(result)
c.Assert(err, IsNil)
c.Assert(result["n"], Equals, 43)

err = coll.Update(M{"k": 47}, M{"k": 47, "n": 47})
_, err = coll.Update(M{"k": 47}, M{"k": 47, "n": 47})
c.Assert(err, Equals, mgo.ErrNotFound)

err = coll.Find(M{"k": 47}).One(result)
Expand All @@ -675,15 +675,15 @@ func (s *S) TestUpdateId(c *C) {
c.Assert(err, IsNil)
}

err = coll.UpdateId(42, M{"$inc": M{"n": 1}})
_, err = coll.UpdateId(42, M{"$inc": M{"n": 1}})
c.Assert(err, IsNil)

result := make(M)
err = coll.FindId(42).One(result)
c.Assert(err, IsNil)
c.Assert(result["n"], Equals, 43)

err = coll.UpdateId(47, M{"k": 47, "n": 47})
_, err = coll.UpdateId(47, M{"k": 47, "n": 47})
c.Assert(err, Equals, mgo.ErrNotFound)

err = coll.FindId(47).One(result)
Expand All @@ -699,7 +699,7 @@ func (s *S) TestUpdateNil(c *C) {

err = coll.Insert(M{"k": 42, "n": 42})
c.Assert(err, IsNil)
err = coll.Update(nil, M{"$inc": M{"n": 1}})
_, err = coll.Update(nil, M{"$inc": M{"n": 1}})
c.Assert(err, IsNil)

result := make(M)
Expand All @@ -709,7 +709,7 @@ func (s *S) TestUpdateNil(c *C) {

err = coll.Insert(M{"k": 45, "n": 45})
c.Assert(err, IsNil)
_, err = coll.UpdateAll(nil, M{"$inc": M{"n": 1}})
_, _, err = coll.UpdateAll(nil, M{"$inc": M{"n": 1}})
c.Assert(err, IsNil)

err = coll.Find(M{"k": 42}).One(result)
Expand Down Expand Up @@ -843,7 +843,7 @@ func (s *S) TestUpdateAll(c *C) {
c.Assert(info.Matched, Equals, 4)
}

info, err = coll.UpdateAll(M{"k": M{"$gt": 42}}, M{"$inc": M{"n": 1}})
info, _, err = coll.UpdateAll(M{"k": M{"$gt": 42}}, M{"$inc": M{"n": 1}})
c.Assert(err, IsNil)
c.Assert(info.Updated, Equals, 4)
c.Assert(info.Matched, Equals, 4)
Expand All @@ -863,7 +863,7 @@ func (s *S) TestUpdateAll(c *C) {

if !s.versionAtLeast(2, 6) {
// 2.6 made this invalid.
info, err = coll.UpdateAll(M{"k": 47}, M{"k": 47, "n": 47})
info, _, err = coll.UpdateAll(M{"k": 47}, M{"k": 47, "n": 47})
c.Assert(err, Equals, nil)
c.Assert(info.Updated, Equals, 0)
}
Expand All @@ -882,7 +882,7 @@ func (s *S) TestRemove(c *C) {
c.Assert(err, IsNil)
}

err = coll.Remove(M{"n": M{"$gt": 42}})
_, err = coll.Remove(M{"n": M{"$gt": 42}})
c.Assert(err, IsNil)

result := &struct{ N int }{}
Expand Down Expand Up @@ -1160,7 +1160,7 @@ func (s *S) TestCreateCollectionValidator(c *C) {
c.Assert(err, IsNil)
err = coll.Insert(M{"a": 2})
c.Assert(err, ErrorMatches, "Document failed validation")
err = coll.Update(M{"a": 1}, M{"c": 1})
_, err = coll.Update(M{"a": 1}, M{"c": 1})
c.Assert(err, IsNil)
err = coll.DropCollection()
c.Assert(err, IsNil)
Expand Down Expand Up @@ -1556,10 +1556,10 @@ func (s *S) TestView(c *C) {
err = view.Insert(bson.M{"_id": 5, "nm": "b"})
c.Assert(err, NotNil)

err = view.Remove(bson.M{"_id": 2})
_, err = view.Remove(bson.M{"_id": 2})
c.Assert(err, NotNil)

err = view.Update(bson.M{"_id": 2}, bson.M{"$set": bson.M{"d": true}})
_, err = view.Update(bson.M{"_id": 2}, bson.M{"$set": bson.M{"d": true}})
c.Assert(err, NotNil)

err = db.C("myview").DropCollection()
Expand Down Expand Up @@ -5018,19 +5018,19 @@ func (s *S) TestBypassValidation(c *C) {
err = coll.Insert(M{"n": 2})
c.Assert(err, ErrorMatches, "Document failed validation")

err = coll.Update(M{"n": 1}, M{"n": 10})
_, err = coll.Update(M{"n": 1}, M{"n": 10})
c.Assert(err, ErrorMatches, "Document failed validation")

session.SetBypassValidation(true)

err = coll.Insert(M{"n": 3})
c.Assert(err, IsNil)

err = coll.Update(M{"n": 3}, M{"n": 4})
_, err = coll.Update(M{"n": 3}, M{"n": 4})
c.Assert(err, IsNil)

// Ensure this still works. Shouldn't be affected.
err = coll.Remove(M{"n": 1})
_, err = coll.Remove(M{"n": 1})
c.Assert(err, IsNil)

var result struct{ N int }
Expand Down
18 changes: 9 additions & 9 deletions txn/flusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ NextDoc:
qdoc := bson.D{{Name: "_id", Value: t.Id}, {Name: "s", Value: tpreparing}}
udoc := bson.D{{Name: "$set", Value: bson.D{{Name: "s", Value: tprepared}, {Name: "n", Value: nonce}}}}
chaos("set-prepared")
err = f.tc.Update(qdoc, udoc)
_, err = f.tc.Update(qdoc, udoc)
if err == nil {
t.State = tprepared
t.Nonce = nonce
Expand Down Expand Up @@ -430,9 +430,9 @@ func (f *flusher) unstashToken(tt token, dkey docKey) error {
qdoc := bson.D{{Name: "_id", Value: dkey}, {Name: "txn-queue", Value: tt}}
udoc := bson.D{{Name: "$pull", Value: bson.D{{Name: "txn-queue", Value: tt}}}}
chaos("")
if err := f.sc.Update(qdoc, udoc); err == nil {
if _, err := f.sc.Update(qdoc, udoc); err == nil {
chaos("")
err = f.sc.Remove(bson.D{{Name: "_id", Value: dkey}, {Name: "txn-queue", Value: bson.D{}}})
_, err = f.sc.Remove(bson.D{{Name: "_id", Value: dkey}, {Name: "txn-queue", Value: bson.D{}}})
} else if err != mgo.ErrNotFound {
return err
}
Expand Down Expand Up @@ -682,7 +682,7 @@ func (f *flusher) abortOrReload(t *transaction, revnos []int64, pull map[bson.Ob
qdoc := bson.D{{Name: "_id", Value: t.Id}, {Name: "s", Value: tprepared}}
udoc := bson.D{{Name: "$set", Value: bson.D{{Name: "s", Value: taborting}}}}
chaos("set-aborting")
if err = f.tc.Update(qdoc, udoc); err == nil {
if _, err = f.tc.Update(qdoc, udoc); err == nil {
t.State = taborting
} else if err == mgo.ErrNotFound {
if err = f.reload(t); err != nil || t.State != taborting {
Expand Down Expand Up @@ -750,7 +750,7 @@ func (f *flusher) checkpoint(t *transaction, revnos []int64) error {
qdoc := bson.D{{Name: "_id", Value: t.Id}, {Name: "s", Value: tprepared}}
udoc := bson.D{{Name: "$set", Value: bson.D{{Name: "s", Value: tapplying}, {Name: "r", Value: revnos}}}}
chaos("set-applying")
err := f.tc.Update(qdoc, udoc)
_, err := f.tc.Update(qdoc, udoc)
if err == nil {
t.State = tapplying
t.Revnos = revnos
Expand Down Expand Up @@ -826,7 +826,7 @@ func (f *flusher) apply(t *transaction, pull map[bson.ObjectId]*transaction) err
return err
}
chaos("")
err = c.Update(qdoc, d)
_, err = c.Update(qdoc, d)
}
case op.Remove:
if revno < 0 {
Expand Down Expand Up @@ -867,7 +867,7 @@ func (f *flusher) apply(t *transaction, pull map[bson.ObjectId]*transaction) err
}
qdoc := bson.D{{Name: "_id", Value: dkey}, {Name: "n", Value: nonce}}
udoc := bson.D{{Name: "$set", Value: set}, {Name: "$unset", Value: unset}}
if err = f.sc.Update(qdoc, udoc); err == nil {
if _, err = f.sc.Update(qdoc, udoc); err == nil {
updated = true
} else if err != mgo.ErrNotFound {
return err
Expand All @@ -878,7 +878,7 @@ func (f *flusher) apply(t *transaction, pull map[bson.ObjectId]*transaction) err
} else {
f.debugf("Stash for document %v was up-to-date", dkey)
}
err = c.Remove(qdoc)
_, err = c.Remove(qdoc)
}
}
case op.Insert != nil:
Expand Down Expand Up @@ -913,7 +913,7 @@ func (f *flusher) apply(t *transaction, pull map[bson.ObjectId]*transaction) err
f.debugf("Document %v already existed", dkey)
}
chaos("")
if err = f.sc.Remove(qdoc); err == nil {
if _, err = f.sc.Remove(qdoc); err == nil {
f.debugf("Stash for document %v removed", dkey)
}
}
Expand Down

0 comments on commit 6ae1285

Please sign in to comment.