Skip to content
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
@@ -1,5 +1,20 @@
package com.scalar.dl.genericcontracts.object;

import static java.time.temporal.ChronoField.DAY_OF_MONTH;
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
import static java.time.temporal.ChronoField.YEAR;

import java.time.ZoneOffset;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.ResolverStyle;
import java.time.format.SignStyle;

public class Constants {

// Object authenticity management
Expand Down Expand Up @@ -49,5 +64,62 @@ public class Constants {
public static final String COLLECTION_ID_IS_MISSING_OR_INVALID =
"The collection ID is not specified in the arguments or is invalid.";
public static final String INVALID_PUT_MUTABLE_FUNCTION_ARGUMENT_FORMAT =
"The specified format of the PutMutable function argument is invalid.";
"The specified format of the PutToMutableDatabase function argument is invalid.";

/** A formatter for a DATE literal. The format is "YYYY-MM-DD". For example, "2020-03-04". */
public static final DateTimeFormatter DATE_FORMATTER =
new DateTimeFormatterBuilder()
.appendValue(YEAR, 4, 4, SignStyle.NEVER)
.appendLiteral('-')
.appendValue(MONTH_OF_YEAR, 2)
.appendLiteral('-')
.appendValue(DAY_OF_MONTH, 2)
.toFormatter()
.withResolverStyle(ResolverStyle.STRICT)
.withChronology(IsoChronology.INSTANCE);
/**
* A formatter for a TIME literal. The format is "HH:MM:SS[.FFFFFF]". For example,
* "12:34:56.123456". The fractional second is optional.
*/
public static final DateTimeFormatter TIME_FORMATTER =
new DateTimeFormatterBuilder()
.appendValue(HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 2)
.optionalStart()
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2)
.optionalStart()
.appendFraction(NANO_OF_SECOND, 0, 6, true)
.toFormatter()
.withResolverStyle(ResolverStyle.STRICT)
.withChronology(IsoChronology.INSTANCE);
/**
* A formatter for a TIMESTAMP literal. The format is "YYYY-MM-DD HH:MM:SS[.FFF]". For example,
* "2020-03-04 12:34:56.123". The fractional second is optional.
*/
public static final DateTimeFormatter TIMESTAMP_FORMATTER =
new DateTimeFormatterBuilder()
.append(DATE_FORMATTER)
.appendLiteral(' ')
.appendValue(HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 2)
.optionalStart()
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2)
.optionalStart()
.appendFraction(NANO_OF_SECOND, 0, 3, true)
.toFormatter();
/**
* A formatter for a TIMESTAMPTZ literal. The format is "YYYY-MM-DD HH:MM:SS[.FFF] Z". For
* example, "2020-03-04 12:34:56.123 Z". The fractional second is optional.
*/
public static final DateTimeFormatter TIMESTAMPTZ_FORMATTER =
new DateTimeFormatterBuilder()
.append(TIMESTAMP_FORMATTER)
.appendLiteral(' ')
.appendLiteral('Z')
.toFormatter()
.withZone(ZoneOffset.UTC);
}
2 changes: 1 addition & 1 deletion generic-contracts/scripts/objects-table-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"object_id": "TEXT",
"version": "TEXT",
"status": "INT",
"registered_at": "BIGINT"
"registered_at": "TIMESTAMP"
},
"compaction-strategy": "LCS"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@
import com.scalar.db.io.Key;
import com.scalar.dl.client.exception.ClientException;
import com.scalar.dl.client.service.GenericContractClientService;
import com.scalar.dl.genericcontracts.object.Constants;
import com.scalar.dl.ledger.error.LedgerError;
import com.scalar.dl.ledger.model.ContractExecutionResult;
import com.scalar.dl.ledger.model.LedgerValidationResult;
import com.scalar.dl.ledger.service.StatusCode;
import com.scalar.dl.ledger.util.JacksonSerDe;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -90,7 +92,7 @@ public class GenericContractObjectAndCollectionEndToEndTest
private static final String ASSET_AGE = "age";
private static final String ASSET_OUTPUT = "output";
private static final String DATA_TYPE_INT = "INT";
private static final String DATA_TYPE_BIGINT = "BIGINT";
private static final String DATA_TYPE_TIMESTAMP = "TIMESTAMP";
private static final String DATA_TYPE_TEXT = "TEXT";

