-
Notifications
You must be signed in to change notification settings - Fork 0
/
merger.go
110 lines (101 loc) · 2 KB
/
merger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import "bytes"
// merger joins the in-order streams of records from multiple bufferScanners
// into a single in-order stream of records.
type merger struct {
lst scanner
nxt scanner
e error
tail int
heap []scanner
}
// next ...
func (m *merger) next() bool {
m.lst = m.nxt
if m.nxt != nil && m.nxt.next() {
if bytes.Compare(m.nxt.lastRecord(), m.nxt.nextRecord()) == 0 {
return true
}
m.push(m.nxt)
}
if m.tail < 0 {
return false
}
m.nxt = m.pop()
if err := m.nxt.err(); err != nil {
m.e = err
return false
}
return true
}
func (m *merger) nextRecord() []byte {
return m.nxt.nextRecord()
}
func (m *merger) lastRecord() []byte {
if m.lst == nil {
return []byte{}
}
return m.lst.lastRecord()
}
// err ...
func (m *merger) err() error {
return m.e
}
// push ...
func (m *merger) push(s scanner) {
m.tail++
i := m.tail
for i > 0 && bytes.Compare(s.nextRecord(), m.heap[(i-1)/2].nextRecord()) < 0 {
m.heap[i] = m.heap[(i-1)/2]
i = (i - 1) / 2
}
m.heap[i] = s
}
// pop ...
func (m *merger) pop() scanner {
root := m.heap[0]
m.tail--
if m.tail >= 0 {
m.heap[0] = m.heap[m.tail+1]
i := 0
leftChild := 2*i + 1
rightChild := 2*i + 2
for leftChild <= m.tail {
minChild := leftChild
minRecord := m.heap[leftChild].nextRecord()
if rightChild <= m.tail {
rightRecord := m.heap[rightChild].nextRecord()
if bytes.Compare(rightRecord, minRecord) < 0 {
minChild = rightChild
minRecord = m.heap[rightChild].nextRecord()
}
}
if bytes.Compare(m.heap[i].nextRecord(), minRecord) <= 0 {
break
}
tmp := m.heap[i]
m.heap[i] = m.heap[minChild]
m.heap[minChild] = tmp
i = minChild
leftChild = 2*i + 1
rightChild = 2*i + 2
}
}
return root
}
// newMerger ...
func newMerger(scanners []scanner) (*merger, error) {
m := &merger{
tail: -1,
heap: make([]scanner, len(scanners)),
}
for _, s := range scanners {
if s.next() {
m.push(s)
}
if err := s.err(); err != nil {
return nil, err
}
}
return m, nil
}