forked from go-ego/riot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
riot.go
190 lines (153 loc) · 4.1 KB
/
riot.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2017 ego authors
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package riot
import (
"bytes"
"log"
"os"
"strings"
"encoding/gob"
"github.com/go-ego/murmur"
"github.com/go-ego/riot/core"
"github.com/go-ego/riot/types"
toml "github.com/go-vgo/gt/conf"
)
// New create a new engine with mode
func New(conf ...interface{}) *Engine {
// func (engine *Engine) New(conf com.Config) *Engine{
if len(conf) > 0 && strings.HasSuffix(conf[0].(string), ".toml") {
var (
config types.EngineOpts
searcher = &Engine{}
)
fs := conf[0].(string)
log.Println("conf path is: ", fs)
toml.Init(fs, &config)
go toml.Watch(fs, &config)
searcher.Init(config)
return searcher
}
return NewEngine(conf...)
}
// NewEngine create a new engine
func NewEngine(conf ...interface{}) *Engine {
var (
searcher = &Engine{}
path = DefaultPath
storageShards = 10
numShards = 10
segmentDict string
)
if len(conf) > 0 {
segmentDict = conf[0].(string)
}
if len(conf) > 1 {
path = conf[1].(string)
}
if len(conf) > 2 {
numShards = conf[2].(int)
storageShards = conf[2].(int)
}
searcher.Init(types.EngineOpts{
// Using: using,
StoreShards: storageShards,
NumShards: numShards,
IndexerOpts: &types.IndexerOpts{
IndexType: types.DocIdsIndex,
},
UseStore: true,
StoreFolder: path,
// StoreEngine: storageEngine,
GseDict: segmentDict,
// StopTokenFile: stopTokenFile,
})
// defer searcher.Close()
os.MkdirAll(path, 0777)
// 等待索引刷新完毕
// searcher.Flush()
// log.Println("recover index number: ", searcher.NumDocsIndexed())
return searcher
}
// func (engine *Engine) IsDocExist(docId uint64) bool {
// return core.IsDocExist(docId)
// }
// HasDoc if the document is exist return true
func (engine *Engine) HasDoc(docId string) bool {
for shard := 0; shard < engine.initOptions.NumShards; shard++ {
engine.indexers = append(engine.indexers, core.Indexer{})
has := engine.indexers[shard].HasDoc(docId)
if has {
return true
}
}
return false
}
// HasDocDB if the document is exist in the database
// return true
func (engine *Engine) HasDocDB(docId string) bool {
shard := murmur.Sum32(docId) % uint32(engine.initOptions.StoreShards)
has, err := engine.dbs[shard].Has([]byte(docId))
if err != nil {
log.Println("engine.dbs[shard].Has(b[0:length]): ", err)
}
return has
}
// GetDBAllIds get all the DocId from the storage database
// and return
// 从数据库遍历所有的 DocId, 并返回
func (engine *Engine) GetDBAllIds() []string {
docsId := make([]string, 0)
for i := range engine.dbs {
engine.dbs[i].ForEach(func(k, v []byte) error {
// fmt.Println(k, v)
docsId = append(docsId, string(k))
return nil
})
}
return docsId
}
// GetDBAllDocs get the db all docs
func (engine *Engine) GetDBAllDocs() (docsId []string, docsData []types.DocData) {
for i := range engine.dbs {
engine.dbs[i].ForEach(func(key, val []byte) error {
// fmt.Println(k, v)
docsId = append(docsId, string(key))
buf := bytes.NewReader(val)
dec := gob.NewDecoder(buf)
var data types.DocData
err := dec.Decode(&data)
if err != nil {
log.Println("dec.decode: ", err)
}
docsData = append(docsData, data)
return nil
})
}
return docsId, docsData
}
// GetAllDocIds get all the DocId from the storage database
// and return
// 从数据库遍历所有的 DocId, 并返回
func (engine *Engine) GetAllDocIds() []string {
return engine.GetDBAllIds()
}
// Try handler(err)
func Try(fun func(), handler func(interface{})) {
defer func() {
if err := recover(); err != nil {
handler(err)
}
}()
fun()
}