Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ public void whenPutDataIntoTable1_DataShouldBeWrittenIntoCassandra() throws Exec
// Arrange
String namespace = NAMESPACE1;
String table = TABLE1;
Key partitionKey = new Key(COL_NAME1, 1);
Key clusteringKey = new Key(COL_NAME4, 4);
Key partitionKey = Key.ofInt(COL_NAME1, 1);
Key clusteringKey = Key.ofInt(COL_NAME4, 4);
Put put =
new Put(partitionKey, clusteringKey)
.withValue(COL_NAME2, "val2")
Expand Down Expand Up @@ -225,8 +225,8 @@ public void whenPutDataIntoTable2_DataShouldBeWrittenIntoJdbcDatabase()
// Arrange
String namespace = NAMESPACE1;
String table = TABLE2;
Key partitionKey = new Key(COL_NAME1, 1);
Key clusteringKey = new Key(COL_NAME4, 4);
Key partitionKey = Key.ofInt(COL_NAME1, 1);
Key clusteringKey = Key.ofInt(COL_NAME4, 4);
Put put =
new Put(partitionKey, clusteringKey)
.withValue(COL_NAME2, "val2")
Expand Down Expand Up @@ -267,8 +267,8 @@ public void whenPutDataIntoTable3_DataShouldBeWrittenIntoDefaultStorage()
// Arrange
String namespace = NAMESPACE1;
String table = TABLE3;
Key partitionKey = new Key(COL_NAME1, 1);
Key clusteringKey = new Key(COL_NAME4, 4);
Key partitionKey = Key.ofInt(COL_NAME1, 1);
Key clusteringKey = Key.ofInt(COL_NAME4, 4);

Put put =
new Put(partitionKey, clusteringKey)
Expand Down Expand Up @@ -310,10 +310,10 @@ public void whenScanDataFromTable1_DataShouldBeScannedFromCassandra()
// Arrange
String namespace = NAMESPACE1;
String table = TABLE1;
Key partitionKey = new Key(COL_NAME1, 1);
Key clusteringKey1 = new Key(COL_NAME4, 0);
Key clusteringKey2 = new Key(COL_NAME4, 1);
Key clusteringKey3 = new Key(COL_NAME4, 2);
Key partitionKey = Key.ofInt(COL_NAME1, 1);
Key clusteringKey1 = Key.ofInt(COL_NAME4, 0);
Key clusteringKey2 = Key.ofInt(COL_NAME4, 1);
Key clusteringKey3 = Key.ofInt(COL_NAME4, 2);

