Skip to content

Commit 30b14b6

Browse files
committed
GODRIVER-3898 Move the FindAndModify logic into the "mongo" package
1 parent 7bcfafd commit 30b14b6

4 files changed

Lines changed: 288 additions & 591 deletions

File tree

mongo/collection.go

Lines changed: 70 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,7 +1737,7 @@ func (coll *Collection) FindOne(ctx context.Context, filter any,
17371737
}
17381738
}
17391739

1740-
func (coll *Collection) findAndModify(ctx context.Context, op *operation.FindAndModify) *SingleResult {
1740+
func (coll *Collection) findAndModify(ctx context.Context, op *findAndModifyOp) *SingleResult {
17411741
if ctx == nil {
17421742
ctx = context.Background()
17431743
}
@@ -1771,27 +1771,27 @@ func (coll *Collection) findAndModify(ctx context.Context, op *operation.FindAnd
17711771

17721772
maxAdaptiveRetries := coll.client.effectiveAdaptiveRetries(coll.client.retryWrites)
17731773

1774-
op = op.Session(sess).
1775-
WriteConcern(wc).
1776-
CommandMonitor(coll.client.monitor).
1777-
ServerSelector(selector).
1778-
ClusterClock(coll.client.clock).
1779-
Database(coll.db.name).
1780-
Collection(coll.name).
1781-
Deployment(coll.client.deployment).
1782-
Retry(retry).
1783-
MaxAdaptiveRetries(maxAdaptiveRetries).
1784-
EnableOverloadRetargeting(coll.client.enableOverloadRetargeting).
1785-
Crypt(coll.client.cryptFLE)
1786-
1787-
rr, err := processWriteError(op.Execute(ctx))
1774+
op.session = sess
1775+
op.writeConcern = wc
1776+
op.monitor = coll.client.monitor
1777+
op.selector = selector
1778+
op.clock = coll.client.clock
1779+
op.database = coll.db.name
1780+
op.collection = coll.name
1781+
op.deployment = coll.client.deployment
1782+
op.retry = &retry
1783+
op.maxAdaptiveRetries = maxAdaptiveRetries
1784+
op.enableOverloadRetargeting = coll.client.enableOverloadRetargeting
1785+
op.crypt = coll.client.cryptFLE
1786+
1787+
rr, err := processWriteError(op.execute(ctx))
17881788
if err != nil {
17891789
return &SingleResult{err: err}
17901790
}
17911791

17921792
return &SingleResult{
17931793
ctx: ctx,
1794-
rdr: bson.Raw(op.Result().Value),
1794+
rdr: bson.Raw(op.result().Value),
17951795
bsonOpts: coll.bsonOpts,
17961796
reg: coll.registry,
17971797
Acknowledged: rr.isAcknowledged(),
@@ -1824,23 +1824,30 @@ func (coll *Collection) FindOneAndDelete(
18241824
return &SingleResult{err: fmt.Errorf("failed to construct options from builder: %w", err)}
18251825
}
18261826

1827-
op := operation.NewFindAndModify(f).Remove(true).ServerAPI(coll.client.serverAPI).Timeout(coll.client.timeout).Authenticator(coll.client.authenticator)
1827+
remove := true
1828+
op := &findAndModifyOp{
1829+
query: f,
1830+
remove: &remove,
1831+
serverAPI: coll.client.serverAPI,
1832+
timeout: coll.client.timeout,
1833+
authenticator: coll.client.authenticator,
1834+
}
18281835
if args.Collation != nil {
1829-
op = op.Collation(bsoncore.Document(toDocument(args.Collation)))
1836+
op.collation = bsoncore.Document(toDocument(args.Collation))
18301837
}
18311838
if args.Comment != nil {
18321839
comment, err := marshalValue(args.Comment, coll.bsonOpts, coll.registry)
18331840
if err != nil {
18341841
return &SingleResult{err: err}
18351842
}
1836-
op = op.Comment(comment)
1843+
op.comment = comment
18371844
}
18381845
if args.Projection != nil {
18391846
proj, err := marshal(args.Projection, coll.bsonOpts, coll.registry)
18401847
if err != nil {
18411848
return &SingleResult{err: err}
18421849
}
1843-
op = op.Fields(proj)
1850+
op.fields = proj
18441851
}
18451852
if args.Sort != nil {
18461853
if isUnorderedMap(args.Sort) {
@@ -1850,7 +1857,7 @@ func (coll *Collection) FindOneAndDelete(
18501857
if err != nil {
18511858
return &SingleResult{err: err}
18521859
}
1853-
op = op.Sort(sort)
1860+
op.sort = sort
18541861
}
18551862
if args.Hint != nil {
18561863
if isUnorderedMap(args.Hint) {
@@ -1860,17 +1867,17 @@ func (coll *Collection) FindOneAndDelete(
18601867
if err != nil {
18611868
return &SingleResult{err: err}
18621869
}
1863-
op = op.Hint(hint)
1870+
op.hint = hint
18641871
}
18651872
if args.Let != nil {
18661873
let, err := marshal(args.Let, coll.bsonOpts, coll.registry)
18671874
if err != nil {
18681875
return &SingleResult{err: err}
18691876
}
1870-
op = op.Let(let)
1877+
op.let = let
18711878
}
18721879
if rawData, ok := optionsutil.Value(args.Internal, "rawData").(bool); ok {
1873-
op = op.RawData(rawData)
1880+
op.rawData = &rawData
18741881
}
18751882

18761883
return coll.findAndModify(ctx, op)
@@ -1913,30 +1920,36 @@ func (coll *Collection) FindOneAndReplace(
19131920
return &SingleResult{err: fmt.Errorf("failed to construct options from builder: %w", err)}
19141921
}
19151922

1916-
op := operation.NewFindAndModify(f).Update(bsoncore.Value{Type: bsoncore.TypeEmbeddedDocument, Data: r}).
1917-
ServerAPI(coll.client.serverAPI).Timeout(coll.client.timeout).Authenticator(coll.client.authenticator)
1923+
op := &findAndModifyOp{
1924+
query: f,
1925+
update: bsoncore.Value{Type: bsoncore.TypeEmbeddedDocument, Data: r},
1926+
serverAPI: coll.client.serverAPI,
1927+
timeout: coll.client.timeout,
1928+
authenticator: coll.client.authenticator,
1929+
}
19181930
if args.BypassDocumentValidation != nil && *args.BypassDocumentValidation {
1919-
op = op.BypassDocumentValidation(*args.BypassDocumentValidation)
1931+
op.bypassDocumentValidation = args.BypassDocumentValidation
19201932
}
19211933
if args.Collation != nil {
1922-
op = op.Collation(bsoncore.Document(toDocument(args.Collation)))
1934+
op.collation = bsoncore.Document(toDocument(args.Collation))
19231935
}
19241936
if args.Comment != nil {
19251937
comment, err := marshalValue(args.Comment, coll.bsonOpts, coll.registry)
19261938
if err != nil {
19271939
return &SingleResult{err: err}
19281940
}
1929-
op = op.Comment(comment)
1941+
op.comment = comment
19301942
}
19311943
if args.Projection != nil {
19321944
proj, err := marshal(args.Projection, coll.bsonOpts, coll.registry)
19331945
if err != nil {
19341946
return &SingleResult{err: err}
19351947
}
1936-
op = op.Fields(proj)
1948+
op.fields = proj
19371949
}
19381950
if args.ReturnDocument != nil {
1939-
op = op.NewDocument(*args.ReturnDocument == options.After)
1951+
newDocument := *args.ReturnDocument == options.After
1952+
op.newDocument = &newDocument
19401953
}
19411954
if args.Sort != nil {
19421955
if isUnorderedMap(args.Sort) {
@@ -1946,10 +1959,10 @@ func (coll *Collection) FindOneAndReplace(
19461959
if err != nil {
19471960
return &SingleResult{err: err}
19481961
}
1949-
op = op.Sort(sort)
1962+
op.sort = sort
19501963
}
19511964
if args.Upsert != nil {
1952-
op = op.Upsert(*args.Upsert)
1965+
op.upsert = args.Upsert
19531966
}
19541967
if args.Hint != nil {
19551968
if isUnorderedMap(args.Hint) {
@@ -1959,20 +1972,20 @@ func (coll *Collection) FindOneAndReplace(
19591972
if err != nil {
19601973
return &SingleResult{err: err}
19611974
}
1962-
op = op.Hint(hint)
1975+
op.hint = hint
19631976
}
19641977
if args.Let != nil {
19651978
let, err := marshal(args.Let, coll.bsonOpts, coll.registry)
19661979
if err != nil {
19671980
return &SingleResult{err: err}
19681981
}
1969-
op = op.Let(let)
1982+
op.let = let
19701983
}
19711984
if rawData, ok := optionsutil.Value(args.Internal, "rawData").(bool); ok {
1972-
op = op.RawData(rawData)
1985+
op.rawData = &rawData
19731986
}
19741987
if additionalCmd, ok := optionsutil.Value(args.Internal, "addCommandFields").(bson.D); ok {
1975-
op = op.AdditionalCmd(additionalCmd)
1988+
op.additionalCmd = additionalCmd
19761989
}
19771990

19781991
return coll.findAndModify(ctx, op)
@@ -2013,13 +2026,18 @@ func (coll *Collection) FindOneAndUpdate(
20132026
return &SingleResult{err: fmt.Errorf("failed to construct options from builder: %w", err)}
20142027
}
20152028

2016-
op := operation.NewFindAndModify(f).ServerAPI(coll.client.serverAPI).Timeout(coll.client.timeout).Authenticator(coll.client.authenticator)
2029+
op := &findAndModifyOp{
2030+
query: f,
2031+
serverAPI: coll.client.serverAPI,
2032+
timeout: coll.client.timeout,
2033+
authenticator: coll.client.authenticator,
2034+
}
20172035

20182036
u, err := marshalUpdateValue(update, coll.bsonOpts, coll.registry, true)
20192037
if err != nil {
20202038
return &SingleResult{err: err}
20212039
}
2022-
op = op.Update(u)
2040+
op.update = u
20232041

20242042
if args.ArrayFilters != nil {
20252043
af := args.ArrayFilters
@@ -2028,30 +2046,31 @@ func (coll *Collection) FindOneAndUpdate(
20282046
if err != nil {
20292047
return &SingleResult{err: err}
20302048
}
2031-
op = op.ArrayFilters(filtersDoc.Data)
2049+
op.arrayFilters = filtersDoc.Data
20322050
}
20332051
if args.BypassDocumentValidation != nil && *args.BypassDocumentValidation {
2034-
op = op.BypassDocumentValidation(*args.BypassDocumentValidation)
2052+
op.bypassDocumentValidation = args.BypassDocumentValidation
20352053
}
20362054
if args.Collation != nil {
2037-
op = op.Collation(bsoncore.Document(toDocument(args.Collation)))
2055+
op.collation = bsoncore.Document(toDocument(args.Collation))
20382056
}
20392057
if args.Comment != nil {
20402058
comment, err := marshalValue(args.Comment, coll.bsonOpts, coll.registry)
20412059
if err != nil {
20422060
return &SingleResult{err: err}
20432061
}
2044-
op = op.Comment(comment)
2062+
op.comment = comment
20452063
}
20462064
if args.Projection != nil {
20472065
proj, err := marshal(args.Projection, coll.bsonOpts, coll.registry)
20482066
if err != nil {
20492067
return &SingleResult{err: err}
20502068
}
2051-
op = op.Fields(proj)
2069+
op.fields = proj
20522070
}
20532071
if args.ReturnDocument != nil {
2054-
op = op.NewDocument(*args.ReturnDocument == options.After)
2072+
newDocument := *args.ReturnDocument == options.After
2073+
op.newDocument = &newDocument
20552074
}
20562075
if args.Sort != nil {
20572076
if isUnorderedMap(args.Sort) {
@@ -2061,10 +2080,10 @@ func (coll *Collection) FindOneAndUpdate(
20612080
if err != nil {
20622081
return &SingleResult{err: err}
20632082
}
2064-
op = op.Sort(sort)
2083+
op.sort = sort
20652084
}
20662085
if args.Upsert != nil {
2067-
op = op.Upsert(*args.Upsert)
2086+
op.upsert = args.Upsert
20682087
}
20692088
if args.Hint != nil {
20702089
if isUnorderedMap(args.Hint) {
@@ -2074,20 +2093,20 @@ func (coll *Collection) FindOneAndUpdate(
20742093
if err != nil {
20752094
return &SingleResult{err: err}
20762095
}
2077-
op = op.Hint(hint)
2096+
op.hint = hint
20782097
}
20792098
if args.Let != nil {
20802099
let, err := marshal(args.Let, coll.bsonOpts, coll.registry)
20812100
if err != nil {
20822101
return &SingleResult{err: err}
20832102
}
2084-
op = op.Let(let)
2103+
op.let = let
20852104
}
20862105
if rawData, ok := optionsutil.Value(args.Internal, "rawData").(bool); ok {
2087-
op = op.RawData(rawData)
2106+
op.rawData = &rawData
20882107
}
20892108
if additionalCmd, ok := optionsutil.Value(args.Internal, "addCommandFields").(bson.D); ok {
2090-
op = op.AdditionalCmd(additionalCmd)
2109+
op.additionalCmd = additionalCmd
20912110
}
20922111

20932112
return coll.findAndModify(ctx, op)

0 commit comments

Comments
 (0)