Blink
Blink
Review
B+
Tree:
Most
Widely
Used
Index
• Insert/delete
at
log
F
N
cost;
keep
tree
height-‐
balanced.
(F
=
fanout,
N
=
#
leaf
pages)
• Minimum
50%
occupancy
(except
for
root).
Each
node
contains
d
<=
m
<=
2d
entries.
The
parameter
d
is
called
the
order
of
the
tree.
• Supports
equality
and
range-‐searches
efficiently.
Index Entries
(Direct search)
Data Entries
("Sequence set")
Example
B+
Tree
• Search
begins
at
root,
and
key
comparisons
direct
it
to
a
leaf
(as
in
ISAM).
• Search
for
5*,
15*,
all
data
entries
>=
24*
...
Root
13 17 24 30
2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39*
Root
13 17 24 30
2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39*
5 13 24 30
2* 3* 5* 7* 8* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39*
5 13 24 30
2* 3* 5* 7* 8* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39*
17
5 13 27 30
2* 3* 5* 7* 8* 14* 16* 22* 24* 27* 29* 33* 34* 38* 39*
22
5 13 17 20 30
2* 3* 5* 7* 8* 14* 16* 17* 18* 20* 21* 22* 27* 29* 33* 34* 38* 39*
Afer
Re-‐distribu[on
• Entries
are
re-‐distributed
by
`pushing
through’
the
splilng
entry
in
the
parent
node.
• It
suffices
to
re-‐distribute
index
entry
with
key
20;
we’ve
re-‐distributed
17
as
well
Root
17
5 13 20 22 30
2* 3* 5* 7* 8* 14* 16* 17* 18* 20* 21* 22* 27* 29* 33* 34* 38* 39*
B+
Concurrency
Model
• We
consider
page
lock(x)/unlock(x)
of
pages
(only
for
writes!)
P1
P2
P2
8
10
12
15
… 10 15
P1
P2
P1
P2
8
9
10
12
15
Keys
240<=280
Keys
240<=k
Keys
k
<
30
Keys
30<=k<120
Keys
120<=k<240
P1
P2
P2
8
10
12
15
P1
P2
8
10
12
15
P1
P2
8
9
10
A
=
get(current);
end
The
move_right
procedure
scans
right
across
the
leaves
with
lock
coupling.
Easy
case:
DoInsert:
if
A
is
safe
{
insert
new
key/ptr
pair
on
A;
put(A,
current);
unlock(current);
}
Fun
Case:
Must
split
u
=
allocate(1
new
page
for
B);
redistribute
A
over
A
and
B
;
y
=
max
value
on
A
now;
make
high
key
of
B
equal
old
high
key
of
A;
make
right-‐link
of
B
equal
old
right-‐link
of
A;
make
high
key
of
A
equal
y;
make
right-‐link
of
A
point
to
B;
Insert
put
(B,
u);
put
(A,
current);
oldnode
=
current;
new
key/ptr
pair
=
(y,
u);
//
high
key
of
new
page,
new
page
current
=
pop(stack);
lock(current);
A
=
get(current);
move_right();
may
have
3
locks:
oldnode,
and
unlock(oldnode)
two
at
the
parent
level
while
goto
Doinser[on;
moving
right
Deadlock
Free
Total
Order
<
on
Nodes
Consider
pages
a,b
define
a
total
order
<
1. a
<
b
if
b
is
closer
to
the
root
than
a
(different
height)
2. If
a
and
b
are
at
the
same
height,
then
a
<
b
if
b
is
reachable.
P1
P2
8
10
12
15
P1
P2
8
9
10
P1
P2
8
10
9
15
10
15
P1
P2
7
10
12
15
• P1
searches
for
8
12
15
• P2
inserts
9
No[ce
that
P1
would
have
followed
9s
pointer!
How
will
P1
find
8?
Livelock
Livelock
problem
P2
P3
P6
P5
P4
P1