Skip to content

Commit be2b9dc

Browse files
committed
tmp
1 parent f0c3fc9 commit be2b9dc

File tree

5 files changed

+127
-4
lines changed

5 files changed

+127
-4
lines changed

src/main/kotlin/simpledb/index/btree2/BTree.kt

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,65 @@ package simpledb.index.btree2
33
import simpledb.query.Constant
44

55
class BTree {
6-
fun search(key: Constant): BottomInternalNode {
6+
7+
// 検索してリーフの一つ上の中間ノードを返す
8+
// 返されるノードの共有 latch はかけられたままとなる
9+
fun search(key: Constant): BottomInternalNode? {
10+
// ルートを取得
11+
val root = getRoot() ?: return null
12+
13+
var success = false
14+
var index: Int
15+
var parent = root
16+
var child: InternalNode? = null
17+
18+
try {
19+
do {
20+
check(parent.getEntrySize() != 0) {
21+
"parent with 0 child"
22+
}
23+
24+
index = parent.findEntry(key)
25+
26+
child = parent.fetchWithoutLatch(index, key)
27+
28+
// 無限ループにならないか?
29+
if (child == null) {
30+
parent = getRoot() ?: return null
31+
32+
continue
33+
}
34+
35+
// child を latch
36+
// BottomInternalNode は常に排他 latch がされている前提
37+
child.latchShared()
38+
39+
parent.releaseLatch()
40+
parent = child
41+
42+
child = null
43+
} while (!parent.isBottom())
44+
45+
success = true
46+
return parent as BottomInternalNode
47+
} finally {
48+
if (!success) {
49+
if (child != null && child.isLatchOwner()) {
50+
child.releaseLatch()
51+
}
52+
53+
if (parent != child && parent.isLatchOwner()) {
54+
parent.releaseLatch()
55+
}
56+
}
57+
}
58+
}
59+
60+
private fun getRoot(): InternalNode? {
761
TODO()
862
}
963

1064
fun insert(leaf: Leaf) {
1165
TODO()
1266
}
13-
1467
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
package simpledb.index.btree2
22

3+
import simpledb.query.Constant
4+
35
class BottomInternalNode {
46
fun delete(idx: Int) {
57
TODO()
68
}
9+
10+
fun findEntry(key: Constant): Int {
11+
TODO("Not yet implemented")
12+
}
13+
14+
fun getTarget(index: Int): Leaf {
15+
TODO()
16+
}
17+
18+
fun releaseLatch() {
19+
TODO("Not yet implemented")
20+
}
721
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package simpledb.index.btree2
2+
3+
import simpledb.query.Constant
4+
5+
class InternalNode {
6+
fun isBottom(): Boolean {
7+
TODO()
8+
}
9+
10+
fun getEntrySize(): Int {
11+
TODO()
12+
}
13+
14+
fun findEntry(key: Constant): Int {
15+
TODO()
16+
}
17+
18+
fun fetchWithoutLatch(index: Int, key: Constant): InternalNode? {
19+
TODO()
20+
}
21+
22+
fun latchShared() {
23+
TODO("Not yet implemented")
24+
}
25+
26+
fun releaseLatch() {
27+
TODO("Not yet implemented")
28+
}
29+
30+
fun isLatchOwner(): Boolean {
31+
TODO()
32+
}
33+
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
package simpledb.index.btree2
22

3-
class Leaf {
4-
}
3+
class Leaf
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package simpledb.index.btree2
2+
3+
import org.junit.jupiter.api.Assertions.*
4+
import org.junit.jupiter.api.Test
5+
import simpledb.query.Constant
6+
7+
class BTreeTest {
8+
@Test
9+
fun retrieveLeaf() {
10+
val key = Constant(1)
11+
val tree = BTree()
12+
13+
val bin = tree.search(key)
14+
checkNotNull(bin)
15+
try {
16+
val index = bin.findEntry(key)
17+
assertNotEquals(index, -1)
18+
val leaf = bin.getTarget(index)
19+
assertNotNull(leaf)
20+
} finally {
21+
bin.releaseLatch()
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)