Skip to content

Commit 44ddffb

Browse files
committed
updateOne
1 parent 90c3bcd commit 44ddffb

File tree

9 files changed

+574
-24
lines changed

9 files changed

+574
-24
lines changed

astra-db-java/src/main/java/com/datastax/astra/client/core/commands/Command.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.datastax.astra.client.collections.documents.Document;
2424
import com.datastax.astra.client.core.query.Filter;
2525
import com.datastax.astra.client.collections.documents.Update;
26+
import com.datastax.astra.client.tables.row.TableUpdate;
2627
import com.datastax.astra.internal.utils.Assert;
2728
import com.fasterxml.jackson.core.JsonGenerator;
2829
import com.fasterxml.jackson.databind.SerializerProvider;
@@ -201,6 +202,19 @@ public Command withUpdate(Update update) {
201202
return this;
202203
}
203204

205+
/**
206+
* Builder pattern, Update.
207+
*
208+
* @param update
209+
* update of the command
210+
* @return
211+
* self-reference
212+
*/
213+
public Command withUpdate(TableUpdate update) {
214+
payload.appendIfNotNull("update", update);
215+
return this;
216+
}
217+
204218
/**
205219
* Specialization of the command.
206220
*

astra-db-java/src/main/java/com/datastax/astra/client/tables/Table.java

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
* #L%
2121
*/
2222

23+
import com.datastax.astra.client.collections.commands.FindOneAndDeleteOptions;
24+
import com.datastax.astra.client.collections.commands.UpdateOneOptions;
25+
import com.datastax.astra.client.collections.commands.UpdateResult;
26+
import com.datastax.astra.client.collections.documents.Update;
2327
import com.datastax.astra.client.core.options.DataAPIOptions;
2428
import com.datastax.astra.client.collections.documents.Document;
2529
import com.datastax.astra.client.core.commands.Command;
@@ -31,11 +35,13 @@
3135
import com.datastax.astra.client.tables.commands.EstimatedCountRowsOptions;
3236
import com.datastax.astra.client.tables.commands.TableDeleteManyOptions;
3337
import com.datastax.astra.client.tables.commands.TableDeleteOneOptions;
38+
import com.datastax.astra.client.tables.commands.TableFindOneAndDeleteOptions;
3439
import com.datastax.astra.client.tables.commands.TableFindOneOptions;
3540
import com.datastax.astra.client.tables.commands.TableInsertManyOptions;
3641
import com.datastax.astra.client.tables.commands.TableInsertManyResult;
3742
import com.datastax.astra.client.tables.commands.TableInsertOneOptions;
3843
import com.datastax.astra.client.tables.commands.TableInsertOneResult;
44+
import com.datastax.astra.client.tables.commands.TableUpdateOneOptions;
3945
import com.datastax.astra.client.tables.commands.ddl.AlterTableOperation;
4046
import com.datastax.astra.client.tables.commands.ddl.AlterTableOptions;
4147
import com.datastax.astra.client.tables.commands.ddl.CreateIndexOptions;
@@ -46,6 +52,7 @@
4652
import com.datastax.astra.client.tables.mapping.EntityTable;
4753
import com.datastax.astra.client.tables.mapping.EntityBeanDefinition;
4854
import com.datastax.astra.client.tables.row.Row;
55+
import com.datastax.astra.client.tables.row.TableUpdate;
4956
import com.datastax.astra.internal.api.DataAPIData;
5057
import com.datastax.astra.internal.api.DataAPIResponse;
5158
import com.datastax.astra.internal.api.DataAPIStatus;
@@ -545,24 +552,6 @@ public CompletableFuture<Optional<T>> findOneASync(Filter filter, TableFindOneOp
545552
return CompletableFuture.supplyAsync(() -> findOne(filter, findOneOptions));
546553
}
547554

