Home
>
Backend Development
>
π[Backend Development] findChildrenTopPath(String descendantsTopPath) λ©μλμ μ¬μ© λ°©λ², μ¬μ© μκΈ°, λμ λ°©λ²
Backend Ddevelopment
βπ[Backend Development] findChildrenTopPath(String descendantsTopPath) λ©μλμ μ¬μ© λ°©λ², μ¬μ© μκΈ°, λμ λ°©λ²β
β
1οΈβ£ findChildrenTopPath() λ©μλμ μν
- findChildrenTopPath() λ©μλλ μ£Όμ΄μ§ descendantsTopPath(μμ λκΈμ κ²½λ‘)μμ νμ¬ λκΈμ μ§κ³ μμ λκΈμ pathλ₯Ό μΆμΆνλ λ©μλμ
λλ€.
- μ¦, descendantsTopPathκ° νμ¬ λκΈμ μ¬λ¬ μμ λκΈ μ€ νλμΌ λ, νμ¬ λκΈμ μμλ€ μ€ μ΅μμ λκΈμ pathλ₯Ό κ°μ Έμ€λ μν μ ν©λλ€.
β
2οΈβ£ findChildrenTopPath() λ©μλμ λμ λ°©μ
private String findChildrenTopPath(String descendantsTopPath) {
return descendantsTopPath.substring(0, (getDepth() + 1) * DEPTH_CHUNK_SIZE);
}
- μ΄ λ©μλλ λ€μκ³Ό κ°μ λ°©μμΌλ‘ λμν©λλ€.
-
-
descendantsTopPath.substring(0, (getDepth() + 1) * DEPTH_CHUNK_SIZE)
- descendantsTopPath(μμ λκΈμ κ²½λ‘)μμ νμ¬ λκΈμ λ°λ‘ λ€μ κΉμ΄(μμ λκΈ)μ path λΆλΆλ§ κ°μ Έμ΅λλ€.
- getDepth()λ νμ¬ λκΈμ κΉμ΄λ₯Ό κ³μ°νλ λ©μλλ‘, path.length() / DEPTH_CHUNK_SIZEλ₯Ό λ°νν©λλ€.
- (getDepth() + 1) * DEPTH_CHUNK_SIZEλ₯Ό ν΅ν΄ νμ¬ λκΈλ³΄λ€ ν λ¨κ³ λ κΉμ μμΉκΉμ§μ λ¬Έμμ΄μ μΆμΆν©λλ€.
β
3οΈβ£ findChildrenTopPath() λ©μλμ μ¬μ© μμ
π μμ 1: κΈ°λ³Έμ μΈ λΆλͺ¨-μμ κ΄κ³μμ μ¬μ©.
CommentPath parent = CommentPath.create("00000"); // λΆλͺ¨ λκΈ
CommentPath child = CommentPath.creat("0000000000"); // μμ λκΈ
CommentPath grandChild = CommentPath.create("000000000000000"); // μμ λκΈ
String childrenTopPath = parent.findChildrenTopPath(grandChild.getPath());
System.out.println(childrenTopPath); // μΆλ ₯: "0000000000"
βΆοΈ μ€ν κ³Όμ .
-
- parent.getPath() β β00000β (λΆλͺ¨ λκΈ)
-
- grandChild.getPath() β β000000000000000β (μμ λκΈ)
-
- findChildrenTopPath(grandChild.getPath()) μ€ν:
- parent.getDepth() = 1
- (getDepth() + 1) * DEPTH_CHUNK_SIZE = (1 + 1) * 5 = 10
- β000000000000000β.substring(0, 10) β β0000000000β (λΆλͺ¨μ μ§κ³ μμ λκΈ κ²½λ‘ λ°ν)
- μ¦, μμ λκΈ pathλ₯Ό μ
λ ₯λ°μ νμ¬ λκΈμ 첫 λ²μ§Έ μμμ pathλ₯Ό λ°νν©λλ€.
β
4οΈβ£ findChildrenTopPath() λ©μλ μ¬μ© μκΈ°
public CommentPath createChildCommentPath(String descentsTopPath) {
if (descentsTopPath == null) {
return CommentPath.creat(path + MIN_CHUNK);
}
String childrenTopPath = findChildrenTopPath(descendantsTopPath);
return CommentPath.creat(increase(childrenTopPath));
}
- descendantsTopPathκ° μ‘΄μ¬νλ©΄ findChildrenTopPath()λ₯Ό μ¬μ©νμ¬ νμ¬ λκΈμ 첫 λ²μ§Έ μμ λκΈμ pathλ₯Ό κ°μ Έμ΄.
- μ΄ν increase()λ₯Ό νΈμΆνμ¬ μ λκΈμ pathλ₯Ό νλ μ¦κ°μμΌ μλ‘μ΄ μμ λκΈμ μμ±ν¨
2οΈβ£ κ³μΈ΅ν λκΈ μ‘°ν μ λΆλͺ¨-μμ κ΄κ³λ₯Ό νμ
ν λ
- λΆλͺ¨ λκΈκ³Ό μμ λκΈμ κ΄κ³λ₯Ό νμ
νμ¬ νΈλ¦¬ ꡬ쑰λ₯Ό ꡬμ±ν λ μ¬μ©λ μ μμ.
- μλ₯Ό λ€μ΄, νΉμ λκΈμ΄ descentdantsTopPath(μμ λκΈμ path)λ₯Ό κ°μ§ λ, λΆλͺ¨ λκΈμ μ§κ³ μμμ΄ λ¬΄μμΈμ§ νλ¨νλ λ° νμ©λ μ μμ.
β
5οΈβ£ findChildrenTopPath() λ©μλ μ€ν μμ
CommentPath parent = CommentPath.create("00000"); // λΆλͺ¨ λκΈ
CommentPath child = CommentPath.create("0000000000"); // μμ λκΈ
CommentPath grandChild = CommentPath.create("000000000000000"); // μμ λκΈ
String childPath = parent.findChildrenTopPath(grandChild.getPath());
System.out.println("λΆλͺ¨ λκΈμ 첫 λ²μ§Έ μμ path: " + childPath);
π μΆλ ₯
λΆλͺ¨ λκΈμ 첫 λ²μ§Έ μμ path: 0000000000
β
6οΈβ£ μ 리.
-
μν : descendantsTopPath(μμ λκΈμ κ²½λ‘)μμ νμ¬ λκΈμ μ§κ³ μμ λκΈμ pathλ₯Ό μΆμΆ.
-
λμ λ°©μ : descendantsTopPath.substring(0, (getDepth() + 1) * DEPTH_CHUNK_SIZE)μ μ¬μ©νμ¬ νΉμ μμΉκΉμ§μ λ¬Έμμ΄μ λ°ν.
-
μ¬μ© μκΈ°
- μλ‘μ΄ λλκΈ μμ±μ (createChildCommentPath() λ΄λΆμμ μ¬μ©λ¨)
- κ³μΈ΅ν λκΈμ μ‘°νν λ λΆλͺ¨-μμ κ΄κ³ νμ
-
μ£Όμν μ
- descendantsTopPathκ° nullμ΄λ©΄ substring()μμ NullPointerExceptionμ΄ λ°μν μ μμ.
- λκΈμ΄ λ무 κΉμ΄μ§λ©΄ MAX_DEPTHλ₯Ό μ΄κ³Όν μ μμ.
- μ¦, findChildrenTopPath()λ νμ¬ λκΈμ μμ μ€ μ²« λ²μ§Έ λκΈμ pathλ₯Ό κ°μ Έμ€λ μν μ νλ©°, μλ‘μ΄ λλκΈμ μμ±ν λ λ§€μ° μ€μν μν μ ν©λλ€.π