forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_collections.go
133 lines (111 loc) · 3.67 KB
/
list_collections.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package driver
import (
"context"
"errors"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
"go.mongodb.org/mongo-driver/x/mongo/driver/session"
"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
"go.mongodb.org/mongo-driver/x/mongo/driver/uuid"
"go.mongodb.org/mongo-driver/x/network/command"
"go.mongodb.org/mongo-driver/x/network/connection"
"go.mongodb.org/mongo-driver/x/network/description"
)
// ErrFilterType is thrown when a non-string filter is specified.
var ErrFilterType = errors.New("filter must be a string")
// ListCollections handles the full cycle dispatch and execution of a listCollections command against the provided
// topology.
func ListCollections(
ctx context.Context,
cmd command.ListCollections,
topo *topology.Topology,
selector description.ServerSelector,
clientID uuid.UUID,
pool *session.Pool,
opts ...*options.ListCollectionsOptions,
) (*ListCollectionsBatchCursor, error) {
ss, err := topo.SelectServer(ctx, selector)
if err != nil {
return nil, err
}
conn, err := ss.Connection(ctx)
if err != nil {
return nil, err
}
defer conn.Close()
if ss.Description().WireVersion.Max < 3 {
return legacyListCollections(ctx, cmd, ss, conn)
}
rp, err := getReadPrefBasedOnTransaction(cmd.ReadPref, cmd.Session)
if err != nil {
return nil, err
}
cmd.ReadPref = rp
// If no explicit session and deployment supports sessions, start implicit session.
if cmd.Session == nil && topo.SupportsSessions() {
cmd.Session, err = session.NewClientSession(pool, clientID, session.Implicit)
if err != nil {
return nil, err
}
}
lc := options.MergeListCollectionsOptions(opts...)
if lc.NameOnly != nil {
cmd.Opts = append(cmd.Opts, bsonx.Elem{"nameOnly", bsonx.Boolean(*lc.NameOnly)})
}
res, err := cmd.RoundTrip(ctx, ss.Description(), conn)
if err != nil {
closeImplicitSession(cmd.Session)
return nil, err
}
batchCursor, err := NewBatchCursor(bsoncore.Document(res), cmd.Session, cmd.Clock, ss.Server, cmd.CursorOpts...)
if err != nil {
closeImplicitSession(cmd.Session)
return nil, err
}
return NewListCollectionsBatchCursor(batchCursor)
}
func legacyListCollections(
ctx context.Context,
cmd command.ListCollections,
ss *topology.SelectedServer,
conn connection.Connection,
) (*ListCollectionsBatchCursor, error) {
filter, err := transformFilter(cmd.Filter, cmd.DB)
if err != nil {
return nil, err
}
findCmd := command.Find{
NS: command.NewNamespace(cmd.DB, "system.namespaces"),
ReadPref: cmd.ReadPref,
Filter: filter,
}
// don't need registry because it's used to create BSON docs for find options that don't exist in this case
batchCursor, err := legacyFind(ctx, findCmd, nil, ss, conn)
if err != nil {
return nil, err
}
return NewLegacyListCollectionsBatchCursor(batchCursor)
}
// modify the user-supplied filter to prefix the "name" field with the database name.
// returns the original filter if the name field is not present or a copy with the modified name field if it is
func transformFilter(filter bsonx.Doc, dbName string) (bsonx.Doc, error) {
if filter == nil {
return filter, nil
}
if nameVal, err := filter.LookupErr("name"); err == nil {
name, ok := nameVal.StringValueOK()
if !ok {
return nil, ErrFilterType
}
filterCopy := filter.Copy()
filterCopy.Set("name", bsonx.String(dbName+"."+name))
return filterCopy, nil
}
return filter, nil
}