-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmmap.go
More file actions
67 lines (61 loc) · 1.48 KB
/
Copy pathmmap.go
File metadata and controls
67 lines (61 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package goblin
import (
"fmt"
"time"
"golang.org/x/sys/unix"
)
func mb(size int) string {
if size < 1024 {
return fmt.Sprintf("%dB", size)
} else if size < 1024*1024 {
return fmt.Sprintf("%.1fKB", float64(size)/1024)
} else if size < 1024*1024*1024 {
return fmt.Sprintf("%.1fMB", float64(size)/1024/1024)
} else {
return fmt.Sprintf("%.1fGB", float64(size)/1024/1024/1024)
}
}
func (this *DB) remmap(fsize int) error {
var err error
if this.mmap != nil {
err = unix.Munmap(this.mmap)
if err != nil {
return fmt.Errorf("munmap: %w", err)
}
}
this.mmap, err = unix.Mmap(int(this.data.Fd()), 0, fsize, unix.PROT_READ|unix.PROT_WRITE, unix.MAP_SHARED_VALIDATE)
return err
}
func (this *DB) grow() error {
t0 := time.Now()
this.max *= 2
newSize := this.max * this.pageSize
err := this.data.Truncate(int64(newSize))
if err != nil {
return fmt.Errorf("truncate: %w", err)
}
err = this.remmap(newSize)
if err != nil {
return fmt.Errorf("mmap: %w", err)
}
Logger("grow to %s in %v", mb(newSize), time.Since(t0))
return nil
}
func (this *DB) fetch(size int, pages []int) []byte {
out := make([]byte, 0, size)
todo := size
this.m.Lock()
defer this.m.Unlock()
for _, page := range pages {
snap := todo
if snap > this.pageSize {
snap = this.pageSize
}
todo -= snap
start := page * this.pageSize
end := start + snap
//Logger("fetch %d from page %d (%q)", snap, page, string(this.mmap[start:end]))
out = append(out, this.mmap[start:end]...)
}
return out
}