Home
>
Backend Development
>
π[Backend Development] findChildTopPath λ©μλμ μ€ν κ²°κ³Όμ λμ λ°©μ.
Backend Ddevelopment
βπ[Backend Development] findChildTopPath λ©μλμ μ€ν κ²°κ³Όμ λμ λ°©μ.β
β
1οΈβ£ μμ μ½λ.
package kobe.board.comment.entity;
import jakarta.persistence.Embeddable;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Getter
@ToString
@Embeddable
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class CommentPath {
private String path;
private static final String CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private static final int DEPTH_CHUNK_SIZE = 5;
private static final int MAX_DEPTH = 5;
// MIN_CHUNK = "00000", MAX_CHUNK = "zzzzz"
private static final String MIN_CHUNK = String.valueOf(CHARSET.charAt(0)).repeat(DEPTH_CHUNK_SIZE);
private static final String MAX_CHUNK = String.valueOf(CHARSET.charAt(CHARSET.length() - 1)).repeat(DEPTH_CHUNK_SIZE);
public static CommentPath create(String path) {
if (isDepthOverflowed(path)) {
throw new IllegalStateException("depth overflowed");
}
CommentPath commentPath = new CommentPath();
commentPath.path = path;
return commentPath;
}
private static boolean isDepthOverflowed(String path) {
return calculateDepth(path) > MAX_DEPTH;
}
private static int calculateDepth(String path) {
// 25κ°μ λ¬Έμμ΄ / 5 = 5depth
return path.length() / DEPTH_CHUNK_SIZE;
}
// CommentPath ν΄λμ€μ pathμ depthλ₯Ό ꡬνλ 맀μλ
public int getDepth() {
return calculateDepth(path);
}
// rootμΈμ§ νμΈνλ 맀μλ
public boolean isRoot() {
// νμ¬μ depthκ° 1μΈμ§ νμΈν΄μ£Όλ©΄ λ¨
return calculateDepth(path) == 1;
}
// νμ¬ pathμ parentPathλ₯Ό λ°νν΄μ£Όλ 맀μλ
public String getParentPath() {
// λ 5μλ¦¬λ§ μλΌλ΄λ©΄ λ¨
return path.substring(0, path.length() - DEPTH_CHUNK_SIZE);
}
// νμ¬ pathμ νμ λκΈμ pathμ λ§λλ 맀μλ
public CommentPath createChildCommentPath(String descendantsTopPath) {
if (descendantsTopPath == null) {
return CommentPath.create(path + MIN_CHUNK);
}
String childrenTopPath = findChildrenTopPath(descendantsTopPath);
return CommentPath.create(increase(childrenTopPath));
}
// 00a0z 00002 00000 <- descendantsTopPath
private String findChildrenTopPath(String descendantsTopPath) {
return descendantsTopPath.substring(0, (getDepth() + 1) * DEPTH_CHUNK_SIZE);
}
}
β
2οΈβ£ findChildrenTopPath(String descendantsTopPath) μ€ν κ²°κ³Ό.
- descendantsTopPathμ β00a0z0000200000β κ°μ΄ λ€μ΄κ°λ€κ³ κ°μ νλ€.
- findChildrenTopPath λ©μλλ νμ¬ κ°μ²΄μ depthλ₯Ό κΈ°λ°μΌλ‘ descendantsTopPathμ νΉμ κΈΈμ΄λ§νΌ μλΌλΈ κ°μ λ°νν©λλ€.
- νμ¬ pathμ depthλ getDepth() λ©μλλ₯Ό ν΅ν΄ κ³μ°λ©λλ€.
- getDepth()λ νμ¬ pathμ κΈΈμ΄λ₯Ό DEPTH_CHUNK_SIZE(5)λ‘ λλμ΄ κ΅¬ν©λλ€.
π μμ μ€ν.
- μλ₯Ό λ€μ΄, CommentPath κ°μ²΄κ° path = β00a0zβλΌλ©΄:
- getDepth() = 1(λ¬Έμμ΄ κΈΈμ΄ 5/5)
- (getDepth() + 1) * DEPTH_CHUNK_SIZE = (1 + 1) * 5 = 10
- descendantsTopPath.substring(0, 10)
π κ²°κ³Όκ°:
β
3οΈβ£ findChildrenTopPath λ©μλμ λμ λ°©μ
private String findChildrenTopPath(String descendantsTopPath) {
return descendantsTopPath.substring(0, (getDepth() + 1) * DEPTH_CHUNK_SIZE);
}
π λ¨κ³λ³ λμ.
π1οΈβ£ νμ¬ κ°μ²΄μ depthλ₯Ό ꡬν¨.
- getDepth()λ₯Ό νΈμΆνμ¬ νμ¬ pathκ° λͺ λ¨κ³μΈμ§ κ³μ°.
- getDepth()λ path.length() / DEPTH_CHUNK_SIZEλ‘ κ³μ°λ¨.
π2οΈβ£ (getDepth() + 1) * DEPTH_CHUNK_SIZE κ° κ³μ°.
- νμ¬ depthμμ νμ λκΈμ depthλ₯Ό ν¬ν¨ν κΈΈμ΄λ₯Ό κ³μ°.
- μ¦, λ€μ depthκΉμ§ ν¬ν¨ν descendantsTopPathμ μΌλΆλ§ κ°μ Έμ€λλ‘ μ€μ ,
π3οΈβ£ descendantsTopPathμ μΌλΆλ₯Ό μλΌ λ°ν.
- descendantsTopPath.substring(0, (getDepth() + 1) * DEPTH_CHUNK_SIZE);
- descendantsTopPathμμ μ λΆλΆμ κ°μ Έμ childrenTopPathλ₯Ό μμ±.
π κ²°λ‘
- findChildrenTopPath(β00a0z0000200000β)μ μ€ν κ²°κ³Όλ β00a0z00002β
- μ΄ λ©μλλ descendantsTopPathμμ νμ¬ depth κΈ°μ€μΌλ‘ ν λ¨κ³λ§ λ ν¬ν¨ν κ²½λ‘λ₯Ό μλΌ λ°ν.
- κ²°κ΅ νμ¬ κ°μ²΄μ νμ λκΈλ€μ΄ 곡ν΅μ μΌλ‘ κ°μ§λ prefixλ₯Ό μ°Ύμμ£Όλ μν μ νλ€.