Skip to content

Commit a70034d

Browse files
authored
Add scanner API to transaction abstraction (#2729)
1 parent e45325b commit a70034d

File tree

51 files changed

+3783
-266
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+3783
-266
lines changed

core/src/integration-test/java/com/scalar/db/storage/cassandra/ConsensusCommitCassandraEnv.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.scalar.db.storage.cassandra;
22

3-
import com.scalar.db.common.ConsensusCommitTestUtils;
43
import com.scalar.db.transaction.consensuscommit.ConsensusCommitIntegrationTestUtils;
4+
import com.scalar.db.transaction.consensuscommit.ConsensusCommitTestUtils;
55
import java.util.Properties;
66

77
public final class ConsensusCommitCassandraEnv {

core/src/integration-test/java/com/scalar/db/storage/cosmos/ConsensusCommitCosmosEnv.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.scalar.db.storage.cosmos;
22

3-
import com.scalar.db.common.ConsensusCommitTestUtils;
43
import com.scalar.db.transaction.consensuscommit.ConsensusCommitIntegrationTestUtils;
4+
import com.scalar.db.transaction.consensuscommit.ConsensusCommitTestUtils;
55
import java.util.Map;
66
import java.util.Properties;
77

core/src/integration-test/java/com/scalar/db/storage/dynamo/ConsensusCommitDynamoEnv.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.scalar.db.storage.dynamo;
22

3-
import com.scalar.db.common.ConsensusCommitTestUtils;
43
import com.scalar.db.transaction.consensuscommit.ConsensusCommitIntegrationTestUtils;
4+
import com.scalar.db.transaction.consensuscommit.ConsensusCommitTestUtils;
55
import java.util.Map;
66
import java.util.Properties;
77

core/src/integration-test/java/com/scalar/db/storage/jdbc/ConsensusCommitJdbcEnv.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.scalar.db.storage.jdbc;
22

3-
import com.scalar.db.common.ConsensusCommitTestUtils;
43
import com.scalar.db.transaction.consensuscommit.ConsensusCommitIntegrationTestUtils;
4+
import com.scalar.db.transaction.consensuscommit.ConsensusCommitTestUtils;
55
import java.util.Properties;
66

77
public final class ConsensusCommitJdbcEnv {

core/src/integration-test/java/com/scalar/db/storage/multistorage/ConsensusCommitNullMetadataIntegrationTestWithMultiStorage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.scalar.db.storage.multistorage;
22

3-
import com.scalar.db.common.ConsensusCommitTestUtils;
43
import com.scalar.db.config.DatabaseConfig;
54
import com.scalar.db.storage.cassandra.CassandraAdmin;
65
import com.scalar.db.transaction.consensuscommit.ConsensusCommitNullMetadataIntegrationTestBase;
6+
import com.scalar.db.transaction.consensuscommit.ConsensusCommitTestUtils;
77
import com.scalar.db.transaction.consensuscommit.Coordinator;
88
import java.util.Collections;
99
import java.util.Map;

core/src/integration-test/java/com/scalar/db/storage/multistorage/ConsensusCommitSpecificIntegrationTestWithMultiStorage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.scalar.db.storage.multistorage;
22

3-
import com.scalar.db.common.ConsensusCommitTestUtils;
43
import com.scalar.db.config.DatabaseConfig;
54
import com.scalar.db.storage.cassandra.CassandraAdmin;
65
import com.scalar.db.transaction.consensuscommit.ConsensusCommitSpecificIntegrationTestBase;
6+
import com.scalar.db.transaction.consensuscommit.ConsensusCommitTestUtils;
77
import com.scalar.db.transaction.consensuscommit.Coordinator;
88
import java.util.Collections;
99
import java.util.Map;

core/src/integration-test/java/com/scalar/db/transaction/jdbc/JdbcTransactionIntegrationTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.scalar.db.storage.jdbc.JdbcEnv;
77
import java.util.Properties;
88
import org.junit.jupiter.api.Disabled;
9+
import org.junit.jupiter.api.Test;
910

1011
public class JdbcTransactionIntegrationTest extends DistributedTransactionIntegrationTestBase {
1112

@@ -24,17 +25,21 @@ protected Properties getProperties(String testName) {
2425

2526
@Disabled("JDBC transactions don't support getState()")
2627
@Override
28+
@Test
2729
public void getState_forSuccessfulTransaction_ShouldReturnCommittedState() {}
2830

2931
@Disabled("JDBC transactions don't support getState()")
3032
@Override
33+
@Test
3134
public void getState_forFailedTransaction_ShouldReturnAbortedState() {}
3235

3336
@Disabled("JDBC transactions don't support abort()")
3437
@Override
38+
@Test
3539
public void abort_forOngoingTransaction_ShouldAbortCorrectly() {}
3640

3741
@Disabled("JDBC transactions don't support rollback()")
3842
@Override
43+
@Test
3944
public void rollback_forOngoingTransaction_ShouldRollbackCorrectly() {}
4045
}

core/src/main/java/com/scalar/db/api/CrudOperable.java

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
* An interface for transactional CRUD operations. Note that the LINEARIZABLE consistency level is
1313
* always used in transactional CRUD operations, so {@link Consistency} specified for CRUD
1414
* operations is ignored.
15+
*
16+
* @param <E> the type of {@link TransactionException} that the implementation throws if the
17+
* operation fails
1518
*/
1619
public interface CrudOperable<E extends TransactionException> {
1720

@@ -26,16 +29,37 @@ public interface CrudOperable<E extends TransactionException> {
2629
Optional<Result> get(Get get) throws E;
2730

2831
/**
29-
* Retrieves results from the storage through a transaction with the specified {@link Scan}
30-
* command with a partition key and returns a list of {@link Result}. Results can be filtered by
31-
* specifying a range of clustering keys.
32+
* Retrieves results from the storage through a transaction with the specified {@link Scan} or
33+
* {@link ScanAll} or {@link ScanWithIndex} command with a partition key and returns a list of
34+
* {@link Result}. Results can be filtered by specifying a range of clustering keys.
35+
*
36+
* <ul>
37+
* <li>{@link Scan} : by specifying a partition key, it will return results within the
38+
* partition. Results can be filtered by specifying a range of clustering keys.
39+
* <li>{@link ScanAll} : for a given table, it will return all its records even if they span
40+
* several partitions.
41+
* <li>{@link ScanWithIndex} : by specifying an index key, it will return results within the
42+
* index.
43+
* </ul>
3244
*
3345
* @param scan a {@code Scan} command
3446
* @return a list of {@link Result}
3547
* @throws E if the transaction CRUD operation fails
3648
*/
3749
List<Result> scan(Scan scan) throws E;
3850

51+
/**
52+
* Retrieves results from the storage through a transaction with the specified {@link Scan} or
53+
* {@link ScanAll} or {@link ScanWithIndex} command with a partition key and returns a {@link
54+
* Scanner} to iterate over the results. Results can be filtered by specifying a range of
55+
* clustering keys.
56+
*
57+
* @param scan a {@code Scan} command
58+
* @return a {@code Scanner} to iterate over the results
59+
* @throws E if the transaction CRUD operation fails
60+
*/
61+
Scanner<E> getScanner(Scan scan) throws E;
62+
3963
/**
4064
* Inserts an entry into or updates an entry in the underlying storage through a transaction with
4165
* the specified {@link Put} command. If a condition is specified in the {@link Put} command, and
@@ -131,4 +155,32 @@ public interface CrudOperable<E extends TransactionException> {
131155
* @throws E if the transaction CRUD operation fails
132156
*/
133157
void mutate(List<? extends Mutation> mutations) throws E;
158+
159+
/** A scanner abstraction for iterating results. */
160+
interface Scanner<E extends TransactionException> extends AutoCloseable, Iterable<Result> {
161+
/**
162+
* Returns the next result.
163+
*
164+
* @return an {@code Optional} containing the next result if available, or empty if no more
165+
* results
166+
* @throws E if the operation fails
167+
*/
168+
Optional<Result> one() throws E;
169+
170+
/**
171+
* Returns all remaining results.
172+
*
173+
* @return a {@code List} containing all remaining results
174+
* @throws E if the operation fails
175+
*/
176+
List<Result> all() throws E;
177+
178+
/**
179+
* Closes the scanner.
180+
*
181+
* @throws E if closing the scanner fails
182+
*/
183+
@Override
184+
void close() throws E;
185+
}
134186
}

core/src/main/java/com/scalar/db/api/Scanner.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@
1313
public interface Scanner extends Closeable, Iterable<Result> {
1414

1515
/**
16-
* Returns the first result in the results.
16+
* Returns the next result.
1717
*
18-
* @return the first result in the results
18+
* @return an {@code Optional} containing the next result if available, or empty if no more
19+
* results
1920
* @throws ExecutionException if the operation fails
2021
*/
2122
Optional<Result> one() throws ExecutionException;
2223

2324
/**
24-
* Returns all the results.
25+
* Returns all remaining results.
2526
*
26-
* @return the list of {@code Result}s
27+
* @return a {@code List} containing all remaining results
2728
* @throws ExecutionException if the operation fails
2829
*/
2930
List<Result> all() throws ExecutionException;

core/src/main/java/com/scalar/db/api/TransactionCrudOperable.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ public interface TransactionCrudOperable extends CrudOperable<CrudException> {
3333
@Override
3434
List<Result> scan(Scan scan) throws CrudConflictException, CrudException;
3535

36+
/**
37+
* {@inheritDoc}
38+
*
39+
* @throws CrudConflictException if the transaction CRUD operation fails due to transient faults
40+
* (e.g., a conflict error). You can retry the transaction from the beginning
41+
* @throws CrudException if the transaction CRUD operation fails due to transient or nontransient
42+
* faults. You can try retrying the transaction from the beginning, but the transaction may
43+
* still fail if the cause is nontranient
44+
*/
45+
@Override
46+
Scanner getScanner(Scan scan) throws CrudConflictException, CrudException;
47+
3648
/**
3749
* {@inheritDoc}
3850
*
@@ -154,4 +166,38 @@ void delete(List<Delete> deletes)
154166
@Override
155167
void mutate(List<? extends Mutation> mutations)
156168
throws CrudConflictException, CrudException, UnsatisfiedConditionException;
169+
170+
interface Scanner extends CrudOperable.Scanner<CrudException> {
171+
/**
172+
* {@inheritDoc}
173+
*
174+
* @throws CrudConflictException if the transaction CRUD operation fails due to transient faults
175+
* (e.g., a conflict error). You can retry the transaction from the beginning
176+
* @throws CrudException if the transaction CRUD operation fails due to transient or
177+
* nontransient faults. You can try retrying the transaction from the beginning, but the
178+
* transaction may still fail if the cause is nontranient
179+
*/
180+
@Override
181+
Optional<Result> one() throws CrudConflictException, CrudException;
182+
183+
/**
184+
* {@inheritDoc}
185+
*
186+
* @throws CrudConflictException if the transaction CRUD operation fails due to transient faults
187+
* (e.g., a conflict error). You can retry the transaction from the beginning
188+
* @throws CrudException if the transaction CRUD operation fails due to transient or
189+
* nontransient faults. You can try retrying the transaction from the beginning, but the
190+
* transaction may still fail if the cause is nontranient
191+
*/
192+
@Override
193+
List<Result> all() throws CrudConflictException, CrudException;
194+
195+
/**
196+
* {@inheritDoc}
197+
*
198+
* @throws CrudException if closing the scanner fails
199+
*/
200+
@Override
201+
void close() throws CrudException;
202+
}
157203
}

0 commit comments

Comments
 (0)