private static final String PACKAGE_OBJECT = "object";
Expand Down Expand Up @@ -138,6 +140,8 @@ public class GenericContractObjectAndCollectionEndToEndTest
private static final String SOME_COLUMN_NAME_2 = "version";
private static final String SOME_COLUMN_NAME_3 = "status";
private static final String SOME_COLUMN_NAME_4 = "registered_at";
private static final String SOME_TIMESTAMP_TEXT = "2021-02-03 05:45:00";
private static final LocalDateTime SOME_TIMESTAMP_VALUE = LocalDateTime.of(2021, 2, 3, 5, 45);
private static final String SOME_COLLECTION_ID = "set";
private static final ArrayNode SOME_DEFAULT_OBJECT_IDS =
mapper.createArrayNode().add("object1").add("object2").add("object3").add("object4");
Expand Down Expand Up @@ -241,20 +245,20 @@ private JsonNode createColumn(String name, int value) {
.put(DATA_TYPE, DATA_TYPE_INT);
}

private JsonNode createColumn(String name, long value) {
private JsonNode createColumn(String name, String value) {
return mapper
.createObjectNode()
.put(COLUMN_NAME, name)
.put(VALUE, value)
.put(DATA_TYPE, DATA_TYPE_BIGINT);
.put(DATA_TYPE, DATA_TYPE_TEXT);
}

private JsonNode createColumn(String name, String value) {
private JsonNode createTimestampColumn(String name, String value) {
return mapper
.createObjectNode()
.put(COLUMN_NAME, name)
.put(VALUE, value)
.put(DATA_TYPE, DATA_TYPE_TEXT);
.put(DATA_TYPE, DATA_TYPE_TIMESTAMP);
}

private JsonNode createFunctionArguments(
Expand All @@ -267,7 +271,7 @@ private JsonNode createFunctionArguments(
mapper
.createArrayNode()
.add(createColumn(SOME_COLUMN_NAME_3, status))
.add(createColumn(SOME_COLUMN_NAME_4, registeredAt));
.add(createTimestampColumn(SOME_COLUMN_NAME_4, SOME_TIMESTAMP_TEXT));

ObjectNode arguments = mapper.createObjectNode();
arguments.put(NAMESPACE, getFunctionNamespace());
Expand Down Expand Up @@ -574,11 +578,11 @@ public void putObject_FunctionArgumentsGiven_ShouldPutRecordToFunctionTable()
assertThat(results.get(0).getText(SOME_COLUMN_NAME_1)).isEqualTo(SOME_OBJECT_ID);
assertThat(results.get(0).getText(SOME_COLUMN_NAME_2)).isEqualTo(SOME_VERSION_ID_0);
assertThat(results.get(0).getInt(SOME_COLUMN_NAME_3)).isEqualTo(0);
assertThat(results.get(0).getBigInt(SOME_COLUMN_NAME_4)).isEqualTo(1L);
assertThat(results.get(0).getTimestamp(SOME_COLUMN_NAME_4)).isEqualTo(SOME_TIMESTAMP_VALUE);
assertThat(results.get(1).getText(SOME_COLUMN_NAME_1)).isEqualTo(SOME_OBJECT_ID);
assertThat(results.get(1).getText(SOME_COLUMN_NAME_2)).isEqualTo(SOME_VERSION_ID_1);
assertThat(results.get(1).getInt(SOME_COLUMN_NAME_3)).isEqualTo(1);
assertThat(results.get(1).getBigInt(SOME_COLUMN_NAME_4)).isEqualTo(1234567890123L);
assertThat(results.get(1).getTimestamp(SOME_COLUMN_NAME_4)).isEqualTo(SOME_TIMESTAMP_VALUE);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -662,6 +666,45 @@ public void putObject_FunctionArgumentsGiven_ShouldPutRecordToFunctionTable()
.isEqualTo(StatusCode.CONFLICT);
}

@Test
public void
putObject_FunctionArgumentsWithInvalidTimeRelatedTypeFormatGiven_ShouldThrowClientException() {
// Arrange
JsonNode contractArguments =
mapper
.createObjectNode()
.put(OBJECT_ID, SOME_OBJECT_ID)
.put(HASH_VALUE, SOME_HASH_VALUE_0)
.set(METADATA, SOME_METADATA_0);
ObjectNode functionArguments =
mapper
.createObjectNode()
.put(NAMESPACE, getFunctionNamespace())
.put(TABLE, getFunctionTable());
functionArguments.set(
PARTITION_KEY,
mapper.createArrayNode().add(createColumn(SOME_COLUMN_NAME_1, SOME_OBJECT_ID)));
functionArguments.set(
CLUSTERING_KEY,
mapper.createArrayNode().add(createColumn(SOME_COLUMN_NAME_2, SOME_VERSION_ID_0)));
functionArguments.set(
COLUMNS,
mapper
.createArrayNode()
.add(createColumn(SOME_COLUMN_NAME_3, 0))
.add(createTimestampColumn(SOME_COLUMN_NAME_4, "2024-05-19")));

// Act Assert
assertThatThrownBy(
() ->
clientService.executeContract(
ID_OBJECT_PUT, contractArguments, ID_OBJECT_PUT_MUTABLE, functionArguments))
.isExactlyInstanceOf(ClientException.class)
.hasMessage(Constants.INVALID_PUT_MUTABLE_FUNCTION_ARGUMENT_FORMAT)
.extracting("code")
.isEqualTo(StatusCode.CONTRACT_CONTEXTUAL_ERROR);
}

@Test
public void putObject_InvalidMetadataGiven_ShouldThrowClientException() {
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@
import com.scalar.db.io.BooleanColumn;
import com.scalar.db.io.Column;
import com.scalar.db.io.DataType;
import com.scalar.db.io.DateColumn;
import com.scalar.db.io.DoubleColumn;
import com.scalar.db.io.FloatColumn;
import com.scalar.db.io.IntColumn;
import com.scalar.db.io.Key;
import com.scalar.db.io.TextColumn;
import com.scalar.db.io.TimeColumn;
import com.scalar.db.io.TimestampColumn;
import com.scalar.db.io.TimestampTZColumn;
import com.scalar.dl.ledger.database.Database;
import com.scalar.dl.ledger.exception.ContractContextException;
import com.scalar.dl.ledger.function.JacksonBasedFunction;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -193,6 +202,42 @@ private Column<?> getColumn(JsonNode jsonColumn) {
}
}

if (dataType.equals(DataType.DATE)
|| dataType.equals(DataType.TIME)
|| dataType.equals(DataType.TIMESTAMP)
|| dataType.equals(DataType.TIMESTAMPTZ)) {
if (!value.isTextual()) {
throw new ContractContextException(Constants.INVALID_PUT_MUTABLE_FUNCTION_ARGUMENT_FORMAT);
}
return getTimeRelatedColumn(columnName, value.textValue(), dataType);
}

throw new ContractContextException(Constants.INVALID_PUT_MUTABLE_FUNCTION_ARGUMENT_FORMAT);
}

private Column<?> getTimeRelatedColumn(String columnName, String value, DataType dataType) {
try {
if (dataType.equals(DataType.DATE)) {
return DateColumn.of(columnName, LocalDate.parse(value, Constants.DATE_FORMATTER));
}

if (dataType.equals(DataType.TIME)) {
return TimeColumn.of(columnName, LocalTime.parse(value, Constants.TIME_FORMATTER));
}

if (dataType.equals(DataType.TIMESTAMP)) {
return TimestampColumn.of(
columnName, LocalDateTime.parse(value, Constants.TIMESTAMP_FORMATTER));
}

if (dataType.equals(DataType.TIMESTAMPTZ)) {
return TimestampTZColumn.of(
columnName, Constants.TIMESTAMPTZ_FORMATTER.parse(value, Instant::from));
}
} catch (DateTimeParseException e) {
throw new ContractContextException(Constants.INVALID_PUT_MUTABLE_FUNCTION_ARGUMENT_FORMAT);
}

throw new ContractContextException(Constants.INVALID_PUT_MUTABLE_FUNCTION_ARGUMENT_FORMAT);
}

Expand Down Expand Up @@ -227,6 +272,22 @@ private Column<?> createNullColumn(String columnName, DataType dataType) {
return BlobColumn.ofNull(columnName);
}

if (dataType.equals(DataType.DATE)) {
return DateColumn.ofNull(columnName);
}

if (dataType.equals(DataType.TIME)) {
return TimeColumn.ofNull(columnName);
}

if (dataType.equals(DataType.TIMESTAMP)) {
return TimestampColumn.ofNull(columnName);
}

if (dataType.equals(DataType.TIMESTAMPTZ)) {
return TimestampTZColumn.ofNull(columnName);
}

throw new ContractContextException(Constants.INVALID_PUT_MUTABLE_FUNCTION_ARGUMENT_FORMAT);
}

Expand All @@ -246,6 +307,14 @@ private DataType getDataType(String dataType) {
return DataType.TEXT;
case "BLOB":
return DataType.BLOB;
case "DATE":
return DataType.DATE;
case "TIME":
return DataType.TIME;
case "TIMESTAMP":
return DataType.TIMESTAMP;
case "TIMESTAMPTZ":
return DataType.TIMESTAMPTZ;
default:
throw new ContractContextException(Constants.INVALID_PUT_MUTABLE_FUNCTION_ARGUMENT_FORMAT);
}
Expand Down
Loading