-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatafile.go
More file actions
220 lines (192 loc) · 4.6 KB
/
Copy pathdatafile.go
File metadata and controls
220 lines (192 loc) · 4.6 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package beecask
import (
"container/list"
"encoding/binary"
"hash/crc32"
"io"
"os"
"sync"
"github.com/yplusplus/ylog"
)
const (
defaultCacheCapacity = 512
)
type RecordFn func(*Record, uint64, int64) error
type DataFile struct {
file RandomAccessFile
fileId uint64
}
func NewDataFile(path string, fileId uint64) (*DataFile, error) {
f, err := os.OpenFile(path, os.O_RDONLY, 0644)
if err != nil {
ylog.Error(err)
return nil, err
}
file, err := NewMmapFile(f)
if err != nil {
ylog.Error(err)
f.Close()
return nil, err
}
ylog.Tracef("Open datafile[%d]", fileId)
return &DataFile{file: file, fileId: fileId}, nil
}
// ReadRecordAt reads a record from specific offset
func (df *DataFile) ReadRecordAt(offset int64) (*Record, error) {
buff, err := df.file.ReadAt(offset, DATA_ITEM_HEADER_SIZE)
if err != nil {
ylog.Warn(err)
return nil, err
}
// calculate crc
r := &Record{
crc: binary.LittleEndian.Uint32(buff[0:4]),
flag: binary.LittleEndian.Uint32(buff[4:8]),
expiration: int64(binary.LittleEndian.Uint64(buff[8:16])),
keySize: binary.LittleEndian.Uint32(buff[16:20]),
valueSize: binary.LittleEndian.Uint32(buff[20:24]),
key: nil,
value: nil,
}
offset += DATA_ITEM_HEADER_SIZE
r.key, err = df.file.ReadAt(offset, int64(r.keySize))
if err != nil {
ylog.Warn(err)
return nil, err
}
offset += int64(r.keySize)
r.value, err = df.file.ReadAt(offset, int64(r.valueSize))
if err != nil {
ylog.Warn(err)
return nil, err
}
crc := crc32.ChecksumIEEE(buff[4:])
crc = crc32.Update(crc, crc32.IEEETable, r.key)
crc = crc32.Update(crc, crc32.IEEETable, r.value)
if crc != r.crc {
ylog.Errorf("check crc32 failed")
return nil, ErrDataCorruption
}
return r, nil
}
// ForEachRecord runs fn on each record until encounters error
func (df *DataFile) ForEachRecord(fn RecordFn) error {
var offset int64 = 0
for {
r, err := df.ReadRecordAt(offset)
if err != nil {
if err == io.EOF {
break
}
ylog.Warn(err)
return err
}
err = fn(r, df.fileId, offset)
if err != nil {
return err
}
offset += r.Size()
}
return nil
}
func (df *DataFile) Size() int64 {
return df.file.Size()
}
func (df *DataFile) Close() error {
ylog.Tracef("Close datafile[%d]", df.fileId)
return df.file.Close()
}
type CacheEntry struct {
df *DataFile
refCount int32 // Contains references from external and cache.list
inList bool // indicating entry is in list or not
}
// DataFileCache is a LRU cache which caches data files
type DataFileCache struct {
l *list.List
hash map[uint64]*list.Element
capacity int
mu sync.Mutex
}
func NewDataFileCache(capacity int) *DataFileCache {
if capacity <= 0 {
capacity = defaultCacheCapacity
}
return &DataFileCache{
l: list.New(),
hash: make(map[uint64]*list.Element, capacity),
capacity: capacity,
}
}
func (cache *DataFileCache) Ref(path string, fileId uint64) (*CacheEntry, error) {
cache.mu.Lock()
defer cache.mu.Unlock()
var entry *CacheEntry
ele, ok := cache.hash[fileId]
if !ok {
ylog.Tracef("Datafile[%d] not in cache, create a cache entry associated with it.", fileId)
// Create a new cache entry when not in cache
df, err := NewDataFile(path, fileId)
if err != nil {
ylog.Errorf("New datafile[%s] failed, err = %s", path, err)
return nil, err
}
entry = &CacheEntry{
df: df,
refCount: 1,
inList: true,
}
ele = cache.l.PushFront(entry)
cache.hash[fileId] = ele
} else {
ylog.Tracef("Datafile[%d] in cache", fileId)
}
entry = ele.Value.(*CacheEntry)
entry.refCount++
if entry.inList {
cache.l.MoveToFront(ele)
}
for cache.l.Len() > cache.capacity {
e := cache.l.Remove(cache.l.Back()).(*CacheEntry)
e.inList = false
cache.unref(e)
}
return entry, nil
}
// unref requires cache.mu held
func (cache *DataFileCache) unref(entry *CacheEntry) {
entry.refCount--
if entry.refCount == 0 && !entry.inList {
// No reference and not in cache
// Close the associated file
delete(cache.hash, entry.df.fileId)
entry.df.Close()
}
}
func (cache *DataFileCache) Unref(entry *CacheEntry) {
cache.mu.Lock()
defer cache.mu.Unlock()
cache.unref(entry)
}
func (cache *DataFileCache) Evict(fileId uint64) {
cache.mu.Lock()
defer cache.mu.Unlock()
ele, ok := cache.hash[fileId]
if ok {
entry := ele.Value.(*CacheEntry)
if entry.inList {
cache.l.Remove(ele)
entry.inList = false
cache.unref(entry)
}
}
}
func (cache *DataFileCache) Close() {
cache.mu.Lock()
defer cache.mu.Unlock()
for cache.l.Len() > 0 {
entry := cache.l.Remove(cache.l.Back()).(*CacheEntry)
entry.inList = false
cache.unref(entry)
}
}