Skip to content

Commit bbd7e03

Browse files
committed
base: add DiskFile interface
Add a DiskFile interface for types with a DiskFileNum() method.
1 parent cbfea02 commit bbd7e03

File tree

11 files changed

+41
-21
lines changed

11 files changed

+41
-21
lines changed

blob_rewrite.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,12 @@ func (c *blobFileRewriteCompaction) Execute(jobID JobID, d *DB) error {
181181
// Now that we have the manifest lock, check if the blob file is
182182
// still current. If not, we bubble up ErrCancelledCompaction.
183183
v := d.mu.versions.currentVersion()
184-
currentDiskFileNum, ok := v.BlobFiles.Lookup(c.input.FileID)
184+
currentDiskFile, ok := v.BlobFiles.Lookup(c.input.FileID)
185185
if !ok {
186186
return versionUpdate{}, errors.Wrapf(ErrCancelledCompaction,
187187
"blob file %s became unreferenced", c.input.FileID)
188188
}
189+
currentDiskFileNum := currentDiskFile.DiskFileNum()
189190
// Assert that the current version's disk file number for the blob
190191
// matches the one we rewrote. This compaction should be the only
191192
// rewrite compaction running for this blob file.

blob_rewrite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,6 @@ type constantFileMapping base.DiskFileNum
421421
// Assert that (*inputFileMapping) implements base.BlobFileMapping.
422422
var _ base.BlobFileMapping = constantFileMapping(0)
423423

424-
func (m constantFileMapping) Lookup(fileID base.BlobFileID) (base.DiskFileNum, bool) {
424+
func (m constantFileMapping) Lookup(fileID base.BlobFileID) (base.DiskFile, bool) {
425425
return base.DiskFileNum(m), true
426426
}

checkpoint.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,11 @@ func (d *DB) Checkpoint(
326326
includedBlobFiles[ref.FileID] = struct{}{}
327327

328328
// Map the BlobFileID to a DiskFileNum in the current version.
329-
diskFileNum, ok := current.BlobFiles.Lookup(ref.FileID)
329+
diskFile, ok := current.BlobFiles.Lookup(ref.FileID)
330330
if !ok {
331331
return errors.Errorf("blob file %s not found", ref.FileID)
332332
}
333-
ckErr = copyFile(base.FileTypeBlob, diskFileNum)
333+
ckErr = copyFile(base.FileTypeBlob, diskFile.DiskFileNum())
334334
if ckErr != nil {
335335
return ckErr
336336
}

internal/base/filenames.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,19 @@ func (id BlobFileID) SafeFormat(w redact.SafePrinter, _ rune) {
6464
// manifest.
6565
type BlobReferenceID uint32
6666

67+
// DiskFile is an interface that provides access to a disk file number.
68+
type DiskFile interface {
69+
// DiskFileNum returns the disk file number.
70+
DiskFileNum() DiskFileNum
71+
}
72+
6773
// BlobFileMapping defines the mapping between blob file IDs and disk file numbers.
6874
// It's implemented by *manifest.BlobFileSet.
6975
type BlobFileMapping interface {
70-
// Lookup returns the disk file number for the given blob file ID. It
76+
// Lookup returns a DiskFile for the given blob file ID. It
7177
// returns false for the second return value if the blob file ID is not
7278
// present in the mapping.
73-
Lookup(BlobFileID) (DiskFileNum, bool)
79+
Lookup(BlobFileID) (DiskFile, bool)
7480
}
7581

7682
// A DiskFileNum identifies a file or object with exists on disk.
@@ -83,6 +89,11 @@ func (dfn DiskFileNum) SafeFormat(w redact.SafePrinter, verb rune) {
8389
w.Printf("%06d", redact.SafeUint(dfn))
8490
}
8591

92+
// DiskFileNum implements the DiskFile interface.
93+
func (fdn DiskFileNum) DiskFileNum() DiskFileNum {
94+
return fdn
95+
}
96+
8697
// FileType enumerates the types of files found in a DB.
8798
type FileType int
8899

internal/manifest/blob_metadata.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ func (m *PhysicalBlobFile) String() string {
148148
return redact.StringWithoutMarkers(m)
149149
}
150150

151+
// DiskFileNum implements base.DiskFile.
152+
func (m *PhysicalBlobFile) DiskFileNum() base.DiskFileNum {
153+
return m.FileNum
154+
}
155+
151156
// ref increments the reference count for the blob file.
152157
func (m *PhysicalBlobFile) ref() {
153158
m.refs.Add(+1)
@@ -338,15 +343,14 @@ func (s *BlobFileSet) Count() int {
338343
return s.tree.Count()
339344
}
340345

341-
// Lookup returns the file number of the physical blob file backing the given
342-
// file ID. It returns false for the second return value if the FileID is not
343-
// present in the set.
344-
func (s *BlobFileSet) Lookup(fileID base.BlobFileID) (base.DiskFileNum, bool) {
346+
// Lookup returns the physical blob file backing the given file ID. It returns
347+
// false for the second return value if the FileID is not present in the set.
348+
func (s *BlobFileSet) Lookup(fileID base.BlobFileID) (base.DiskFile, bool) {
345349
phys, ok := s.LookupPhysical(fileID)
346350
if !ok {
347-
return 0, false
351+
return nil, false
348352
}
349-
return phys.FileNum, true
353+
return phys, true
350354
}
351355

352356
// LookupPhysical returns the *PhysicalBlobFile backing the given file ID. It

internal/manifest/blob_metadata_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func TestBlobFileSet_Lookup(t *testing.T) {
182182
for i := 0; i < numBlobFiles; i++ {
183183
fn, ok := set.Lookup(base.BlobFileID(i))
184184
require.True(t, ok)
185-
require.Equal(t, files[i].FileNum, fn)
185+
require.Equal(t, files[i].FileNum, fn.DiskFileNum())
186186
}
187187
}
188188

replay/replay.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,9 +1025,12 @@ func findManifestStart(
10251025

10261026
type blobFileMap map[base.BlobFileID]base.DiskFileNum
10271027

1028-
func (m blobFileMap) Lookup(fileID base.BlobFileID) (base.DiskFileNum, bool) {
1028+
func (m blobFileMap) Lookup(fileID base.BlobFileID) (base.DiskFile, bool) {
10291029
diskFileNum, ok := m[fileID]
1030-
return diskFileNum, ok
1030+
if !ok {
1031+
return nil, false
1032+
}
1033+
return diskFileNum, true
10311034
}
10321035

10331036
// loadFlushedSSTableKeys copies keys from the sstables specified by `fileNums`

sstable/blob/fetcher.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,11 @@ func (r *ValueFetcher) retrieve(ctx context.Context, vh Handle) (val []byte, err
144144
return nil, err
145145
}
146146
}
147-
diskFileNum, ok := r.fileMapping.Lookup(vh.BlobFileID)
147+
diskFile, ok := r.fileMapping.Lookup(vh.BlobFileID)
148148
if !ok {
149149
return nil, errors.AssertionFailedf("blob file %s not found", vh.BlobFileID)
150150
}
151+
diskFileNum := diskFile.DiskFileNum()
151152
if cr.r, cr.closeFunc, err = r.readerProvider.GetValueReader(ctx, diskFileNum); err != nil {
152153
return nil, err
153154
}

sstable/blob/fetcher_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type identityFileMapping struct{}
3232
// Assert that (identityFileMapping) implements base.BlobFileMapping.
3333
var _ base.BlobFileMapping = identityFileMapping{}
3434

35-
func (identityFileMapping) Lookup(blobFileID base.BlobFileID) (base.DiskFileNum, bool) {
35+
func (identityFileMapping) Lookup(blobFileID base.BlobFileID) (base.DiskFile, bool) {
3636
return base.DiskFileNum(blobFileID), true
3737
}
3838

sstable/blob/rewrite.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,6 @@ type inputFileMapping base.DiskFileNum
116116
// Assert that (*inputFileMapping) implements base.BlobFileMapping.
117117
var _ base.BlobFileMapping = inputFileMapping(0)
118118

119-
func (m inputFileMapping) Lookup(fileID base.BlobFileID) (base.DiskFileNum, bool) {
119+
func (m inputFileMapping) Lookup(fileID base.BlobFileID) (base.DiskFile, bool) {
120120
return base.DiskFileNum(m), true
121121
}

0 commit comments

Comments
 (0)