Skip to content

Commit 7c85a0d

Browse files
committed
Merge v2-unstable into v2.
2 parents c6a7dce + e1e5be4 commit 7c85a0d

27 files changed

+942
-119
lines changed

auth_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ func (s *S) TestAuthLoginCachingWithNewSession(c *C) {
576576

577577
coll := session.DB("mydb").C("mycoll")
578578
err = coll.Insert(M{"n": 1})
579-
c.Assert(err, ErrorMatches, "unauthorized|need to login|not authorized for .*")
579+
c.Assert(err, ErrorMatches, "unauthorized|need to login|not authorized .*")
580580
}
581581

582582
func (s *S) TestAuthLoginCachingAcrossPool(c *C) {

bson/bson_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,25 @@ func (i *getterSetterInt) SetBSON(raw bson.Raw) error {
10561056
return err
10571057
}
10581058

1059+
type ifaceType interface {
1060+
Hello()
1061+
}
1062+
1063+
type ifaceSlice []ifaceType
1064+
1065+
func (s *ifaceSlice) SetBSON(raw bson.Raw) error {
1066+
var ns []int
1067+
if err := raw.Unmarshal(&ns); err != nil {
1068+
return err
1069+
}
1070+
*s = make(ifaceSlice, ns[0])
1071+
return nil
1072+
}
1073+
1074+
func (s ifaceSlice) GetBSON() (interface{}, error) {
1075+
return []int{len(s)}, nil
1076+
}
1077+
10591078
type (
10601079
MyString string
10611080
MyBytes []byte
@@ -1249,6 +1268,7 @@ var twoWayCrossItems = []crossTypeItem{
12491268

12501269
// arrays
12511270
{&struct{ V [2]int }{[...]int{1, 2}}, map[string][2]int{"v": [2]int{1, 2}}},
1271+
{&struct{ V [2]byte }{[...]byte{1, 2}}, map[string][2]byte{"v": [2]byte{1, 2}}},
12521272

12531273
// zero time
12541274
{&struct{ V time.Time }{}, map[string]interface{}{"v": time.Time{}}},
@@ -1281,6 +1301,9 @@ var twoWayCrossItems = []crossTypeItem{
12811301
// bson.D <=> non-struct getter/setter
12821302
{&bson.D{{"a", 1}}, &getterSetterD{{"a", 1}, {"suffix", true}}},
12831303
{&bson.D{{"a", 42}}, &gsintvar},
1304+
1305+
// Interface slice setter.
1306+
{&struct{ V ifaceSlice }{ifaceSlice{nil, nil, nil}}, bson.M{"v": []interface{}{3}}},
12841307
}
12851308

12861309
// Same thing, but only one way (obj1 => obj2).

bson/decode.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// BSON library for Go
2-
//
2+
//
33
// Copyright (c) 2010-2012 - Gustavo Niemeyer <[email protected]>
4-
//
4+
//
55
// All rights reserved.
66
//
77
// Redistribution and use in source and binary forms, with or without
8-
// modification, are permitted provided that the following conditions are met:
9-
//
8+
// modification, are permitted provided that the following conditions are met:
9+
//
1010
// 1. Redistributions of source code must retain the above copyright notice, this
11-
// list of conditions and the following disclaimer.
11+
// list of conditions and the following disclaimer.
1212
// 2. Redistributions in binary form must reproduce the above copyright notice,
1313
// this list of conditions and the following disclaimer in the documentation
14-
// and/or other materials provided with the distribution.
15-
//
14+
// and/or other materials provided with the distribution.
15+
//
1616
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1717
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1818
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -474,6 +474,11 @@ func (d *decoder) readElemTo(out reflect.Value, kind byte) (good bool) {
474474
panic("Can't happen. Handled above.")
475475
case 0x04: // Array
476476
outt := out.Type()
477+
if setterStyle(outt) != setterNone {
478+
// Skip the value so its data is handed to the setter below.
479+
d.dropElem(kind)
480+
break
481+
}
477482
for outt.Kind() == reflect.Ptr {
478483
outt = outt.Elem()
479484
}

bson/encode.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// BSON library for Go
2-
//
2+
//
33
// Copyright (c) 2010-2012 - Gustavo Niemeyer <[email protected]>
4-
//
4+
//
55
// All rights reserved.
66
//
77
// Redistribution and use in source and binary forms, with or without
8-
// modification, are permitted provided that the following conditions are met:
9-
//
8+
// modification, are permitted provided that the following conditions are met:
9+
//
1010
// 1. Redistributions of source code must retain the above copyright notice, this
11-
// list of conditions and the following disclaimer.
11+
// list of conditions and the following disclaimer.
1212
// 2. Redistributions in binary form must reproduce the above copyright notice,
1313
// this list of conditions and the following disclaimer in the documentation
14-
// and/or other materials provided with the distribution.
15-
//
14+
// and/or other materials provided with the distribution.
15+
//
1616
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1717
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1818
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -212,7 +212,7 @@ func (e *encoder) addSlice(v reflect.Value) {
212212
return
213213
}
214214
l := v.Len()
215-
et := v.Type().Elem()
215+
et := v.Type().Elem()
216216
if et == typeDocElem {
217217
for i := 0; i < l; i++ {
218218
elem := v.Index(i).Interface().(DocElem)
@@ -365,7 +365,17 @@ func (e *encoder) addElem(name string, v reflect.Value, minSize bool) {
365365
et := v.Type().Elem()
366366
if et.Kind() == reflect.Uint8 {
367367
e.addElemName('\x05', name)
368-
e.addBinary('\x00', v.Slice(0, v.Len()).Interface().([]byte))
368+
if v.CanAddr() {
369+
e.addBinary('\x00', v.Slice(0, v.Len()).Interface().([]byte))
370+
} else {
371+
n := v.Len()
372+
e.addInt32(int32(n))
373+
e.addBytes('\x00')
374+
for i := 0; i < n; i++ {
375+
el := v.Index(i)
376+
e.addBytes(byte(el.Uint()))
377+
}
378+
}
369379
} else {
370380
e.addElemName('\x04', name)
371381
e.addDoc(v)
@@ -415,7 +425,7 @@ func (e *encoder) addElem(name string, v reflect.Value, minSize bool) {
415425
case time.Time:
416426
// MongoDB handles timestamps as milliseconds.
417427
e.addElemName('\x09', name)
418-
e.addInt64(s.Unix() * 1000 + int64(s.Nanosecond() / 1e6))
428+
e.addInt64(s.Unix()*1000 + int64(s.Nanosecond()/1e6))
419429

420430
case url.URL:
421431
e.addElemName('\x02', name)

bulk_test.go

+39-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (s *S) TestBulkInsertError(c *C) {
5858

5959
coll := session.DB("mydb").C("mycoll")
6060
bulk := coll.Bulk()
61-
bulk.Insert(M{"_id": 1}, M{"_id": 2}, M{"_id": 2}, M{"n": 3})
61+
bulk.Insert(M{"_id": 1}, M{"_id": 2}, M{"_id": 2}, M{"_id": 3})
6262
_, err = bulk.Run()
6363
c.Assert(err, ErrorMatches, ".*duplicate key.*")
6464

@@ -91,3 +91,41 @@ func (s *S) TestBulkInsertErrorUnordered(c *C) {
9191
c.Assert(err, IsNil)
9292
c.Assert(res, DeepEquals, []doc{{1}, {2}, {3}})
9393
}
94+
95+
func (s *S) TestBulkInsertErrorUnorderedSplitBatch(c *C) {
96+
// The server has a batch limit of 1000 documents when using write commands.
97+
// This artificial limit did not exist with the old wire protocol, so to
98+
// avoid compatibility issues the implementation internally split batches
99+
// into the proper size and delivers them one by one. This test ensures that
100+
// the behavior of unordered (that is, continue on error) remains correct
101+
// when errors happen and there are batches left.
102+
session, err := mgo.Dial("localhost:40001")
103+
c.Assert(err, IsNil)
104+
defer session.Close()
105+
106+
coll := session.DB("mydb").C("mycoll")
107+
bulk := coll.Bulk()
108+
bulk.Unordered()
109+
110+
const total = 4096
111+
type doc struct {
112+
Id int `_id`
113+
}
114+
docs := make([]interface{}, total)
115+
for i := 0; i < total; i++ {
116+
docs[i] = doc{i}
117+
}
118+
docs[1] = doc{0}
119+
bulk.Insert(docs...)
120+
_, err = bulk.Run()
121+
c.Assert(err, ErrorMatches, ".*duplicate key.*")
122+
123+
n, err := coll.Count()
124+
c.Assert(err, IsNil)
125+
c.Assert(n, Equals, total-1)
126+
127+
var res doc
128+
err = coll.FindId(1500).One(&res)
129+
c.Assert(err, IsNil)
130+
c.Assert(res.Id, Equals, 1500)
131+
}

cluster.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,18 @@ func (cluster *mongoCluster) syncServer(server *mongoServer) (info *mongoServerI
209209

210210
if result.IsMaster {
211211
debugf("SYNC %s is a master.", addr)
212-
// Made an incorrect assumption above, so fix stats.
213-
stats.conn(-1, false)
214-
stats.conn(+1, true)
212+
if !server.info.Master {
213+
// Made an incorrect assumption above, so fix stats.
214+
stats.conn(-1, false)
215+
stats.conn(+1, true)
216+
}
215217
} else if result.Secondary {
216218
debugf("SYNC %s is a slave.", addr)
217219
} else if cluster.direct {
218220
logf("SYNC %s in unknown state. Pretending it's a slave due to direct connection.", addr)
219221
} else {
220222
logf("SYNC %s is neither a master nor a slave.", addr)
221-
// Made an incorrect assumption above, so fix stats.
222-
stats.conn(-1, false)
223+
// Let stats track it as whatever was known before.
223224
return nil, nil, errors.New(addr + " is not a master nor slave")
224225
}
225226

cluster_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -1185,8 +1185,6 @@ func (s *S) TestRemovalOfClusterMember(c *C) {
11851185
c.Logf("========== Removing slave: %s ==========", slaveAddr)
11861186

11871187
master.Run(bson.D{{"$eval", `rs.remove("` + slaveAddr + `")`}}, nil)
1188-
err = master.Ping()
1189-
c.Assert(err, Equals, io.EOF)
11901188

11911189
master.Refresh()
11921190

sasl/sasl.c internal/sasl/sasl.c

File renamed without changes.

sasl/sasl.go internal/sasl/sasl.go

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

saslimpl.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package mgo
44

55
import (
6-
"gopkg.in/mgo.v2/sasl"
6+
"gopkg.in/mgo.v2/internal/sasl"
77
)
88

99
func saslNew(cred Credential, host string) (saslStepper, error) {

0 commit comments

Comments
 (0)