File tree Expand file tree Collapse file tree 12 files changed +224
-9
lines changed Expand file tree Collapse file tree 12 files changed +224
-9
lines changed Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ import (
13
13
// Parser 解析器
14
14
func Parser () * global.Config {
15
15
16
- var addr = flag .String ("addr" , "0 .0.0.0 :5678" , "设置监听地址和端口" )
16
+ var addr = flag .String ("addr" , "127 .0.0.1 :5678" , "设置监听地址和端口" )
17
17
//兼容windows
18
18
dir := fmt .Sprintf (".%sdata" , string (os .PathSeparator ))
19
19
Original file line number Diff line number Diff line change 1
1
package main
2
2
3
- import "gofound/core"
3
+ import (
4
+ "gofound/core"
5
+ )
4
6
5
7
func main () {
6
8
//初始化容器和参数解析
Original file line number Diff line number Diff line change
1
+ # GoFound SDK设计指南
2
+
3
+ ## 支持自定义配置
4
+ 在支持自定义配置的时候,同时提供默认配置项
5
+ ##
6
+ 支持` gofound ` 提供的所有操作,增删改查等
7
+
Original file line number Diff line number Diff line change
1
+ package gofound
2
+
3
+ import (
4
+ "gofound/searcher/model"
5
+ "gofound/searcher/system"
6
+ "runtime"
7
+ )
8
+
9
+ // Query 查询
10
+ func (c * Client ) Query (req * model.SearchRequest ) (* model.SearchResult , error ) {
11
+ r , err := c .container .GetDataBase (req .Database ).MultiSearch (req )
12
+ if err != nil {
13
+ return nil , err
14
+ }
15
+
16
+ return r , nil
17
+ }
18
+
19
+ func (* Client ) GC () {
20
+ runtime .GC ()
21
+ }
22
+ func (c * Client ) Status () (map [string ]interface {}, error ) {
23
+ var m runtime.MemStats
24
+ runtime .ReadMemStats (& m )
25
+
26
+ // TODO 其他系统信息
27
+ r := map [string ]interface {}{
28
+ "memory" : system .GetMemStat (),
29
+ "cpu" : system .GetCPUStatus (),
30
+ "disk" : system .GetDiskStat (),
31
+ }
32
+ return r , nil
33
+ }
Original file line number Diff line number Diff line change
1
+ package gofound
2
+
3
+ import (
4
+ "fmt"
5
+ "gofound/core"
6
+ "gofound/global"
7
+ "gofound/searcher"
8
+ "os"
9
+ "runtime"
10
+ "sync"
11
+ )
12
+
13
+ var once sync.Once
14
+
15
+ // Client 应该对外部屏蔽细节
16
+ // 尽量少的提供接口,但是又要保证功能性
17
+ type Client struct {
18
+ config * global.Config //服务配置
19
+ container * searcher.Container //运行实体
20
+ }
21
+
22
+ func newDefaultConfig () * global.Config {
23
+ return & global.Config {
24
+ Addr : "127.0.0.1:5678" ,
25
+ Data : fmt .Sprintf (".%sdata" , string (os .PathSeparator )),
26
+ Debug : true ,
27
+ Dictionary : "./data/dictionary.txt" ,
28
+ EnableAdmin : true ,
29
+ Gomaxprocs : runtime .NumCPU () * 2 ,
30
+ Shard : 0 ,
31
+ Auth : "" ,
32
+ EnableGzip : true ,
33
+ Timeout : 10 * 60 ,
34
+ }
35
+ }
36
+ func newTokenizerAndContainer (config * global.Config ) * searcher.Container {
37
+ tokenizer := core .NewTokenizer (global .CONFIG .Dictionary )
38
+ return core .NewContainer (tokenizer )
39
+ }
40
+
41
+ // NewClient 通过参数进行配置,必须指定全部参数
42
+ func NewClient (config * global.Config ) * Client {
43
+ global .CONFIG = config
44
+ //初始化分词器
45
+ container := newTokenizerAndContainer (config )
46
+ global .Container = container
47
+ return & Client {
48
+ config : config ,
49
+ container : container ,
50
+ }
51
+ }
52
+
53
+ // Default 使用默认参数创建服务
54
+ func Default () * Client {
55
+ global .CONFIG = newDefaultConfig ()
56
+ container := newTokenizerAndContainer (global .CONFIG )
57
+ global .Container = container
58
+ return & Client {
59
+ config : global .CONFIG ,
60
+ container : container ,
61
+ }
62
+ }
63
+
64
+ // SetAddr 设置Web服务地址
65
+ func (c * Client ) SetAddr (addr string ) * Client {
66
+ if addr == "" {
67
+ return c
68
+ }
69
+ c .config .Addr = addr
70
+ return c
71
+ }
72
+
73
+ // SetData 设置数据存放地址
74
+ func (c * Client ) SetData (dir string ) * Client {
75
+ if dir == "" {
76
+ return c
77
+ }
78
+ c .config .Data = dir
79
+ return c
80
+ }
81
+
82
+ //TODO 其他配置项
Original file line number Diff line number Diff line change
1
+ package gofound
2
+
3
+ import (
4
+ "gofound/searcher"
5
+
6
+ "github.com/syndtr/goleveldb/leveldb/errors"
7
+ )
8
+
9
+ // Show 查看数据库
10
+ func (c * Client ) Show () (map [string ]* searcher.Engine , error ) {
11
+ // 保持分格一致
12
+ return c .container .GetDataBases (), nil
13
+ }
14
+
15
+ // Drop 删除数据库
16
+ func (c * Client ) Drop (dbName string ) error {
17
+ if dbName == "" {
18
+ return errors .New ("database not exist" )
19
+ }
20
+ if err := c .container .DropDataBase (dbName ); err != nil {
21
+ return err
22
+ }
23
+ return nil
24
+ }
25
+
26
+ // Create 创建数据库
27
+ func (c * Client ) Create (dbName string ) (* searcher.Engine , error ) {
28
+ if dbName == "" {
29
+ return nil , errors .New ("database name is empty" )
30
+ }
31
+ return c .container .GetDataBase (dbName ), nil
32
+ }
Original file line number Diff line number Diff line change
1
+ package gofound
2
+
3
+ import (
4
+ "errors"
5
+ "gofound/searcher/model"
6
+ )
7
+
8
+ // AddIndex 添加索引
9
+ func (c * Client ) AddIndex (dbName string , request * model.IndexDoc ) error {
10
+ if request .Text == "" {
11
+ return errors .New ("text is empty" )
12
+ }
13
+ c .container .GetDataBase (dbName ).IndexDocument (request )
14
+
15
+ return nil
16
+ }
17
+
18
+ // BatchAddIndex 批次添加索引
19
+ func (c * Client ) BatchAddIndex (dbName string , documents []* model.IndexDoc ) error {
20
+ db := c .container .GetDataBase (dbName )
21
+ // 数据预处理
22
+ for _ , doc := range documents {
23
+ if doc .Text == "" {
24
+ return errors .New ("text is empty" )
25
+ }
26
+ if doc .Document == nil {
27
+ return errors .New ("document is empty" )
28
+ }
29
+ }
30
+ for _ , doc := range documents {
31
+ go db .IndexDocument (doc )
32
+ }
33
+ return nil
34
+ }
35
+
36
+ // RemoveIndex 删除索引
37
+ func (c * Client ) RemoveIndex (dbName string , data * model.RemoveIndexModel ) error {
38
+ db := c .container .GetDataBase (dbName )
39
+ if err := db .RemoveIndex (data .Id ); err != nil {
40
+ return err
41
+ }
42
+ return nil
43
+ }
Original file line number Diff line number Diff line change
1
+ package gofound
2
+
3
+ // WordCut 分词
4
+ func (c * Client ) WordCut (keyword string ) []string {
5
+ return c .container .Tokenizer .Cut (keyword )
6
+ }
7
+
8
+ // BatchWordCut 批量分词
9
+ func (c * Client ) BatchWordCut (keywords []string ) * [][]string {
10
+ res := make ([][]string , len (keywords ))
11
+ for _ , w := range keywords {
12
+ res = append (res , c .container .Tokenizer .Cut (w ))
13
+ }
14
+ return & res
15
+ }
Original file line number Diff line number Diff line change @@ -81,7 +81,6 @@ func (c *Container) GetDataBase(name string) *Engine {
81
81
engine = c .NewEngine (name )
82
82
c .engines [name ] = engine
83
83
//释放引擎
84
-
85
84
}
86
85
87
86
return engine
Original file line number Diff line number Diff line change 1
1
package storage
2
2
3
3
import (
4
- "github.com/syndtr/goleveldb/leveldb"
5
- "github.com/syndtr/goleveldb/leveldb/filter"
6
- "github.com/syndtr/goleveldb/leveldb/opt"
7
4
"log"
8
5
"sync"
9
6
"time"
7
+
8
+ "github.com/syndtr/goleveldb/leveldb"
9
+ "github.com/syndtr/goleveldb/leveldb/filter"
10
+ "github.com/syndtr/goleveldb/leveldb/opt"
10
11
)
11
12
12
13
// LeveldbStorage TODO 要支持事务
Original file line number Diff line number Diff line change @@ -2,9 +2,10 @@ package words
2
2
3
3
import (
4
4
"embed"
5
- "github.com/wangbin/jiebago"
6
5
"gofound/searcher/utils"
7
6
"strings"
7
+
8
+ "github.com/wangbin/jiebago"
8
9
)
9
10
10
11
var (
Original file line number Diff line number Diff line change @@ -23,8 +23,8 @@ func Callback() map[string]interface{} {
23
23
"dataSize" : system .GetFloat64MB (utils .DirSizeB (global .CONFIG .Data )),
24
24
"executable" : os .Args [0 ],
25
25
"dbs" : global .Container .GetDataBaseNumber (),
26
- //"indexCount": global.Container .GetIndexCount(),
27
- //"documentCount": global.Container .GetDocumentCount(),
26
+ //"indexCount": global.container .GetIndexCount(),
27
+ //"documentCount": global.container .GetDocumentCount(),
28
28
"pid" : os .Getpid (),
29
29
"enableAuth" : global .CONFIG .Auth != "" ,
30
30
"enableGzip" : global .CONFIG .EnableGzip ,
You can’t perform that action at this time.
0 commit comments