-
Notifications
You must be signed in to change notification settings - Fork 40
Add getScanner() method to transaction interfaces #2698
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,9 @@ | |
| * An interface for transactional CRUD operations. Note that the LINEARIZABLE consistency level is | ||
| * always used in transactional CRUD operations, so {@link Consistency} specified for CRUD | ||
| * operations is ignored. | ||
| * | ||
| * @param <E> the type of {@link TransactionException} that the implementation throws if the | ||
| * operation fails | ||
| */ | ||
| public interface CrudOperable<E extends TransactionException> { | ||
|
|
||
|
|
@@ -26,16 +29,37 @@ public interface CrudOperable<E extends TransactionException> { | |
| Optional<Result> get(Get get) throws E; | ||
|
|
||
| /** | ||
| * Retrieves results from the storage through a transaction with the specified {@link Scan} | ||
| * command with a partition key and returns a list of {@link Result}. Results can be filtered by | ||
| * specifying a range of clustering keys. | ||
| * Retrieves results from the storage through a transaction with the specified {@link Scan} or | ||
| * {@link ScanAll} or {@link ScanWithIndex} command with a partition key and returns a list of | ||
| * {@link Result}. Results can be filtered by specifying a range of clustering keys. | ||
| * | ||
| * <ul> | ||
| * <li>{@link Scan} : by specifying a partition key, it will return results within the | ||
| * partition. Results can be filtered by specifying a range of clustering keys. | ||
| * <li>{@link ScanAll} : for a given table, it will return all its records even if they span | ||
| * several partitions. | ||
| * <li>{@link ScanWithIndex} : by specifying an index key, it will return results within the | ||
| * index. | ||
| * </ul> | ||
| * | ||
| * @param scan a {@code Scan} command | ||
| * @return a list of {@link Result} | ||
| * @throws E if the transaction CRUD operation fails | ||
| */ | ||
| List<Result> scan(Scan scan) throws E; | ||
|
|
||
| /** | ||
| * Retrieves results from the storage through a transaction with the specified {@link Scan} or | ||
| * {@link ScanAll} or {@link ScanWithIndex} command with a partition key and returns a {@link | ||
| * Scanner} to iterate over the results. Results can be filtered by specifying a range of | ||
| * clustering keys. | ||
| * | ||
| * @param scan a {@code Scan} command | ||
| * @return a {@code Scanner} to iterate over the results | ||
| * @throws E if the transaction CRUD operation fails | ||
| */ | ||
| Scanner<E> getScanner(Scan scan) throws E; | ||
|
|
||
| /** | ||
| * Inserts an entry into or updates an entry in the underlying storage through a transaction with | ||
| * 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> { | |
| * @throws E if the transaction CRUD operation fails | ||
| */ | ||
| void mutate(List<? extends Mutation> mutations) throws E; | ||
|
|
||
| /** A scanner abstraction for iterating results. */ | ||
| interface Scanner<E extends TransactionException> extends AutoCloseable, Iterable<Result> { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, added the |
||
| /** | ||
| * Returns the next result. | ||
| * | ||
| * @return an {@code Optional} containing the next result if available, or empty if no more | ||
| * results | ||
| * @throws E if the operation fails | ||
| */ | ||
| Optional<Result> one() throws E; | ||
|
|
||
| /** | ||
| * Returns all remaining results. | ||
| * | ||
| * @return a {@code List} containing all remaining results | ||
| * @throws E if the operation fails | ||
| */ | ||
| List<Result> all() throws E; | ||
|
|
||
| /** | ||
| * Closes the scanner. | ||
| * | ||
| * @throws E if closing the scanner fails | ||
| */ | ||
| @Override | ||
| void close() throws E; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -33,6 +33,18 @@ public interface TransactionCrudOperable extends CrudOperable<CrudException> { | |||||
| @Override | ||||||
| List<Result> scan(Scan scan) throws CrudConflictException, CrudException; | ||||||
|
|
||||||
| /** | ||||||
| * {@inheritDoc} | ||||||
| * | ||||||
| * @throws CrudConflictException if the transaction CRUD operation fails due to transient faults | ||||||
| * (e.g., a conflict error). You can retry the transaction from the beginning | ||||||
| * @throws CrudException if the transaction CRUD operation fails due to transient or nontransient | ||||||
| * faults. You can try retrying the transaction from the beginning, but the transaction may | ||||||
| * still fail if the cause is nontranient | ||||||
|
||||||
| * still fail if the cause is nontranient | |
| * still fail if the cause is nontransient |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fix it in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, added the getScanner() method and the Scanner interface to TransactionCrudOperable.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -39,6 +39,18 @@ Optional<Result> get(Get get) | |||||
| List<Result> scan(Scan scan) | ||||||
| throws CrudConflictException, CrudException, UnknownTransactionStatusException; | ||||||
|
|
||||||
| /** | ||||||
| * {@inheritDoc} | ||||||
| * | ||||||
| * @throws CrudConflictException if the transaction CRUD operation fails due to transient faults | ||||||
| * (e.g., a conflict error). You can retry the transaction from the beginning | ||||||
| * @throws CrudException if the transaction CRUD operation fails due to transient or nontransient | ||||||
| * faults. You can try retrying the transaction from the beginning, but the transaction may | ||||||
| * still fail if the cause is nontranient | ||||||
|
||||||
| * still fail if the cause is nontranient | |
| * still fail if the cause is nontransient |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fix it in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, added the getScanner() method and the Scanner interface to TransactionManagerCrudOperable.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package com.scalar.db.common; | ||
|
|
||
| import com.google.errorprone.annotations.concurrent.LazyInit; | ||
| import com.scalar.db.api.CrudOperable; | ||
| import com.scalar.db.api.Result; | ||
| import com.scalar.db.exception.transaction.TransactionException; | ||
| import java.util.Iterator; | ||
| import java.util.NoSuchElementException; | ||
| import java.util.Objects; | ||
| import javax.annotation.Nonnull; | ||
| import javax.annotation.concurrent.NotThreadSafe; | ||
|
|
||
| public abstract class AbstractCrudOperableScanner<E extends TransactionException> | ||
| implements CrudOperable.Scanner<E> { | ||
|
|
||
| @LazyInit private ScannerIterator scannerIterator; | ||
|
|
||
| @Override | ||
| @Nonnull | ||
| public Iterator<Result> iterator() { | ||
| if (scannerIterator == null) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on this implementation, this class should be also There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think we should add |
||
| scannerIterator = new ScannerIterator(this); | ||
| } | ||
| return scannerIterator; | ||
| } | ||
|
|
||
| @NotThreadSafe | ||
| public class ScannerIterator implements Iterator<Result> { | ||
|
|
||
| private final CrudOperable.Scanner<E> scanner; | ||
| private Result next; | ||
|
|
||
| public ScannerIterator(CrudOperable.Scanner<E> scanner) { | ||
| this.scanner = Objects.requireNonNull(scanner); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasNext() { | ||
| if (next != null) { | ||
| return true; | ||
| } | ||
|
|
||
| try { | ||
| return (next = scanner.one().orElse(null)) != null; | ||
| } catch (TransactionException e) { | ||
| throw new RuntimeException(e.getMessage(), e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Result next() { | ||
| if (!hasNext()) { | ||
| throw new NoSuchElementException(); | ||
| } | ||
|
|
||
| Result ret = next; | ||
| next = null; | ||
| return ret; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.scalar.db.common; | ||
|
|
||
| import com.scalar.db.api.TransactionCrudOperable; | ||
| import com.scalar.db.exception.transaction.CrudException; | ||
|
|
||
| public abstract class AbstractTransactionCrudOperableScanner | ||
| extends AbstractCrudOperableScanner<CrudException> implements TransactionCrudOperable.Scanner {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.scalar.db.common; | ||
|
|
||
| import com.scalar.db.api.TransactionManagerCrudOperable; | ||
| import com.scalar.db.exception.transaction.TransactionException; | ||
|
|
||
| public abstract class AbstractTransactionManagerCrudOperableScanner | ||
| extends AbstractCrudOperableScanner<TransactionException> | ||
| implements TransactionManagerCrudOperable.Scanner {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the
getScanner()method toCrudOperable.