Skip to content

Commit d6a08ab

Browse files
rscFiloSottile
authored andcommitted
mpt: minor doc updates and bug fixes
Address issues identified by a code review from an LLM.
1 parent d46f4e9 commit d6a08ab

5 files changed

Lines changed: 24 additions & 11 deletions

File tree

mpt/disk.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,13 @@ var _ pmem.File = File(nil)
8484
// A diskTree is an on-disk [Tree].
8585
type diskTree struct {
8686
// mmu is the memory mapping mutex.
87+
//
8788
// All methods except Close do mmu.RLock and mmu.RUnlock
88-
// in order to be allowed to use pmem.Data() aka mem.
89+
// in order to be allowed to read and write pmem.Data() aka mem.
90+
// Note that it is OK to write the memory while holding the "RLock".
91+
// The point of the shared RLock is to stop Close from unmapping
92+
// the memory entirely.
93+
//
8994
// Close calls mmu.Lock/mmu.Unlock to wait for all other
9095
// method calls to finish before unmapping the memory.
9196
mmu sync.RWMutex
@@ -245,8 +250,9 @@ func (t *diskTree) Close() error {
245250
if t.closed {
246251
return fmt.Errorf("tree already closed")
247252
}
253+
t.closed = true
248254
if err := t.pmem.Sync(); err != nil {
249-
return t.broken(err)
255+
t.broken(err)
250256
}
251257
if err := t.pmem.Release(); err != nil {
252258
t.broken(err)
@@ -262,10 +268,12 @@ func (t *diskTree) Close() error {
262268
if err := t.file2.Close(); err != nil {
263269
t.broken(err)
264270
}
271+
if err := t.leaf.Close(); err != nil {
272+
t.broken(err)
273+
}
265274
if t.err != nil {
266275
return t.err
267276
}
268-
t.closed = true
269277
t.err = errors.New("tree is closed") // stop future method calls
270278
return nil
271279
}

mpt/disk_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func testDiskRecovery(t *testing.T) {
236236
t.Fatal(err)
237237
}
238238
tree := xtree.(*diskTree)
239-
defer tree.Close() // relelase pmem on test failure
239+
defer tree.Close() // release pmem on test failure
240240

241241
tree.pmem.SetConstantFlushing(true)
242242
tt.tree = tree

mpt/dmem.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ func (t *diskTree) snap(version int64) error {
8888
// updating arbitrarily many hashes during rehash.
8989
// Without group, ordering matters: write hash before dirty
9090
// and both before version.
91+
//
92+
// Also note: dirty implies that tree is non-empty, so there is a root.
9193
root, err := t.node(t.hdr().root())
9294
if err != nil {
9395
return err
@@ -113,6 +115,9 @@ func (t *diskTree) snap(version int64) error {
113115

114116
// Version returns version information about the tree.
115117
func (t *diskTree) Version() (version int64, exact bool) {
118+
t.mmu.RLock()
119+
defer t.mmu.RUnlock()
120+
116121
hdr := t.hdr()
117122
return hdr.version(), hdr.exact()
118123
}

mpt/internal/slicemath/slicemath.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import "unsafe"
77

88
// contains reports whether big contains little;
99
// that is, it reports whether little is a subslice of big.
10+
// If either big or little is empty, contains returns false.
1011
func contains(big, little []byte) bool {
11-
return uintptr(unsafe.Pointer(&big[0])) <= uintptr(unsafe.Pointer(&little[0])) &&
12+
return len(big) > 0 && len(little) > 0 &&
13+
uintptr(unsafe.Pointer(&big[0])) <= uintptr(unsafe.Pointer(&little[0])) &&
1214
uintptr(unsafe.Pointer(&little[len(little)-1])) <= uintptr(unsafe.Pointer(&big[len(big)-1]))
1315
}
1416

1517
// Offset reports little's starting position within big.
1618
// If big does not contain little, Offset returns ^uintptr(0), false.
17-
// The caller must have checked sliceContains(big, little) already.
1819
func Offset(big, little []byte) (offset uintptr, ok bool) {
1920
if !contains(big, little) {
2021
return ^uintptr(0), false

mpt/tree.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ type Tree interface {
6767
// to flush the changes to disk. (If the files are *os.File files,
6868
// Sync calls fsync(2).)
6969
//
70+
// Sync must not be called concurrently with other calls to Sync
71+
// or (as noted above) with calls to Set.
72+
//
7073
// Even in the absence of calls to Sync, a Tree provides the
7174
// guarantee that on recovery from a crash, it can identify the
7275
// latest snapshot whose Set calls are fully included in the tree.
@@ -366,11 +369,7 @@ func hashInner(b int, left, right Hash) Hash {
366369
copy(enc[:32], left[:])
367370
copy(enc[32:64], right[:])
368371
enc[64] = byte(b)
369-
h := sha256.Sum256(enc[:])
370-
if right == (Hash{}) {
371-
panic("zero")
372-
}
373-
return h
372+
return sha256.Sum256(enc[:])
374373
}
375374

376375
func reduce(s []node) []node {

0 commit comments

Comments
 (0)