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

Use the new utility methods/constructors #268

Merged
merged 1 commit into from
Aug 10, 2021
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
import com.scalar.db.api.Scanner;
import com.scalar.db.config.DatabaseConfig;
import com.scalar.db.exception.storage.ExecutionException;
import com.scalar.db.io.IntValue;
import com.scalar.db.io.Key;
import com.scalar.db.io.TextValue;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
Expand Down Expand Up @@ -99,8 +97,8 @@ public void get_GetWithReservedKeywordProjection_ShouldReturnWhatsPut()
int pKey = 0;
int cKey = 0;
String status = "s0";
Key partitionKey = new Key(new IntValue(COL_NAME1, pKey));
Key clusteringKey = new Key(new IntValue(COL_NAME2, cKey));
Key partitionKey = new Key(COL_NAME1, pKey);
Key clusteringKey = new Key(COL_NAME2, cKey);
dynamo.put(new Put(partitionKey, clusteringKey).withValue(COL_NAME4, status));

// Act
Expand All @@ -121,15 +119,15 @@ public void get_getWithSecondaryIndexWithReservedKeywordProjection_ShouldReturnW
int cKey = 0;
String status = "s0";
String col3Value = "value3";
Key partitionKey = new Key(new IntValue(COL_NAME1, pKey));
Key clusteringKey = new Key(new IntValue(COL_NAME2, cKey));
Key partitionKey = new Key(COL_NAME1, pKey);
Key clusteringKey = new Key(COL_NAME2, cKey);
dynamo.put(
new Put(partitionKey, clusteringKey)
.withValue(COL_NAME3, col3Value)
.withValue(COL_NAME4, status));

// Act
Get get = new Get(new Key(new TextValue(COL_NAME3, col3Value))).withProjection(COL_NAME4);
Get get = new Get(new Key(COL_NAME3, col3Value)).withProjection(COL_NAME4);
Optional<Result> result = dynamo.get(get);

