File tree 5 files changed +127
-4
lines changed
main/kotlin/simpledb/index/btree2
test/kotlin/simpledb/index/btree2
5 files changed +127
-4
lines changed Original file line number Diff line number Diff line change @@ -3,12 +3,65 @@ package simpledb.index.btree2
3
3
import simpledb.query.Constant
4
4
5
5
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 ? {
7
61
TODO ()
8
62
}
9
63
10
64
fun insert (leaf : Leaf ) {
11
65
TODO ()
12
66
}
13
-
14
67
}
Original file line number Diff line number Diff line change 1
1
package simpledb.index.btree2
2
2
3
+ import simpledb.query.Constant
4
+
3
5
class BottomInternalNode {
4
6
fun delete (idx : Int ) {
5
7
TODO ()
6
8
}
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
+ }
7
21
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
package simpledb.index.btree2
2
2
3
- class Leaf {
4
- }
3
+ class Leaf
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments