Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for Insert and Update operation to handle date field #379

Merged
merged 3 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.stargate.sgv2.jsonapi.service.shredding.JsonPath;
import io.stargate.sgv2.jsonapi.service.shredding.model.DocumentId;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -61,6 +62,15 @@ public static Map<QueryOuterClass.Value, QueryOuterClass.Value> getDoubleMapValu
return to;
}

public static Map<QueryOuterClass.Value, QueryOuterClass.Value> getTimestampMapValues(
Map<JsonPath, Date> from) {
final Map<QueryOuterClass.Value, QueryOuterClass.Value> to = new HashMap<>(from.size());
for (Map.Entry<JsonPath, Date> entry : from.entrySet()) {
to.put(Values.of(entry.getKey().toString()), Values.of(entry.getValue().getTime()));
}
return to;
}

public static List<QueryOuterClass.Value> getDocumentIdValue(DocumentId documentId) {
// Temporary implementation until we convert it to Tuple in DB
List<QueryOuterClass.Value> tupleValues =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ private static Uni<DocumentId> insertDocument(
private QueryOuterClass.Query buildInsertQuery() {
String insert =
"INSERT INTO \"%s\".\"%s\""
+ " (key, tx_id, doc_json, exist_keys, sub_doc_equals, array_size, array_equals, array_contains, query_bool_values, query_dbl_values , query_text_values, query_null_values)"
+ " (key, tx_id, doc_json, exist_keys, sub_doc_equals, array_size, array_equals, array_contains, query_bool_values, query_dbl_values , query_text_values, query_null_values, query_timestamp_values)"
+ " VALUES"
+ " (?, now(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) IF NOT EXISTS";
+ " (?, now(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) IF NOT EXISTS";

return QueryOuterClass.Query.newBuilder()
.setCql(String.format(insert, commandContext.namespace(), commandContext.collection()))
Expand All @@ -167,7 +167,10 @@ private static QueryOuterClass.Query bindInsertValues(
.addValues(
Values.of(CustomValueSerializers.getDoubleMapValues(doc.queryNumberValues())))
.addValues(Values.of(CustomValueSerializers.getStringMapValues(doc.queryTextValues())))
.addValues(Values.of(CustomValueSerializers.getSetValue(doc.queryNullValues())));
.addValues(Values.of(CustomValueSerializers.getSetValue(doc.queryNullValues())))
.addValues(
Values.of(
CustomValueSerializers.getTimestampMapValues(doc.queryTimestampValues())));
return QueryOuterClass.Query.newBuilder(builtQuery).setValues(values).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ private QueryOuterClass.Query buildUpdateQuery() {
+ " query_dbl_values = ?,"
+ " query_text_values = ?,"
+ " query_null_values = ?,"
+ " query_timestamp_values = ?,"
+ " doc_json = ?"
+ " WHERE "
+ " key = ?"
Expand All @@ -253,6 +254,8 @@ protected static QueryOuterClass.Query bindUpdateValues(
Values.of(CustomValueSerializers.getDoubleMapValues(doc.queryNumberValues())))
.addValues(Values.of(CustomValueSerializers.getStringMapValues(doc.queryTextValues())))
.addValues(Values.of(CustomValueSerializers.getSetValue(doc.queryNullValues())))
.addValues(
Values.of(CustomValueSerializers.getTimestampMapValues(doc.queryTimestampValues())))
.addValues(Values.of(doc.docJson()))
.addValues(Values.of(CustomValueSerializers.getDocumentIdValue(doc.id())))
.addValues(doc.txID() == null ? Values.NULL : Values.of(doc.txID()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,63 @@ public void insertDocument() {
.body("errors", is(nullValue()));
}

@Test
public void insertDocumentWithDate() {
String json =
"""
{
"insertOne": {
"document": {
"_id": "doc_date",
"username": "doc_date_user3",
"date_created": {"$date": 1672531200000}
}
}
}
""";

given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
.when()
.post(CollectionResource.BASE_PATH, namespaceName, collectionName)
.then()
.statusCode(200)
.body("status.insertedIds[0]", is("doc_date"))
.body("data", is(nullValue()))
.body("errors", is(nullValue()));

String expected =
"""
{
"_id": "doc_date",
"username": "doc_date_user3",
"date_created": {"$date": 1672531200000}
}
""";

String query_json =
"""
{
"find": {
"filter" : {"_id" : "doc_date"}
}
}
""";

given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(query_json)
.when()
.post(CollectionResource.BASE_PATH, namespaceName, collectionName)
.then()
.statusCode(200)
.body("data.docs[0]", jsonEquals(expected))
.body("errors", is(nullValue()));
}

@Test
public void insertDocumentWithNumberId() {
String json =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void byIdAndSet() {
"document": {
"_id": "update_doc1",
"username": "update_user3",
"date_col": {"$date" : 1672531200000},
"active_user" : true
}
}
Expand Down Expand Up @@ -76,6 +77,7 @@ public void byIdAndSet() {
{
"_id":"update_doc1",
"username":"update_user3",
"date_col": {"$date" : 1672531200000},
"active_user":false
}
""";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.stargate.bridge.proto.QueryOuterClass;
import io.stargate.sgv2.jsonapi.service.shredding.JsonPath;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -83,5 +84,16 @@ public void getListValue() {
final QueryOuterClass.Value next = to.get(0);
assertThat(Values.string(next)).isEqualTo("field1");
}

@Test
public void getTimestampMapValues() {
Map<JsonPath, Date> from = Map.of(JsonPath.from("field1"), new Date(10L));
final Map<QueryOuterClass.Value, QueryOuterClass.Value> to =
CustomValueSerializers.getTimestampMapValues(from);
final Map.Entry<QueryOuterClass.Value, QueryOuterClass.Value> next =
to.entrySet().iterator().next();
assertThat(Values.string(next.getKey())).isEqualTo("field1");
assertThat(Values.bigint(next.getValue())).isEqualTo(10L);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ class Execute {

static final String INSERT_CQL =
"INSERT INTO \"%s\".\"%s\""
+ " (key, tx_id, doc_json, exist_keys, sub_doc_equals, array_size, array_equals, array_contains, query_bool_values, query_dbl_values , query_text_values, query_null_values)"
+ " (key, tx_id, doc_json, exist_keys, sub_doc_equals, array_size, array_equals, array_contains, query_bool_values, query_dbl_values , query_text_values, query_null_values, query_timestamp_values)"
+ " VALUES"
+ " (?, now(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) IF NOT EXISTS";
+ " (?, now(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) IF NOT EXISTS";

@Test
public void insertOne() throws Exception {
Expand All @@ -62,7 +62,8 @@ public void insertOne() throws Exception {
"boolean": true,
"nullval" : null,
"array" : ["a", "b"],
"sub_doc" : {"col": "val"}
"sub_doc" : {"col": "val"},
"date_val" : {"$date": 1672531200000 }
}
""";

Expand All @@ -88,7 +89,10 @@ public void insertOne() throws Exception {
CustomValueSerializers.getDoubleMapValues(shredDocument.queryNumberValues())),
Values.of(
CustomValueSerializers.getStringMapValues(shredDocument.queryTextValues())),
Values.of(CustomValueSerializers.getSetValue(shredDocument.queryNullValues())))
Values.of(CustomValueSerializers.getSetValue(shredDocument.queryNullValues())),
Values.of(
CustomValueSerializers.getTimestampMapValues(
shredDocument.queryTimestampValues())))
.withColumnSpec(
List.of(
QueryOuterClass.ColumnSpec.newBuilder()
Expand Down Expand Up @@ -155,7 +159,10 @@ public void insertDuplicate() throws Exception {
CustomValueSerializers.getDoubleMapValues(shredDocument.queryNumberValues())),
Values.of(
CustomValueSerializers.getStringMapValues(shredDocument.queryTextValues())),
Values.of(CustomValueSerializers.getSetValue(shredDocument.queryNullValues())))
Values.of(CustomValueSerializers.getSetValue(shredDocument.queryNullValues())),
Values.of(
CustomValueSerializers.getTimestampMapValues(
shredDocument.queryTimestampValues())))
.withColumnSpec(
List.of(
QueryOuterClass.ColumnSpec.newBuilder()
Expand Down Expand Up @@ -247,7 +254,10 @@ public void insertManyOrdered() throws Exception {
shredDocument1.queryNumberValues())),
Values.of(
CustomValueSerializers.getStringMapValues(shredDocument1.queryTextValues())),
Values.of(CustomValueSerializers.getSetValue(shredDocument1.queryNullValues())))
Values.of(CustomValueSerializers.getSetValue(shredDocument1.queryNullValues())),
Values.of(
CustomValueSerializers.getTimestampMapValues(
shredDocument1.queryTimestampValues())))
.withColumnSpec(
List.of(
QueryOuterClass.ColumnSpec.newBuilder()
Expand Down Expand Up @@ -276,7 +286,10 @@ public void insertManyOrdered() throws Exception {
shredDocument2.queryNumberValues())),
Values.of(
CustomValueSerializers.getStringMapValues(shredDocument2.queryTextValues())),
Values.of(CustomValueSerializers.getSetValue(shredDocument2.queryNullValues())))
Values.of(CustomValueSerializers.getSetValue(shredDocument2.queryNullValues())),
Values.of(
CustomValueSerializers.getTimestampMapValues(
shredDocument2.queryTimestampValues())))
.withColumnSpec(
List.of(
QueryOuterClass.ColumnSpec.newBuilder()
Expand Down Expand Up @@ -364,7 +377,10 @@ public void insertManyUnordered() throws Exception {
shredDocument1.queryNumberValues())),
Values.of(
CustomValueSerializers.getStringMapValues(shredDocument1.queryTextValues())),
Values.of(CustomValueSerializers.getSetValue(shredDocument1.queryNullValues())))
Values.of(CustomValueSerializers.getSetValue(shredDocument1.queryNullValues())),
Values.of(
CustomValueSerializers.getTimestampMapValues(
shredDocument1.queryTimestampValues())))
.withColumnSpec(
List.of(
QueryOuterClass.ColumnSpec.newBuilder()
Expand Down Expand Up @@ -393,7 +409,10 @@ public void insertManyUnordered() throws Exception {
shredDocument2.queryNumberValues())),
Values.of(
CustomValueSerializers.getStringMapValues(shredDocument2.queryTextValues())),
Values.of(CustomValueSerializers.getSetValue(shredDocument2.queryNullValues())))
Values.of(CustomValueSerializers.getSetValue(shredDocument2.queryNullValues())),
Values.of(
CustomValueSerializers.getTimestampMapValues(
shredDocument2.queryTimestampValues())))
.withColumnSpec(
List.of(
QueryOuterClass.ColumnSpec.newBuilder()
Expand Down Expand Up @@ -484,7 +503,10 @@ public void failureOrdered() throws Exception {
shredDocument1.queryNumberValues())),
Values.of(
CustomValueSerializers.getStringMapValues(shredDocument1.queryTextValues())),
Values.of(CustomValueSerializers.getSetValue(shredDocument1.queryNullValues())))
Values.of(CustomValueSerializers.getSetValue(shredDocument1.queryNullValues())),
Values.of(
CustomValueSerializers.getTimestampMapValues(
shredDocument1.queryTimestampValues())))
.withColumnSpec(
List.of(
QueryOuterClass.ColumnSpec.newBuilder()
Expand Down Expand Up @@ -576,7 +598,10 @@ public void failureOrderedLastFails() throws Exception {
shredDocument1.queryNumberValues())),
Values.of(
CustomValueSerializers.getStringMapValues(shredDocument1.queryTextValues())),
Values.of(CustomValueSerializers.getSetValue(shredDocument1.queryNullValues())))
Values.of(CustomValueSerializers.getSetValue(shredDocument1.queryNullValues())),
Values.of(
CustomValueSerializers.getTimestampMapValues(
shredDocument1.queryTimestampValues())))
.withColumnSpec(
List.of(
QueryOuterClass.ColumnSpec.newBuilder()
Expand Down Expand Up @@ -605,7 +630,10 @@ public void failureOrderedLastFails() throws Exception {
shredDocument2.queryNumberValues())),
Values.of(
CustomValueSerializers.getStringMapValues(shredDocument2.queryTextValues())),
Values.of(CustomValueSerializers.getSetValue(shredDocument2.queryNullValues())))
Values.of(CustomValueSerializers.getSetValue(shredDocument2.queryNullValues())),
Values.of(
CustomValueSerializers.getTimestampMapValues(
shredDocument2.queryTimestampValues())))
.withColumnSpec(
List.of(
QueryOuterClass.ColumnSpec.newBuilder()
Expand Down Expand Up @@ -701,7 +729,10 @@ public void failureUnorderedPartial() throws Exception {
shredDocument1.queryNumberValues())),
Values.of(
CustomValueSerializers.getStringMapValues(shredDocument1.queryTextValues())),
Values.of(CustomValueSerializers.getSetValue(shredDocument1.queryNullValues())))
Values.of(CustomValueSerializers.getSetValue(shredDocument1.queryNullValues())),
Values.of(
CustomValueSerializers.getTimestampMapValues(
shredDocument1.queryTimestampValues())))
.withColumnSpec(
List.of(
QueryOuterClass.ColumnSpec.newBuilder()
Expand Down Expand Up @@ -730,7 +761,10 @@ public void failureUnorderedPartial() throws Exception {
shredDocument2.queryNumberValues())),
Values.of(
CustomValueSerializers.getStringMapValues(shredDocument2.queryTextValues())),
Values.of(CustomValueSerializers.getSetValue(shredDocument2.queryNullValues())))
Values.of(CustomValueSerializers.getSetValue(shredDocument2.queryNullValues())),
Values.of(
CustomValueSerializers.getTimestampMapValues(
shredDocument2.queryTimestampValues())))
.withColumnSpec(
List.of(
QueryOuterClass.ColumnSpec.newBuilder()
Expand Down Expand Up @@ -825,7 +859,10 @@ public void failureUnorderedAll() throws Exception {
shredDocument1.queryNumberValues())),
Values.of(
CustomValueSerializers.getStringMapValues(shredDocument1.queryTextValues())),
Values.of(CustomValueSerializers.getSetValue(shredDocument1.queryNullValues())))
Values.of(CustomValueSerializers.getSetValue(shredDocument1.queryNullValues())),
Values.of(
CustomValueSerializers.getTimestampMapValues(
shredDocument1.queryTimestampValues())))
.withColumnSpec(
List.of(
QueryOuterClass.ColumnSpec.newBuilder()
Expand Down Expand Up @@ -854,7 +891,10 @@ public void failureUnorderedAll() throws Exception {
shredDocument2.queryNumberValues())),
Values.of(
CustomValueSerializers.getStringMapValues(shredDocument2.queryTextValues())),
Values.of(CustomValueSerializers.getSetValue(shredDocument2.queryNullValues())))
Values.of(CustomValueSerializers.getSetValue(shredDocument2.queryNullValues())),
Values.of(
CustomValueSerializers.getTimestampMapValues(
shredDocument2.queryTimestampValues())))
.withColumnSpec(
List.of(
QueryOuterClass.ColumnSpec.newBuilder()
Expand Down
Loading