use of com.scalar.db.io.Value in project scalardb by scalar-labs.
the class DistributedStorageSinglePartitionKeyIntegrationTestBase method prepareRecords.
private List<Value<?>> prepareRecords(DataType partitionKeyType) throws ExecutionException {
RANDOM.setSeed(seed);
List<Value<?>> ret = new ArrayList<>();
List<Put> puts = new ArrayList<>();
if (partitionKeyType == DataType.BOOLEAN) {
TestUtils.booleanValues(PARTITION_KEY).forEach(partitionKeyValue -> {
ret.add(partitionKeyValue);
puts.add(preparePut(partitionKeyType, partitionKeyValue));
});
} else {
Set<Value<?>> valueSet = new HashSet<>();
// Add min and max partition key values
Arrays.asList(getMinValue(PARTITION_KEY, partitionKeyType), getMaxValue(PARTITION_KEY, partitionKeyType)).forEach(partitionKeyValue -> {
valueSet.add(partitionKeyValue);
ret.add(partitionKeyValue);
puts.add(preparePut(partitionKeyType, partitionKeyValue));
});
IntStream.range(0, PARTITION_KEY_NUM - 2).forEach(i -> {
Value<?> partitionKeyValue;
while (true) {
partitionKeyValue = getRandomValue(RANDOM, PARTITION_KEY, partitionKeyType);
// reject duplication
if (!valueSet.contains(partitionKeyValue)) {
valueSet.add(partitionKeyValue);
break;
}
}
ret.add(partitionKeyValue);
puts.add(preparePut(partitionKeyType, partitionKeyValue));
});
}
try {
for (Put put : puts) {
storage.put(put);
}
} catch (ExecutionException e) {
throw new ExecutionException("put data to database failed", e);
}
return ret;
}
use of com.scalar.db.io.Value in project scalardb by scalar-labs.
the class ResultInterpreterTest method interpret_ShouldReturnWhatsSet.
@Test
public void interpret_ShouldReturnWhatsSet() {
// Arrange
when(row.getString(ANY_NAME_1)).thenReturn(ANY_TEXT_1);
when(row.getString(ANY_NAME_2)).thenReturn(ANY_TEXT_2);
when(row.getBool(ANY_COLUMN_NAME_1)).thenReturn(true);
when(row.getInt(ANY_COLUMN_NAME_2)).thenReturn(Integer.MAX_VALUE);
when(row.getLong(ANY_COLUMN_NAME_3)).thenReturn(BigIntValue.MAX_VALUE);
when(row.getFloat(ANY_COLUMN_NAME_4)).thenReturn(Float.MAX_VALUE);
when(row.getDouble(ANY_COLUMN_NAME_5)).thenReturn(Double.MAX_VALUE);
when(row.getString(ANY_COLUMN_NAME_6)).thenReturn("string");
byte[] bytesValue = "bytes".getBytes(StandardCharsets.UTF_8);
when(row.getBytes(ANY_COLUMN_NAME_7)).thenReturn((ByteBuffer) ByteBuffer.allocate(bytesValue.length).put(bytesValue).flip());
List<String> projections = Collections.emptyList();
ResultInterpreter spy = spy(new ResultInterpreter(projections, TABLE_METADATA));
// Act
Result result = spy.interpret(row);
// Assert
assertThat(result.getValue(ANY_NAME_1).isPresent()).isTrue();
assertThat(result.getValue(ANY_NAME_1).get().getAsString().isPresent()).isTrue();
assertThat(result.getValue(ANY_NAME_1).get().getAsString().get()).isEqualTo(ANY_TEXT_1);
assertThat(result.getValue(ANY_NAME_2).isPresent()).isTrue();
assertThat(result.getValue(ANY_NAME_2).get().getAsString().isPresent()).isTrue();
assertThat(result.getValue(ANY_NAME_2).get().getAsString().get()).isEqualTo(ANY_TEXT_2);
assertThat(result.getValue(ANY_COLUMN_NAME_1).isPresent()).isTrue();
assertThat(result.getValue(ANY_COLUMN_NAME_1).get().getAsBoolean()).isTrue();
assertThat(result.getValue(ANY_COLUMN_NAME_2).isPresent()).isTrue();
assertThat(result.getValue(ANY_COLUMN_NAME_2).get().getAsInt()).isEqualTo(Integer.MAX_VALUE);
assertThat(result.getValue(ANY_COLUMN_NAME_3).isPresent()).isTrue();
assertThat(result.getValue(ANY_COLUMN_NAME_3).get().getAsLong()).isEqualTo(BigIntValue.MAX_VALUE);
assertThat(result.getValue(ANY_COLUMN_NAME_4).isPresent()).isTrue();
assertThat(result.getValue(ANY_COLUMN_NAME_4).get().getAsFloat()).isEqualTo(Float.MAX_VALUE);
assertThat(result.getValue(ANY_COLUMN_NAME_5).isPresent()).isTrue();
assertThat(result.getValue(ANY_COLUMN_NAME_5).get().getAsDouble()).isEqualTo(Double.MAX_VALUE);
assertThat(result.getValue(ANY_COLUMN_NAME_6).isPresent()).isTrue();
assertThat(result.getValue(ANY_COLUMN_NAME_6).get().getAsString().isPresent()).isTrue();
assertThat(result.getValue(ANY_COLUMN_NAME_6).get().getAsString().get()).isEqualTo("string");
assertThat(result.getValue(ANY_COLUMN_NAME_7).isPresent()).isTrue();
assertThat(result.getValue(ANY_COLUMN_NAME_7).get().getAsBytes().isPresent()).isTrue();
assertThat(result.getValue(ANY_COLUMN_NAME_7).get().getAsBytes().get()).isEqualTo("bytes".getBytes(StandardCharsets.UTF_8));
Map<String, Value<?>> values = result.getValues();
assertThat(values.containsKey(ANY_NAME_1)).isTrue();
assertThat(values.get(ANY_NAME_1).getAsString().isPresent()).isTrue();
assertThat(values.get(ANY_NAME_1).getAsString().get()).isEqualTo(ANY_TEXT_1);
assertThat(values.containsKey(ANY_NAME_2)).isTrue();
assertThat(values.get(ANY_NAME_2).getAsString().isPresent()).isTrue();
assertThat(values.get(ANY_NAME_2).getAsString().get()).isEqualTo(ANY_TEXT_2);
assertThat(values.containsKey(ANY_COLUMN_NAME_1)).isTrue();
assertThat(values.get(ANY_COLUMN_NAME_1).getAsBoolean()).isTrue();
assertThat(values.containsKey(ANY_COLUMN_NAME_2)).isTrue();
assertThat(values.get(ANY_COLUMN_NAME_2).getAsInt()).isEqualTo(Integer.MAX_VALUE);
assertThat(values.containsKey(ANY_COLUMN_NAME_3)).isTrue();
assertThat(values.get(ANY_COLUMN_NAME_3).getAsLong()).isEqualTo(BigIntValue.MAX_VALUE);
assertThat(values.containsKey(ANY_COLUMN_NAME_4)).isTrue();
assertThat(values.get(ANY_COLUMN_NAME_4).getAsFloat()).isEqualTo(Float.MAX_VALUE);
assertThat(values.containsKey(ANY_COLUMN_NAME_5)).isTrue();
assertThat(values.get(ANY_COLUMN_NAME_5).getAsDouble()).isEqualTo(Double.MAX_VALUE);
assertThat(values.containsKey(ANY_COLUMN_NAME_6)).isTrue();
assertThat(values.get(ANY_COLUMN_NAME_6).getAsString().isPresent()).isTrue();
assertThat(values.get(ANY_COLUMN_NAME_6).getAsString().get()).isEqualTo("string");
assertThat(values.containsKey(ANY_COLUMN_NAME_7)).isTrue();
assertThat(values.get(ANY_COLUMN_NAME_7).getAsBytes().isPresent()).isTrue();
assertThat(values.get(ANY_COLUMN_NAME_7).getAsBytes().get()).isEqualTo("bytes".getBytes(StandardCharsets.UTF_8));
assertThat(result.isNull(ANY_NAME_1)).isFalse();
assertThat(result.getText(ANY_NAME_1)).isEqualTo(ANY_TEXT_1);
assertThat(result.isNull(ANY_NAME_2)).isFalse();
assertThat(result.getText(ANY_NAME_2)).isEqualTo(ANY_TEXT_2);
assertThat(result.isNull(ANY_COLUMN_NAME_1)).isFalse();
assertThat(result.getBoolean(ANY_COLUMN_NAME_1)).isTrue();
assertThat(result.isNull(ANY_COLUMN_NAME_2)).isFalse();
assertThat(result.getInt(ANY_COLUMN_NAME_2)).isEqualTo(Integer.MAX_VALUE);
assertThat(result.isNull(ANY_COLUMN_NAME_3)).isFalse();
assertThat(result.getBigInt(ANY_COLUMN_NAME_3)).isEqualTo(BigIntValue.MAX_VALUE);
assertThat(result.isNull(ANY_COLUMN_NAME_4)).isFalse();
assertThat(result.getFloat(ANY_COLUMN_NAME_4)).isEqualTo(Float.MAX_VALUE);
assertThat(result.isNull(ANY_COLUMN_NAME_5)).isFalse();
assertThat(result.getDouble(ANY_COLUMN_NAME_5)).isEqualTo(Double.MAX_VALUE);
assertThat(result.isNull(ANY_COLUMN_NAME_6)).isFalse();
assertThat(result.getText(ANY_COLUMN_NAME_6)).isEqualTo("string");
assertThat(result.isNull(ANY_COLUMN_NAME_7)).isFalse();
assertThat(result.getBlob(ANY_COLUMN_NAME_7)).isEqualTo(ByteBuffer.wrap("bytes".getBytes(StandardCharsets.UTF_8)));
assertThat(result.getBlobAsBytes(ANY_COLUMN_NAME_7)).isEqualTo("bytes".getBytes(StandardCharsets.UTF_8));
}
use of com.scalar.db.io.Value in project scalardb by scalar-labs.
the class OperationCheckerTest method whenCheckingPutOperationWithInvalidPartitionKey_shouldThrowIllegalArgumentException.
@Test
public void whenCheckingPutOperationWithInvalidPartitionKey_shouldThrowIllegalArgumentException() {
// Arrange
Key partitionKey = new Key(PKEY1, 1, "c3", "val1");
Key clusteringKey = new Key(CKEY1, 2, CKEY2, "val1");
List<Value<?>> values = Arrays.asList(new IntValue(COL1, 1), new DoubleValue(COL2, 0.1), new BooleanValue(COL3, true));
MutationCondition condition = new PutIfExists();
Put put = new Put(partitionKey, clusteringKey).withValues(values).withCondition(condition).forNamespace(NAMESPACE).forTable(TABLE_NAME);
// Act Assert
assertThatThrownBy(() -> operationChecker.check(put)).isInstanceOf(IllegalArgumentException.class);
}
use of com.scalar.db.io.Value in project scalardb by scalar-labs.
the class OperationCheckerTest method whenCheckingPutOperationWithInvalidPutIfCondition_shouldThrowIllegalArgumentException.
@Test
public void whenCheckingPutOperationWithInvalidPutIfCondition_shouldThrowIllegalArgumentException() {
// Arrange
Key partitionKey = new Key(PKEY1, 1, PKEY2, "val1");
Key clusteringKey = new Key(CKEY1, 2, CKEY2, "val1");
List<Value<?>> values = Arrays.asList(new IntValue(COL1, 1), new DoubleValue(COL2, 0.1), new BooleanValue(COL3, true));
MutationCondition condition = new PutIf(new ConditionalExpression(COL1, new TextValue("1"), ConditionalExpression.Operator.EQ));
Put put = new Put(partitionKey, clusteringKey).withValues(values).withCondition(condition).forNamespace(NAMESPACE).forTable(TABLE_NAME);
// Act Assert
assertThatThrownBy(() -> operationChecker.check(put)).isInstanceOf(IllegalArgumentException.class);
}
use of com.scalar.db.io.Value in project scalardb by scalar-labs.
the class OperationCheckerTest method whenCheckingPutOperationWithClusteringKeyWithNullBlobValue_shouldThrowIllegalArgumentException.
@Test
public void whenCheckingPutOperationWithClusteringKeyWithNullBlobValue_shouldThrowIllegalArgumentException() throws ExecutionException {
// Arrange
when(metadataManager.getTableMetadata(any())).thenReturn(TableMetadata.newBuilder().addColumn(PKEY1, DataType.BLOB).addColumn(CKEY1, DataType.BLOB).addColumn(COL1, DataType.INT).addPartitionKey(PKEY1).addClusteringKey(CKEY1).build());
operationChecker = new OperationChecker(metadataManager);
Key partitionKey = new Key(PKEY1, new byte[] { 1, 1, 1 });
Key clusteringKey = new Key(CKEY1, (byte[]) null);
List<Value<?>> values = Collections.singletonList(new IntValue(COL1, 1));
Put put = new Put(partitionKey, clusteringKey).withValues(values).forNamespace(NAMESPACE).forTable(TABLE_NAME);
// Act Assert
assertThatThrownBy(() -> operationChecker.check(put)).isInstanceOf(IllegalArgumentException.class);
}
Aggregations