-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.go
68 lines (62 loc) · 1.01 KB
/
find.go
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
68
package main
import (
"fmt"
"log"
"os"
"sync"
"syscall"
)
func main() {
chunk := 1024 * 1024 * 10
path := "TestData.txt"
f, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
fi, err := f.Stat()
if err != nil {
log.Fatal(err)
}
size := int(fi.Size())
mem, err := syscall.Mmap(int(f.Fd()), 0, size, syscall.PROT_READ, syscall.MAP_SHARED)
if err != nil {
log.Fatal(err)
}
needles := make(chan int)
var wg sync.WaitGroup
done := false
for i := 0; i < size; i += chunk {
to := i + chunk
if to > size {
to = size
}
wg.Add(1)
go func(from, to int) {
for ; from < to; from += 2 {
if mem[from] == '1' ||
mem[from+1] == '1' {
if mem[from+1] == '1' {
needles <- from + 1
} else {
needles <- from
}
done = true
close(needles)
}
}
wg.Done()
}(i, to)
if done {
break
}
}
for needle := range needles {
fmt.Printf("needle at %d\n", needle)
}
wg.Wait()
err = syscall.Munmap(mem)
if err != nil {
log.Fatal(err)
}
f.Close()
}