Skip to content

Commit 0d92ab1

Browse files
headerfs: use headerFile in headerStore
1 parent da3a069 commit 0d92ab1

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

headerfs/file_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,11 @@ func TestAppendRow(t *testing.T) {
158158
truncFn: test.truncFn,
159159
}
160160

161+
hdrFile := &headerFile{file: mockFile}
162+
161163
// Create a header store with our mock file.
162164
h := &headerStore{
163-
file: mockFile,
165+
headerFile: hdrFile,
164166
headerIndex: &headerIndex{indexType: Block},
165167
}
166168

@@ -207,8 +209,10 @@ func BenchmarkHeaderStoreAppendRaw(b *testing.B) {
207209
tmpFile, cleanup := createFile(b, "header_benchmark")
208210
defer cleanup()
209211

212+
hdrFile := &headerFile{file: tmpFile}
213+
210214
store := &headerStore{
211-
file: tmpFile,
215+
headerFile: hdrFile,
212216
headerIndex: &headerIndex{indexType: Block},
213217
}
214218

headerfs/store.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ type File interface {
105105
Name() string
106106
}
107107

108+
type headerFile struct {
109+
file File
110+
}
111+
108112
// headerStore combines a on-disk set of headers within a flat file in addition
109113
// to a database which indexes that flat file. Together, these two abstractions
110114
// can be used in order to build an indexed header store for any type of
@@ -115,7 +119,7 @@ type File interface {
115119
type headerStore struct {
116120
mtx sync.RWMutex // nolint:structcheck // false positive because used as embedded struct only
117121

118-
file File
122+
*headerFile
119123

120124
*headerIndex
121125
}
@@ -126,6 +130,8 @@ type headerStore struct {
126130
func newHeaderStore(db walletdb.DB, filePath string,
127131
hType HeaderType) (*headerStore, error) {
128132

133+
var headerFile = &headerFile{}
134+
129135
var flatFileName string
130136
switch hType {
131137
case Block:
@@ -141,10 +147,11 @@ func newHeaderStore(db walletdb.DB, filePath string,
141147
// We'll open the file, creating it if necessary and ensuring that all
142148
// writes are actually appends to the end of the file.
143149
fileFlags := os.O_RDWR | os.O_APPEND | os.O_CREATE
144-
headerFile, err := os.OpenFile(flatFileName, fileFlags, 0644)
150+
file, err := os.OpenFile(flatFileName, fileFlags, 0644)
145151
if err != nil {
146152
return nil, err
147153
}
154+
headerFile.file = file
148155

149156
// With the file open, we'll then create the header index so we can
150157
// have random access into the flat files.
@@ -154,7 +161,7 @@ func newHeaderStore(db walletdb.DB, filePath string,
154161
}
155162

156163
return &headerStore{
157-
file: headerFile,
164+
headerFile: headerFile,
158165
headerIndex: index,
159166
}, nil
160167
}

0 commit comments

Comments
 (0)