@@ -73,7 +73,13 @@ func searchInDiskTable(dbDir string, index int, key []byte) ([]byte, bool, error
73
73
prefix := strconv .Itoa (index ) + "-"
74
74
75
75
sparseIndexPath := path .Join (dbDir , prefix + diskTableSparseIndexFileName )
76
- from , to , ok , err := searchInSparseIndex (sparseIndexPath , key )
76
+ sparseIndexFile , err := os .OpenFile (sparseIndexPath , os .O_RDONLY , 0600 )
77
+ if err != nil {
78
+ return nil , false , fmt .Errorf ("failed to open sparse index file: %w" , err )
79
+ }
80
+ defer sparseIndexFile .Close ()
81
+
82
+ from , to , ok , err := searchInSparseIndex (sparseIndexFile , key )
77
83
if err != nil {
78
84
return nil , false , fmt .Errorf ("failed to search in sparse index file %s: %w" , sparseIndexPath , err )
79
85
}
@@ -82,7 +88,13 @@ func searchInDiskTable(dbDir string, index int, key []byte) ([]byte, bool, error
82
88
}
83
89
84
90
indexPath := path .Join (dbDir , prefix + diskTableIndexFileName )
85
- offset , ok , err := searchInIndex (indexPath , from , to , key )
91
+ indexFile , err := os .OpenFile (indexPath , os .O_RDONLY , 0600 )
92
+ if err != nil {
93
+ return nil , false , fmt .Errorf ("failed to open index file: %w" , err )
94
+ }
95
+ defer indexFile .Close ()
96
+
97
+ offset , ok , err := searchInIndex (indexFile , from , to , key )
86
98
if err != nil {
87
99
return nil , false , fmt .Errorf ("failed to search in index file %s: %w" , indexPath , err )
88
100
}
@@ -91,29 +103,41 @@ func searchInDiskTable(dbDir string, index int, key []byte) ([]byte, bool, error
91
103
}
92
104
93
105
dataPath := path .Join (dbDir , prefix + diskTableDataFileName )
94
- value , ok , err := searchInDataFile (dataPath , offset , key )
106
+ dataFile , err := os .OpenFile (dataPath , os .O_RDONLY , 0600 )
107
+ if err != nil {
108
+ return nil , false , fmt .Errorf ("failed to open data file: %w" , err )
109
+ }
110
+ defer dataFile .Close ()
111
+
112
+ value , ok , err := searchInDataFile (dataFile , offset , key )
95
113
if err != nil {
96
114
return nil , false , fmt .Errorf ("failed to search in data file %s: %w" , dataPath , err )
97
115
}
98
116
117
+ if err := sparseIndexFile .Close (); err != nil {
118
+ return nil , false , fmt .Errorf ("failed to close sparse index file: %w" , err )
119
+ }
120
+
121
+ if err := indexFile .Close (); err != nil {
122
+ return nil , false , fmt .Errorf ("failed to close index file: %w" , err )
123
+ }
124
+
125
+ if err := dataFile .Close (); err != nil {
126
+ return nil , false , fmt .Errorf ("failed to close data file: %w" , err )
127
+ }
128
+
99
129
return value , ok , nil
100
130
}
101
131
102
132
// searchInDataFile searches a value by the key in the data file from the given offset.
103
133
// The offset must always point to the beginning of the record.
104
- func searchInDataFile (path string , offset int , searchKey []byte ) ([]byte , bool , error ) {
105
- f , err := os .OpenFile (path , os .O_RDONLY , 0600 )
106
- if err != nil {
107
- return nil , false , err
108
- }
109
- defer f .Close ()
110
-
111
- if _ , err := f .Seek (int64 (offset ), io .SeekCurrent ); err != nil {
134
+ func searchInDataFile (r io.ReadSeeker , offset int , searchKey []byte ) ([]byte , bool , error ) {
135
+ if _ , err := r .Seek (int64 (offset ), io .SeekStart ); err != nil {
112
136
return nil , false , fmt .Errorf ("failed to seek: %w" , err )
113
137
}
114
138
115
139
for {
116
- key , value , err := decode (f )
140
+ key , value , err := decode (r )
117
141
if err != nil && err != io .EOF {
118
142
return nil , false , fmt .Errorf ("failed to read: %w" , err )
119
143
}
@@ -128,19 +152,13 @@ func searchInDataFile(path string, offset int, searchKey []byte) ([]byte, bool,
128
152
}
129
153
130
154
// searchInIndex searches key in the index file in specified range.
131
- func searchInIndex (path string , from , to int , searchKey []byte ) (int , bool , error ) {
132
- f , err := os .OpenFile (path , os .O_RDONLY , 0600 )
133
- if err != nil {
134
- return 0 , false , err
135
- }
136
- defer f .Close ()
137
-
138
- if _ , err := f .Seek (int64 (from ), io .SeekStart ); err != nil {
155
+ func searchInIndex (r io.ReadSeeker , from , to int , searchKey []byte ) (int , bool , error ) {
156
+ if _ , err := r .Seek (int64 (from ), io .SeekStart ); err != nil {
139
157
return 0 , false , fmt .Errorf ("failed to seek: %w" , err )
140
158
}
141
159
142
160
for {
143
- key , value , err := decode (f )
161
+ key , value , err := decode (r )
144
162
if err != nil && err != io .EOF {
145
163
return 0 , false , fmt .Errorf ("failed to read: %w" , err )
146
164
}
@@ -154,7 +172,7 @@ func searchInIndex(path string, from, to int, searchKey []byte) (int, bool, erro
154
172
}
155
173
156
174
if to > from {
157
- current , err := f .Seek (0 , io .SeekCurrent )
175
+ current , err := r .Seek (0 , io .SeekCurrent )
158
176
if err != nil {
159
177
return 0 , false , fmt .Errorf ("failed to seek: %w" , err )
160
178
}
@@ -167,16 +185,10 @@ func searchInIndex(path string, from, to int, searchKey []byte) (int, bool, erro
167
185
}
168
186
169
187
// searchInSparseIndex searches a range between which the key is located.
170
- func searchInSparseIndex (path string , searchKey []byte ) (int , int , bool , error ) {
171
- f , err := os .OpenFile (path , os .O_RDONLY , 0600 )
172
- if err != nil {
173
- return 0 , 0 , false , err
174
- }
175
- defer f .Close ()
176
-
188
+ func searchInSparseIndex (r io.Reader , searchKey []byte ) (int , int , bool , error ) {
177
189
from := - 1
178
190
for {
179
- key , value , err := decode (f )
191
+ key , value , err := decode (r )
180
192
if err != nil && err != io .EOF {
181
193
return 0 , 0 , false , fmt .Errorf ("failed to read: %w" , err )
182
194
}
0 commit comments