Skip to content

Commit 12769cf

Browse files
authored
Merge pull request #56 from Tom-debug110/main
使用os包中的ReadFile、ReadDir,详情见golang/go#42026
2 parents 68cc5bb + 2a2893c commit 12769cf

File tree

12 files changed

+224
-9
lines changed

12 files changed

+224
-9
lines changed

core/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// Parser 解析器
1414
func Parser() *global.Config {
1515

16-
var addr = flag.String("addr", "0.0.0.0:5678", "设置监听地址和端口")
16+
var addr = flag.String("addr", "127.0.0.1:5678", "设置监听地址和端口")
1717
//兼容windows
1818
dir := fmt.Sprintf(".%sdata", string(os.PathSeparator))
1919

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

3-
import "gofound/core"
3+
import (
4+
"gofound/core"
5+
)
46

57
func main() {
68
//初始化容器和参数解析

sdk/SDK 设计指南.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# GoFound SDK设计指南
2+
3+
## 支持自定义配置
4+
在支持自定义配置的时候,同时提供默认配置项
5+
##
6+
支持`gofound` 提供的所有操作,增删改查等
7+

sdk/base.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}

sdk/client.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 其他配置项

sdk/database.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

sdk/index.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
}

sdk/word.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

searcher/container.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ func (c *Container) GetDataBase(name string) *Engine {
8181
engine = c.NewEngine(name)
8282
c.engines[name] = engine
8383
//释放引擎
84-
8584
}
8685

8786
return engine

searcher/storage/leveldb_storage.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package storage
22

33
import (
4-
"github.com/syndtr/goleveldb/leveldb"
5-
"github.com/syndtr/goleveldb/leveldb/filter"
6-
"github.com/syndtr/goleveldb/leveldb/opt"
74
"log"
85
"sync"
96
"time"
7+
8+
"github.com/syndtr/goleveldb/leveldb"
9+
"github.com/syndtr/goleveldb/leveldb/filter"
10+
"github.com/syndtr/goleveldb/leveldb/opt"
1011
)
1112

1213
// LeveldbStorage TODO 要支持事务

searcher/words/tokenizer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package words
22

33
import (
44
"embed"
5-
"github.com/wangbin/jiebago"
65
"gofound/searcher/utils"
76
"strings"
7+
8+
"github.com/wangbin/jiebago"
89
)
910

1011
var (

web/service/service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func Callback() map[string]interface{} {
2323
"dataSize": system.GetFloat64MB(utils.DirSizeB(global.CONFIG.Data)),
2424
"executable": os.Args[0],
2525
"dbs": global.Container.GetDataBaseNumber(),
26-
//"indexCount": global.Container.GetIndexCount(),
27-
//"documentCount": global.Container.GetDocumentCount(),
26+
//"indexCount": global.container.GetIndexCount(),
27+
//"documentCount": global.container.GetDocumentCount(),
2828
"pid": os.Getpid(),
2929
"enableAuth": global.CONFIG.Auth != "",
3030
"enableGzip": global.CONFIG.EnableGzip,

0 commit comments

Comments
 (0)