// Assert
Expand All @@ -142,9 +140,9 @@ public void get_getWithSecondaryIndexWithReservedKeywordProjection_ShouldReturnW
public void scan_ScanWithReservedKeywordProjection_ShouldReturnWhatsPut()
throws ExecutionException, IOException {
// Arrange
Key partitionKey = new Key(new IntValue(COL_NAME1, 0));
Key partitionKey = new Key(COL_NAME1, 0);
for (int cKey = 0; cKey < 3; cKey++) {
Key clusteringKey = new Key(new IntValue(COL_NAME2, cKey));
Key clusteringKey = new Key(COL_NAME2, cKey);
String status = "s" + cKey;
dynamo.put(new Put(partitionKey, clusteringKey).withValue(COL_NAME4, status));
}
Expand All @@ -170,10 +168,10 @@ public void scan_ScanWithReservedKeywordProjection_ShouldReturnWhatsPut()
public void scan_ScanWithSecondaryIndexWithReservedKeywordProjection_ShouldReturnWhatsPut()
throws ExecutionException, IOException {
// Arrange
Key partitionKey = new Key(new IntValue(COL_NAME1, 0));
Key partitionKey = new Key(COL_NAME1, 0);
String col3Value = "value3";
for (int cKey = 0; cKey < 3; cKey++) {
Key clusteringKey = new Key(new IntValue(COL_NAME2, cKey));
Key clusteringKey = new Key(COL_NAME2, cKey);
String status = "s" + cKey;
dynamo.put(
new Put(partitionKey, clusteringKey)
Expand All @@ -182,7 +180,7 @@ public void scan_ScanWithSecondaryIndexWithReservedKeywordProjection_ShouldRetur
}

// Act
Scan scan = new Scan(new Key(new TextValue(COL_NAME3, col3Value))).withProjection(COL_NAME4);
Scan scan = new Scan(new Key(COL_NAME3, col3Value)).withProjection(COL_NAME4);
List<Result> results;
try (Scanner scanner = dynamo.scan(scan)) {
results = scanner.all();
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void putAndCommit_PutGivenForExistingAfterRead_ShouldUpdateRecord()

// Act
Optional<Result> result = transaction.get(get);
int afterBalance = ((IntValue) result.get().getValue(BALANCE).get()).get() + 100;
int afterBalance = result.get().getValue(BALANCE).get().getAsInt() + 100;
IntValue expected = new IntValue(BALANCE, afterBalance);
Put put = preparePut(0, 0, NAMESPACE, TABLE).withValue(expected);
transaction.put(put);
Expand Down Expand Up @@ -205,9 +205,9 @@ private JdbcTransaction prepareTransfer(int fromId, int toId, int amount)
Optional<Result> result1 = transaction.get(gets.get(fromId));
Optional<Result> result2 = transaction.get(gets.get(toId));
IntValue fromBalance =
new IntValue(BALANCE, ((IntValue) result1.get().getValue(BALANCE).get()).get() - amount);
new IntValue(BALANCE, result1.get().getValue(BALANCE).get().getAsInt() - amount);
IntValue toBalance =
new IntValue(BALANCE, ((IntValue) result2.get().getValue(BALANCE).get()).get() + amount);
new IntValue(BALANCE, result2.get().getValue(BALANCE).get().getAsInt() + amount);
List<Put> puts = preparePuts(NAMESPACE, TABLE);
puts.get(fromId).withValue(fromBalance);
puts.get(toId).withValue(toBalance);
Expand All @@ -224,13 +224,13 @@ private void populateRecords() throws TransactionException {
IntStream.range(0, NUM_TYPES)
.forEach(
j -> {
Key partitionKey = new Key(new IntValue(ACCOUNT_ID, i));
Key clusteringKey = new Key(new IntValue(ACCOUNT_TYPE, j));
Key partitionKey = new Key(ACCOUNT_ID, i);
Key clusteringKey = new Key(ACCOUNT_TYPE, j);
Put put =
new Put(partitionKey, clusteringKey)
.forNamespace(NAMESPACE)
.forTable(TABLE)
.withValue(new IntValue(BALANCE, INITIAL_BALANCE));
.withValue(BALANCE, INITIAL_BALANCE);
try {
transaction.put(put);
} catch (CrudException e) {
Expand All @@ -241,8 +241,8 @@ private void populateRecords() throws TransactionException {
}

private Get prepareGet(int id, int type, String namespace, String table) {
Key partitionKey = new Key(new IntValue(ACCOUNT_ID, id));
Key clusteringKey = new Key(new IntValue(ACCOUNT_TYPE, type));
Key partitionKey = new Key(ACCOUNT_ID, id);
Key clusteringKey = new Key(ACCOUNT_TYPE, type);
return new Get(partitionKey, clusteringKey)
.forNamespace(namespace)
.forTable(table)
Expand All @@ -260,18 +260,18 @@ private List<Get> prepareGets(String namespace, String table) {
}

private Scan prepareScan(int id, int fromType, int toType, String namespace, String table) {
Key partitionKey = new Key(new IntValue(ACCOUNT_ID, id));
Key partitionKey = new Key(ACCOUNT_ID, id);
return new Scan(partitionKey)
.forNamespace(namespace)
.forTable(table)
.withConsistency(Consistency.LINEARIZABLE)
.withStart(new Key(new IntValue(ACCOUNT_TYPE, fromType)))
.withEnd(new Key(new IntValue(ACCOUNT_TYPE, toType)));
.withStart(new Key(ACCOUNT_TYPE, fromType))
.withEnd(new Key(ACCOUNT_TYPE, toType));
}

private Put preparePut(int id, int type, String namespace, String table) {
Key partitionKey = new Key(new IntValue(ACCOUNT_ID, id));
Key clusteringKey = new Key(new IntValue(ACCOUNT_TYPE, type));
Key partitionKey = new Key(ACCOUNT_ID, id);
Key clusteringKey = new Key(ACCOUNT_TYPE, type);
return new Put(partitionKey, clusteringKey)
.forNamespace(namespace)
.forTable(table)
Expand All @@ -289,8 +289,8 @@ private List<Put> preparePuts(String namespace, String table) {
}

private Delete prepareDelete(int id, int type, String namespace, String table) {
Key partitionKey = new Key(new IntValue(ACCOUNT_ID, id));
Key clusteringKey = new Key(new IntValue(ACCOUNT_TYPE, type));
Key partitionKey = new Key(ACCOUNT_ID, id);
Key clusteringKey = new Key(ACCOUNT_TYPE, type);
return new Delete(partitionKey, clusteringKey)
.forNamespace(namespace)
.forTable(table)
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/com/scalar/db/api/DistributedStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
*
* // Inserts a new entry which has the primary key value 0 to the storage.
* // Assumes that the primary key is composed of a integer value named COL_NAME.
* Put put = new Put(new Key(new IntValue(COL_NAME, 0));
* Put put = new Put(new Key(COL_NAME, 0);
* storage.put(put);
*
* // Retrieves the entry from the storage.
* Get get = new Get(new Key(new IntValue(COL_NAME, 0));
* Get get = new Get(new Key(COL_NAME, 0);
* Optional<Result> result = storage.get(get);
*
* // Deletes an entry which has the primary key value 1.
* Delete delete = new Delete(new Key(new IntValue(COL_NAME, 1));
* Delete delete = new Delete(new Key(COL_NAME, 1);
* storage.delete(delete);
* }</pre>
*
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/com/scalar/db/io/TextValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public Optional<String> get() {
* Returns the content of this {@code Value}
*
* @return an {@code Optional} of the content of this {@code Value} in byte array
* @deprecated As of release 3.2.0, replaced by {@link #get()}. Will be removed in release 4.0.0.
* @deprecated As of release 3.2.0, replaced by {@link #getAsBytes()}. Will be removed in release
* 4.0.0.
*/
@SuppressWarnings("InlineMeSuggester")
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public void visit(DoubleValue value) {
*/
@Override
public void visit(TextValue value) {
LOGGER.debug(value.getString() + " is bound to " + i);
value.getString().ifPresent(s -> bound.setString(i, s));
LOGGER.debug(value.get() + " is bound to " + i);
value.get().ifPresent(s -> bound.setString(i, s));
i++;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import com.scalar.db.api.TableMetadata;
import com.scalar.db.io.Key;
import com.scalar.db.io.Value;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -38,16 +36,16 @@ public Optional<Key> getClusteringKey() {
}

private Optional<Key> getKey(LinkedHashSet<String> names) {
List<Value<?>> list = new ArrayList<>();
Key.Builder builder = Key.newBuilder();
for (String name : names) {
Value<?> value = values.get(name);
if (value == null) {
LOGGER.warn("full key doesn't seem to be projected into the result");
return Optional.empty();
}
list.add(value);
builder.add(value);
}
return Optional.of(new Key(list));
return Optional.of(builder.build());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void visit(DoubleValue value) {
*/
@Override
public void visit(TextValue value) {
value.getString().ifPresent(s -> values.add(s));
value.get().ifPresent(s -> values.add(s));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void visit(DoubleValue value) {
*/
@Override
public void visit(TextValue value) {
value.getString().ifPresent(s -> values.put(value.getName(), s));
value.get().ifPresent(s -> values.put(value.getName(), s));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void visit(DoubleValue value) {
*/
@Override
public void visit(TextValue value) {
value.getString().ifPresent(s -> consumer.accept(s));
value.get().ifPresent(s -> consumer.accept(s));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ public void visit(DoubleValue value) {
*/
@Override
public void visit(TextValue value) {
value
.getString()
.ifPresent(s -> values.put(value.getName(), AttributeValue.builder().s(s).build()));
value.get().ifPresent(s -> values.put(value.getName(), AttributeValue.builder().s(s).build()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public void visit(DoubleValue value) {
@Override
public void visit(TextValue value) {
AttributeValue.Builder builder = AttributeValue.builder();
if (value.getString().isPresent()) {
builder.s(value.getString().get());
if (value.get().isPresent()) {
builder.s(value.get().get());
} else {
builder.nul(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void visit(DoubleValue value) {
@Override
public void visit(TextValue value) {
try {
preparedStatement.setString(index++, value.getString().orElse(null));
preparedStatement.setString(index++, value.get().orElse(null));
} catch (SQLException e) {
sqlException = e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
import com.scalar.db.exception.storage.NoMutationException;
import com.scalar.db.exception.transaction.CoordinatorException;
import com.scalar.db.exception.transaction.RequiredValueMissingException;
import com.scalar.db.io.BigIntValue;
import com.scalar.db.io.IntValue;
import com.scalar.db.io.Key;
import com.scalar.db.io.TextValue;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -88,7 +85,7 @@ Put createPutWith(Coordinator.State state) {
.getMetadata()
.ifPresent(
m -> {
put.withValue(new TextValue(Attribute.METADATA, m));
put.withValue(Attribute.METADATA, m);
});
return put;
}
Expand Down Expand Up @@ -126,10 +123,9 @@ public static class State {

public State(Result result) {
checkNotMissingRequired(result);
id = ((TextValue) result.getValue(Attribute.ID).get()).getString().get();
state =
TransactionState.getInstance(((IntValue) result.getValue(Attribute.STATE).get()).get());
createdAt = ((BigIntValue) result.getValue(Attribute.CREATED_AT).get()).get();
id = result.getValue(Attribute.ID).get().getAsString().get();
state = TransactionState.getInstance(result.getValue(Attribute.STATE).get().getAsInt());
createdAt = result.getValue(Attribute.CREATED_AT).get().getAsLong();
}

public State(String id, TransactionState state) {
Expand Down Expand Up @@ -187,15 +183,15 @@ public int hashCode() {

private void checkNotMissingRequired(Result result) {
if (!result.getValue(Attribute.ID).isPresent()
|| !((TextValue) result.getValue(Attribute.ID).get()).getString().isPresent()) {
|| !result.getValue(Attribute.ID).get().getAsString().isPresent()) {
throw new RequiredValueMissingException("id is missing in the coordinator state");
}
if (!result.getValue(Attribute.STATE).isPresent()
|| ((IntValue) result.getValue(Attribute.STATE).get()).get() == 0) {
|| result.getValue(Attribute.STATE).get().getAsInt() == 0) {
throw new RequiredValueMissingException("state is missing in the coordinator state");
}
if (!result.getValue(Attribute.CREATED_AT).isPresent()
|| ((BigIntValue) result.getValue(Attribute.CREATED_AT).get()).get() == 0) {
|| result.getValue(Attribute.CREATED_AT).get().getAsLong() == 0) {
throw new RequiredValueMissingException("created_at is missing in the coordinator state");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void add(Operation base, TransactionResult result) {
}

TextValue beforeId = (TextValue) latest.getValue(Attribute.BEFORE_ID).get();
if (beforeId.getString().isPresent()) {
if (beforeId.get().isPresent()) {
mutations.add(composePut(base, latest));
} else {
// no record to rollback, so it should be deleted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import com.google.common.collect.ImmutableMap;
import com.scalar.db.api.Result;
import com.scalar.db.api.TransactionState;
import com.scalar.db.io.BigIntValue;
import com.scalar.db.io.IntValue;
import com.scalar.db.io.Key;
import com.scalar.db.io.TextValue;
import com.scalar.db.io.Value;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -81,23 +78,23 @@ public String toString() {
}

public String getId() {
return ((TextValue) getValue(Attribute.ID).get()).getString().get();
return getValue(Attribute.ID).get().getAsString().get();
}

public TransactionState getState() {
return TransactionState.getInstance(((IntValue) getValue(Attribute.STATE).get()).get());
return TransactionState.getInstance(getValue(Attribute.STATE).get().getAsInt());
}

public int getVersion() {
return ((IntValue) getValue(Attribute.VERSION).get()).get();
return getValue(Attribute.VERSION).get().getAsInt();
}

public long getPreparedAt() {
return ((BigIntValue) getValue(Attribute.PREPARED_AT).get()).get();
return getValue(Attribute.PREPARED_AT).get().getAsLong();
}

public long getCommittedAt() {
return ((BigIntValue) getValue(Attribute.COMMITTED_AT).get()).get();
return getValue(Attribute.COMMITTED_AT).get().getAsLong();
}

public boolean isCommitted() {
Expand Down
Loading