Skip to content

Commit a1316e4

Browse files
committed
Clarify logic
1 parent 43fb9c2 commit a1316e4

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

x/mongo/driver/operation.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,13 @@ type opReply struct {
103103

104104
// startedInformation keeps track of all of the information necessary for monitoring started events.
105105
type startedInformation struct {
106-
cmd bsoncore.Document
107-
requestID int32
108-
cmdName string
109-
docArray bsoncore.Array
106+
cmd bsoncore.Document
107+
requestID int32
108+
cmdName string
109+
documentSequences []struct {
110+
identifier string
111+
data []byte
112+
}
110113
processedBatches int
111114
connID string
112115
driverConnectionID int64
@@ -164,10 +167,12 @@ func redactStartedInformationCmd(info startedInformation) bson.Raw {
164167
cmdCopy = make([]byte, 0, len(info.cmd))
165168
cmdCopy = append(cmdCopy, info.cmd...)
166169

167-
if len(info.docArray) > 0 {
170+
if len(info.documentSequences) > 0 {
168171
// remove 0 byte at end
169172
cmdCopy = cmdCopy[:len(info.cmd)-1]
170-
cmdCopy = append(cmdCopy, info.docArray...)
173+
for _, seq := range info.documentSequences {
174+
cmdCopy = appendDocumentArray(cmdCopy, seq.identifier, seq.data)
175+
}
171176
// add back 0 byte and update length
172177
cmdCopy, _ = bsoncore.AppendDocumentEnd(cmdCopy, 0)
173178
}
@@ -1410,14 +1415,21 @@ func (op Operation) createWireMessage(
14101415
}
14111416
}
14121417
if err == nil && batchOffset > 0 {
1418+
// the remaining of dst is expected to be document sequences such as "ops", "nsInfo".
14131419
for b := dst[batchOffset:]; len(b) > 0; /* nothing */ {
1414-
var array bsoncore.Array
1415-
var ok bool
1416-
array, b, ok = documentSequenceToArray(b)
1420+
stype, data, ok := wiremessage.ReadMsgSectionType(b)
1421+
if !ok || stype != wiremessage.DocumentSequence {
1422+
break
1423+
}
1424+
var identifier string
1425+
identifier, data, b, ok = wiremessage.ReadMsgSectionRawDocumentSequence(data)
14171426
if !ok {
14181427
break
14191428
}
1420-
info.docArray = append(info.docArray, array...)
1429+
info.documentSequences = append(info.documentSequences, struct {
1430+
identifier string
1431+
data []byte
1432+
}{identifier, data})
14211433
}
14221434
}
14231435
}
@@ -2219,34 +2231,22 @@ func retryWritesSupported(s description.Server) bool {
22192231
return s.SessionTimeoutMinutes != nil && s.Kind != description.ServerKindStandalone
22202232
}
22212233

2222-
func documentSequenceToArray(src []byte) (bsoncore.Array, []byte, bool) {
2223-
stype, rem, ok := wiremessage.ReadMsgSectionType(src)
2224-
if !ok || stype != wiremessage.DocumentSequence {
2225-
return nil, src, false
2226-
}
2227-
var identifier string
2228-
var ret []byte
2229-
identifier, rem, ret, ok = wiremessage.ReadMsgSectionRawDocumentSequence(rem)
2230-
if !ok {
2231-
return nil, src, false
2232-
}
2233-
2234-
aidx, dst := bsoncore.AppendArrayElementStart(nil, identifier)
2234+
// appendDocumentArray will append an array header and document elements in data to dst and return the extended buffer.
2235+
func appendDocumentArray(dst []byte, key string, data []byte) []byte {
2236+
aidx, dst := bsoncore.AppendArrayElementStart(dst, key)
2237+
var doc bsoncore.Document
2238+
var ok bool
22352239
i := 0
22362240
for {
2237-
var doc bsoncore.Document
2238-
doc, rem, ok = bsoncore.ReadDocument(rem)
2241+
doc, data, ok = bsoncore.ReadDocument(data)
22392242
if !ok {
22402243
break
22412244
}
22422245
dst = bsoncore.AppendDocumentElement(dst, strconv.Itoa(i), doc)
22432246
i++
22442247
}
2245-
if len(rem) > 0 {
2246-
return nil, src, false
2247-
}
22482248

22492249
dst, _ = bsoncore.AppendArrayEnd(dst, aidx)
22502250

2251-
return dst, ret, true
2251+
return dst
22522252
}

0 commit comments

Comments
 (0)