forked from nutsdb/nutsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datafile.go
161 lines (130 loc) · 3.73 KB
/
datafile.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
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
// Copyright 2019 The nutsdb Author. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nutsdb
import (
"errors"
)
var (
// ErrCrcZero is returned when crc is 0
ErrCrcZero = errors.New("error crc is 0")
// ErrCrc is returned when crc is error
ErrCrc = errors.New("crc error")
// ErrCapacity is returned when capacity is error.
ErrCapacity = errors.New("capacity error")
ErrEntryZero = errors.New("entry is zero ")
)
const (
// DataSuffix returns the data suffix
DataSuffix = ".dat"
// DataEntryHeaderSize returns the entry header size
DataEntryHeaderSize = 42
)
// DataFile records about data file information.
type DataFile struct {
path string
fileID int64
writeOff int64
ActualSize int64
rwManager RWManager
}
// NewDataFile will return a new DataFile Object.
func NewDataFile(path string, rwManager RWManager) *DataFile {
dataFile := &DataFile{
path: path,
rwManager: rwManager,
}
return dataFile
}
// ReadAt returns entry at the given off(offset).
func (df *DataFile) ReadAt(off int) (e *Entry, err error) {
buf := make([]byte, DataEntryHeaderSize)
if _, err := df.rwManager.ReadAt(buf, int64(off)); err != nil {
return nil, err
}
e = NewEntry()
err = e.ParseMeta(buf)
if err != nil {
return nil, err
}
if e.IsZero() {
return nil, nil
}
meta := e.Meta
off += DataEntryHeaderSize
dataSize := meta.PayloadSize()
dataBuf := make([]byte, dataSize)
_, err = df.rwManager.ReadAt(dataBuf, int64(off))
if err != nil {
return nil, err
}
err = e.ParsePayload(dataBuf)
if err != nil {
return nil, err
}
crc := e.GetCrc(buf)
if crc != e.Meta.Crc {
return nil, ErrCrc
}
return
}
// ReadRecord returns entry at the given off(offset).
// payloadSize = bucketSize + keySize + valueSize
func (df *DataFile) ReadRecord(off int, payloadSize int64) (e *Entry, err error) {
buf := make([]byte, DataEntryHeaderSize+payloadSize)
if _, err := df.rwManager.ReadAt(buf, int64(off)); err != nil {
return nil, err
}
e = new(Entry)
err = e.ParseMeta(buf)
if err != nil {
return nil, err
}
if e.IsZero() {
return nil, ErrEntryZero
}
if err := e.checkPayloadSize(payloadSize); err != nil {
return nil, err
}
err = e.ParsePayload(buf[DataEntryHeaderSize:])
if err != nil {
return nil, err
}
crc := e.GetCrc(buf[:DataEntryHeaderSize])
if crc != e.Meta.Crc {
return nil, ErrCrc
}
return
}
// WriteAt copies data to mapped region from the b slice starting at
// given off and returns number of bytes copied to the mapped region.
func (df *DataFile) WriteAt(b []byte, off int64) (n int, err error) {
return df.rwManager.WriteAt(b, off)
}
// Sync commits the current contents of the file to stable storage.
// Typically, this means flushing the file system's in-memory copy
// of recently written data to disk.
func (df *DataFile) Sync() (err error) {
return df.rwManager.Sync()
}
// Close closes the RWManager.
// If RWManager is FileRWManager represents closes the File,
// rendering it unusable for I/O.
// If RWManager is a MMapRWManager represents Unmap deletes the memory mapped region,
// flushes any remaining changes.
func (df *DataFile) Close() (err error) {
return df.rwManager.Close()
}
func (df *DataFile) Release() (err error) {
return df.rwManager.Release()
}