Skip to content

Commit f462302

Browse files
author
iwysiu
committed
GODRIVER-1150 generate Count operation
Change-Id: Idbfeaa9aa27ceb03511b765cd2977ab78765b7b1
1 parent 898070f commit f462302

File tree

5 files changed

+266
-30
lines changed

5 files changed

+266
-30
lines changed

mongo/collection.go

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"go.mongodb.org/mongo-driver/mongo/readconcern"
2020
"go.mongodb.org/mongo-driver/mongo/readpref"
2121
"go.mongodb.org/mongo-driver/mongo/writeconcern"
22-
"go.mongodb.org/mongo-driver/x/bsonx"
2322
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
2423
"go.mongodb.org/mongo-driver/x/mongo/driver"
2524
"go.mongodb.org/mongo-driver/x/mongo/driver/description"
@@ -787,42 +786,39 @@ func (coll *Collection) EstimatedDocumentCount(ctx context.Context,
787786

788787
sess := sessionFromContext(ctx)
789788

789+
if sess == nil && coll.client.topology.SessionPool != nil {
790+
sess, err := session.NewClientSession(coll.client.topology.SessionPool, coll.client.id, session.Implicit)
791+
if err != nil {
792+
return 0, err
793+
}
794+
defer sess.EndSession()
795+
}
796+
790797
err := coll.client.validSession(sess)
791798
if err != nil {
792799
return 0, err
793800
}
794801

795802
rc := coll.readConcern
796-
if sess != nil && (sess.TransactionInProgress()) {
803+
if sess.TransactionRunning() {
797804
rc = nil
798805
}
799806

800-
oldns := coll.namespace()
801-
cmd := command.Count{
802-
NS: command.Namespace{DB: oldns.DB, Collection: oldns.Collection},
803-
Query: bsonx.Doc{},
804-
ReadPref: coll.readPreference,
805-
ReadConcern: rc,
806-
Session: sess,
807-
Clock: coll.client.clock,
808-
}
807+
selector := makePinnedSelector(sess, coll.readSelector)
809808

810-
countOpts := options.Count()
811-
if len(opts) >= 1 {
812-
countOpts = countOpts.SetMaxTime(*opts[len(opts)-1].MaxTime)
809+
op := operation.NewCount().Session(sess).ClusterClock(coll.client.clock).
810+
Database(coll.db.name).Collection(coll.name).CommandMonitor(coll.client.monitor).
811+
Deployment(coll.client.topology).ReadConcern(rc).ReadPreference(coll.readPreference).
812+
ServerSelector(selector)
813+
814+
co := options.MergeEstimatedDocumentCountOptions(opts...)
815+
if co.MaxTime != nil {
816+
op = op.MaxTimeMS(int64(*co.MaxTime / time.Millisecond))
813817
}
814818

815-
count, err := driverlegacy.Count(
816-
ctx, cmd,
817-
coll.client.topology,
818-
coll.readSelector,
819-
coll.client.id,
820-
coll.client.topology.SessionPool,
821-
coll.registry,
822-
countOpts,
823-
)
819+
err = op.Execute(ctx)
824820

825-
return count, replaceErrors(err)
821+
return op.Result().N, replaceErrors(err)
826822
}
827823

828824
// Distinct finds the distinct values for a specified field across a single

mongo/session.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,7 @@ func (s *sessionImpl) AbortTransaction(ctx context.Context) error {
182182
return s.clientSession.AbortTransaction()
183183
}
184184

185-
var selector description.ServerSelectorFunc = func(t description.Topology, svrs []description.Server) ([]description.Server, error) {
186-
if s.clientSession.PinnedServer != nil {
187-
return s.clientSession.PinnedServer.SelectServer(t, svrs)
188-
}
189-
return description.WriteSelector().SelectServer(t, svrs)
190-
}
185+
selector := makePinnedSelector(s.clientSession, description.WriteSelector())
191186

192187
s.clientSession.Aborting = true
193188
err = operation.NewAbortTransaction().Session(s.clientSession).ClusterClock(s.client.clock).Database("admin").

x/mongo/driver/operation/count.go

Lines changed: 219 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x/mongo/driver/operation/count.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
version = 0
2+
name = "Count"
3+
documentation = "Performs a count operation"
4+
5+
[properties]
6+
enabled = ["read concern", "read preference"]
7+
8+
[command]
9+
name = "count"
10+
parameter = "collection"
11+
12+
[request.maxTimeMS]
13+
type = "int64"
14+
documentation = "MaxTimeMS specifies the maximum amount of time to allow the query to run."
15+
16+
[request.query]
17+
type = "document"
18+
documentation = "Query determines what results are returned from find."
19+
20+
[response]
21+
name = "CountResult"
22+
23+
[response.field.n]
24+
type = "int64"
25+
documentation = "The number of documents found"

x/mongo/driver/operation/operation.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ package operation
1111
//go:generate operationgen drop_database.toml operation drop_database.go
1212
//go:generate operationgen commit_transaction.toml operation commit_transaction.go
1313
//go:generate operationgen abort_transaction.toml operation abort_transaction.go
14+
//go:generate operationgen count.toml operation count.go

0 commit comments

Comments
 (0)