Skip to content

Commit fa28de8

Browse files
ethantkoeniglafriks
authored andcommitted
Make indexer code more reusable (#2590)
1 parent 0b0d85c commit fa28de8

File tree

3 files changed

+83
-35
lines changed

3 files changed

+83
-35
lines changed

models/issue_indexer.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func InitIssueIndexer() {
2525

2626
// populateIssueIndexer populate the issue indexer with issue data
2727
func populateIssueIndexer() error {
28+
batch := indexer.IssueIndexerBatch()
2829
for page := 1; ; page++ {
2930
repos, _, err := Repositories(&SearchRepoOptions{
3031
Page: page,
@@ -34,37 +35,45 @@ func populateIssueIndexer() error {
3435
return fmt.Errorf("Repositories: %v", err)
3536
}
3637
if len(repos) == 0 {
37-
return nil
38+
return batch.Flush()
3839
}
3940
for _, repo := range repos {
4041
issues, err := Issues(&IssuesOptions{
4142
RepoID: repo.ID,
4243
IsClosed: util.OptionalBoolNone,
4344
IsPull: util.OptionalBoolNone,
4445
})
45-
updates := make([]indexer.IssueIndexerUpdate, len(issues))
46-
for i, issue := range issues {
47-
updates[i] = issue.update()
46+
if err != nil {
47+
return err
4848
}
49-
if err = indexer.BatchUpdateIssues(updates...); err != nil {
50-
return fmt.Errorf("BatchUpdate: %v", err)
49+
for _, issue := range issues {
50+
if err := batch.Add(issue.update()); err != nil {
51+
return err
52+
}
5153
}
5254
}
5355
}
5456
}
5557

5658
func processIssueIndexerUpdateQueue() {
59+
batch := indexer.IssueIndexerBatch()
5760
for {
61+
var issueID int64
5862
select {
59-
case issueID := <-issueIndexerUpdateQueue:
60-
issue, err := GetIssueByID(issueID)
61-
if err != nil {
62-
log.Error(4, "issuesIndexer.Index: %v", err)
63-
continue
64-
}
65-
if err = indexer.UpdateIssue(issue.update()); err != nil {
66-
log.Error(4, "issuesIndexer.Index: %v", err)
63+
case issueID = <-issueIndexerUpdateQueue:
64+
default:
65+
// flush whatever updates we currently have, since we
66+
// might have to wait a while
67+
if err := batch.Flush(); err != nil {
68+
log.Error(4, "IssueIndexer: %v", err)
6769
}
70+
issueID = <-issueIndexerUpdateQueue
71+
}
72+
issue, err := GetIssueByID(issueID)
73+
if err != nil {
74+
log.Error(4, "GetIssueByID: %v", err)
75+
} else if err = batch.Add(issue.update()); err != nil {
76+
log.Error(4, "IssueIndexer: %v", err)
6877
}
6978
}
7079
}

modules/indexer/indexer.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"strconv"
1010

1111
"github.com/blevesearch/bleve"
12+
"github.com/blevesearch/bleve/analysis/token/unicodenorm"
13+
"github.com/blevesearch/bleve/mapping"
1214
"github.com/blevesearch/bleve/search/query"
1315
)
1416

@@ -41,3 +43,50 @@ func newMatchPhraseQuery(matchPhrase, field, analyzer string) *query.MatchPhrase
4143
q.Analyzer = analyzer
4244
return q
4345
}
46+
47+
const unicodeNormalizeName = "unicodeNormalize"
48+
49+
func addUnicodeNormalizeTokenFilter(m *mapping.IndexMappingImpl) error {
50+
return m.AddCustomTokenFilter(unicodeNormalizeName, map[string]interface{}{
51+
"type": unicodenorm.Name,
52+
"form": unicodenorm.NFC,
53+
})
54+
}
55+
56+
// Update represents an update to an indexer
57+
type Update interface {
58+
addToBatch(batch *bleve.Batch) error
59+
}
60+
61+
const maxBatchSize = 16
62+
63+
// Batch batch of indexer updates that automatically flushes once it
64+
// reaches a certain size
65+
type Batch struct {
66+
batch *bleve.Batch
67+
index bleve.Index
68+
}
69+
70+
// Add add update to batch, possibly flushing
71+
func (batch *Batch) Add(update Update) error {
72+
if err := update.addToBatch(batch.batch); err != nil {
73+
return err
74+
}
75+
return batch.flushIfFull()
76+
}
77+
78+
func (batch *Batch) flushIfFull() error {
79+
if batch.batch.Size() >= maxBatchSize {
80+
return batch.Flush()
81+
}
82+
return nil
83+
}
84+
85+
// Flush manually flush the batch, regardless of its size
86+
func (batch *Batch) Flush() error {
87+
if err := batch.index.Batch(batch.batch); err != nil {
88+
return err
89+
}
90+
batch.batch.Reset()
91+
return nil
92+
}

modules/indexer/issue.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/blevesearch/bleve"
1414
"github.com/blevesearch/bleve/analysis/analyzer/custom"
1515
"github.com/blevesearch/bleve/analysis/token/lowercase"
16-
"github.com/blevesearch/bleve/analysis/token/unicodenorm"
1716
"github.com/blevesearch/bleve/analysis/tokenizer/unicode"
1817
"github.com/blevesearch/bleve/index/upsidedown"
1918
)
@@ -35,6 +34,10 @@ type IssueIndexerUpdate struct {
3534
Data *IssueIndexerData
3635
}
3736

37+
func (update IssueIndexerUpdate) addToBatch(batch *bleve.Batch) error {
38+
return batch.Index(indexerID(update.IssueID), update.Data)
39+
}
40+
3841
const issueIndexerAnalyzer = "issueIndexer"
3942

4043
// InitIssueIndexer initialize issue indexer
@@ -74,17 +77,13 @@ func createIssueIndexer() error {
7477
docMapping.AddFieldMappingsAt("Content", textFieldMapping)
7578
docMapping.AddFieldMappingsAt("Comments", textFieldMapping)
7679

77-
const unicodeNormNFC = "unicodeNormNFC"
78-
if err := mapping.AddCustomTokenFilter(unicodeNormNFC, map[string]interface{}{
79-
"type": unicodenorm.Name,
80-
"form": unicodenorm.NFC,
81-
}); err != nil {
80+
if err := addUnicodeNormalizeTokenFilter(mapping); err != nil {
8281
return err
8382
} else if err = mapping.AddCustomAnalyzer(issueIndexerAnalyzer, map[string]interface{}{
8483
"type": custom.Name,
8584
"char_filters": []string{},
8685
"tokenizer": unicode.Name,
87-
"token_filters": []string{unicodeNormNFC, lowercase.Name},
86+
"token_filters": []string{unicodeNormalizeName, lowercase.Name},
8887
}); err != nil {
8988
return err
9089
}
@@ -97,21 +96,12 @@ func createIssueIndexer() error {
9796
return err
9897
}
9998

100-
// UpdateIssue update the issue indexer
101-
func UpdateIssue(update IssueIndexerUpdate) error {
102-
return issueIndexer.Index(indexerID(update.IssueID), update.Data)
103-
}
104-
105-
// BatchUpdateIssues perform a batch update of the issue indexer
106-
func BatchUpdateIssues(updates ...IssueIndexerUpdate) error {
107-
batch := issueIndexer.NewBatch()
108-
for _, update := range updates {
109-
err := batch.Index(indexerID(update.IssueID), update.Data)
110-
if err != nil {
111-
return err
112-
}
99+
// IssueIndexerBatch batch to add updates to
100+
func IssueIndexerBatch() *Batch {
101+
return &Batch{
102+
batch: issueIndexer.NewBatch(),
103+
index: issueIndexer,
113104
}
114-
return issueIndexer.Batch(batch)
115105
}
116106

117107
// SearchIssuesByKeyword searches for issues by given conditions.

0 commit comments

Comments
 (0)