Skip to content

Commit 975716b

Browse files
author
Divjot Arora
committed
Revert "Add All method to mongo.Cursor"
This reverts commit 506e97a. Change-Id: I5f4e6afe68e1e9876813690b5476de2684b72699
1 parent f418f6a commit 975716b

File tree

4 files changed

+5
-125
lines changed

4 files changed

+5
-125
lines changed

mongo/cursor.go

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"context"
1111
"errors"
1212
"io"
13-
"reflect"
1413

1514
"go.mongodb.org/mongo-driver/bson"
1615
"go.mongodb.org/mongo-driver/bson/bsoncodec"
@@ -129,68 +128,3 @@ func (c *Cursor) Err() error { return c.err }
129128

130129
// Close closes this cursor.
131130
func (c *Cursor) Close(ctx context.Context) error { return c.bc.Close(ctx) }
132-
133-
// All iterates the cursor and decodes each document into results.
134-
// The results parameter must be a pointer to a slice. The slice pointed to by results will be completely overwritten.
135-
// If the cursor has been iterated, any previously iterated documents will not be included in results.
136-
func (c *Cursor) All(ctx context.Context, results interface{}) error {
137-
resultsVal := reflect.ValueOf(results)
138-
if resultsVal.Kind() != reflect.Ptr {
139-
return errors.New("results argument must be a pointer to a slice")
140-
}
141-
142-
sliceVal := resultsVal.Elem()
143-
elementType := sliceVal.Type().Elem()
144-
var index int
145-
var err error
146-
147-
batch := c.batch // exhaust the current batch before iterating the batch cursor
148-
for {
149-
sliceVal, index, err = c.addFromBatch(sliceVal, elementType, batch, index)
150-
if err != nil {
151-
return err
152-
}
153-
154-
if !c.bc.Next(ctx) {
155-
break
156-
}
157-
158-
batch = c.bc.Batch()
159-
}
160-
161-
if err = c.bc.Err(); err != nil {
162-
return err
163-
}
164-
165-
resultsVal.Elem().Set(sliceVal.Slice(0, index))
166-
return nil
167-
}
168-
169-
// addFromBatch adds all documents from batch to sliceVal starting at the given index. It returns the new slice value,
170-
// the next empty index in the slice, and an error if one occurs.
171-
func (c *Cursor) addFromBatch(sliceVal reflect.Value, elemType reflect.Type, batch *bsoncore.DocumentSequence,
172-
index int) (reflect.Value, int, error) {
173-
174-
docs, err := batch.Documents()
175-
if err != nil {
176-
return sliceVal, index, err
177-
}
178-
179-
for _, doc := range docs {
180-
if sliceVal.Len() == index {
181-
// slice is full
182-
newElem := reflect.New(elemType)
183-
sliceVal = reflect.Append(sliceVal, newElem.Elem())
184-
sliceVal = sliceVal.Slice(0, sliceVal.Cap())
185-
}
186-
187-
currElem := sliceVal.Index(index).Addr().Interface()
188-
if err = bson.UnmarshalWithRegistry(c.registry, doc, currElem); err != nil {
189-
return sliceVal, index, err
190-
}
191-
192-
index++
193-
}
194-
195-
return sliceVal, index, nil
196-
}

mongo/cursor_test.go

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/stretchr/testify/require"
8-
"go.mongodb.org/mongo-driver/bson"
97
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
108
"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
119
)
@@ -82,56 +80,4 @@ func TestCursor(t *testing.T) {
8280
t.Run("returns false on context cancellation", func(t *testing.T) {})
8381
t.Run("returns false if error occurred", func(t *testing.T) {})
8482
t.Run("returns false if ID is zero and no more docs", func(t *testing.T) {})
85-
86-
t.Run("TestAll", func(t *testing.T) {
87-
t.Run("errors if argument is not pointer to slice", func(t *testing.T) {
88-
cursor, err := newCursor(newTestBatchCursor(1, 5), nil)
89-
require.Nil(t, err)
90-
err = cursor.All(context.Background(), []bson.D{})
91-
require.NotNil(t, err)
92-
})
93-
94-
t.Run("fills slice with all documents", func(t *testing.T) {
95-
cursor, err := newCursor(newTestBatchCursor(1, 5), nil)
96-
require.Nil(t, err)
97-
98-
var docs []bson.D
99-
err = cursor.All(context.Background(), &docs)
100-
require.Nil(t, err)
101-
require.Equal(t, 5, len(docs))
102-
103-
for index, doc := range docs {
104-
require.Equal(t, doc, bson.D{{"foo", int32(index)}})
105-
}
106-
})
107-
108-
t.Run("decodes each document into slice type", func(t *testing.T) {
109-
cursor, err := newCursor(newTestBatchCursor(1, 5), nil)
110-
require.Nil(t, err)
111-
112-
type Document struct {
113-
Foo int32 `bson:"foo"`
114-
}
115-
var docs []Document
116-
err = cursor.All(context.Background(), &docs)
117-
require.Nil(t, err)
118-
require.Equal(t, 5, len(docs))
119-
120-
for index, doc := range docs {
121-
require.Equal(t, doc, Document{Foo: int32(index)})
122-
}
123-
})
124-
125-
t.Run("multiple batches are included", func(t *testing.T) {
126-
cursor, err := newCursor(newTestBatchCursor(2, 5), nil)
127-
var docs []bson.D
128-
err = cursor.All(context.Background(), &docs)
129-
require.Nil(t, err)
130-
require.Equal(t, 10, len(docs))
131-
132-
for index, doc := range docs {
133-
require.Equal(t, doc, bson.D{{"foo", int32(index)}})
134-
}
135-
})
136-
})
13783
}

x/bsonx/bsoncore/document_sequence.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ func (ds *DocumentSequence) ResetIterator() {
8383
ds.Pos = 0
8484
}
8585

86-
// Documents returns a slice of the documents. If nil either the Data field is also nil or could not
86+
// documents returns a slice of the documents. If nil either the Data field is also nil or could not
8787
// be properly read.
88-
func (ds *DocumentSequence) Documents() ([]Document, error) {
88+
func (ds *DocumentSequence) documents() ([]Document, error) {
8989
if ds == nil {
9090
return nil, nil
9191
}

x/bsonx/bsoncore/document_sequence_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func TestDocumentSequence(t *testing.T) {
103103
Style: tc.style,
104104
Data: tc.data,
105105
}
106-
documents, err := ds.Documents()
106+
documents, err := ds.documents()
107107
if !cmp.Equal(documents, tc.documents) {
108108
t.Errorf("Documents do not match. got %v; want %v", documents, tc.documents)
109109
}
@@ -252,7 +252,7 @@ func TestDocumentSequence(t *testing.T) {
252252
Style: tc.style,
253253
Data: tc.data,
254254
}
255-
docs, err := ds.Documents()
255+
docs, err := ds.documents()
256256
if err != nil {
257257
t.Fatalf("Unexpected error: %v", err)
258258
}
@@ -376,7 +376,7 @@ func TestDocumentSequence(t *testing.T) {
376376
t.Run("Documents", func(t *testing.T) {
377377
defer capturePanic()
378378
var ds *DocumentSequence
379-
_, _ = ds.Documents()
379+
_, _ = ds.documents()
380380
})
381381
t.Run("Next", func(t *testing.T) {
382382
defer capturePanic()

0 commit comments

Comments
 (0)