Skip to content

Commit

Permalink
close map on error
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Feb 1, 2024
1 parent 010dabb commit ecb5c40
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions memiavl/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func NewEmptySnapshot(version uint32) *Snapshot {

// OpenSnapshot parse the version number and the root node index from metadata file,
// and mmap the other files.
func OpenSnapshot(snapshotDir string) (*Snapshot, error) {
func OpenSnapshot(snapshotDir string) (snapshot *Snapshot, err error) {
// read metadata file
bz, err := os.ReadFile(filepath.Join(snapshotDir, FileNameMetadata))
if err != nil {
Expand All @@ -81,28 +81,30 @@ func OpenSnapshot(snapshotDir string) (*Snapshot, error) {
version := binary.LittleEndian.Uint32(bz[8:])

var nodesMap, leavesMap, kvsMap *MmapFile
cleanupHandles := func(err error) error {
errs := []error{err}
if nodesMap != nil {
errs = append(errs, nodesMap.Close())
}
if leavesMap != nil {
errs = append(errs, leavesMap.Close())
}
if kvsMap != nil {
errs = append(errs, kvsMap.Close())
defer func() {
if err != nil {
errs := []error{err}
if nodesMap != nil {
errs = append(errs, nodesMap.Close())
}
if leavesMap != nil {
errs = append(errs, leavesMap.Close())
}
if kvsMap != nil {
errs = append(errs, kvsMap.Close())
}
err = errors.Join(errs...)

Check warning on line 96 in memiavl/snapshot.go

View check run for this annotation

Codecov / codecov/patch

memiavl/snapshot.go#L86-L96

Added lines #L86 - L96 were not covered by tests
}
return errors.Join(errs...)
}
}()

if nodesMap, err = NewMmap(filepath.Join(snapshotDir, FileNameNodes)); err != nil {
return nil, cleanupHandles(err)
return nil, err

Check warning on line 101 in memiavl/snapshot.go

View check run for this annotation

Codecov / codecov/patch

memiavl/snapshot.go#L101

Added line #L101 was not covered by tests
}
if leavesMap, err = NewMmap(filepath.Join(snapshotDir, FileNameLeaves)); err != nil {
return nil, cleanupHandles(err)
return nil, err

Check warning on line 104 in memiavl/snapshot.go

View check run for this annotation

Codecov / codecov/patch

memiavl/snapshot.go#L104

Added line #L104 was not covered by tests
}
if kvsMap, err = NewMmap(filepath.Join(snapshotDir, FileNameKVs)); err != nil {
return nil, cleanupHandles(err)
return nil, err

Check warning on line 107 in memiavl/snapshot.go

View check run for this annotation

Codecov / codecov/patch

memiavl/snapshot.go#L107

Added line #L107 was not covered by tests
}

nodes := nodesMap.Data()
Expand All @@ -111,22 +113,16 @@ func OpenSnapshot(snapshotDir string) (*Snapshot, error) {

// validate nodes length
if len(nodes)%SizeNode != 0 {
return nil, cleanupHandles(
fmt.Errorf("corrupted snapshot, nodes file size %d is not a multiple of %d", len(nodes), SizeNode),
)
return nil, fmt.Errorf("corrupted snapshot, nodes file size %d is not a multiple of %d", len(nodes), SizeNode)

Check warning on line 116 in memiavl/snapshot.go

View check run for this annotation

Codecov / codecov/patch

memiavl/snapshot.go#L116

Added line #L116 was not covered by tests
}
if len(leaves)%SizeLeaf != 0 {
return nil, cleanupHandles(
fmt.Errorf("corrupted snapshot, leaves file size %d is not a multiple of %d", len(leaves), SizeLeaf),
)
return nil, fmt.Errorf("corrupted snapshot, leaves file size %d is not a multiple of %d", len(leaves), SizeLeaf)

Check warning on line 119 in memiavl/snapshot.go

View check run for this annotation

Codecov / codecov/patch

memiavl/snapshot.go#L119

Added line #L119 was not covered by tests
}

nodesLen := len(nodes) / SizeNode
leavesLen := len(leaves) / SizeLeaf
if (leavesLen > 0 && nodesLen+1 != leavesLen) || (leavesLen == 0 && nodesLen != 0) {
return nil, cleanupHandles(
fmt.Errorf("corrupted snapshot, branch nodes size %d don't match leaves size %d", nodesLen, leavesLen),
)
return nil, fmt.Errorf("corrupted snapshot, branch nodes size %d don't match leaves size %d", nodesLen, leavesLen)

Check warning on line 125 in memiavl/snapshot.go

View check run for this annotation

Codecov / codecov/patch

memiavl/snapshot.go#L125

Added line #L125 was not covered by tests
}

nodesData, err := NewNodes(nodes)
Expand All @@ -139,7 +135,7 @@ func OpenSnapshot(snapshotDir string) (*Snapshot, error) {
return nil, err
}

snapshot := &Snapshot{
snapshot = &Snapshot{
nodesMap: nodesMap,
leavesMap: leavesMap,
kvsMap: kvsMap,
Expand Down

0 comments on commit ecb5c40

Please sign in to comment.