548-
// -------------------------
549-
// --- findOneAndDelete ----
550-
// -------------------------
551-
552-
// FIXME
553-
554-
// -------------------------
555-
// --- findOneAndReplace ---
556-
// -------------------------
557-
558-
// FIXME
559-
560-
// -------------------------
561-
// --- findOneAndUpdate ----
562-
// -------------------------
563-
564-
// FIXME
565-
566555
// -------------------------
567556
// --- find ----
568557
// -------------------------
@@ -573,7 +562,70 @@ public CompletableFuture<Optional<T>> findOneASync(Filter filter, TableFindOneOp
573562
// --- updateOne ----
574563
// -------------------------
575564

576-
// FIXME
565+
/**
566+
* Update a single row in the table according to the specified arguments.
567+
*
568+
* @param filter
569+
* a row describing the query filter, which may not be null.
570+
* @param update
571+
* a row describing the update, which may not be null. The update to apply must include at least one update operator.
572+
* @return
573+
* the result of the update one operation
574+
*/
575+
public UpdateResult updateOne(Filter filter, TableUpdate update) {
576+
return updateOne(filter, update, new TableUpdateOneOptions());
577+
}
578+
579+
/**
580+
* Update a single document in the collection according to the specified arguments.
581+
*
582+
* @param filter
583+
* a document describing the query filter, which may not be null.
584+
* @param update
585+
* a document describing the update, which may not be null. The update to apply must include at least one update operator.
586+
* @param updateOptions
587+
* the options to apply to the update operation
588+
* @return
589+
* the result of the update one operation
590+
*/
591+
public UpdateResult updateOne(Filter filter, TableUpdate update, TableUpdateOneOptions updateOptions) {
592+
notNull(update, ARG_UPDATE);
593+
notNull(updateOptions, ARG_OPTIONS);
594+
Command cmd = Command
595+
.create("updateOne")
596+
.withFilter(filter)
597+
.withUpdate(update)
598+
.withSort(updateOptions.getSort())
599+
.withOptions(new Document()
600+
.appendIfNotNull(INPUT_UPSERT, updateOptions.getUpsert())
601+
);
602+
return getUpdateResult(runCommand(cmd, updateOptions));
603+
}
604+
605+
/**
606+
* Update all documents in the collection according to the specified arguments.
607+
*
608+
* @param apiResponse
609+
* response for the API
610+
* @return
611+
* the result of the update many operation
612+
*/
613+
private static UpdateResult getUpdateResult(DataAPIResponse apiResponse) {
614+
UpdateResult result = new UpdateResult();
615+
DataAPIStatus status = apiResponse.getStatus();
616+
if (status != null) {
617+
if (status.containsKey(RESULT_MATCHED_COUNT)) {
618+
result.setMatchedCount(status.getInteger(RESULT_MATCHED_COUNT));
619+
}
620+
if (status.containsKey(RESULT_MODIFIED_COUNT)) {
621+
result.setModifiedCount(status.getInteger(RESULT_MODIFIED_COUNT));
622+
}
623+
if (status.containsKey(RESULT_UPSERTED_ID)) {
624+
result.setMatchedCount(status.getInteger(RESULT_UPSERTED_ID));
625+
}
626+
}
627+
return result;
628+
}
577629

578630
// -------------------------
579631
// --- deleteOne ----
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package com.datastax.astra.client.tables.commands;
2+
3+
/*-
4+
* #%L
5+
* Data API Java Client
6+
* --
7+
* Copyright (C) 2024 DataStax
8+
* --
9+
* Licensed under the Apache License, Version 2.0
10+
* You may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import com.datastax.astra.client.collections.documents.Document;
24+
import com.datastax.astra.client.core.commands.CommandOptions;
25+
import com.datastax.astra.client.core.query.Projection;
26+
import com.datastax.astra.client.core.query.Sort;
27+
import com.datastax.astra.client.core.query.Sorts;
28+
import com.datastax.astra.internal.utils.OptionsUtils;
29+
import lombok.Getter;
30+
import lombok.Setter;
31+
32+
import java.util.Map;
33+
34+
/**
35+
* Options to find one and delete.
36+
*/
37+
@Getter
38+
@Setter
39+
public class TableFindOneAndDeleteOptions extends CommandOptions<TableFindOneAndDeleteOptions> {
40+
41+
/**
42+
* Order by.
43+
*/
44+
private Document sort;
45+
46+
/**
47+
* Select.
48+
*/
49+
private Map<String, Object> projection;
50+
51+
/**
52+
* Default constructor.
53+
*/
54+
public TableFindOneAndDeleteOptions() {
55+
// left blank as sort is populated in static way
56+
}
57+
58+
// ----------------
59+
// ---- Sort ------
60+
// ----------------
61+
62+
/**
63+
* Syntax sugar as delete option is only a sort
64+
*
65+
* @param sort
66+
* add a filter
67+
* @return
68+
* current command.
69+
*/
70+
public TableFindOneAndDeleteOptions sort(Sort... sort) {
71+
return sort(OptionsUtils.sort(sort));
72+
}
73+
74+
/**
75+
* Syntax sugar as delete option is only a sort
76+
* Could be like Map.of("$vectorize", "command, "field1", 1, "field2", -1);
77+
*
78+
* @param rawSort
79+
* raw sort clause
80+
* @return
81+
* current command.
82+
*/
83+
public TableFindOneAndDeleteOptions sort(Map<String, Object> rawSort) {
84+
Document doc = new Document();
85+
doc.putAll(rawSort);
86+
return sort(doc);
87+
}
88+
89+
/**
90+
* Syntax sugar as delete option is only a sort
91+
* Could be like Map.of("$vectorize", "command, "field1", 1, "field2", -1);
92+
*
93+
* @param sorClause
94+
* sort clause as a document
95+
* @return
96+
* current command.
97+
*/
98+
public TableFindOneAndDeleteOptions sort(Document sorClause) {
99+
setSort(sorClause);
100+
return this;
101+
}
102+
103+
/**
104+
* Add a criteria with $vectorize in the sort clause.
105+
*
106+
* @param vectorize an expression to look for vectorization
107+
* @param sorts The sort criteria to be applied to the findOne operation.
108+
* @return current command
109+
*/
110+
public TableFindOneAndDeleteOptions sort(String vectorize, Sort ... sorts) {
111+
Document doc = Sorts.vectorize(vectorize);
112+
if (sorts != null) {
113+
doc.putAll(OptionsUtils.sort(sorts));
114+
}
115+
return sort(doc);
116+
}
117+
118+
/**
119+
* Add a criteria with $vector in the sort clause
120+
*
121+
* @param vector vector float
122+
* @param sorts The sort criteria to be applied to the findOne operation.
123+
* @return current command
124+
*/
125+
public TableFindOneAndDeleteOptions sort(float[] vector, Sort... sorts) {
126+
Document doc = Sorts.vector(vector);
127+
if (sorts != null) {
128+
doc.putAll(OptionsUtils.sort(sorts));
129+
}
130+
return sort(doc);
131+
}
132+
133+
// ----------------------
134+
// ---- Projection ------
135+
// ----------------------
136+
137+
/**
138+
* Syntax sugar as delete option is only a sort
139+
*
140+
* @param projection
141+
* add a filter
142+
* @return
143+
* current command.
144+
*/
145+
public TableFindOneAndDeleteOptions projection(Projection... projection) {
146+
setProjection(OptionsUtils.projection(projection));
147+
return this;
148+
}
149+
150+
151+
152+
}

0 commit comments

Comments
 (0)