Skip to content

Commit badfa46

Browse files
feat: update lessons
1 parent 76abae2 commit badfa46

File tree

12 files changed

+98
-62
lines changed

12 files changed

+98
-62
lines changed

dev-1/lesson-4.1/java/src/main/java/tech/ydb/app/IssueYdbRepository.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
import java.time.Instant;
44
import java.util.ArrayList;
55
import java.util.List;
6-
import java.util.concurrent.CompletableFuture;
76
import java.util.concurrent.ThreadLocalRandom;
87

98
import tech.ydb.common.transaction.TxMode;
10-
import tech.ydb.core.Result;
119
import tech.ydb.query.tools.QueryReader;
1210
import tech.ydb.query.tools.SessionRetryContext;
1311
import tech.ydb.table.query.Params;
@@ -21,11 +19,9 @@
2119
*/
2220
public class IssueYdbRepository {
2321

24-
private final SessionRetryContext retryCtx;
2522
private final QueryServiceHelper queryServiceHelper;
2623

2724
public IssueYdbRepository(SessionRetryContext retryCtx) {
28-
this.retryCtx = retryCtx;
2925
this.queryServiceHelper = new QueryServiceHelper(retryCtx);
3026
}
3127

@@ -71,11 +67,9 @@ WHERE id IN ($t1, $t2)
7167
* для определения стоит ли продолжать транзакцию и какой запрос выполнить следующим.
7268
*/
7369
public List<IssueLinkCount> linkTicketsInteractive(long idT1, long idT2) {
74-
return retryCtx.supplyResult(
75-
session -> {
76-
// Транзакция будет изменять данные, поэтому используем режим SERIALIZABLE_RW
77-
var tx = new TransactionHelper(session.createNewTransaction(TxMode.SERIALIZABLE_RW));
78-
70+
return queryServiceHelper.executeInTx(
71+
TxMode.SERIALIZABLE_RW, // Транзакция будет изменять данные, поэтому используем режим SERIALIZABLE_RW
72+
tx -> {
7973
// Обновляем счетчики связей
8074
tx.executeQuery("""
8175
DECLARE $t1 AS Int64;
@@ -110,11 +104,9 @@ WHERE id IN ($t1, $t2)
110104
Params.of("$t1", PrimitiveValue.newInt64(idT1), "$t2", PrimitiveValue.newInt64(idT2))
111105
);
112106

113-
var linkTicketPairs = getLinkTicketPairs(valueReader);
114-
115-
return CompletableFuture.completedFuture(Result.success(linkTicketPairs));
107+
return getLinkTicketPairs(valueReader);
116108
}
117-
).join().getValue();
109+
);
118110
}
119111

120112
/**

dev-1/lesson-4.1/java/src/main/java/tech/ydb/app/QueryServiceHelper.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package tech.ydb.app;
22

3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.function.Function;
35
import tech.ydb.common.transaction.TxMode;
6+
import tech.ydb.core.Result;
47
import tech.ydb.query.tools.QueryReader;
58
import tech.ydb.query.tools.SessionRetryContext;
69
import tech.ydb.table.query.Params;
@@ -33,4 +36,16 @@ public QueryReader executeQuery(String yql, TxMode txMode, Params params) {
3336
session -> QueryReader.readFrom(session.createQuery(yql, txMode, params))
3437
).join().getValue();
3538
}
39+
40+
public <T> T executeInTx(TxMode txMode, Function<TransactionHelper, T> tx) {
41+
return retryCtx.supplyResult(
42+
session -> {
43+
var transaction = session.createNewTransaction(txMode);
44+
45+
return CompletableFuture.completedFuture(
46+
Result.success(tx.apply(new TransactionHelper(transaction)))
47+
);
48+
}
49+
).join().getValue();
50+
}
3651
}

dev-1/lesson-5.1/java/src/main/java/tech/ydb/app/IssueYdbRepository.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
import java.time.Instant;
44
import java.util.ArrayList;
55
import java.util.List;
6-
import java.util.concurrent.CompletableFuture;
76
import java.util.concurrent.ThreadLocalRandom;
87

98
import tech.ydb.common.transaction.TxMode;
10-
import tech.ydb.core.Result;
119
import tech.ydb.query.tools.QueryReader;
1210
import tech.ydb.query.tools.SessionRetryContext;
1311
import tech.ydb.table.query.Params;
@@ -19,11 +17,9 @@
1917
* @author Kirill Kurdyukov
2018
*/
2119
public class IssueYdbRepository {
22-
private final SessionRetryContext retryCtx;
2320
private final QueryServiceHelper queryServiceHelper;
2421

2522
public IssueYdbRepository(SessionRetryContext retryCtx) {
26-
this.retryCtx = retryCtx;
2723
this.queryServiceHelper = new QueryServiceHelper(retryCtx);
2824
}
2925

@@ -58,10 +54,8 @@ WHERE id IN ($t1, $t2)
5854
* Связывает два тикета в рамках интерактивной транзакции
5955
*/
6056
public List<IssueLinkCount> linkTicketsInteractive(long idT1, long idT2) {
61-
return retryCtx.supplyResult(
62-
session -> {
63-
var tx = new TransactionHelper(session.createNewTransaction(TxMode.SERIALIZABLE_RW));
64-
57+
return queryServiceHelper.executeInTx(TxMode.SERIALIZABLE_RW,
58+
tx -> {
6559
tx.executeQuery("""
6660
DECLARE $t1 AS Int64;
6761
DECLARE $t2 AS Int64;
@@ -93,11 +87,9 @@ WHERE id IN ($t1, $t2)
9387
Params.of("$t1", PrimitiveValue.newInt64(idT1), "$t2", PrimitiveValue.newInt64(idT2))
9488
);
9589

96-
var linkTicketPairs = getLinkTicketPairs(valueReader);
97-
98-
return CompletableFuture.completedFuture(Result.success(linkTicketPairs));
90+
return getLinkTicketPairs(valueReader);
9991
}
100-
).join().getValue();
92+
);
10193
}
10294

10395
/**

dev-1/lesson-5.1/java/src/main/java/tech/ydb/app/QueryServiceHelper.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package tech.ydb.app;
22

3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.function.Function;
35
import tech.ydb.common.transaction.TxMode;
6+
import tech.ydb.core.Result;
47
import tech.ydb.query.tools.QueryReader;
58
import tech.ydb.query.tools.SessionRetryContext;
69
import tech.ydb.table.query.Params;
@@ -33,4 +36,16 @@ public QueryReader executeQuery(String yql, TxMode txMode, Params params) {
3336
session -> QueryReader.readFrom(session.createQuery(yql, txMode, params))
3437
).join().getValue();
3538
}
39+
40+
public <T> T executeInTx(TxMode txMode, Function<TransactionHelper, T> tx) {
41+
return retryCtx.supplyResult(
42+
session -> {
43+
var transaction = session.createNewTransaction(txMode);
44+
45+
return CompletableFuture.completedFuture(
46+
Result.success(tx.apply(new TransactionHelper(transaction)))
47+
);
48+
}
49+
).join().getValue();
50+
}
3651
}

dev-1/lesson-6.1/java/src/main/java/tech/ydb/app/IssueYdbRepository.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
* @author Kirill Kurdyukov
1818
*/
1919
public class IssueYdbRepository {
20-
private final SessionRetryContext retryCtx;
20+
2121
private final QueryServiceHelper queryServiceHelper;
2222

2323
public IssueYdbRepository(SessionRetryContext retryCtx) {
24-
this.retryCtx = retryCtx;
2524
this.queryServiceHelper = new QueryServiceHelper(retryCtx);
2625
}
2726

@@ -60,10 +59,8 @@ WHERE id IN ($t1, $t2)
6059
}
6160

6261
public List<IssueLinkCount> linkTicketsInteractive(long idT1, long idT2) {
63-
return retryCtx.supplyResult(
64-
session -> {
65-
var tx = new TransactionHelper(session.createNewTransaction(TxMode.SERIALIZABLE_RW));
66-
62+
return queryServiceHelper.executeInTx(TxMode.SERIALIZABLE_RW,
63+
tx -> {
6764
tx.executeQuery("""
6865
DECLARE $t1 AS Int64;
6966
DECLARE $t2 AS Int64;
@@ -95,11 +92,9 @@ WHERE id IN ($t1, $t2)
9592
Params.of("$t1", PrimitiveValue.newInt64(idT1), "$t2", PrimitiveValue.newInt64(idT2))
9693
);
9794

98-
var linkTicketPairs = getLinkTicketPairs(valueReader);
99-
100-
return CompletableFuture.completedFuture(Result.success(linkTicketPairs));
95+
return getLinkTicketPairs(valueReader);
10196
}
102-
).join().getValue();
97+
);
10398
}
10499

105100
public void addIssue(String title, String author) {

dev-1/lesson-6.1/java/src/main/java/tech/ydb/app/QueryServiceHelper.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package tech.ydb.app;
22

3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.function.Function;
35
import tech.ydb.common.transaction.TxMode;
6+
import tech.ydb.core.Result;
47
import tech.ydb.query.tools.QueryReader;
58
import tech.ydb.query.tools.SessionRetryContext;
69
import tech.ydb.table.query.Params;
@@ -33,4 +36,16 @@ public QueryReader executeQuery(String yql, TxMode txMode, Params params) {
3336
session -> QueryReader.readFrom(session.createQuery(yql, txMode, params))
3437
).join().getValue();
3538
}
39+
40+
public <T> T executeInTx(TxMode txMode, Function<TransactionHelper, T> tx) {
41+
return retryCtx.supplyResult(
42+
session -> {
43+
var transaction = session.createNewTransaction(txMode);
44+
45+
return CompletableFuture.completedFuture(
46+
Result.success(tx.apply(new TransactionHelper(transaction)))
47+
);
48+
}
49+
).join().getValue();
50+
}
3651
}

dev-1/lesson-7.1/java/src/main/java/tech/ydb/app/IssueYdbRepository.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
import java.time.Instant;
44
import java.util.ArrayList;
55
import java.util.List;
6-
import java.util.concurrent.CompletableFuture;
76
import java.util.concurrent.ThreadLocalRandom;
87

98
import tech.ydb.common.transaction.TxMode;
10-
import tech.ydb.core.Result;
119
import tech.ydb.query.tools.QueryReader;
1210
import tech.ydb.query.tools.SessionRetryContext;
1311
import tech.ydb.table.query.Params;
@@ -17,11 +15,9 @@
1715
* @author Kirill Kurdyukov
1816
*/
1917
public class IssueYdbRepository {
20-
private final SessionRetryContext retryCtx;
2118
private final QueryServiceHelper queryServiceHelper;
2219

2320
public IssueYdbRepository(SessionRetryContext retryCtx) {
24-
this.retryCtx = retryCtx;
2521
this.queryServiceHelper = new QueryServiceHelper(retryCtx);
2622
}
2723

@@ -61,10 +57,8 @@ WHERE id IN ($t1, $t2)
6157
}
6258

6359
public List<IssueLinkCount> linkTicketsInteractive(long idT1, long idT2) {
64-
return retryCtx.supplyResult(
65-
session -> {
66-
var tx = new TransactionHelper(session.createNewTransaction(TxMode.SERIALIZABLE_RW));
67-
60+
return queryServiceHelper.executeInTx(TxMode.SERIALIZABLE_RW,
61+
tx -> {
6862
tx.executeQuery("""
6963
DECLARE $t1 AS Int64;
7064
DECLARE $t2 AS Int64;
@@ -96,11 +90,9 @@ WHERE id IN ($t1, $t2)
9690
Params.of("$t1", PrimitiveValue.newInt64(idT1), "$t2", PrimitiveValue.newInt64(idT2))
9791
);
9892

99-
var linkTicketPairs = getLinkTicketPairs(valueReader);
100-
101-
return CompletableFuture.completedFuture(Result.success(linkTicketPairs));
93+
return getLinkTicketPairs(valueReader);
10294
}
103-
).join().getValue();
95+
);
10496
}
10597

10698
public Issue addIssue(String title, String author) {

dev-1/lesson-7.1/java/src/main/java/tech/ydb/app/QueryServiceHelper.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package tech.ydb.app;
22

3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.function.Function;
35
import tech.ydb.common.transaction.TxMode;
6+
import tech.ydb.core.Result;
47
import tech.ydb.query.tools.QueryReader;
58
import tech.ydb.query.tools.SessionRetryContext;
69
import tech.ydb.table.query.Params;
@@ -33,4 +36,16 @@ public QueryReader executeQuery(String yql, TxMode txMode, Params params) {
3336
session -> QueryReader.readFrom(session.createQuery(yql, txMode, params))
3437
).join().getValue();
3538
}
39+
40+
public <T> T executeInTx(TxMode txMode, Function<TransactionHelper, T> tx) {
41+
return retryCtx.supplyResult(
42+
session -> {
43+
var transaction = session.createNewTransaction(txMode);
44+
45+
return CompletableFuture.completedFuture(
46+
Result.success(tx.apply(new TransactionHelper(transaction)))
47+
);
48+
}
49+
).join().getValue();
50+
}
3651
}

dev-1/lesson-7.1/java/src/main/java/tech/ydb/app/TransactionHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public TransactionHelper(QueryTransaction transaction) {
1515
this.transaction = transaction;
1616
}
1717

18-
public QueryReader executeQuery(String yql, Params params) {
19-
return QueryReader.readFrom(transaction.createQueryWithCommit(yql, params)).join().getValue();
18+
public void executeQuery(String yql, Params params) {
19+
QueryReader.readFrom(transaction.createQueryWithCommit(yql, params)).join().getValue();
2020
}
2121

2222
public QueryReader executeQueryWithCommit(String yql, Params params) {

dev-1/lesson-8.1/java/src/main/java/tech/ydb/app/IssueYdbRepository.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
import java.time.Instant;
44
import java.util.ArrayList;
55
import java.util.List;
6-
import java.util.concurrent.CompletableFuture;
76
import java.util.concurrent.ThreadLocalRandom;
87

98
import tech.ydb.common.transaction.TxMode;
10-
import tech.ydb.core.Result;
119
import tech.ydb.query.tools.QueryReader;
1210
import tech.ydb.query.tools.SessionRetryContext;
1311
import tech.ydb.table.query.Params;
@@ -21,11 +19,9 @@
2119
* @author Kirill Kurdyukov
2220
*/
2321
public class IssueYdbRepository {
24-
private final SessionRetryContext retryCtx;
2522
private final QueryServiceHelper queryServiceHelper;
2623

2724
public IssueYdbRepository(SessionRetryContext retryCtx) {
28-
this.retryCtx = retryCtx;
2925
this.queryServiceHelper = new QueryServiceHelper(retryCtx);
3026
}
3127

@@ -124,10 +120,7 @@ WHERE id IN ($t1, $t2)
124120
}
125121

126122
public List<IssueLinkCount> linkTicketsInteractive(long idT1, long idT2) {
127-
return retryCtx.supplyResult(
128-
session -> {
129-
var tx = new TransactionHelper(session.createNewTransaction(TxMode.SERIALIZABLE_RW));
130-
123+
return queryServiceHelper.executeInTx(TxMode.SERIALIZABLE_RW, tx -> {
131124
tx.executeQuery("""
132125
DECLARE $t1 AS Int64;
133126
DECLARE $t2 AS Int64;
@@ -159,11 +152,9 @@ WHERE id IN ($t1, $t2)
159152
Params.of("$t1", PrimitiveValue.newInt64(idT1), "$t2", PrimitiveValue.newInt64(idT2))
160153
);
161154

162-
var linkTicketPairs = getIssueLinkCount(valueReader);
163-
164-
return CompletableFuture.completedFuture(Result.success(linkTicketPairs));
155+
return getIssueLinkCount(valueReader);
165156
}
166-
).join().getValue();
157+
);
167158
}
168159

169160
public void addIssue(String title, String author) {

0 commit comments

Comments
 (0)