Skip to content
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

✨ Feat : Database #30

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/com/thread/concurrency/db/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.thread.concurrency.db;

public record Account(long balance, long updateMilli, long updateNano) {

Check warning on line 3 in src/main/java/com/thread/concurrency/db/Account.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/thread/concurrency/db/Account.java#L3

Added line #L3 was not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.thread.concurrency.db;

public interface BalanceController {
Account balance(long id);

Account deposit(long id, long amount);

Account withdraw(long id, long amount);
}
9 changes: 9 additions & 0 deletions src/main/java/com/thread/concurrency/db/Database.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.thread.concurrency.db;

public interface Database {
// deposit balance: 일급 함수 스타일
Account balance(long id, long balance);

// initial balance
Account balance(long id);
}
32 changes: 32 additions & 0 deletions src/main/java/com/thread/concurrency/db/SimpleDatabase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.thread.concurrency.db;


import org.springframework.stereotype.Component;

import java.util.HashMap;

import static java.lang.Math.random;

@Component
public final class SimpleDatabase {
private final HashMap<Long, Account> db = new HashMap<>();

Account balance(long id, long balance) {
try {
Thread.sleep((long) (random() * 300L + 100));
} catch (InterruptedException ignored) {
}
var account = new Account(balance, System.currentTimeMillis(), System.nanoTime());
db.put(id, account);
return account;

Check warning on line 21 in src/main/java/com/thread/concurrency/db/SimpleDatabase.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/thread/concurrency/db/SimpleDatabase.java#L16-L21

Added lines #L16 - L21 were not covered by tests
}

Account balance(long id) {
try {
Thread.sleep((long) (random() * 300L + 100));
} catch (InterruptedException ignored) {
}
var account = db.get(id);

Check warning on line 29 in src/main/java/com/thread/concurrency/db/SimpleDatabase.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/thread/concurrency/db/SimpleDatabase.java#L26-L29

Added lines #L26 - L29 were not covered by tests
return account != null ? account : new Account(0, System.currentTimeMillis(), System.nanoTime());
}
}