-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre_sentence.go
83 lines (76 loc) · 1.72 KB
/
pre_sentence.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"github.com/shurcooL/httpfs/vfsutil"
"net/http"
"os"
"path/filepath"
"runtime"
"sync"
)
var sen_dir = "/zh_sutta"
var sen_public_ctn = make(chan interface{})
var sen_wg sync.WaitGroup
var sen_list []string
var page_m = make(map[string]string)
func sen_searcher(private_ctn chan interface{}, sen_dir string, word string) {
sen_wg.Add(1)
fs := assets
go sen_walk_paths_recursive(private_ctn, fs, sen_dir, word)
sen_wg.Wait()
close(sen_public_ctn)
runtime.GC()
}
func sen_walk_paths_recursive(private_ctn chan interface{}, fs http.FileSystem, sen_dir string, word string) (err error) {
defer sen_wg.Done()
visit := func(path string, info os.FileInfo, err error) error {
if info.IsDir() && path != sen_dir {
sen_wg.Add(1)
go sen_walk_paths_recursive(private_ctn, fs, path, word)
return filepath.SkipDir
}
if info == nil {
return err
}
if !info.Mode().IsRegular() {
return nil
}
sen_wg.Add(1)
go sen_parse_file(private_ctn, fs, path, word)
return err
}
vfsutil.Walk(fs, sen_dir, visit)
return err
}
func sen_parse_file(private_ctn chan interface{}, fs http.FileSystem, path string, word string) error {
defer sen_wg.Done()
defer runtime.GC()
data, err := vfsutil.ReadFile(fs, path)
if err != nil {
return err
}
switch filepath.Ext(path) {
case ".htm":
m := make(map[string]string)
m[path] = string(data)
println("read page: ", path)
sen_public_ctn <- m
}
return nil
}
func prepare_pages() {
sen_searcher(sen_public_ctn, sen_dir, "")
for {
page, ok := <-sen_public_ctn
if page != nil {
for k, v := range page.(map[string]string) {
page_m[k] = v
}
}
if !ok {
println(len(sen_list))
break
}
}
fmt.Println("finish prepare pages")
}