cassandra.mutate(
Arrays.asList(
Expand Down Expand Up @@ -352,10 +352,10 @@ public void whenScanDataFromTable2_DataShouldBeScannedFromJdbcDatabase()
// Arrange
String namespace = NAMESPACE1;
String table = TABLE2;
Key partitionKey = new Key(COL_NAME1, 1);
Key clusteringKey1 = new Key(COL_NAME4, 0);
Key clusteringKey2 = new Key(COL_NAME4, 1);
Key clusteringKey3 = new Key(COL_NAME4, 2);
Key partitionKey = Key.ofInt(COL_NAME1, 1);
Key clusteringKey1 = Key.ofInt(COL_NAME4, 0);
Key clusteringKey2 = Key.ofInt(COL_NAME4, 1);
Key clusteringKey3 = Key.ofInt(COL_NAME4, 2);

jdbcDatabase.mutate(
Arrays.asList(
Expand Down Expand Up @@ -394,10 +394,10 @@ public void whenScanDataFromTable3_DataShouldBeScannedFromDefaultStorage()
// Arrange
String namespace = NAMESPACE1;
String table = TABLE3;
Key partitionKey = new Key(COL_NAME1, 1);
Key clusteringKey1 = new Key(COL_NAME4, 0);
Key clusteringKey2 = new Key(COL_NAME4, 1);
Key clusteringKey3 = new Key(COL_NAME4, 2);
Key partitionKey = Key.ofInt(COL_NAME1, 1);
Key clusteringKey1 = Key.ofInt(COL_NAME4, 0);
Key clusteringKey2 = Key.ofInt(COL_NAME4, 1);
Key clusteringKey3 = Key.ofInt(COL_NAME4, 2);

cassandra.mutate(
Arrays.asList(
Expand Down Expand Up @@ -442,8 +442,8 @@ public void whenDeleteDataFromTable1_DataShouldBeDeletedFromCassandra()
// Arrange
String namespace = NAMESPACE1;
String table = TABLE1;
Key partitionKey = new Key(COL_NAME1, 1);
Key clusteringKey = new Key(COL_NAME4, 4);
Key partitionKey = Key.ofInt(COL_NAME1, 1);
Key clusteringKey = Key.ofInt(COL_NAME4, 4);
Put put =
new Put(partitionKey, clusteringKey)
.withValue(COL_NAME3, 3)
Expand Down Expand Up @@ -479,8 +479,8 @@ public void whenDeleteDataFromTable2_DataShouldBeDeletedFromJdbcDatabase()
// Arrange
String namespace = NAMESPACE1;
String table = TABLE2;
Key partitionKey = new Key(COL_NAME1, 1);
Key clusteringKey = new Key(COL_NAME4, 4);
Key partitionKey = Key.ofInt(COL_NAME1, 1);
Key clusteringKey = Key.ofInt(COL_NAME4, 4);
Put put =
new Put(partitionKey, clusteringKey)
.withValue(COL_NAME3, 3)
Expand Down Expand Up @@ -516,8 +516,8 @@ public void whenDeleteDataFromTable3_DataShouldBeDeletedFromDefaultStorage()
// Arrange
String namespace = NAMESPACE1;
String table = TABLE3;
Key partitionKey = new Key(COL_NAME1, 1);
Key clusteringKey = new Key(COL_NAME4, 4);
Key partitionKey = Key.ofInt(COL_NAME1, 1);
Key clusteringKey = Key.ofInt(COL_NAME4, 4);
Put put =
new Put(partitionKey, clusteringKey)
.withValue(COL_NAME3, 3)
Expand Down Expand Up @@ -552,9 +552,9 @@ public void whenMutateDataToTable1_ShouldExecuteForCassandra() throws ExecutionE
// Arrange
String namespace = NAMESPACE1;
String table = TABLE1;
Key partitionKey = new Key(COL_NAME1, 1);
Key clusteringKey1 = new Key(COL_NAME4, 1);
Key clusteringKey2 = new Key(COL_NAME4, 2);
Key partitionKey = Key.ofInt(COL_NAME1, 1);
Key clusteringKey1 = Key.ofInt(COL_NAME4, 1);
Key clusteringKey2 = Key.ofInt(COL_NAME4, 2);
Put put =
new Put(partitionKey, clusteringKey1)
.withValue(COL_NAME3, 3)
Expand Down Expand Up @@ -607,9 +607,9 @@ public void whenMutateDataToTable2_ShouldExecuteForJdbcDatabase() throws Executi
// Arrange
String namespace = NAMESPACE1;
String table = TABLE2;
Key partitionKey = new Key(COL_NAME1, 1);
Key clusteringKey1 = new Key(COL_NAME4, 1);
Key clusteringKey2 = new Key(COL_NAME4, 2);
Key partitionKey = Key.ofInt(COL_NAME1, 1);
Key clusteringKey1 = Key.ofInt(COL_NAME4, 1);
Key clusteringKey2 = Key.ofInt(COL_NAME4, 2);
Put put =
new Put(partitionKey, clusteringKey1)
.withValue(COL_NAME3, 3)
Expand Down Expand Up @@ -662,9 +662,9 @@ public void whenMutateDataToTable3_ShouldExecuteForDefaultStorage() throws Execu
// Arrange
String namespace = NAMESPACE1;
String table = TABLE3;
Key partitionKey = new Key(COL_NAME1, 1);
Key clusteringKey1 = new Key(COL_NAME4, 1);
Key clusteringKey2 = new Key(COL_NAME4, 2);
Key partitionKey = Key.ofInt(COL_NAME1, 1);
Key clusteringKey1 = Key.ofInt(COL_NAME4, 1);
Key clusteringKey2 = Key.ofInt(COL_NAME4, 2);
Put put =
new Put(partitionKey, clusteringKey1)
.withValue(COL_NAME3, 3)
Expand Down Expand Up @@ -718,8 +718,8 @@ public void whenPutDataIntoTable1InNamespace2_DataShouldBeWrittenIntoJdbcDatabas
// Arrange
String namespace = NAMESPACE2;
String table = TABLE1;
Key partitionKey = new Key(COL_NAME1, 1);
Key clusteringKey = new Key(COL_NAME4, 4);
Key partitionKey = Key.ofInt(COL_NAME1, 1);
Key clusteringKey = Key.ofInt(COL_NAME4, 4);
Put put =
new Put(partitionKey, clusteringKey)
.withValue(COL_NAME2, "val2")
Expand Down Expand Up @@ -760,10 +760,10 @@ public void whenScanDataFromTable1InNamespace2_DataShouldBeScannedFromJdbcDataba
// Arrange
String namespace = NAMESPACE2;
String table = TABLE1;
Key partitionKey = new Key(COL_NAME1, 1);
Key clusteringKey1 = new Key(COL_NAME4, 0);
Key clusteringKey2 = new Key(COL_NAME4, 1);
Key clusteringKey3 = new Key(COL_NAME4, 2);
Key partitionKey = Key.ofInt(COL_NAME1, 1);
Key clusteringKey1 = Key.ofInt(COL_NAME4, 0);
Key clusteringKey2 = Key.ofInt(COL_NAME4, 1);
Key clusteringKey3 = Key.ofInt(COL_NAME4, 2);

jdbcDatabase.mutate(
Arrays.asList(
Expand Down Expand Up @@ -802,8 +802,8 @@ public void whenDeleteDataFromTable1InNamespace2_DataShouldBeDeletedFromJdbcData
// Arrange
String namespace = NAMESPACE2;
String table = TABLE1;
Key partitionKey = new Key(COL_NAME1, 1);
Key clusteringKey = new Key(COL_NAME4, 4);
Key partitionKey = Key.ofInt(COL_NAME1, 1);
Key clusteringKey = Key.ofInt(COL_NAME4, 4);
Put put =
new Put(partitionKey, clusteringKey)
.withValue(COL_NAME3, 3)
Expand Down Expand Up @@ -839,9 +839,9 @@ public void whenMutateDataToTable1InNamespace2_ShouldExecuteForCassandra()
// Arrange
String namespace = NAMESPACE2;
String table = TABLE1;
Key partitionKey = new Key(COL_NAME1, 1);
Key clusteringKey1 = new Key(COL_NAME4, 1);
Key clusteringKey2 = new Key(COL_NAME4, 2);
Key partitionKey = Key.ofInt(COL_NAME1, 1);
Key clusteringKey1 = Key.ofInt(COL_NAME4, 1);
Key clusteringKey2 = Key.ofInt(COL_NAME4, 2);
Put put =
new Put(partitionKey, clusteringKey1)
.withValue(COL_NAME3, 3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void visit(DeleteIfExists condition) {
}

private Clause createClauseWith(ConditionalExpression e) {
String name = quoteIfNecessary(e.getName());
String name = quoteIfNecessary(e.getColumn().getName());
switch (e.getOperator()) {
case EQ:
case IS_NULL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public Map<String, String> getConditionColumnMap() {
if (mutation.getCondition().isPresent()) {
int index = 0;
for (ConditionalExpression expression : mutation.getCondition().get().getExpressions()) {
ret.put(CONDITION_COLUMN_NAME_ALIAS + index, expression.getName());
ret.put(CONDITION_COLUMN_NAME_ALIAS + index, expression.getColumn().getName());
index++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@ private String orderBySqlString() {
order = Scan.Ordering.Order.ASC;
}
}
orderingList.add(new Scan.Ordering(clusteringKeyName, order));
orderingList.add(
order == Scan.Ordering.Order.ASC
? Scan.Ordering.asc(clusteringKeyName)
: Scan.Ordering.desc(clusteringKeyName));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package com.scalar.db.transaction.consensuscommit;

import com.scalar.db.api.TransactionState;
import com.scalar.db.io.BigIntValue;
import com.scalar.db.io.IntValue;
import com.scalar.db.io.TextValue;

public final class Attribute {
public static final String ID = "tx_id";
public static final String CHILD_IDS = "tx_child_ids";
Expand All @@ -19,52 +14,4 @@ public final class Attribute {
public static final String BEFORE_VERSION = BEFORE_PREFIX + VERSION;
public static final String BEFORE_PREPARED_AT = BEFORE_PREFIX + PREPARED_AT;
public static final String BEFORE_COMMITTED_AT = BEFORE_PREFIX + COMMITTED_AT;

public static TextValue toIdValue(String transactionId) {
return new TextValue(Attribute.ID, transactionId);
}

public static TextValue toChildIdsValue(String childTransactionIds) {
return new TextValue(Attribute.CHILD_IDS, childTransactionIds);
}

public static IntValue toStateValue(TransactionState state) {
return new IntValue(Attribute.STATE, state.get());
}

public static IntValue toVersionValue(int version) {
return new IntValue(Attribute.VERSION, version);
}

public static BigIntValue toPreparedAtValue(long preparedAt) {
return new BigIntValue(Attribute.PREPARED_AT, preparedAt);
}

public static BigIntValue toCommittedAtValue(long committedAt) {
return new BigIntValue(Attribute.COMMITTED_AT, committedAt);
}

public static BigIntValue toCreatedAtValue(long createdAt) {
return new BigIntValue(Attribute.CREATED_AT, createdAt);
}

public static TextValue toBeforeIdValue(String transactionId) {
return new TextValue(Attribute.BEFORE_ID, transactionId);
}

public static IntValue toBeforeStateValue(TransactionState state) {
return new IntValue(Attribute.BEFORE_STATE, state.get());
}

public static IntValue toBeforeVersionValue(int version) {
return new IntValue(Attribute.BEFORE_VERSION, version);
}

public static BigIntValue toBeforePreparedAtValue(long preparedAt) {
return new BigIntValue(Attribute.BEFORE_PREPARED_AT, preparedAt);
}

public static BigIntValue toBeforeCommittedAtValue(long committedAt) {
return new BigIntValue(Attribute.BEFORE_COMMITTED_AT, committedAt);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package com.scalar.db.transaction.consensuscommit;

import static com.scalar.db.api.ConditionalExpression.Operator;
import static com.scalar.db.transaction.consensuscommit.Attribute.COMMITTED_AT;
import static com.scalar.db.transaction.consensuscommit.Attribute.ID;
import static com.scalar.db.transaction.consensuscommit.Attribute.STATE;
import static com.scalar.db.transaction.consensuscommit.Attribute.toIdValue;
import static com.scalar.db.transaction.consensuscommit.Attribute.toStateValue;
import static com.scalar.db.transaction.consensuscommit.ConsensusCommitUtils.getTransactionTableMetadata;

import com.google.common.annotations.VisibleForTesting;
import com.scalar.db.api.ConditionBuilder;
import com.scalar.db.api.ConditionalExpression;
import com.scalar.db.api.Consistency;
import com.scalar.db.api.Delete;
import com.scalar.db.api.DeleteIf;
import com.scalar.db.api.Mutation;
import com.scalar.db.api.Operation;
import com.scalar.db.api.Put;
Expand Down Expand Up @@ -124,10 +119,9 @@ private Delete composeDelete(Operation base, @Nullable TransactionResult result)
.forTable(base.forTable().get())
.withConsistency(Consistency.LINEARIZABLE)
.withCondition(
new DeleteIf(
new ConditionalExpression(ID, toIdValue(id), Operator.EQ),
new ConditionalExpression(
STATE, toStateValue(TransactionState.DELETED), Operator.EQ)));
ConditionBuilder.deleteIf(ConditionBuilder.column(ID).isEqualToText(id))
.and(ConditionBuilder.column(STATE).isEqualToInt(TransactionState.DELETED.get()))
.build());
}

private Key getPartitionKey(Operation base, @Nullable TransactionResult result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.util.concurrent.Uninterruptibles;
import com.scalar.db.api.ConditionBuilder;
import com.scalar.db.api.Consistency;
import com.scalar.db.api.DistributedStorage;
import com.scalar.db.api.Get;
import com.scalar.db.api.Put;
import com.scalar.db.api.PutIfNotExists;
import com.scalar.db.api.Result;
import com.scalar.db.api.TableMetadata;
import com.scalar.db.api.TransactionState;
Expand Down Expand Up @@ -286,7 +286,7 @@ private void putStateForLazyRecoveryRollbackForGroupCommit(String id)

@VisibleForTesting
Get createGetWith(String id) {
return new Get(new Key(Attribute.toIdValue(id)))
return new Get(Key.ofText(Attribute.ID, id))
.withConsistency(Consistency.LINEARIZABLE)
.forNamespace(coordinatorNamespace)
.forTable(TABLE);
Expand Down Expand Up @@ -327,15 +327,15 @@ private Optional<Coordinator.State> get(Get get, String id) throws CoordinatorEx

@VisibleForTesting
Put createPutWith(Coordinator.State state) {
Put put = new Put(new Key(Attribute.toIdValue(state.getId())));
Put put = new Put(Key.ofText(Attribute.ID, state.getId()));
String childIds = state.getChildIdsAsString();
if (!childIds.isEmpty()) {
put.withValue(Attribute.toChildIdsValue(childIds));
put.withTextValue(Attribute.CHILD_IDS, childIds);
}
return put.withValue(Attribute.toStateValue(state.getState()))
.withValue(Attribute.toCreatedAtValue(state.getCreatedAt()))
return put.withIntValue(Attribute.STATE, state.getState().get())
.withBigIntValue(Attribute.CREATED_AT, state.getCreatedAt())
.withConsistency(Consistency.LINEARIZABLE)
.withCondition(new PutIfNotExists())
.withCondition(ConditionBuilder.putIfNotExists())
.forNamespace(coordinatorNamespace)
.forTable(TABLE);
}
Expand Down
Loading