From a4279e6422eeb889f9349c8037c9cab1304225eb Mon Sep 17 00:00:00 2001 From: Hyeon-hak Kim Date: Fri, 29 Mar 2024 13:58:52 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Feat=20:=20`Database`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/thread/concurrency/db/Account.java | 4 +++ .../concurrency/db/BalanceController.java | 9 ++++++ .../com/thread/concurrency/db/Database.java | 9 ++++++ .../thread/concurrency/db/SimpleDatabase.java | 32 +++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 src/main/java/com/thread/concurrency/db/Account.java create mode 100644 src/main/java/com/thread/concurrency/db/BalanceController.java create mode 100644 src/main/java/com/thread/concurrency/db/Database.java create mode 100644 src/main/java/com/thread/concurrency/db/SimpleDatabase.java diff --git a/src/main/java/com/thread/concurrency/db/Account.java b/src/main/java/com/thread/concurrency/db/Account.java new file mode 100644 index 0000000..f88c319 --- /dev/null +++ b/src/main/java/com/thread/concurrency/db/Account.java @@ -0,0 +1,4 @@ +package com.thread.concurrency.db; + +public record Account(long balance, long updateMilli, long updateNano) { +} diff --git a/src/main/java/com/thread/concurrency/db/BalanceController.java b/src/main/java/com/thread/concurrency/db/BalanceController.java new file mode 100644 index 0000000..38d705a --- /dev/null +++ b/src/main/java/com/thread/concurrency/db/BalanceController.java @@ -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); +} diff --git a/src/main/java/com/thread/concurrency/db/Database.java b/src/main/java/com/thread/concurrency/db/Database.java new file mode 100644 index 0000000..ffe88d2 --- /dev/null +++ b/src/main/java/com/thread/concurrency/db/Database.java @@ -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); +} diff --git a/src/main/java/com/thread/concurrency/db/SimpleDatabase.java b/src/main/java/com/thread/concurrency/db/SimpleDatabase.java new file mode 100644 index 0000000..5181136 --- /dev/null +++ b/src/main/java/com/thread/concurrency/db/SimpleDatabase.java @@ -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 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; + } + + Account balance(long id) { + try { + Thread.sleep((long) (random() * 300L + 100)); + } catch (InterruptedException ignored) { + } + var account = db.get(id); + return account != null ? account : new Account(0, System.currentTimeMillis(), System.nanoTime()); + } +}