Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contributing:findAndModify support writeConcern #185

Merged
merged 8 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2893,6 +2893,7 @@ 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 @@ -4894,18 +4895,22 @@ type findModifyCmd struct {
Collection string `bson:"findAndModify"`
Query, Update, Sort, Fields interface{} `bson:",omitempty"`
Upsert, Remove, New bool `bson:",omitempty"`
WriteConcern interface{} `bson:"writeConcern"`
}

type valueResult struct {
Value bson.Raw
LastError LastError `bson:"lastErrorObject"`
Value bson.Raw
LastError LastError `bson:"lastErrorObject"`
ConcernError writeConcernError `bson:"writeConcernError"`
}

// Apply runs the findAndModify MongoDB command, which allows updating, upserting
// or removing a document matching a query and atomically returning either the old
// version (the default) or the new version of the document (when ReturnNew is true).
// If no objects are found Apply returns ErrNotFound.
//
// If the session is in safe mode, the LastError result will be returned as err.
//
// The Sort and Select query methods affect the result of Apply. In case
// multiple documents match the query, Sort enables selecting which document to
// act upon by ordering it first. Select enables retrieving only a selection
Expand Down Expand Up @@ -4942,15 +4947,27 @@ func (q *Query) Apply(change Change, result interface{}) (info *ChangeInfo, err
dbname := op.collection[:c]
cname := op.collection[c+1:]

// https://docs.mongodb.com/manual/reference/command/findAndModify/#dbcmd.findAndModify
session.m.RLock()
safeOp := session.safeOp
session.m.RUnlock()
var writeConcern interface{}
if safeOp == nil {
writeConcern = bson.D{{Name: "w", Value: 0}}
} else {
writeConcern = safeOp.query.(*getLastError)
}

cmd := findModifyCmd{
Collection: cname,
Update: change.Update,
Upsert: change.Upsert,
Remove: change.Remove,
New: change.ReturnNew,
Query: op.query,
Sort: op.options.OrderBy,
Fields: op.selector,
Collection: cname,
Update: change.Update,
Upsert: change.Upsert,
Remove: change.Remove,
New: change.ReturnNew,
Query: op.query,
Sort: op.options.OrderBy,
Fields: op.selector,
WriteConcern: writeConcern,
}

session = session.Clone()
Expand Down Expand Up @@ -4993,6 +5010,14 @@ func (q *Query) Apply(change Change, result interface{}) (info *ChangeInfo, err
} else if change.Upsert {
info.UpsertedId = lerr.UpsertedId
}
if doc.ConcernError.Code != 0 {
var lerr LastError
e := doc.ConcernError
lerr.Code = e.Code
lerr.Err = e.ErrMsg
err = &lerr
return info, err
}
return info, nil
}

Expand Down
31 changes: 31 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,37 @@ func (s *S) TestFindAndModify(c *C) {
c.Assert(info, IsNil)
}

func (s *S) TestFindAndModifyWriteConcern(c *C) {
session, err := mgo.Dial("localhost:40011")
c.Assert(err, IsNil)
defer session.Close()

coll := session.DB("mydb").C("mycoll")
err = coll.Insert(M{"id": 42})
c.Assert(err, IsNil)

// Tweak the safety parameters to something unachievable.
session.SetSafe(&mgo.Safe{W: 4, WTimeout: 100})

var ret struct {
Id uint64 `bson:"id"`
}

change := mgo.Change{
Update: M{"$inc": M{"id": 8}},
ReturnNew: false,
}
info, err := coll.Find(M{"id": M{"$exists": true}}).Apply(change, &ret)
c.Assert(info.Updated, Equals, 1)
c.Assert(info.Matched, Equals, 1)
c.Assert(ret.Id, Equals, uint64(42))

if s.versionAtLeast(3, 2) {
// findAndModify support writeConcern after version 3.2.
c.Assert(err, ErrorMatches, "timeout|timed out waiting for slaves|Not enough data-bearing nodes|waiting for replication timed out")
}
}

func (s *S) TestFindAndModifyBug997828(c *C) {
session, err := mgo.Dial("localhost:40001")
c.Assert(err, IsNil)
Expand Down