-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbufferheap.go
58 lines (45 loc) · 1001 Bytes
/
bufferheap.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
package main
import (
"os"
)
type BufferHeap struct {
bp *BufferPool
Size int
report *Report
}
func NewBufferHeap(file string, recordSize, blockSize, capacity int, report *Report) *BufferHeap {
info, err := os.Stat(file)
if err != nil {
panic(err)
}
size := int(info.Size() / int64(recordSize))
return &BufferHeap{
NewBufferPool(file, recordSize, blockSize, capacity, report),
size,
report,
}
}
func (heap *BufferHeap) Len() int {
return heap.Size
}
func (heap *BufferHeap) Less(i, j int) bool {
i64 := int64(i)
j64 := int64(j)
return heap.bp.GetRecord(i64).Key < heap.bp.GetRecord(j64).Key
}
func (heap *BufferHeap) Swap(i, j int) {
i64 := int64(i)
j64 := int64(j)
iRecord := heap.bp.GetRecord(i64)
jRecord := heap.bp.GetRecord(j64)
heap.bp.WriteRecord(jRecord, i64)
heap.bp.WriteRecord(iRecord, j64)
}
func (heap *BufferHeap) Sort() {
heap.report.Start()
HeapSort(heap)
heap.report.End()
}
func (heap *BufferHeap) Shutdown() {
heap.bp.Shutdown()
}