Skip to content

Commit 329fc6a

Browse files
authored
Merge pull request #14 from ydb-platform/bug-fixes
Small refactor: remove uuid, drop scheme before create, use not null types for some fields
2 parents b40e2f8 + 5723d9d commit 329fc6a

File tree

41 files changed

+292
-277
lines changed

Some content is hidden

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

41 files changed

+292
-277
lines changed

dev-1/lesson-2.1/java/src/main/java/tech/ydb/app/Application.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* @author Kirill Kurdyukov
1212
*/
13-
public class Application {
13+
public class Application {
1414
private static final String CONNECTION_STRING = "grpc://localhost:2136/local";
1515

1616
public static void main(String[] args) {

dev-1/lesson-3.1/java/src/main/java/tech/ydb/app/Application.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static void main(String[] args) {
1919
var schemaYdbRepository = new SchemaYdbRepository(retryCtx);
2020
var issueYdbRepository = new IssueYdbRepository(retryCtx);
2121

22+
schemaYdbRepository.dropSchema();
2223
schemaYdbRepository.createSchema();
2324

2425
issueYdbRepository.addIssue("Ticket 1");
@@ -28,8 +29,6 @@ public static void main(String[] args) {
2829
for (var ticket : issueYdbRepository.findAll()) {
2930
System.out.println("Ticket: {id: " + ticket.id() + ", title: " + ticket.title() + ", timestamp: " + ticket.now() + "}");
3031
}
31-
32-
schemaYdbRepository.dropSchema();
3332
}
3433
}
3534
}

dev-1/lesson-3.1/java/src/main/java/tech/ydb/app/Issue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
/**
77
* @author Kirill Kurdyukov
88
*/
9-
public record Issue(UUID id, String title, Instant now) {
9+
public record Issue(long id, String title, Instant now) {
1010
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.ArrayList;
55
import java.util.List;
66
import java.util.UUID;
7+
import java.util.concurrent.ThreadLocalRandom;
8+
79
import tech.ydb.common.transaction.TxMode;
810
import tech.ydb.query.tools.QueryReader;
911
import tech.ydb.query.tools.SessionRetryContext;
@@ -21,21 +23,21 @@ public IssueYdbRepository(SessionRetryContext retryCtx) {
2123
}
2224

2325
public Issue addIssue(String title) {
24-
var id = UUID.randomUUID();
26+
var id = ThreadLocalRandom.current().nextLong();
2527
var now = Instant.now();
2628

2729
retryCtx.supplyResult(
2830
session -> session.createQuery(
2931
"""
30-
DECLARE $id AS UUID;
32+
DECLARE $id AS Int64;
3133
DECLARE $title AS Text;
3234
DECLARE $created_at AS Timestamp;
3335
UPSERT INTO issues (id, title, created_at)
3436
VALUES ($id, $title, $created_at);
3537
""",
3638
TxMode.SERIALIZABLE_RW,
3739
Params.of(
38-
"$id", PrimitiveValue.newUuid(id),
40+
"$id", PrimitiveValue.newInt64(id),
3941
"$title", PrimitiveValue.newText(title),
4042
"$created_at", PrimitiveValue.newTimestamp(now)
4143
)
@@ -57,7 +59,7 @@ public List<Issue> findAll() {
5759

5860
while (resultSetReader.next()) {
5961
titles.add(new Issue(
60-
resultSetReader.getColumn(0).getUuid(),
62+
resultSetReader.getColumn(0).getInt64(),
6163
resultSetReader.getColumn(1).getText(),
6264
resultSetReader.getColumn(2).getTimestamp()
6365
));

dev-1/lesson-3.1/java/src/main/java/tech/ydb/app/SchemaYdbRepository.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public void createSchema() {
1919
session -> session.createQuery(
2020
"""
2121
CREATE TABLE issues (
22-
id UUID NOT NULL,
23-
title Text,
24-
created_at Timestamp,
22+
id Int64 NOT NULL,
23+
title Text NOT NULL,
24+
created_at Timestamp NOT NULL,
2525
PRIMARY KEY (id)
2626
);
2727
""", TxMode.NONE

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static void main(String[] args) {
1919
var schemaYdbRepository = new SchemaYdbRepository(retryCtx);
2020
var issueYdbRepository = new IssueYdbRepository(retryCtx);
2121

22+
schemaYdbRepository.dropSchema();
2223
schemaYdbRepository.createSchema();
2324

2425
issueYdbRepository.addIssue("Ticket 1", "Author 1");
@@ -54,8 +55,6 @@ public static void main(String[] args) {
5455
for (var ticket : issueYdbRepository.findAll()) {
5556
printIssue(ticket);
5657
}
57-
58-
schemaYdbRepository.dropSchema();
5958
}
6059
}
6160
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
/**
77
* @author Kirill Kurdyukov
88
*/
9-
public record Issue(UUID id, String title, Instant now, String author, long linkCounts) {
9+
public record Issue(long id, String title, Instant now, String author, long linkCounts) {
1010
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
/**
66
* @author Kirill Kurdyukov
77
*/
8-
public record IssueLinkCount(UUID id, long linkCount) {
8+
public record IssueLinkCount(long id, long linkCount) {
99
}

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.util.List;
66
import java.util.UUID;
77
import java.util.concurrent.CompletableFuture;
8+
import java.util.concurrent.ThreadLocalRandom;
9+
810
import tech.ydb.common.transaction.TxMode;
911
import tech.ydb.core.Result;
1012
import tech.ydb.query.tools.QueryReader;
@@ -22,12 +24,12 @@ public IssueYdbRepository(SessionRetryContext retryCtx) {
2224
this.retryCtx = retryCtx;
2325
}
2426

25-
public List<IssueLinkCount> linkTicketsNoInteractive(UUID idT1, UUID idT2) {
27+
public List<IssueLinkCount> linkTicketsNoInteractive(long idT1, long idT2) {
2628
var valueReader = retryCtx.supplyResult(
2729
session -> QueryReader.readFrom(session.createQuery(
2830
"""
29-
DECLARE $t1 AS UUID;
30-
DECLARE $t2 AS UUID;
31+
DECLARE $t1 AS Int64;
32+
DECLARE $t2 AS Int64;
3133
3234
UPDATE issues
3335
SET link_count = COALESCE(link_count, 0) + 1
@@ -40,48 +42,48 @@ INSERT INTO links (source, destination)
4042
WHERE id IN ($t1, $t2)
4143
""",
4244
TxMode.SERIALIZABLE_RW,
43-
Params.of("$t1", PrimitiveValue.newUuid(idT1), "$t2", PrimitiveValue.newUuid(idT2))
45+
Params.of("$t1", PrimitiveValue.newInt64(idT1), "$t2", PrimitiveValue.newInt64(idT2))
4446
))
4547
).join().getValue();
4648

4749
return getLinkTicketPairs(valueReader);
4850
}
4951

50-
public List<IssueLinkCount> linkTicketsInteractive(UUID idT1, UUID idT2) {
52+
public List<IssueLinkCount> linkTicketsInteractive(long idT1, long idT2) {
5153
return retryCtx.supplyResult(
5254
session -> {
5355
var tx = session.createNewTransaction(TxMode.SERIALIZABLE_RW);
5456

5557
tx.createQuery("""
56-
DECLARE $t1 AS UUID;
57-
DECLARE $t2 AS UUID;
58+
DECLARE $t1 AS Int64;
59+
DECLARE $t2 AS Int64;
5860
5961
UPDATE issues
6062
SET link_count = COALESCE(link_count, 0) + 1
6163
WHERE id IN ($t1, $t2);
6264
""",
63-
Params.of("$t1", PrimitiveValue.newUuid(idT1), "$t2", PrimitiveValue.newUuid(idT2))
65+
Params.of("$t1", PrimitiveValue.newInt64(idT1), "$t2", PrimitiveValue.newInt64(idT2))
6466
).execute().join().getStatus().expectSuccess();
6567

6668
tx.createQuery("""
67-
DECLARE $t1 AS UUID;
68-
DECLARE $t2 AS UUID;
69+
DECLARE $t1 AS Int64;
70+
DECLARE $t2 AS Int64;
6971
7072
INSERT INTO links (source, destination)
7173
VALUES ($t1, $t2), ($t2, $t1);
7274
""",
73-
Params.of("$t1", PrimitiveValue.newUuid(idT1), "$t2", PrimitiveValue.newUuid(idT2))
75+
Params.of("$t1", PrimitiveValue.newInt64(idT1), "$t2", PrimitiveValue.newInt64(idT2))
7476
).execute().join().getStatus().expectSuccess();
7577

7678
var valueReader = QueryReader.readFrom(
7779
tx.createQueryWithCommit("""
78-
DECLARE $t1 AS UUID;
79-
DECLARE $t2 AS UUID;
80+
DECLARE $t1 AS Int64;
81+
DECLARE $t2 AS Int64;
8082
8183
SELECT id, link_count FROM issues
8284
WHERE id IN ($t1, $t2)
8385
""",
84-
Params.of("$t1", PrimitiveValue.newUuid(idT1), "$t2", PrimitiveValue.newUuid(idT2)))
86+
Params.of("$t1", PrimitiveValue.newInt64(idT1), "$t2", PrimitiveValue.newInt64(idT2)))
8587
).join().getValue();
8688

8789
var linkTicketPairs = getLinkTicketPairs(valueReader);
@@ -92,13 +94,13 @@ WHERE id IN ($t1, $t2)
9294
}
9395

9496
public void addIssue(String title, String author) {
95-
var id = UUID.randomUUID();
97+
var id = ThreadLocalRandom.current().nextLong();
9698
var now = Instant.now();
9799

98100
retryCtx.supplyResult(
99101
session -> session.createQuery(
100102
"""
101-
DECLARE $id AS UUID;
103+
DECLARE $id AS Int64;
102104
DECLARE $title AS Text;
103105
DECLARE $created_at AS Timestamp;
104106
DECLARE $author AS Text;
@@ -107,7 +109,7 @@ UPSERT INTO issues (id, title, created_at, author)
107109
""",
108110
TxMode.SERIALIZABLE_RW,
109111
Params.of(
110-
"$id", PrimitiveValue.newUuid(id),
112+
"$id", PrimitiveValue.newInt64(id),
111113
"$title", PrimitiveValue.newText(title),
112114
"$created_at", PrimitiveValue.newTimestamp(now),
113115
"$author", PrimitiveValue.newText(author)
@@ -128,7 +130,7 @@ public List<Issue> findAll() {
128130

129131
while (resultSetReader.next()) {
130132
titles.add(new Issue(
131-
resultSetReader.getColumn(0).getUuid(),
133+
resultSetReader.getColumn(0).getInt64(),
132134
resultSetReader.getColumn(1).getText(),
133135
resultSetReader.getColumn(2).getTimestamp(),
134136
resultSetReader.getColumn(3).getText(),
@@ -145,7 +147,7 @@ private static List<IssueLinkCount> getLinkTicketPairs(QueryReader valueReader)
145147

146148
while (resultSet.next()) {
147149
linkTicketPairs.add(
148-
new IssueLinkCount(resultSet.getColumn(0).getUuid(), resultSet.getColumn(1).getInt64())
150+
new IssueLinkCount(resultSet.getColumn(0).getInt64(), resultSet.getColumn(1).getInt64())
149151
);
150152
}
151153
return linkTicketPairs;

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public void createSchema() {
1919
session -> session.createQuery(
2020
"""
2121
CREATE TABLE issues (
22-
id UUID NOT NULL,
23-
title Text,
24-
created_at Timestamp,
22+
id Int64 NOT NULL,
23+
title Text NOT NULL,
24+
created_at Timestamp NOT NULL,
2525
author Text,
2626
PRIMARY KEY (id)
2727
);
@@ -34,8 +34,8 @@ PRIMARY KEY (id)
3434
"""
3535
ALTER TABLE issues ADD COLUMN link_count Int64;
3636
CREATE TABLE links (
37-
source UUID NOT NULL,
38-
destination UUID NOT NULL,
37+
source Int64 NOT NULL,
38+
destination Int64 NOT NULL,
3939
PRIMARY KEY(source, destination)
4040
);
4141
""", TxMode.NONE
@@ -47,8 +47,8 @@ public void dropSchema() {
4747
retryCtx.supplyResult(
4848
session -> session.createQuery(
4949
"""
50-
DROP TABLE issues;
51-
DROP TABLE links;
50+
DROP TABLE IF EXISTS issues;
51+
DROP TABLE IF EXISTS links;
5252
""", TxMode.NONE
5353
).execute()
5454
).join().getStatus().expectSuccess("Can't drop table issues");

0 commit comments

Comments
 (0)