-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatafile.go
More file actions
147 lines (125 loc) · 4.01 KB
/
datafile.go
File metadata and controls
147 lines (125 loc) · 4.01 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
// Copyright 2019 The bmdb Authors. 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 bmdb
import (
"errors"
"log"
"sync/atomic"
)
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 ")
// ErrNotEnoughSpace indicates a reservation failed because segment is full
ErrNotEnoughSpace = errors.New("not enough file space")
)
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()
}
// Reserve atomically reserves n bytes in the data file and returns the
// starting offset. It will return ok=false if the reservation would exceed
// the provided segmentSize.
func (df *DataFile) Reserve(n int64, segmentSize int64) (offset int64, ok bool) {
for {
old := atomic.LoadInt64(&df.ActualSize)
if old+n > segmentSize {
if ioDebug {
log.Printf("DataFile.Reserve: reject reserve n=%d old=%d segmentSize=%d path=%s", n, old, segmentSize, df.path)
}
return 0, false
}
if atomic.CompareAndSwapInt64(&df.ActualSize, old, old+n) {
if ioDebug {
log.Printf("DataFile.Reserve: reserved n=%d at offset=%d path=%s newActual=%d", n, old, df.path, old+n)
}
return old, true
}
}
}