use of com.scalar.db.api.Put in project scalardb by scalar-labs.
the class StorageIntegrationTestBase method put_PutWithIfExistsGivenWhenSuchRecordExists_ShouldUpdateRecord.
@Test
public void put_PutWithIfExistsGivenWhenSuchRecordExists_ShouldUpdateRecord() throws ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
Get get = prepareGet(pKey, cKey);
// Act Assert
storage.put(puts.get(0));
puts.get(0).withCondition(new PutIfExists());
puts.get(0).withValue(COL_NAME3, Integer.MAX_VALUE);
assertThatCode(() -> storage.put(puts.get(0))).doesNotThrowAnyException();
// Assert
Optional<Result> actual = storage.get(get);
assertThat(actual.isPresent()).isTrue();
Result result = actual.get();
assertThat(result.getValue(COL_NAME1)).isEqualTo(Optional.of(new IntValue(COL_NAME1, pKey)));
assertThat(result.getValue(COL_NAME4)).isEqualTo(Optional.of(new IntValue(COL_NAME4, cKey)));
assertThat(result.getValue(COL_NAME3)).isEqualTo(Optional.of(new IntValue(COL_NAME3, Integer.MAX_VALUE)));
}
use of com.scalar.db.api.Put in project scalardb by scalar-labs.
the class StorageIntegrationTestBase method put_PutWithIfGivenWhenNoSuchRecord_ShouldThrowNoMutationException.
@Test
public void put_PutWithIfGivenWhenNoSuchRecord_ShouldThrowNoMutationException() throws ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
Get get = prepareGet(pKey, cKey);
// Act Assert
storage.put(puts.get(0));
puts.get(0).withCondition(new PutIf(new ConditionalExpression(COL_NAME3, new IntValue(pKey + cKey + 1), ConditionalExpression.Operator.EQ)));
puts.get(0).withValue(COL_NAME3, Integer.MAX_VALUE);
assertThatThrownBy(() -> storage.put(puts.get(0))).isInstanceOf(NoMutationException.class);
// Assert
Optional<Result> actual = storage.get(get);
assertThat(actual.isPresent()).isTrue();
Result result = actual.get();
assertThat(result.getValue(COL_NAME1)).isEqualTo(Optional.of(new IntValue(COL_NAME1, pKey)));
assertThat(result.getValue(COL_NAME4)).isEqualTo(Optional.of(new IntValue(COL_NAME4, cKey)));
assertThat(result.getValue(COL_NAME3)).isEqualTo(Optional.of(new IntValue(COL_NAME3, pKey + cKey)));
}
use of com.scalar.db.api.Put in project scalardb by scalar-labs.
the class StorageSingleClusteringKeyScanIntegrationTestBase method prepareRecords.
private List<Value<?>> prepareRecords(DataType clusteringKeyType, Order clusteringOrder) throws ExecutionException {
RANDOM.setSeed(seed);
List<Value<?>> ret = new ArrayList<>();
List<Put> puts = new ArrayList<>();
if (clusteringKeyType == DataType.BOOLEAN) {
TestUtils.booleanValues(CLUSTERING_KEY).forEach(clusteringKeyValue -> {
ret.add(clusteringKeyValue);
puts.add(preparePut(clusteringKeyType, clusteringOrder, clusteringKeyValue));
});
} else {
Set<Value<?>> valueSet = new HashSet<>();
// Add min and max clustering key values
Arrays.asList(getMinValue(CLUSTERING_KEY, clusteringKeyType), getMaxValue(CLUSTERING_KEY, clusteringKeyType)).forEach(clusteringKeyValue -> {
valueSet.add(clusteringKeyValue);
ret.add(clusteringKeyValue);
puts.add(preparePut(clusteringKeyType, clusteringOrder, clusteringKeyValue));
});
IntStream.range(0, CLUSTERING_KEY_NUM - 2).forEach(i -> {
Value<?> clusteringKeyValue;
while (true) {
clusteringKeyValue = getRandomValue(RANDOM, CLUSTERING_KEY, clusteringKeyType);
// reject duplication
if (!valueSet.contains(clusteringKeyValue)) {
valueSet.add(clusteringKeyValue);
break;
}
}
ret.add(clusteringKeyValue);
puts.add(preparePut(clusteringKeyType, clusteringOrder, clusteringKeyValue));
});
}
try {
List<Put> buffer = new ArrayList<>();
for (Put put : puts) {
buffer.add(put);
if (buffer.size() == 20) {
storage.mutate(buffer);
buffer.clear();
}
}
if (!buffer.isEmpty()) {
storage.mutate(buffer);
}
} catch (ExecutionException e) {
throw new ExecutionException("put data to database failed", e);
}
ret.sort(clusteringOrder == Order.ASC ? com.google.common.collect.Ordering.natural() : com.google.common.collect.Ordering.natural().reverse());
return ret;
}
use of com.scalar.db.api.Put in project scalardb by scalar-labs.
the class AdminIntegrationTestBase method truncateTable_ShouldTruncateProperly.
@Test
public void truncateTable_ShouldTruncateProperly() throws ExecutionException, IOException {
// Arrange
Key partitionKey = new Key(COL_NAME2, "aaa", COL_NAME1, 1);
Key clusteringKey = new Key(COL_NAME4, 2, COL_NAME3, "bbb");
storage.put(new Put(partitionKey, clusteringKey).withValue(COL_NAME5, 3).withValue(COL_NAME6, "ccc").withValue(COL_NAME7, 4L).withValue(COL_NAME8, 1.0f).withValue(COL_NAME9, 1.0d).withValue(COL_NAME10, true).withValue(COL_NAME11, "ddd".getBytes(StandardCharsets.UTF_8)).forNamespace(namespace1).forTable(TABLE1));
// Act
admin.truncateTable(namespace1, TABLE1);
// Assert
Scanner scanner = storage.scan(new Scan(partitionKey).forNamespace(namespace1).forTable(TABLE1));
assertThat(scanner.all()).isEmpty();
scanner.close();
}
use of com.scalar.db.api.Put in project scalardb by scalar-labs.
the class StorageColumnValueIntegrationTestBase method put_WithoutValues_ShouldPutCorrectly.
@Test
public void put_WithoutValues_ShouldPutCorrectly() throws ExecutionException {
// Arrange
IntValue partitionKeyValue = new IntValue(PARTITION_KEY, 1);
BooleanValue col1Value = new BooleanValue(COL_NAME1, false);
IntValue col2Value = new IntValue(COL_NAME2, 0);
BigIntValue col3Value = new BigIntValue(COL_NAME3, 0L);
FloatValue col4Value = new FloatValue(COL_NAME4, 0.0f);
DoubleValue col5Value = new DoubleValue(COL_NAME5, 0.0d);
TextValue col6Value = new TextValue(COL_NAME6, (String) null);
BlobValue col7Value = new BlobValue(COL_NAME7, (byte[]) null);
Put put = new Put(new Key(partitionKeyValue)).forNamespace(namespace).forTable(TABLE);
// Act
storage.put(put);
// Assert
Optional<Result> actual = storage.get(new Get(new Key(partitionKeyValue)).forNamespace(namespace).forTable(TABLE));
assertThat(actual).isPresent();
assertThat(actual.get().getValue(PARTITION_KEY).isPresent()).isTrue();
assertThat(actual.get().getValue(PARTITION_KEY).get()).isEqualTo(partitionKeyValue);
assertThat(actual.get().getValue(COL_NAME1).isPresent()).isTrue();
assertThat(actual.get().getValue(COL_NAME1).get()).isEqualTo(col1Value);
assertThat(actual.get().getValue(COL_NAME2).isPresent()).isTrue();
assertThat(actual.get().getValue(COL_NAME2).get()).isEqualTo(col2Value);
assertThat(actual.get().getValue(COL_NAME3).isPresent()).isTrue();
assertThat(actual.get().getValue(COL_NAME3).get()).isEqualTo(col3Value);
assertThat(actual.get().getValue(COL_NAME4).isPresent()).isTrue();
assertThat(actual.get().getValue(COL_NAME4).get()).isEqualTo(col4Value);
assertThat(actual.get().getValue(COL_NAME5).isPresent()).isTrue();
assertThat(actual.get().getValue(COL_NAME5).get()).isEqualTo(col5Value);
assertThat(actual.get().getValue(COL_NAME6).isPresent()).isTrue();
assertThat(actual.get().getValue(COL_NAME6).get()).isEqualTo(col6Value);
assertThat(actual.get().getValue(COL_NAME7).isPresent()).isTrue();
assertThat(actual.get().getValue(COL_NAME7).get()).isEqualTo(col7Value);
assertThat(actual.get().getContainedColumnNames()).isEqualTo(new HashSet<>(Arrays.asList(PARTITION_KEY, COL_NAME1, COL_NAME2, COL_NAME3, COL_NAME4, COL_NAME5, COL_NAME6, COL_NAME7)));
assertThat(actual.get().contains(PARTITION_KEY)).isTrue();
assertThat(actual.get().isNull(PARTITION_KEY)).isFalse();
assertThat(actual.get().getInt(PARTITION_KEY)).isEqualTo(partitionKeyValue.get());
assertThat(actual.get().getAsObject(PARTITION_KEY)).isEqualTo(partitionKeyValue.get());
assertThat(actual.get().contains(COL_NAME1)).isTrue();
assertThat(actual.get().isNull(COL_NAME1)).isTrue();
assertThat(actual.get().getBoolean(COL_NAME1)).isEqualTo(col1Value.get());
assertThat(actual.get().getAsObject(COL_NAME1)).isNull();
assertThat(actual.get().contains(COL_NAME2)).isTrue();
assertThat(actual.get().isNull(COL_NAME2)).isTrue();
assertThat(actual.get().getInt(COL_NAME2)).isEqualTo(col2Value.get());
assertThat(actual.get().getAsObject(COL_NAME2)).isNull();
assertThat(actual.get().contains(COL_NAME3)).isTrue();
assertThat(actual.get().isNull(COL_NAME3)).isTrue();
assertThat(actual.get().getBigInt(COL_NAME3)).isEqualTo(col3Value.get());
assertThat(actual.get().getAsObject(COL_NAME3)).isNull();
assertThat(actual.get().contains(COL_NAME4)).isTrue();
assertThat(actual.get().isNull(COL_NAME4)).isTrue();
assertThat(actual.get().getFloat(COL_NAME4)).isEqualTo(col4Value.get());
assertThat(actual.get().getAsObject(COL_NAME4)).isNull();
assertThat(actual.get().contains(COL_NAME5)).isTrue();
assertThat(actual.get().isNull(COL_NAME5)).isTrue();
assertThat(actual.get().getDouble(COL_NAME5)).isEqualTo(col5Value.get());
assertThat(actual.get().getAsObject(COL_NAME5)).isNull();
assertThat(actual.get().contains(COL_NAME6)).isTrue();
assertThat(actual.get().isNull(COL_NAME6)).isTrue();
assertThat(actual.get().getText(COL_NAME6)).isNull();
assertThat(actual.get().getAsObject(COL_NAME6)).isNull();
assertThat(actual.get().contains(COL_NAME7)).isTrue();
assertThat(actual.get().isNull(COL_NAME7)).isTrue();
assertThat(actual.get().getBlob(COL_NAME7)).isNull();
assertThat(actual.get().getBlobAsByteBuffer(COL_NAME7)).isNull();
assertThat(actual.get().getBlobAsBytes(COL_NAME7)).isNull();
assertThat(actual.get().getAsObject(COL_NAME7)).isNull();
}
Aggregations