B+Tree
24
● 木構造の一つ
○ インデックス(索引)を表現する一つの方式
■ 本の最後の方にキーワードが何ページにあるか、というの
もインデックスです
○ 2分木も木構造の一つ
By Grundprinzip - Own work, CC BY 3.0,
https://commons.wikimedia.org/w/index.php?curid=10758840
B+Treeの挙動を知りたい場合
27
● 小さいCのコードがあります
○ http://www.amittai.com/prose/bplustree.html
$ gcc -g -O0 btp.c
$ ./a.out
...
Enter any of the following commands after the prompt > :
i -- Insert (an integer) as both key and value).
f -- Find the value under key .
p -- Print the path from the root to key k and its associated value.
r -- Print the keys and values found in the range [,
d -- Delete key and its associated value.
x -- Destroy the whole tree. Start again with an empty tree of the same
order.
t -- Print the B+ tree.
l -- Print the keys of the leaves (bottom row of the tree).
v -- Toggle output of pointer addresses ("verbose") in tree and leaves.
q -- Quit. (Or use Ctl-D.)
? -- Print this help message.
> i 3
3 |
擬似コード (PostgreSQLソースコードのコメントより引用)
41
Join {
get initial outer and inner tuples INITIALIZE
do forever {
while (outer != inner) { SKIP_TEST
if (outer < inner)
advance outer SKIPOUTER_ADVANCE
else
advance inner SKIPINNER_ADVANCE
}
mark inner position SKIP_TEST
do forever {
while (outer == inner) {
join tuples JOINTUPLES
advance inner position NEXTINNER
}
advance outer position NEXTOUTER
if (outer == mark) TESTOUTER
restore inner position to mark TESTOUTER
else
break // return to top of outer loop
}
}
}
擬似コード(再掲)
54
Join {
get initial outer and inner tuples INITIALIZE
do forever {
while (outer != inner) { SKIP_TEST
if (outer < inner)
advance outer SKIPOUTER_ADVANCE
else
advance inner SKIPINNER_ADVANCE
}
mark inner position SKIP_TEST
do forever {
while (outer == inner) {
join tuples JOINTUPLES
advance inner position NEXTINNER
}
advance outer position NEXTOUTER
if (outer == mark) TESTOUTER
restore inner position to mark TESTOUTER
else
break // return to top of outer loop
}
}
}
キューの応用例
66
● キューを管理するミドルウェアもしくはサービスがある
○ リクエストを受けたサーバがメッセージをキューに入れる
○ 誰かがキューからメッセージを拾ったら、他の人は拾わない(アト
ミックな処理)
Web
Server
Web
Server
Job
Worker
Job
Worker
push pull
Queue
Service
Slide 67
Slide 67 text
mkfifoコマンドを使った簡単な実験
67
● コンソールを2つ立ち上げてください(windowsだと動かないかも)
○ mkfifoを通してメッセージを送る
% mkfifo waseda
% tail -f waseda
waseda university
1
2
3
4
5
% echo waseda
university > waseda
% for i in `seq 5`; do echo
$i > waseda; done