@@ -14,49 +14,50 @@ const (
14
14
MergeFinaFileSuffix = "mergeFina"
15
15
)
16
16
17
- // DataFile 数据文件
17
+ // DataFile represents a data file.
18
18
type DataFile struct {
19
- FileID uint32 //文件id
20
- WriteOff int64 //文件写到了哪个位置
21
- IoManager fileio.IOManager //io 读写操作
19
+ FileID uint32 // File ID
20
+ WriteOff int64 // Position where the file is currently being written
21
+ IoManager fileio.IOManager // IO read/write operations
22
22
}
23
23
24
- // OpenDataFile 打开新的数据文件
25
- func OpenDataFile (dirPath string , fildID uint32 , fileSize int64 , fioType int8 ) (* DataFile , error ) {
26
- fileName := GetDataFileName (dirPath , fildID )
27
- return newDataFile (fileName , fildID , fileSize , fioType )
24
+ // OpenDataFile opens a new data file.
25
+ func OpenDataFile (dirPath string , fileID uint32 , fileSize int64 , fioType int8 ) (* DataFile , error ) {
26
+ fileName := GetDataFileName (dirPath , fileID )
27
+ return newDataFile (fileName , fileID , fileSize , fioType )
28
28
}
29
29
30
- func GetDataFileName (dirPath string , fildID uint32 ) string {
31
- return filepath .Join (dirPath , fmt .Sprintf ("%09d" , fildID )+ DataFileSuffix )
30
+ // GetDataFileName returns the file name for a data file.
31
+ func GetDataFileName (dirPath string , fileID uint32 ) string {
32
+ return filepath .Join (dirPath , fmt .Sprintf ("%09d" , fileID )+ DataFileSuffix )
32
33
}
33
34
34
- // OpenHintFile 打开 Hint 索引文件
35
+ // OpenHintFile opens the hint index file.
35
36
func OpenHintFile (dirPath string , fileSize int64 , fioType int8 ) (* DataFile , error ) {
36
37
fileName := filepath .Join (dirPath , HintFileSuffix )
37
38
return newDataFile (fileName , 0 , fileSize , fioType )
38
39
}
39
40
40
- // OpenMergeFinaFile 打开标识 merge 完成的文件
41
+ // OpenMergeFinaFile opens the file that indicates merge completion.
41
42
func OpenMergeFinaFile (dirPath string , fileSize int64 , fioType int8 ) (* DataFile , error ) {
42
43
fileName := filepath .Join (dirPath , MergeFinaFileSuffix )
43
44
return newDataFile (fileName , 0 , fileSize , fioType )
44
45
}
45
46
46
- func newDataFile (dirPath string , fildID uint32 , fileSize int64 , fioType int8 ) (* DataFile , error ) {
47
- //初始化 IOManager 管理器接口
47
+ func newDataFile (dirPath string , fileID uint32 , fileSize int64 , fioType int8 ) (* DataFile , error ) {
48
+ // Initialize the IOManager interface
48
49
ioManager , err := fileio .NewIOManager (dirPath , fileSize , fioType )
49
50
if err != nil {
50
51
return nil , err
51
52
}
52
53
return & DataFile {
53
- FileID : fildID ,
54
+ FileID : fileID ,
54
55
WriteOff : 0 ,
55
56
IoManager : ioManager ,
56
57
}, nil
57
58
}
58
59
59
- // ReadLogRecord 根据 offset 从数据文件中读取 logRecord
60
+ // ReadLogRecord reads a log record from the data file based on the offset.
60
61
func (df * DataFile ) ReadLogRecord (offset int64 ) (* LogRecord , int64 , error ) {
61
62
fileSize , err := df .IoManager .Size ()
62
63
if err != nil {
@@ -68,39 +69,39 @@ func (df *DataFile) ReadLogRecord(offset int64) (*LogRecord, int64, error) {
68
69
headerBytes = fileSize - offset
69
70
}
70
71
71
- // 读取 header 信息
72
+ // Read header information
72
73
headerBuf , err := df .readNBytes (headerBytes , offset )
73
74
if err != nil {
74
75
return nil , 0 , err
75
76
}
76
77
77
78
header , headerSize := decodeLogRecordHeader (headerBuf )
78
- // 下面俩个条件表示读到了文件末尾,直接返回 EOF
79
+ // The following conditions indicate reaching the end of the file, directly return EOF
79
80
if header == nil {
80
81
return nil , 0 , io .EOF
81
82
}
82
83
if header .crc == 0 && header .keySize == 0 && header .valueSize == 0 {
83
84
return nil , 0 , io .EOF
84
85
}
85
86
86
- // 取出对应的 key 和 value 的长度
87
+ // Retrieve the lengths of the key and value
87
88
keySize , valueSize := int64 (header .keySize ), int64 (header .valueSize )
88
89
var recordSize = headerSize + keySize + valueSize
89
90
90
91
logRecord := & LogRecord {Type : header .recordType }
91
92
92
- // 读取用户实际存储的 key/value 数据
93
+ // Read the actual user-stored key/value data
93
94
if keySize > 0 || valueSize > 0 {
94
95
kvBuf , err := df .readNBytes (keySize + valueSize , headerSize + offset )
95
96
if err != nil {
96
97
return nil , 0 , err
97
98
}
98
- // 解码
99
+ // Decode
99
100
logRecord .Key = kvBuf [:keySize ]
100
101
logRecord .Value = kvBuf [keySize :]
101
102
}
102
103
103
- // 校验 crc (检查数据的有效性)
104
+ // Verify CRC (check data integrity)
104
105
crc := getLogRecordCRC (logRecord , headerBuf [crc32 .Size :headerSize ])
105
106
if crc != header .crc {
106
107
return nil , 0 , ErrInvalidCRC
@@ -117,7 +118,7 @@ func (df *DataFile) Write(buf []byte) error {
117
118
return nil
118
119
}
119
120
120
- // WriteHintRecord 写入索引信息到 hint 文件中
121
+ // WriteHintRecord writes index information to the hint file.
121
122
func (df * DataFile ) WriteHintRecord (key []byte , pst * LogRecordPst ) error {
122
123
record := & LogRecord {
123
124
Key : key ,
0 commit comments