Skip to content

Commit

Permalink
Add Snapshot-read-only isolation support
Browse files Browse the repository at this point in the history
You can read more about this type of isolation here https://ydb.tech/docs/en/concepts/transactions#modes
  • Loading branch information
jorki02 committed Jun 27, 2024
1 parent 56fd568 commit 09dcd24
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ private TxControl<?> getTxControl() {
case ONLINE_CONSISTENT_READ_ONLY -> TxControl.onlineRo().setAllowInconsistentReads(false);
case ONLINE_INCONSISTENT_READ_ONLY -> TxControl.onlineRo().setAllowInconsistentReads(true);
case STALE_CONSISTENT_READ_ONLY -> TxControl.staleRo();
case SNAPSHOT -> {
TxControl<?> txControl = (txId != null ? TxControl.id(txId) : TxControl.snapshotRo());
yield txControl.setCommitTx(false);
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import tech.ydb.yoj.databind.schema.Column;
import tech.ydb.yoj.databind.schema.ObjectSchema;
import tech.ydb.yoj.repository.db.EntitySchema;
import tech.ydb.yoj.repository.db.IsolationLevel;
import tech.ydb.yoj.repository.db.Repository;
import tech.ydb.yoj.repository.db.RepositoryTransaction;
import tech.ydb.yoj.repository.db.Tx;
Expand Down Expand Up @@ -304,6 +305,21 @@ public void transactionLevel() {
checkSession(sessionManager, firstSession);
}

@Test
public void snapshotTransactionLevel() {
Project expected = new Project(new Project.Id("SP"), "snapshot");

db.tx(() -> db.projects().save(expected));

Project actual = db.tx(() -> db.projects().find(expected.getId()));
assertThat(actual).isEqualTo(expected);

actual = db.readOnly()
.withStatementIsolationLevel(IsolationLevel.SNAPSHOT)
.run(() -> db.projects().find(expected.getId()));
assertThat(actual).isEqualTo(expected);
}

@SneakyThrows
@Test
public void truncated() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ public enum IsolationLevel {
* An <em>almost</em> recent consistent state of the database. Read only.
* This level is faster then {@code ONLINE_CONSISTENT_READ_ONLY}, but may return stale data.
*/
STALE_CONSISTENT_READ_ONLY("SC");
STALE_CONSISTENT_READ_ONLY("SC"),

/**
* All the read operations within a transaction access the database snapshot. Read only.
* All the data reads are consistent. The snapshot is taken when the transaction begins,
* meaning the transaction sees all changes committed before it began.
*/
SNAPSHOT("SP");

@Getter
private final String txIdSuffix;
Expand Down

0 comments on commit 09dcd24

Please sign in to comment.