-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufferpool.go
More file actions
126 lines (104 loc) · 2.78 KB
/
bufferpool.go
File metadata and controls
126 lines (104 loc) · 2.78 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"container/list"
"os"
)
type BufferPool struct {
File *os.File
Blocks map[int64]*BlockElement
List *list.List
RecordSize int64
BlockSize int64
Capacity int
Report *Report
}
type BlockElement struct {
Block *Block
listElement *list.Element
}
func NewBufferPool(path string, recordSize, blockSize, size int, report *Report) *BufferPool {
file, err := os.OpenFile(path, os.O_RDWR, os.ModePerm)
if err != nil {
panic(err)
}
return &BufferPool{
File: file,
Blocks: make(map[int64]*BlockElement),
List: list.New(),
RecordSize: int64(recordSize),
BlockSize: int64(blockSize),
Capacity: size,
Report: report,
}
}
func (b *BufferPool) GetRecord(index int64) *Record {
block, indexInBlock := b.getBlock(index)
rtn := make([]byte, b.RecordSize)
block.Get(&rtn, indexInBlock)
record := NewRecord(rtn[0:2], rtn[2:4])
return record
}
func (b *BufferPool) WriteRecord(record *Record, index int64) {
block, indexInBlock := b.getBlock(index)
bytes := record.ToBytes()
block.Put(bytes, indexInBlock)
}
func (b *BufferPool) getBlock(recordIndex int64) (*Block, int64) {
var totalOffsetInFile int64 = recordIndex * b.RecordSize
blockOffset := totalOffsetInFile / b.BlockSize * b.BlockSize
indexInBlock := totalOffsetInFile % b.BlockSize
// Check if block exists in cache
blockElement, exists := b.Blocks[blockOffset]
if exists == false {
blockElement = b.load(blockOffset)
b.Report.CacheMisses++
} else {
b.Report.CacheHits++
}
b.List.MoveToFront(blockElement.listElement)
block := blockElement.Block
return block, indexInBlock
}
/*
Loads a block into the buffer pool.
If no available slots are available, a block is evicted by LRU.
Offset is the byte offset of the block to be loaded.
*/
func (b *BufferPool) load(offset int64) *BlockElement {
// Create new block
bytes := make([]byte, b.BlockSize)
_, err := b.File.ReadAt(bytes, offset)
if err != nil {
panic(err)
}
b.Report.DiskReads++
// Evict
if b.List.Len() == b.Capacity {
tail := b.List.Back()
blockElement := b.List.Remove(tail).(*BlockElement)
delete(b.Blocks, blockElement.Block.Offset)
b.flush(blockElement.Block)
}
// Add block to LRU queue
block := NewBlock(bytes, offset)
blockElement := &BlockElement{Block: block}
blockElement.listElement = b.List.PushFront(blockElement)
b.Blocks[block.Offset] = blockElement
return blockElement
}
func (b *BufferPool) IsEmpty() bool {
return b.List.Len() == 0
}
func (b *BufferPool) flushAll() {
for item := b.List.Front(); item != nil; item = item.Next() {
b.flush(item.Value.(*BlockElement).Block)
}
}
func (b *BufferPool) flush(block *Block) {
b.File.WriteAt(block.Bytes, block.Offset)
b.Report.DiskWrites++
}
func (b *BufferPool) Shutdown() {
b.flushAll()
b.File.Close()
}