forked from weberhong/goose
/
GooseBuild.go
107 lines (87 loc) · 2.28 KB
/
GooseBuild.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package goose
import (
"github.com/getwe/goose/config"
. "github.com/getwe/goose/database"
log "github.com/getwe/goose/log"
. "github.com/getwe/goose/utils"
"os"
"runtime"
)
// Goose的静态库生成程序.
type GooseBuild struct {
conf config.Conf
staticDB *DBBuilder
staticIndexer *StaticIndexer
fileHd *os.File
fileIter *FileIter
}
func (this *GooseBuild) Run() (err error) {
defer this.fileHd.Close()
// build index
err = this.staticIndexer.BuildIndex(this.fileIter)
if err != nil {
return err
}
// db sync
err = this.staticDB.Sync()
if err != nil {
return err
}
return nil
}
// 根据配置文件进行初始化.
// 需要外部指定索引策略,策略可以重新设计.
// 需要外部知道被索引文件(这个易变信息不适合放配置)
func (this *GooseBuild) Init(confPath string, indexSty IndexStrategy, toIndexFile string) (err error) {
defer func() {
if r := recover(); r != nil {
err = log.Error(r)
}
}()
// load conf
this.conf, err = config.NewConf(confPath)
if err != nil {
return
}
// set max procs
maxProcs := int(this.conf.Int64("GooseBuild.MaxProcs"))
if maxProcs <= 0 {
maxProcs = runtime.NumCPU()
}
runtime.GOMAXPROCS(maxProcs)
// init dbbuilder
dbPath := this.conf.String("GooseBuild.DataBase.DbPath")
transformMaxTermCnt := this.conf.Int64("GooseBuild.DataBase.TransformMaxTermCnt")
maxId := this.conf.Int64("GooseBuild.DataBase.MaxId")
maxIndexFileSize := this.conf.Int64("GooseBuild.DataBase.MaxIndexFileSize")
maxDataFileSize := this.conf.Int64("GooseBuild.DataBase.MaxDataFileSize")
valueSize := this.conf.Int64("GooseBuild.DataBase.ValueSize")
this.staticDB = NewDBBuilder()
err = this.staticDB.Init(dbPath, int(transformMaxTermCnt), InIdType(maxId),
uint32(valueSize), uint32(maxIndexFileSize), uint32(maxDataFileSize))
if err != nil {
return
}
// index strategy global init
err = indexSty.Init(this.conf)
if err != nil {
return
}
// static indexer
this.staticIndexer, err = NewStaticIndexer(this.staticDB, indexSty)
if err != nil {
return
}
// open data file
this.fileHd, err = os.OpenFile(toIndexFile, os.O_RDONLY, 0644)
if err != nil {
return
}
// file iter
this.fileIter = NewFileIter(this.fileHd)
return nil
}
func NewGooseBuild() *GooseBuild {
bui := GooseBuild{}
return &bui
}