forked from nutsdb/nutsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datafile.go
122 lines (101 loc) · 3.16 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
// 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 (
// 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"
)
// 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
}
// ReadEntry returns entry at the given off(offset).
// payloadSize = bucketSize + keySize + valueSize
func (df *DataFile) ReadEntry(off int, payloadSize int64) (e *Entry, err error) {
size := MaxEntryHeaderSize + payloadSize
// Since MaxEntryHeaderSize + payloadSize may be larger than the actual entry size, it needs to be calculated
if int64(off)+size > df.rwManager.Size() {
size = df.rwManager.Size() - int64(off)
}
buf := make([]byte, size)
if _, err := df.rwManager.ReadAt(buf, int64(off)); err != nil {
return nil, err
}
e = new(Entry)
headerSize, err := e.ParseMeta(buf)
if err != nil {
return nil, err
}
// Remove the content after the Header
buf = buf[:int(headerSize+payloadSize)]
if e.IsZero() {
return nil, ErrEntryZero
}
if err := e.checkPayloadSize(payloadSize); err != nil {
return nil, err
}
err = e.ParsePayload(buf[headerSize:])
if err != nil {
return nil, err
}
crc := e.GetCrc(buf[:headerSize])
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()
}