Skip to content

Commit 6c73638

Browse files
committed
Add prototype of improved BSON support
Migration Notes: The bson.Reader type has been replaced with bson.Raw. This new type has it's own Element and Value types, called bson.RawElement and bson.RawValue respectively. The API of bson.Raw is different from bson.Reader: - the ElementAt method has been replaced with Index and IndexErr - the Lookup method now returns a bson.RawValue instead of a *bson.Element - the Iterator method and ReaderIterator type have been replaced with the Elements method and a []RawElement The bson.Raw family of types are wrappers around their bsoncore equivalents. The errors returned from this types are not wrapped, so users using these types will need to import the bsoncore package when identifying errors. There is no bsoncore.Array type nor a bson.RawArray, instead the bsoncore.Document and bson.Raw types should be used. A Values method on each will return just the values as a slice. Change-Id: Ie170697a07cf21c27839a54894b92d17a4a2f61c
1 parent 386290e commit 6c73638

File tree

119 files changed

+4542
-1208
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+4542
-1208
lines changed

benchmark/bson.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func loadSourceDocument(pathParts ...string) (*bson.Document, error) {
3636
return doc, nil
3737
}
3838

39-
func loadSourceReader(pathParts ...string) (bson.Reader, error) {
39+
func loadSourceRaw(pathParts ...string) (bson.Raw, error) {
4040
doc, err := loadSourceDocument(pathParts...)
4141
if err != nil {
4242
return nil, err
@@ -46,5 +46,5 @@ func loadSourceReader(pathParts ...string) (bson.Reader, error) {
4646
return nil, err
4747
}
4848

49-
return bson.Reader(raw), nil
49+
return bson.Raw(raw), nil
5050
}

benchmark/bson_map.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func bsonMapDecoding(ctx context.Context, tm TimerManager, iters int, dataSet string) error {
12-
r, err := loadSourceReader(getProjectRoot(), perfDataDir, bsonDataDir, dataSet)
12+
r, err := loadSourceRaw(getProjectRoot(), perfDataDir, bsonDataDir, dataSet)
1313
if err != nil {
1414
return err
1515
}
@@ -30,7 +30,7 @@ func bsonMapDecoding(ctx context.Context, tm TimerManager, iters int, dataSet st
3030
}
3131

3232
func bsonMapEncoding(ctx context.Context, tm TimerManager, iters int, dataSet string) error {
33-
r, err := loadSourceReader(getProjectRoot(), perfDataDir, bsonDataDir, dataSet)
33+
r, err := loadSourceRaw(getProjectRoot(), perfDataDir, bsonDataDir, dataSet)
3434
if err != nil {
3535
return err
3636
}

benchmark/bson_reader.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

benchmark/bson_struct.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func BSONFlatStructDecoding(ctx context.Context, tm TimerManager, iters int) error {
11-
r, err := loadSourceReader(getProjectRoot(), perfDataDir, bsonDataDir, flatBSONData)
11+
r, err := loadSourceRaw(getProjectRoot(), perfDataDir, bsonDataDir, flatBSONData)
1212
if err != nil {
1313
return err
1414
}
@@ -26,7 +26,7 @@ func BSONFlatStructDecoding(ctx context.Context, tm TimerManager, iters int) err
2626
}
2727

2828
func BSONFlatStructEncoding(ctx context.Context, tm TimerManager, iters int) error {
29-
r, err := loadSourceReader(getProjectRoot(), perfDataDir, bsonDataDir, flatBSONData)
29+
r, err := loadSourceRaw(getProjectRoot(), perfDataDir, bsonDataDir, flatBSONData)
3030
if err != nil {
3131
return err
3232
}
@@ -53,7 +53,7 @@ func BSONFlatStructEncoding(ctx context.Context, tm TimerManager, iters int) err
5353
}
5454

5555
func BSONFlatStructTagsEncoding(ctx context.Context, tm TimerManager, iters int) error {
56-
r, err := loadSourceReader(getProjectRoot(), perfDataDir, bsonDataDir, flatBSONData)
56+
r, err := loadSourceRaw(getProjectRoot(), perfDataDir, bsonDataDir, flatBSONData)
5757
if err != nil {
5858
return err
5959
}
@@ -80,7 +80,7 @@ func BSONFlatStructTagsEncoding(ctx context.Context, tm TimerManager, iters int)
8080
}
8181

8282
func BSONFlatStructTagsDecoding(ctx context.Context, tm TimerManager, iters int) error {
83-
r, err := loadSourceReader(getProjectRoot(), perfDataDir, bsonDataDir, flatBSONData)
83+
r, err := loadSourceRaw(getProjectRoot(), perfDataDir, bsonDataDir, flatBSONData)
8484
if err != nil {
8585
return err
8686
}

benchmark/bson_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ package benchmark
22

33
import "testing"
44

5-
func BenchmarkBSONFlatReaderDecoding(b *testing.B) { WrapCase(BSONFlatReaderDecoding)(b) }
6-
func BenchmarkBSONDeepReaderDecoding(b *testing.B) { WrapCase(BSONDeepReaderDecoding)(b) }
7-
85
// func BenchmarkBSONFullReaderDecoding(b *testing.B) { WrapCase(BSONFullReaderDecoding)(b) }
96

107
func BenchmarkBSONFlatDocumentEncoding(b *testing.B) { WrapCase(BSONFlatDocumentEncoding)(b) }

benchmark/harness.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,6 @@ func getAllCases() []*CaseDefinition {
107107
// Size: 57340000,
108108
// Runtime: StandardRuntime,
109109
// },
110-
{
111-
Bench: BSONFlatReaderDecoding,
112-
Count: tenThousand,
113-
Size: 75310000,
114-
Runtime: StandardRuntime,
115-
},
116-
{
117-
Bench: BSONDeepReaderDecoding,
118-
Count: tenThousand,
119-
Size: 19640000,
120-
Runtime: StandardRuntime,
121-
},
122110
// {
123111
// Bench: BSONFullReaderDecoding,
124112
// Count: tenThousand,

benchmark/multi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func MultiFindMany(ctx context.Context, tm TimerManager, iters int) error {
5252
if err != nil {
5353
return err
5454
}
55-
var r bson.Reader
55+
var r bson.Raw
5656
r, err = cursor.DecodeBytes()
5757
if err != nil {
5858
return err

bson/array.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ func (a *Array) Set(index uint, value *Value) *Array {
154154
// - *Array
155155
// - *Document
156156
// - []byte
157-
// - bson.Reader
157+
// - bson.Raw
158158
//
159-
// Note that in the case of *Document, []byte, and bson.Reader, the keys will be ignored and only
159+
// Note that in the case of *Document, []byte, and bson.Raw, the keys will be ignored and only
160160
// the values will be appended.
161161
func (a *Array) Concat(docs ...interface{}) error {
162162
for _, arr := range docs {
@@ -194,11 +194,11 @@ func (a *Array) Concat(docs ...interface{}) error {
194194
a.Append(e.value)
195195
}
196196
case []byte:
197-
if err := a.concatReader(Reader(val)); err != nil {
197+
if err := a.concatRaw(Raw(val)); err != nil {
198198
return err
199199
}
200-
case Reader:
201-
if err := a.concatReader(val); err != nil {
200+
case Raw:
201+
if err := a.concatRaw(val); err != nil {
202202
return err
203203
}
204204
default:
@@ -209,12 +209,15 @@ func (a *Array) Concat(docs ...interface{}) error {
209209
return nil
210210
}
211211

212-
func (a *Array) concatReader(r Reader) error {
213-
_, err := r.readElements(func(e *Element) error {
214-
a.Append(e.value)
215-
216-
return nil
217-
})
212+
func (a *Array) concatRaw(r Raw) error {
213+
elems, err := r.Elements()
214+
if err != nil {
215+
return err
216+
}
217+
for _, elem := range elems {
218+
idx := bytes.IndexByte(elem[1:], 0x00) // elements will only contain valid elements
219+
a.Append(&Value{start: 0, offset: uint32(idx + 2), data: elem})
220+
}
218221

219222
return err
220223
}

bson/bson.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// Licensed under the Apache License, Version 2.0 (the "License"); you may
44
// not use this file except in compliance with the License. You may obtain
55
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Based on gopkg.in/mgo.v2/bson by Gustavo Niemeyer
8+
// See THIRD-PARTY-NOTICES for original license terms.
69

710
package bson
811

@@ -23,3 +26,52 @@ type node [2]uint32
2326
type Zeroer interface {
2427
IsZero() bool
2528
}
29+
30+
// D represents a BSON Document. This type can be used to represent BSON in a concise and readable
31+
// manner. It should generally be used when serializing to BSON. For deserializing, the Raw or
32+
// Document types should be used.
33+
//
34+
// Example usage:
35+
//
36+
// bson.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}}
37+
//
38+
// This type should be used in situations where order matters, such as MongoDB commands. If the
39+
// order is not important, a map is more comfortable and concise.
40+
type D []E
41+
42+
// Map creates a map from the elements of the D.
43+
func (d D) Map() M {
44+
m := make(M, len(d))
45+
for _, e := range d {
46+
m[e.Key] = e.Value
47+
}
48+
return m
49+
}
50+
51+
// E represents a BSON element for a D. It is usually used inside a D.
52+
type E struct {
53+
Key string
54+
Value interface{}
55+
}
56+
57+
// M is an unordered, concise representation of a BSON Document. It should generally be used to
58+
// serialize BSON when the order of the elements of a BSON document do not matter. If the element
59+
// order matters, use a D instead.
60+
//
61+
// Example usage:
62+
//
63+
// bson.M{"foo": "bar", "hello": "world", "pi": 3.14159}
64+
//
65+
// This type is handled in the encoders as a regular map[string]interface{}. The elements will be
66+
// serialized in an undefined, random order, and the order will be different each time.
67+
type M map[string]interface{}
68+
69+
// An A represents a BSON array. This type can be used to represent a BSON array in a concise and
70+
// readable manner. It should generally be used when serializing to BSON. For deserializing, the
71+
// RawArray or Array types should be used.
72+
//
73+
// Example usage:
74+
//
75+
// bson.A{"bar", "world", 3.14159, bson.D{{"qux", 12345}}}
76+
//
77+
type A []interface{}

bson/bson_corpus_spec_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func normalizeRelaxedDouble(t *testing.T, key string, rEJ string) string {
163163
// bsonToNative decodes the BSON bytes (b) into a native Document
164164
func bsonToNative(t *testing.T, b []byte, bType, testDesc string) *Document {
165165
doc := NewDocument()
166-
err := pc.DocumentDecodeValue(dc, bsonrw.NewBSONValueReader(b), &doc)
166+
err := pc.DocumentDecodeValue(dc, bsonrw.NewBSONDocumentReader(b), &doc)
167167
expectNoError(t, err, fmt.Sprintf("%s: decoding %s BSON", testDesc, bType))
168168
return doc
169169
}
@@ -299,7 +299,7 @@ func runTest(t *testing.T, file string) {
299299
expectNoError(t, err, d.Description)
300300

301301
doc := NewDocument()
302-
err = pc.DocumentDecodeValue(dc, bsonrw.NewBSONValueReader(b), &doc)
302+
err = pc.DocumentDecodeValue(dc, bsonrw.NewBSONDocumentReader(b), &doc)
303303
expectError(t, err, fmt.Sprintf("%s: expected decode error", d.Description))
304304
}
305305

0 commit comments

Comments
 (0)