-
Notifications
You must be signed in to change notification settings - Fork 166
[๐ ์ฌ์ดํด2 - ๋ฏธ์ (๊ธฐ๋ฌผ ํ์ฅ + DB ์ ์ฉ)] ๋๋ ๋ฏธ์ ์ ์ถํฉ๋๋ค. #342
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
Changes from all commits
ef5351d
1f59cd9
7da0ceb
3f21be8
8acc7d5
f254898
f563692
cc5bd5d
d927240
d706d5b
4c73af0
715bc70
a915e9e
750232a
4265ad4
cdf3d21
4aaaa4d
8ded06e
6c2a330
d8238df
2d5b6f6
eaa6f00
4b6478f
f4eeb5b
4d638e9
9c39a8c
cc703aa
a8aedbe
b2722cb
5ab2e6c
8ee3b62
6651188
4138d89
3490407
576bb71
be6f42f
9a633e4
7013f16
57d22bd
ca87635
f099fba
9ab7557
5d64486
4b0cffe
b354278
14f7a2c
5fcf7c3
9aabd60
707e12f
7df9213
a6789f0
6d6c76c
5703b5d
13d9085
21c418a
30be12a
d8f08d3
92e43d7
e89eae0
550fbca
f264ebc
badbe55
345954c
e6def27
bf14656
0dd4bac
90dafbf
8a45713
dee9112
7471b66
b5aaa6e
a20e0fb
c4286b5
5b97409
f2eec99
f34dfae
f3f04af
e24988c
2cc7a3c
7c2b8e0
06a67be
6d24523
2d00432
0b98381
ae1956c
f0cecfd
6b73bf0
22ad346
ed1155c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,15 @@ | ||
| import controller.JanggiController; | ||
| import dao.mongodb.BoardDao; | ||
| import dao.mongodb.MongoConnection; | ||
| import view.InputView; | ||
| import view.OutputView; | ||
|
|
||
| public class Application { | ||
|
|
||
| public static void main(String[] args) { | ||
| JanggiController janggiController = new JanggiController(new InputView(), new OutputView()); | ||
| janggiController.run(); | ||
| try (MongoConnection connection = new MongoConnection()) { | ||
| JanggiController janggiController = new JanggiController(new InputView(), new OutputView(), new BoardDao(connection)); | ||
| janggiController.run(); | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package dao.mongodb; | ||
|
|
||
| import com.mongodb.client.MongoCollection; | ||
| import com.mongodb.client.model.Filters; | ||
| import dto.PieceDto; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.bson.Document; | ||
| import org.bson.types.ObjectId; | ||
|
|
||
| public class BoardDao { | ||
|
|
||
| private final MongoCollection<Document> collection; | ||
|
|
||
| public BoardDao(MongoConnection connection) { | ||
| this.collection = connection.getCollection("board"); | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MongoClient๋ ๋ด๋ถ์ ์ผ๋ก Connection Pool์ ๊ด๋ฆฌํ๋ ๋ฌด๊ฑฐ์ด ๊ฐ์ฒด์ธ๋ฐ์, ํ์ฌ ์ฝ์ ์ฑ์ด๋ผ JVM ์ข
๋ฃ ์ ์ ๋ฆฌ๋๊ธด ํ์ง๋ง close()๋ฅผ ํธ์ถํ์ง ์์ผ๋ฉด ๋ฆฌ์์ค ๋์๊ฐ ๋ฐ์ํ ์ ์๊ฑฐ๋ ์,, MongoClient๋ฅผ ํ๋๋ก ์ ์ฅํ๊ณ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ํ์ฌ BoardDao ์์ฑ์์์ MongoDB ์ฐ๊ฒฐ ์์ฑ๊ณผ ๋ฐ์ดํฐ ์ ๊ทผ์ด๋ผ๋ ๋ ๊ฐ์ง ์ฑ ์์ ๋์์ ๊ฐ๊ณ ์๋๋ฐ์. ์ฐ๊ฒฐ ์ค์ (URL, ์ธ์ฆ์ ๋ณด, DB๋ช )์ ์ธํ๋ผ ๊ด์ฌ์ฌ์ด๊ณ , CRUD ์ํ์ ๋ฐ์ดํฐ ์ ๊ทผ ๊ด์ฌ์ฌ์ธ๋ฐ ํ๋์ ํด๋์ค์ ์์ฌ ์์ด์. ์๋ฅผ ๋ค์ด ํ ์คํธ ํ๊ฒฝ์์ ๋ค๋ฅธ DB์ ์ฐ๊ฒฐํ๊ณ ์ถ๋ค๋ฉด BoardDao ์ฝ๋๋ฅผ ์ง์ ์์ ํด์ผ ํ๊ฑฐ๋ ์. ์ฐ๊ฒฐ ์ค์ ์ ๋ณ๋ Config ํด๋์ค๋ก ๋ถ๋ฆฌํด๋ณด๋ฉด ์ด๋จ๊น์?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try-with-resource ์ ์ฉ ๋ฐ BoardDao์ ์ฑ
์์ด ๋๊ฐ์ง๋ ๊ฒ์ db ์ฐ๊ฒฐ์ฉ ํด๋์ค(
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
์ปค๋ฅ์
ํ ์ด์ผ๊ธฐ๊ฐ ๋์์ ๊ถ๊ธํ ์ ์ด ์์ต๋๋ค>.! ์ปค๋ฅ์
ํ์ด ์ ์ ์ ์ฐ๊ฒฐ์ ๋๋๋ฐ ์ฌ์ฌ์ฉ์ด๋ผ๋ ๋ถ๋ถ์ด ์๋ ๊ฑธ๋ก ์๊ณ ์์ต๋๋ค! ์์ฒญ ์ ๋ง๋ค ์ปค๋ฅ์
์ ์์ฑํ๋๊ฒ ์๋๋ผ ๋ง๋ค์ด๋๋ค๊ฐ ๋ค์ ํ์ด ์ง์ด ๋ฃ๊ณ ์ฌ์ฌ์ฉํ๋ ๋ฐฉ์์ผ๋ก ์ต์ ํ๋ฅผ ํ๋ ๊ฒ์ผ๋ก ์๋๋ฐ, close()๋ฅผ ๋งค๋ฒ ํ๊ฒ๋๋ฉด ์ด ์ฌ์ฌ์ฉ์ฑ์ ์ด์ ์ ๊ณ ๋ คํ์ง ์๋๊ฑธ๊น์? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์์ ์ข์ ์ง๋ฌธ์ด์์! ์ด ๋ถ๋ถ์ด ํท๊ฐ๋ฆฌ๋ ๊ฐ์ฅ ํฐ ์ด์ ๋ close()๋ผ๋ ๊ฐ์ ์ด๋ฆ์ด ๋ ๊ฐ์ง ๋ค๋ฅธ ๋์์ ํ๊ธฐ ๋๋ฌธ์ด์์. ํ์์ ๋น๋ฆฐ Connection์ close()๋ ์ง์ง ๋ซ๋ ๊ฒ ์๋์์ // ํ ์์ด ์ง์ ๋ง๋ ๊ฒฝ์ฐ
Connection conn = DriverManager.getConnection(url);
conn.close(); // ์ง์ง TCP ์ฐ๊ฒฐ ๋๊น
// ํ์์ ๋น๋ฆฐ ๊ฒฝ์ฐ
Connection conn = dataSource.getConnection(); // ํ์์ ๊บผ๋
conn.close(); // ํ์ ๋๋ ค๋๊ธฐ๋ง ํจ! TCP ์ฐ๊ฒฐ์ ์ด์์์
๊ทธ๋ผ ๋๋ํํ ์๋ ค๋๋ฆฐ close()๋ ๋ญ์๋์? ์ ๋ ๋ ๊ฐ์ง๋ฅผ ๊ตฌ๋ถํด์ ๋ง์๋๋ฆฐ ๊ฑฐ์์
์ค๋ฌด์์ ์ปค๋ฅ์
ํ ์ด์ํ๋ค ๋ณด๋ฉด ์ด๊ฑธ ์ ํํ ๋ชจ๋ฅด๊ณ
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JDBC๋ ๊ฐ๋ฐ์๊ฐ ์ง์ JDBC ์ค๋ช
๊ณผ MongoDB ์ค๋ช
์ด ์์ฌ ์์ต๋๋ค MongoDB ๊ธฐ์ค ํ์์ ์ปค๋ฅ์
์ ๊บผ๋ด๊ฑฐ๋ ๋ฐํํ๋ ๊ฒ์ ๋๋ผ์ด๋ฒ๊ฐ ์๋์ผ๋ก ํฉ๋๋ค. |
||
| public String save(List<PieceDto> pieces, int turnCount) { | ||
| ArrayList<Document> pieceDocuments = new ArrayList<>(); | ||
|
|
||
| for (PieceDto pieceDto : pieces) { | ||
| pieceDocuments.add(toDocument(pieceDto)); | ||
| } | ||
|
|
||
| Document gameDocument = getDocument(turnCount, pieceDocuments); | ||
|
|
||
| collection.insertOne(gameDocument); | ||
| return gameDocument.getObjectId("_id").toString(); | ||
| } | ||
|
|
||
| public List<PieceDto> findPiecesByGameId(String gameId) { | ||
| Document gameDocument = findGameDocument(gameId); | ||
|
|
||
| if (gameDocument.getBoolean("isFinished")) { | ||
| throw new IllegalArgumentException("[ERROR] ์ด๋ฏธ ์ข ๋ฃ๋ ๊ฒ์์ ๋๋ค: " + gameId); | ||
| } | ||
|
|
||
| List<Document> pieces = gameDocument.getList("pieces", Document.class); | ||
| List<PieceDto> pieceDtos = new ArrayList<>(); | ||
|
|
||
| for (Document doc : pieces) { | ||
| pieceDtos.add(toPieceDto(doc)); | ||
| } | ||
|
|
||
| return pieceDtos; | ||
| } | ||
|
|
||
| public int findTurnCountByGameId(String gameId) { | ||
| Document gameDocument = findGameDocument(gameId); | ||
| return gameDocument.getInteger("turnCount"); | ||
| } | ||
|
|
||
| public void update(String gameId, List<PieceDto> pieces, int turnCount) { | ||
| ArrayList<Document> pieceDocuments = new ArrayList<>(); | ||
|
|
||
| for (PieceDto pieceDto : pieces) { | ||
| pieceDocuments.add(toDocument(pieceDto)); | ||
| } | ||
|
|
||
| Document updateFields = new Document("$set", new Document() | ||
| .append("pieces", pieceDocuments) | ||
| .append("turnCount", turnCount)); | ||
|
|
||
| collection.updateOne( | ||
| Filters.eq("_id", new ObjectId(gameId)), | ||
| updateFields | ||
| ); | ||
| } | ||
|
Comment on lines
+64
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋๋๊ฐ ์ ๋ฆฌํ MongoDB ์ฅ์ ์ค ์คํค๋ง ์ ์ฐ์ฑ: pieces ๋ฐฐ์ด๋ก ํ๋์ Document์ ๋ด์ ์ ์์ด JOIN์ด ๋ถํ์๋ผ๋ ๋ถ๋ถ์ด ์์๋๋ฐ์, ํ ๊ฐ์ง ๋ ๊น์ด ์๊ฐํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์์. ํ์ฌ ๊ตฌ์กฐ์์๋ ๋งค ํด๋ง๋ค ์ ์ฒด ๊ธฐ๋ฌผ ๋ฐฐ์ด์ ํต์งธ๋ก ๊ต์ฒดํ๊ณ ์์ด์. RDBMS์๋ค๋ฉด ์ด๋ํ ๊ธฐ๋ฌผ 1~2๊ฐ๋ง UPDATEํ๋ฉด ๋๋๋ฐ, MongoDB Document ๊ตฌ์กฐ์์๋ ๊ธฐ๋ฌผ 32๊ฐ๋ฅผ ๋งค๋ฒ ์ ๋ถ ์ง๋ ฌํํด์ ์ ์ฅํ๊ณ ์๊ฑฐ๋ ์. ์ด trade-off๋ฅผ ์ธ์งํ๊ณ ์์๋์? ํ์ฌ ๊ท๋ชจ์์๋ ๋ฌธ์ ์์ง๋ง, Document๋ก ํ ๋ฒ์ ๋ด๋ ๊ฒ ํญ์ ์ข์ ๊ฑด์ง ์๊ฐํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์์! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PR ์ค๋ช ์์ ์ด๋ ๊ฒ ์ ์ด์ฃผ์ จ๋๋ฐ์!
์ด ๋ถ๋ถ์ ์ฌ์ค ์ ํํ์ง ์์์! MongoDB๋ 4.0(2018๋
)๋ถํฐ ๋ฉํฐ ๋ํ๋จผํธ ํธ๋์ญ์
์ ์ง์ํฉ๋๋ค. ๋๋๊ฐ ์ฌ์ฉ ์ค์ธ ๋๋ผ์ด๋ฒ( // MongoDB์์๋ ํธ๋์ญ์
์ด ๊ฐ๋ฅํด์
try (ClientSession session = client.startSession()) {
session.startTransaction();
collection1.insertOne(session, doc1);
collection2.insertOne(session, doc2);
session.commitTransaction();
}๋ค๋ง ๋๋์ ํ์ฌ ๊ตฌ์กฐ์์๋ ๊ฒ์ ์ํ๊ฐ ๋จ์ผ Document ํ๋์ ์ ๋ถ ๋ค์ด๊ฐ๊ธฐ ๋๋ฌธ์ ํธ๋์ญ์
์ด ํ์ ์๋ ๊ตฌ์กฐ์ด๊ธด ํด์. MongoDB๋ ๋จ์ผ Document์ ๋ํ ์์์ ์ฐ๊ธฐ๋ฅผ ๊ธฐ๋ณธ ๋ณด์ฅํ๊ฑฐ๋ ์. ๊ทธ๋์ ์ ๋ฆฌํ๋ฉด, ํธ๋์ญ์ ์ด ์ ๋ผ์ ๊น๋ค๋กญ๋ค๊ฐ ์๋๋ผ ๋จ์ผ Document ๊ตฌ์กฐ๋ผ์ ํธ๋์ญ์ ์ด ์ ์ด์ ํ์ ์๋ค๊ฐ ๋ ์ ํํ ํํ์ด์์! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ฅ๊ธฐ ๋๋ฉ์ธ์ MongoDB๊ฐ ์ ๋ง ์ ํฉํ ์ ํ์ผ๊น? ๐ค๋๋๊ฐ MongoDB๋ฅผ ์ ํํ ๋์ ์ ์ ์ ์ ๋ง ์ข์์ด์! ๋ค๋ง ์์งํ๊ฒ ์ด์ผ๊ธฐํ๋ฉด, ์ฅ๊ธฐ๋ RDBMS๊ฐ ๋ ์์ฐ์ค๋ฌ์ด ๋๋ฉ์ธ์ด๋ผ๊ณ ์๊ฐํด์. ์ด์ ๋ฅผ ๋ช ๊ฐ์ง ์ ๋ฆฌํด๋ณผ๊ฒ์. ์ฅ๊ธฐ๊ฐ RDBMS์ ๋ ๋ง๋ ์ด์
MongoDB๊ฐ ๋น๋๋ ๋๋ฉ์ธ์
๋ฌผ๋ก ํ์ฌ ๋ฏธ์ ๋ฒ์์์๋ ๋ ๋ค ์ถฉ๋ถํ ๋์ํ๊ณ , MongoDB๋ฅผ ์ง์ ์จ๋ณธ ๊ฒฝํ ์์ฒด๊ฐ ๊ทํ๋ค๊ณ ์๊ฐํด์. ํ์ต๋ชฉ์ ์ผ๋ก ์ ํํ๊ฒ๋ ์ด์ ๊ฐ ๋๋๊น์!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MongoDB ์ ๋ํด ์ฐพ์๋ณด๋ RDB ์ฒ๋ผ ํน์ ํ๋๋ง ์์ ํ ์ ์๋ ๊ธฐ๋ฅ๋ค์ด ์กด์ฌํ์ต๋๋ค. //ํต์ฌ ์ฐจ์ด๋ replaceOne์ _id๋ฅผ ์ ์ธํ ๋ฌธ์ ์ ์ฒด๋ฅผ ์ ๋ฌธ์๋ก ๋ฎ์ด์ฐ์ง๋ง,
// updateOne + $set์ ์ง์ ํ ํ๋๋ง ๋ณ๊ฒฝํ๊ณ ๋๋จธ์ง ํ๋๋ ๊ทธ๋๋ก ์ ์งํ๋ค๋ ์ ์
๋๋ค.
public void update(String gameId, List<PieceDto> pieces, int turnCount) {
ArrayList<Document> pieceDocuments = new ArrayList<>();
for (PieceDto pieceDto : pieces) {
pieceDocuments.add(toDocument(pieceDto));
}
Document updateFields = new Document("$set", new Document()
.append("pieces", pieceDocuments)
.append("turnCount", turnCount));
collection.updateOne(
Filters.eq("_id", new ObjectId(gameId)),
updateFields
);
}์ด๋ ๊ฒ ํ๋ฉด ์ ์ฒด ๋ฌธ์๋ฅผ ๋ฎ์ด์ฐ๋๊ฒ ์๋๋ผ ํน์ ํ๋๋ง ๋ฐ๊พธ๊ฒ ๋ฉ๋๋ค. ์ฐ์ , NoSQL์ ๊ดํ ์ ์๊ฐ์ ๋ง์๋๋ฆฌ๊ณ ์ถ์ด์. NoSQL์ ๋จ์ํ "๋น์ ํ ๋ฐ์ดํฐ์ฉ"์ด๋ผ๋ ํ๋ ์์ ๊ฐ๋๊ธฐ์ ๊ทธ ์ฒ ํ์ด ๋ ๋๋ค๊ณ ์๊ฐํฉ๋๋ค. ์ต๊ทผ ๊ฐ๋ฐ์๋ฅผ ์ํ Redis๋ผ๋ ์ฑ ์ ์ฝ์์ต๋๋ค. ๊ทธ ๊ณณ์์ NoSQL์ ๊ธฐ์กด RDB์ ํ๊ณ๋ฅผ ๊ทน๋ณตํ๊ธฐ ์ํ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ผ๋ ๋ฌธ๊ตฌ๋ฅผ ์ฝ์ ์ ์ด ์์ต๋๋ค. ์ ๋ํ RDB๊ฐ ACID๋ฅผ ์ง์ผ์ ๋ฐ์ดํฐ์ ์ผ๊ด์ฑ์ ์๊ฒฉํ ์งํจ๋ค๋ ์ ์์ ์ฅ์ ์ด ๋ง๋ค๊ณ ์๊ฐํฉ๋๋ค. ํ์ง๋ง NoSQL์ ๊ทธ ์๊ฒฉํจ์ ์๋์ ์ผ๋ก ์ํํ๋ ๋์ , ์ ์ฐ์ฑ๊ณผ ๊ฐ๋ฐ ์์ฐ์ฑ์ด๋ผ๋ ๋ค๋ฅธ ๊ฐ์น๋ฅผ ์ ๊ณตํฉ๋๋ค. ์ฅ๊ธฐ ๋๋ฉ์ธ์ฒ๋ผ ์คํค๋ง๊ฐ ๊ณ ์ ์ ์ด๋ผ ํด๋, ๊ทธ ๊ณ ์ ๋ ๋ฐ์ดํฐ๋ฅผ ํ๋์ ๋ฌธ์๋ก ์์ง๋ ฅ ์๊ฒ ํํํ ์ ์๋ค๋ ๊ฒ ์์ฒด๊ฐ MongoDB์ ์ฅ์ ์ ๋๋ค. "๊ณ ์ ๋ ์คํค๋ง = RDBMS"๋ผ๋ ๋ฑ์์ ๋ํด์๋ ์๊ฐํด๋ณผ ์ฌ์ง๊ฐ ์์ต๋๋ค. ์คํค๋ง ๊ฒ์ฆ์ ๋ํด์๋ MongoDB๋ ๋์์ด ์์ต๋๋ค. ๋ชฝ๊ณ ๋๋น ์คํค๋ง ๊ฒ์ฆ ๋ฃ๊ธฐ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋๋๊ฐ ์ด๋ ๊ฒ ๊น์ด ๊ณ ๋ฏผํ๊ณ ๋ต๋ณํด์ฃผ์ ์ ๋๋ฌด ์ข์์! ์ฑ ๊น์ง ์ฝ์ด๋ณด์ จ๋ค๋ ์ ๋ง ๋ฉ์ ธ์. ๋ค๋ง ๋ช ๊ฐ์ง ์ง์ด๋ณด๊ณ ์ถ์ ๋ถ๋ถ์ด ์์ด์ ๋ค์ ์๊ฒฌ ๋จ๊ฒจ์. MongoDB๋ ๋ถ๋ถ ์ ๋ฐ์ดํธ๊ฐ ๋๋ค๋ ๋ต๋ณ์ ๋ํด
์ด ๋ต๋ณ๋ค์ ๊ณตํต์ ์ MongoDB๋ RDBMS์ฒ๋ผ ~ํ ์ ์๋ค์์. ๊ทธ๋ฐ๋ฐ RDBMS์ฒ๋ผ ์ธ ๊ฑฐ๋ฉด RDBMS๋ฅผ ์ฐ๋ฉด ๋์ง ์์๊น์? ๋๊ตฌ๋ฅผ ์ ํํ ๋ ์ด ๋๊ตฌ๋ก๋ X๋ฅผ ํ ์ ์๋ค๊ฐ ๊ทผ๊ฑฐ๊ฐ ๋๋ฉด ์ข ์ํํด์. ๊ทธ๋ผ ๋ชจ๋ ๋๊ตฌ๊ฐ ๋ค ๊ฐ์์ง๊ฑฐ๋ ์. ์ง์ง ์๊ฐํด๋ณผ ์ง์ ์ ์ด ๋๋ฉ์ธ์์ MongoDB๊ฐ RDBMS๋ณด๋ค ๋ ์ํ๋ ๊ฒ ๋ญ๊ฐ?์์. MongoDB์ ์ง์ง ๊ฐ์ ์ ์ด๋์ ๋น๋๋์? MongoDB๊ฐ ์ ๋ง ๋น๋๋ ์๋๋ฆฌ์ค๋ ์ด์ ์ ๋งํ๋ฏ ์๋์ ๊ฐ์์.
์ฅ๊ธฐ ๊ฒ์์ ์ด ์ค ์ด๋์๋ ํด๋นํ์ง ์์์. ๋๋๊ฐ ๋ฐ์ดํฐ๊ฐ ๊ณ ์ ์ ์ด๋ผ๋ ๊ฒ๊ณผ ๋ฐ์ดํฐ๋ฅผ ์ด๋ค ๋จ์๋ก ๋ฌถ๋๋๋ ๋ณ๊ฐ๋ผ๊ณ ํ์ จ๋๋ฐ, ๊ทธ ๋ง์ ๋ง์์. ๋ค๋ง ํ๋์ ๋จ์๋ก ๋ฌถ๊ณ ์ถ๋ค๊ฐ MongoDB๋ฅผ ์ ํํ๋ ๊ฒฐ์ ์ ์ด์ ๊ฐ ๋ ์ ์๋๊ฐ?๋ ๋ค๋ฅธ ๋ฌธ์ ์์. ํ๋์ Document๋ก ์์ง์ด ์ง์ง MongoDB๋ง์ ๊ฐ์ ์ธ๊ฐ์? PostgreSQL์ JSONB, MySQL์ JSON ์ปฌ๋ผ์ ๋ณด๋ฉด, ์ฌ์ค ํ๋ RDBMS๋ ํ๋์ row์ ๋ณต์กํ JSON ๊ตฌ์กฐ๋ฅผ ๋ด์ ์ ์์ด์. PostgreSQL JSONB๋ ์ธ๋ฑ์ฑ๋ ๋๊ณ ๋ถ๋ถ ์ ๋ฐ์ดํธ๋ ๋ผ์. MySql ์์ ํ๋ ์์ฒด๋ฅผ json ์ผ๋ก ์ ์ฅํด์ ๊บผ๋ด ์ธ ์๋ ์์ด์. -- PostgreSQL์์ ๊ฒ์ ํ๋๋ฅผ ํ row์ ๋ด๋ ๊ฒ๋ ๊ฐ๋ฅ
CREATE TABLE games (
id BIGSERIAL PRIMARY KEY,
pieces JSONB NOT NULL,
turn_count INT NOT NULL
);
UPDATE games SET pieces = jsonb_set(pieces, '{0}', '...') WHERE id = 1;๊ทธ๋์ Document๋ก ๋ฌถ์ ์ ์๋ค๋ MongoDB์ ๊ณ ์ ํ ๊ฐ์ ์ด๋ผ๊ธฐ๋ณด๋ค๋, ๋ชจ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๊ณตํต ๊ธฐ๋ฅ์ ๊ฐ๊น์์. Schema Validation ์ถ๊ฐ๋ MongoDB์ ์๋ณด๋ผ๊ณ ์๊ฐํด์. ์ด๊ฑด ์ข ๋๋ฐ์ ์ธ ํํ์ด๊ธด ํ๋ฐ์, MongoDB๊ฐ Schema Validation์ ๋์ ํ ๊ฑด ์คํค๋ง ์๋ ์์ ๋ก์์ด ์ค๋ฌด์์ ๋๋ฌด ๋ง์ ๋ฐ์ดํฐ ์ค์ผ์ ์ผ์ผ์ผฐ๊ธฐ ๋๋ฌธ์ด์์. NoSQL์ ์ด๊ธฐ ์ฒ ํ("์คํค๋ง๋ ์์ ๋ค!")์ด ์ค์ ์ด์์์ ํ๊ณ๊ฐ ์์๋ค๋ ๊ฑธ ์ธ์ ํ ๊ฑฐ์ฃ . ๊ทธ๋์ ๋๋๊ฐ MongoDB๋ Schema Validation์ผ๋ก ๊ฒ์ฆํ ์ ์๋ค๊ณ ํ์ ๊ฑด ๋ง์ง๋ง, ๊ทธ ๊ธฐ๋ฅ์ ์ฐ๋ ์๊ฐ RDBMS์ ์ฅ์ ์ ํ๋ด๋ด๊ณ ์๋ ๊ฒ์ด๊ฑฐ๋ ์. ์ฒ์๋ถํฐ RDBMS๋ฅผ ์ฐ๋ฉด ๊ทธ ๊ฒ์ฆ์ด ๊ทธ๋ฅ ์์ฐ์ค๋ฝ๊ฒ ๋ฐ๋ผ์์. ๋ค์ ๋์์์ NoSQL์ ์ฒ ํ์ด ์ ํํ ๋ญ๊น์? ๋๋๊ฐ NoSQL์ ๋น์ ํ ๋ฐ์ดํฐ์ฉ์ด๋ผ๋ ํ๋ ์์ ๊ฐ๋๊ธฐ์ ๊ทธ ์ฒ ํ์ด ๋ ๋๋ค ํ์ จ๋๋ฐ, ๊ทธ ๋ ๋์ ์ฒ ํ์ด ๊ตฌ์ฒด์ ์ผ๋ก ๋ญ๊น์? ์ ์๊ฐ์๋ NoSQL์ ๋ณธ์ง์ trade-off๋ CAP ์ ๋ฆฌ์์ ๋์จ๋ค๊ณ ์๊ฐํด์.
์ฅ๊ธฐ ๊ฒ์์์ ์ด trade-off๋ฅผ ๋ฐ์๋ค์๋์?
์๋๋ผ๋ฉด, NoSQL์ ๋ณธ์ง์ ๊ฐ์ ์ ํ์ฉํ์ง ์๊ณ ๋จ์ง Document ํํ๊ฐ ์ง๊ด์ ์ด๋ค๋ผ๋ ๋ถ์์ ์ด์ ๋ก ์ ํํ ๊ฑฐ์์. ๊ทธ๋ผ ๊ทธ๊ฑด NoSQL์ ์ฒ ํ์ ๋ฐ๋ฅธ๋ค๊ธฐ๋ณด๋ค๋ JSON ์ ์ฅ์๋ก ์ฐ๋ ๊ฒ์ ๊ฐ๊น์์. ์ ๋ ๋๋์ ๋์ ์ ์ ๊ณผ MongoDB๋ฅผ ๊น์ด ๊ณต๋ถํ ์ ์ ์ ๋ง ๋์ด ํ๊ฐํด์. ๋ค๋ง ๊ฐ ๋๊ตฌ๋ ์๊ธฐ๊ฐ ์ํ๋ ์์ญ์ด ์๊ณ , ๊ทธ ์์ญ์ ๋ง์ถฐ ์ธ ๋ ์ง์ง ๊ฐ์น๊ฐ ๋์จ๋ค๋ ๊ฒ ์ ์๊ฐ์ด์์. ๋๋๊ฐ MongoDB๋ฅผ ์ง์ ์จ๋ณด๊ณ ๊ทธ trade-off๋ฅผ ์ฒด๊ฐํ ๊ฒ ์์ฒด๊ฐ ๊ฐ์ฅ ํฐ ํ์ต์ด๊ณ , ๊ทธ ๊ฒฝํ์ด ๋์ค์ ์ด ํ๋ก์ ํธ์ ์ด๋ค DB๋ฅผ ์จ์ผ ํ ๊น? ๊ฒฐ์ ํ ๋ ์ง์ง ์์ฐ์ด ๋ ๊ฑฐ์์! ๋ค์ ํ๋ฒ, ์ฑ ๊น์ง ์ฐพ์๋ณด๋ฉด์ ๋ต๋ณํด์ฃผ์ ์ ์ ๋ง ๊ฐ์ฌํด์! ์ด๋ฐ ๊น์ด ์๋ ํ ๋ก ์ด ์ง์ง ์ฑ์ฅ์ ๋ง๋ ๋ค๊ณ ์๊ฐํด์ ใ ใ |
||
|
|
||
| public void finish(String gameId) { | ||
| Document updateFields = new Document("$set", new Document("isFinished", true)); | ||
|
|
||
| collection.updateOne( | ||
| Filters.eq("_id", new ObjectId(gameId)), | ||
| updateFields | ||
| ); | ||
| } | ||
|
|
||
| private Document findGameDocument(String gameId) { | ||
| Document gameDocument = collection.find(Filters.eq("_id", new ObjectId(gameId))).first(); | ||
| if (gameDocument == null) { | ||
| throw new IllegalArgumentException("[ERROR] ํด๋น ๊ฒ์์ ์ฐพ์ ์ ์์ต๋๋ค: " + gameId); | ||
| } | ||
| return gameDocument; | ||
| } | ||
|
|
||
| private Document getDocument(int turnCount, ArrayList<Document> pieceDocuments) { | ||
| return new Document() | ||
| .append("pieces", pieceDocuments) | ||
| .append("turnCount", turnCount) | ||
| .append("isFinished", false); | ||
| } | ||
|
|
||
| private Document toDocument(PieceDto pieceDto) { | ||
| return new Document() | ||
| .append("row", pieceDto.row()) | ||
| .append("col", pieceDto.col()) | ||
| .append("team", pieceDto.team()) | ||
| .append("type", pieceDto.type()); | ||
| } | ||
|
|
||
| private PieceDto toPieceDto(Document doc) { | ||
| return new PieceDto( | ||
| doc.getInteger("row"), | ||
| doc.getInteger("col"), | ||
| doc.getString("team"), | ||
| doc.getString("type") | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package dao.mongodb; | ||
|
|
||
| import com.mongodb.client.MongoClient; | ||
| import com.mongodb.client.MongoClients; | ||
| import com.mongodb.client.MongoCollection; | ||
| import com.mongodb.client.MongoDatabase; | ||
| import org.bson.Document; | ||
|
|
||
| public class MongoConnection implements AutoCloseable { | ||
|
|
||
| private static final String CONNECTION_STRING = "mongodb://admin:password123@localhost:27017"; | ||
| private static final String DATABASE_NAME = "janggi"; | ||
|
|
||
| private final MongoClient client; | ||
| private final MongoDatabase database; | ||
|
|
||
| public MongoConnection() { | ||
| this.client = MongoClients.create(CONNECTION_STRING); | ||
| this.database = client.getDatabase(DATABASE_NAME); | ||
| } | ||
|
|
||
| public MongoCollection<Document> getCollection(String name) { | ||
| return database.getCollection(name); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| client.close(); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.