-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathindex.go
270 lines (238 loc) · 6.76 KB
/
index.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package gcse
import (
"errors"
"io"
"log"
"os"
"path"
"time"
"github.com/golangplus/errors"
"github.com/golangplus/sort"
"github.com/golangplus/strings"
"golang.org/x/crypto/ssh/terminal"
"github.com/cheggaaa/pb"
"github.com/daviddengcn/gcse/utils"
"github.com/daviddengcn/go-index"
"github.com/daviddengcn/sophie"
"github.com/daviddengcn/sophie/mr"
)
const (
HitsArrFn = "hits"
IndexTextField = "text"
IndexNameField = "name"
IndexPkgField = "pkg"
)
var errNotDocInfo = errors.New("Value is not DocInfo")
// Excludes packages in src which has same full-project with any elements in excl.
func excludeImports(src, excl []string) (dst []string) {
exclPrjsSets := stringsp.NewSet()
for _, pkg := range excl {
exclPrjsSets.Add(FullProjectOfPackage(pkg))
}
for _, pkg := range src {
prj := FullProjectOfPackage(string(pkg))
if !exclPrjsSets.Contain(prj) {
dst = append(dst, pkg)
}
}
return dst
}
func filterDocInfo(docInfo *DocInfo) {
for i, imp := range docInfo.Imports {
if imp == docInfo.Package {
docInfo.Imports = stringsp.SliceRemove(docInfo.Imports, i)
break
}
}
for i, imp := range docInfo.TestImports {
if imp == docInfo.Package {
docInfo.TestImports = stringsp.SliceRemove(docInfo.TestImports, i)
break
}
}
}
func indexAndSaveHits(ts *index.TokenSetSearcher, hits []HitInfo, idxs []int, saveFullHit func(*HitInfo) error) error {
rank := 0
var bar *pb.ProgressBar
if terminal.IsTerminal(int(os.Stdout.Fd())) {
bar = pb.New(len(idxs))
bar.Start()
}
for i := range idxs {
hit := &hits[idxs[i]]
if i > 0 && hit.StaticScore < hits[idxs[i-1]].StaticScore {
rank = i
}
hit.StaticRank = rank
if err := saveFullHit(hit); err != nil {
return err
}
var desc, readme string
desc, hit.Description = hit.Description, ""
readme, hit.ReadmeData = hit.ReadmeData, ""
hit.Imported = nil
hit.TestImported = nil
var nameTokens stringsp.Set
nameTokens = AppendTokens(nameTokens, []byte(hit.Name))
var tokens stringsp.Set
tokens.Add(nameTokens.Elements()...)
tokens = AppendTokens(tokens, []byte(hit.Package))
tokens = AppendTokens(tokens, []byte(desc))
tokens = AppendTokens(tokens, []byte(readme))
tokens = AppendTokens(tokens, []byte(hit.Author))
for _, word := range hit.Exported {
AppendTokens(tokens, []byte(word))
}
ts.AddDoc(map[string]stringsp.Set{
IndexTextField: tokens,
IndexNameField: nameTokens,
IndexPkgField: stringsp.NewSet(hit.Package),
}, *hit)
if bar != nil {
bar.Increment()
}
}
if bar != nil {
bar.FinishPrint("Indexing finished!")
}
utils.DumpMemStats()
return nil
}
func Index(docDB mr.Input, outDir string) (*index.TokenSetSearcher, error) {
utils.DumpMemStats()
docPartCnt, err := docDB.PartCount()
if err != nil {
return nil, err
}
docCount := 0
log.Printf("Generating importsDB ...")
importsDB := NewTokenIndexer("", "")
testImportsDB := NewTokenIndexer("", "")
// per project imported by projects
prjImportsDB := NewTokenIndexer("", "")
type projectStart struct {
StarCount int
LastUpdated time.Time
}
prjStars := make(map[string]projectStart)
// generate importsDB
for i := 0; i < docPartCnt; i++ {
it, err := docDB.Iterator(i)
if err != nil {
return nil, err
}
var pkg sophie.RawString
var docInfo DocInfo
for {
if err := it.Next(&pkg, &docInfo); err != nil {
if errorsp.Cause(err) == io.EOF {
break
}
it.Close()
return nil, err
}
filterDocInfo(&docInfo)
importsDB.PutTokens(string(pkg), stringsp.NewSet(docInfo.Imports...))
testImportsDB.PutTokens(string(pkg), stringsp.NewSet(docInfo.TestImports...))
var projects stringsp.Set
for _, imp := range docInfo.Imports {
projects.Add(FullProjectOfPackage(imp))
}
for _, imp := range docInfo.TestImports {
projects.Add(FullProjectOfPackage(imp))
}
prj := FullProjectOfPackage(string(pkg))
orgProjects := prjImportsDB.TokensOfId(prj)
projects.Add(orgProjects...)
prjImportsDB.PutTokens(prj, projects)
// update stars
if cur, ok := prjStars[prj]; !ok ||
docInfo.LastUpdated.After(cur.LastUpdated) {
prjStars[prj] = projectStart{
StarCount: docInfo.StarCount,
LastUpdated: docInfo.LastUpdated,
}
}
docCount++
}
it.Close()
}
utils.DumpMemStats()
log.Printf("Making HitInfos ...")
hits := make([]HitInfo, 0, docCount)
for i := 0; i < docPartCnt; i++ {
it, err := docDB.Iterator(i)
if err != nil {
return nil, err
}
var pkg sophie.RawString
var hitInfo HitInfo
for {
if err := it.Next(&pkg, &hitInfo.DocInfo); err != nil {
if errorsp.Cause(err) == io.EOF {
break
}
it.Close()
return nil, err
}
filterDocInfo(&hitInfo.DocInfo)
hitInfo.Imported = importsDB.IdsOfToken(hitInfo.Package)
hitInfo.ImportedLen = len(hitInfo.Imported)
hitInfo.TestImported = testImportsDB.IdsOfToken(hitInfo.Package)
hitInfo.TestImportedLen = len(hitInfo.TestImported)
realTestImported := excludeImports(testImportsDB.IdsOfToken(hitInfo.Package), hitInfo.Imported)
prj := FullProjectOfPackage(hitInfo.Package)
impPrjsCnt := len(prjImportsDB.IdsOfToken(prj))
var assignedStarCount = float64(prjStars[prj].StarCount)
if prj != hitInfo.Package {
if impPrjsCnt == 0 {
assignedStarCount = 0
} else {
perStarCount :=
float64(prjStars[prj].StarCount) / float64(impPrjsCnt)
var projects stringsp.Set
for _, imp := range hitInfo.Imported {
projects.Add(FullProjectOfPackage(imp))
}
for _, imp := range hitInfo.TestImported {
projects.Add(FullProjectOfPackage(imp))
}
assignedStarCount = perStarCount * float64(len(projects))
}
}
hitInfo.AssignedStarCount = assignedStarCount
readme := ReadmeToText(hitInfo.ReadmeFn, hitInfo.ReadmeData)
hitInfo.ImportantSentences = ChooseImportantSentenses(readme,
hitInfo.Name, hitInfo.Package)
// StaticScore is calculated after setting all other fields of
// hitInfo
hitInfo.StaticScore = CalcStaticScore(&hitInfo)
hitInfo.TestStaticScore = CalcTestStaticScore(&hitInfo, realTestImported)
hits = append(hits, hitInfo)
}
it.Close()
}
utils.DumpMemStats()
importsDB = nil
testImportsDB = nil
utils.DumpMemStats()
log.Printf("%d hits collected, sorting static-scores in descending order", len(hits))
idxs := sortp.IndexSortF(len(hits), func(i, j int) bool {
return hits[i].StaticScore > hits[j].StaticScore
})
ts := &index.TokenSetSearcher{}
utils.DumpMemStats()
log.Printf("Indexing %d packages to TokenSetSearcher ...", len(idxs))
hitsArr, err := index.CreateConstArray(path.Join(outDir, HitsArrFn))
if err != nil {
return nil, err
}
defer hitsArr.Close()
if err := indexAndSaveHits(ts, hits, idxs, func(hit *HitInfo) error {
_, err := hitsArr.AppendGob(*hit)
return err
}); err != nil {
return nil, err
}
return ts, nil
}