Skip to content

Commit ba7fa4e

Browse files
refactor(sep31): optimize sep31 customer id owner claim
* update sep31 customer id owner store to avoid unnecessary database lookup on successful claim * refactor `created_at` column type in sep31 customer id owner table to `timestamp without time zone` * add test case for `illegalstateexception` when sep31 customer id owner row is missing after non-inserting claim * update test cases to verify sep31 claim optimization and non-inserting scenarios
1 parent 9df04f0 commit ba7fa4e

3 files changed

Lines changed: 21 additions & 7 deletions

File tree

platform/src/main/java/org/stellar/anchor/platform/data/JdbcSep31CustomerIdOwnerStore.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ public JdbcSep31CustomerIdOwnerStore(JdbcSep31CustomerIdOwnerRepo repo) {
1212

1313
@Override
1414
public boolean verifyOrClaim(String customerId, String creatorAccount, String creatorMemo) {
15-
repo.claimIfAbsent(customerId, creatorAccount, creatorMemo);
15+
if (repo.claimIfAbsent(customerId, creatorAccount, creatorMemo) == 1) {
16+
return true;
17+
}
18+
1619
JdbcSep31CustomerIdOwner owner =
1720
repo.findById(customerId)
1821
.orElseThrow(
1922
() ->
2023
new IllegalStateException(
21-
"sep31_customer_id_owner row missing immediately after claim for id="
24+
"sep31_customer_id_owner row missing after a non-inserting claim for id="
2225
+ customerId));
2326

2427
return Objects.equals(owner.getCreatorAccount(), creatorAccount)

platform/src/main/resources/db/migration/V29__sep31_customer_id_owner.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ CREATE TABLE sep31_customer_id_owner (
22
customer_id VARCHAR(255) NOT NULL PRIMARY KEY,
33
creator_account VARCHAR(255) NOT NULL,
44
creator_memo VARCHAR(255),
5-
created_at TIMESTAMP NOT NULL DEFAULT now()
5+
created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT now()
66
);

platform/src/test/kotlin/org/stellar/anchor/platform/data/JdbcSep31CustomerIdOwnerStoreTest.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ class JdbcSep31CustomerIdOwnerStoreTest {
2929
}
3030

3131
@Test
32-
fun `first claim of a customer id succeeds`() {
32+
fun `first claim of a customer id succeeds without a follow-up lookup`() {
3333
every { repo.claimIfAbsent("cust-1", "GALICE", "111") } returns 1
34-
every { repo.findById("cust-1") } returns Optional.of(ownerRow("cust-1", "GALICE", "111"))
3534

3635
assertTrue(store.verifyOrClaim("cust-1", "GALICE", "111"))
3736
verify(exactly = 1) { repo.claimIfAbsent("cust-1", "GALICE", "111") }
37+
verify(exactly = 0) { repo.findById(any()) }
3838
}
3939

4040
@Test
@@ -43,6 +43,7 @@ class JdbcSep31CustomerIdOwnerStoreTest {
4343
every { repo.findById("cust-1") } returns Optional.of(ownerRow("cust-1", "GALICE", "111"))
4444

4545
assertTrue(store.verifyOrClaim("cust-1", "GALICE", "111"))
46+
verify(exactly = 1) { repo.findById("cust-1") }
4647
}
4748

4849
@Test
@@ -54,10 +55,20 @@ class JdbcSep31CustomerIdOwnerStoreTest {
5455
}
5556

5657
@Test
57-
fun `null memo is compared correctly`() {
58-
every { repo.claimIfAbsent("cust-1", "GALICE", null) } returns 1
58+
fun `null memo is compared correctly on the conflict path`() {
59+
every { repo.claimIfAbsent("cust-1", "GALICE", null) } returns 0
5960
every { repo.findById("cust-1") } returns Optional.of(ownerRow("cust-1", "GALICE", null))
6061

6162
assertTrue(store.verifyOrClaim("cust-1", "GALICE", null))
6263
}
64+
65+
@Test
66+
fun `throws if the row is missing after a non-inserting claim`() {
67+
every { repo.claimIfAbsent("cust-1", "GALICE", "111") } returns 0
68+
every { repo.findById("cust-1") } returns Optional.empty()
69+
70+
assertThrows(IllegalStateException::class.java) {
71+
store.verifyOrClaim("cust-1", "GALICE", "111")
72+
}
73+
}
6374
}

0 commit comments

Comments
 (0)