Home > Backend Development > πŸ“š[Backend Development] findChildrenTopPath(String descendantsTopPath) λ©”μ„œλ“œμ˜ μ‚¬μš© 방법, μ‚¬μš© μ‹œκΈ°, λ™μž‘ 방법

πŸ“š[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);
}
  • 이 λ©”μ„œλ“œλŠ” λ‹€μŒκ³Ό 같은 λ°©μ‹μœΌλ‘œ λ™μž‘ν•©λ‹ˆλ‹€.
      1. 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"

▢️ μ‹€ν–‰ κ³Όμ •.

    1. parent.getPath() ➞ β€œ00000” (λΆ€λͺ¨ λŒ“κΈ€)
    1. grandChild.getPath() ➞ β€œ000000000000000” (μ†μž λŒ“κΈ€)
    1. findChildrenTopPath(grandChild.getPath()) μ‹€ν–‰:
      • parent.getDepth() = 1
      • (getDepth() + 1) * DEPTH_CHUNK_SIZE = (1 + 1) * 5 = 10
      • β€œ000000000000000”.substring(0, 10) ➞ β€œ0000000000” (λΆ€λͺ¨μ˜ 직계 μžμ‹ λŒ“κΈ€ 경둜 λ°˜ν™˜)
      • 즉, μ†μž λŒ“κΈ€ pathλ₯Ό μž…λ ₯λ°›μ•„ ν˜„μž¬ λŒ“κΈ€μ˜ 첫 번째 μžμ‹μ˜ pathλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

βœ…4️⃣ findChildrenTopPath() λ©”μ„œλ“œ μ‚¬μš© μ‹œκΈ°

1️⃣ μƒˆλ‘œμš΄ λŒ€λŒ“κΈ€μ„ 생성할 λ•Œ(createChildCommentPath()μ—μ„œ μ‚¬μš©)

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λ₯Ό κ°€μ Έμ˜€λŠ” 역할을 ν•˜λ©°, μƒˆλ‘œμš΄ λŒ€λŒ“κΈ€μ„ 생성할 λ•Œ 맀우 μ€‘μš”ν•œ 역할을 ν•©λ‹ˆλ‹€.πŸš€