summaryrefslogtreecommitdiffstats
path: root/modules/indexer/issues
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2019-02-21 13:01:28 +0800
committerGitHub <noreply@github.com>2019-02-21 13:01:28 +0800
commit477ef462510d69a8a31e008fb6e64059dc6cc148 (patch)
tree2c7884ad454e6301d71e6f5ed3fc2247a190dedc /modules/indexer/issues
parent0751153613bfd2e39cf28e83bbe04b76641d222f (diff)
downloadgitea-477ef462510d69a8a31e008fb6e64059dc6cc148.tar.gz
gitea-477ef462510d69a8a31e008fb6e64059dc6cc148.zip
Add more tests and docs for issue indexer, add db indexer type for searching from database (#6144)
* add more tests and docs for issue indexer, add db indexer type for searching from database * fix typo * fix typo * fix lint * improve docs
Diffstat (limited to 'modules/indexer/issues')
-rw-r--r--modules/indexer/issues/db.go45
-rw-r--r--modules/indexer/issues/indexer.go11
-rw-r--r--modules/indexer/issues/indexer_test.go4
-rw-r--r--modules/indexer/issues/queue.go16
-rw-r--r--modules/indexer/issues/queue_channel.go8
-rw-r--r--modules/indexer/issues/queue_disk.go10
6 files changed, 84 insertions, 10 deletions
diff --git a/modules/indexer/issues/db.go b/modules/indexer/issues/db.go
new file mode 100644
index 0000000000..6e7f0c1a6e
--- /dev/null
+++ b/modules/indexer/issues/db.go
@@ -0,0 +1,45 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package issues
+
+import "code.gitea.io/gitea/models"
+
+// DBIndexer implements Indexer inteface to use database's like search
+type DBIndexer struct {
+}
+
+// Init dummy function
+func (db *DBIndexer) Init() (bool, error) {
+ return false, nil
+}
+
+// Index dummy function
+func (db *DBIndexer) Index(issue []*IndexerData) error {
+ return nil
+}
+
+// Delete dummy function
+func (db *DBIndexer) Delete(ids ...int64) error {
+ return nil
+}
+
+// Search dummy function
+func (db *DBIndexer) Search(kw string, repoID int64, limit, start int) (*SearchResult, error) {
+ total, ids, err := models.SearchIssueIDsByKeyword(kw, repoID, limit, start)
+ if err != nil {
+ return nil, err
+ }
+ var result = SearchResult{
+ Total: total,
+ Hits: make([]Match, 0, limit),
+ }
+ for _, id := range ids {
+ result.Hits = append(result.Hits, Match{
+ ID: id,
+ RepoID: repoID,
+ })
+ }
+ return &result, nil
+}
diff --git a/modules/indexer/issues/indexer.go b/modules/indexer/issues/indexer.go
index 41af5c36b6..c5d6d05a60 100644
--- a/modules/indexer/issues/indexer.go
+++ b/modules/indexer/issues/indexer.go
@@ -33,7 +33,8 @@ type Match struct {
// SearchResult represents search results
type SearchResult struct {
- Hits []Match
+ Total int64
+ Hits []Match
}
// Indexer defines an inteface to indexer issues contents
@@ -54,6 +55,7 @@ var (
// all issue index done.
func InitIssueIndexer(syncReindex bool) error {
var populate bool
+ var dummyQueue bool
switch setting.Indexer.IssueType {
case "bleve":
issueIndexer = NewBleveIndexer(setting.Indexer.IssuePath)
@@ -62,10 +64,17 @@ func InitIssueIndexer(syncReindex bool) error {
return err
}
populate = !exist
+ case "db":
+ issueIndexer = &DBIndexer{}
+ dummyQueue = true
default:
return fmt.Errorf("unknow issue indexer type: %s", setting.Indexer.IssueType)
}
+ if dummyQueue {
+ return nil
+ }
+
var err error
switch setting.Indexer.IssueIndexerQueueType {
case setting.LevelQueueType:
diff --git a/modules/indexer/issues/indexer_test.go b/modules/indexer/issues/indexer_test.go
index 1b6bdec53e..66087e3e6b 100644
--- a/modules/indexer/issues/indexer_test.go
+++ b/modules/indexer/issues/indexer_test.go
@@ -48,4 +48,8 @@ func TestSearchIssues(t *testing.T) {
ids, err = SearchIssuesByKeyword(1, "for")
assert.NoError(t, err)
assert.EqualValues(t, []int64{1, 2, 3, 5}, ids)
+
+ ids, err = SearchIssuesByKeyword(1, "good")
+ assert.NoError(t, err)
+ assert.EqualValues(t, []int64{1}, ids)
}
diff --git a/modules/indexer/issues/queue.go b/modules/indexer/issues/queue.go
index 6f4ee4c13a..f93e5c47a4 100644
--- a/modules/indexer/issues/queue.go
+++ b/modules/indexer/issues/queue.go
@@ -7,5 +7,19 @@ package issues
// Queue defines an interface to save an issue indexer queue
type Queue interface {
Run() error
- Push(*IndexerData)
+ Push(*IndexerData) error
+}
+
+// DummyQueue represents an empty queue
+type DummyQueue struct {
+}
+
+// Run starts to run the queue
+func (b *DummyQueue) Run() error {
+ return nil
+}
+
+// Push pushes data to indexer
+func (b *DummyQueue) Push(*IndexerData) error {
+ return nil
}
diff --git a/modules/indexer/issues/queue_channel.go b/modules/indexer/issues/queue_channel.go
index 99a90ad499..bd92f6b7b1 100644
--- a/modules/indexer/issues/queue_channel.go
+++ b/modules/indexer/issues/queue_channel.go
@@ -33,6 +33,11 @@ func (c *ChannelQueue) Run() error {
for {
select {
case data := <-c.queue:
+ if data.IsDelete {
+ c.indexer.Delete(data.IDs...)
+ continue
+ }
+
datas = append(datas, data)
if len(datas) >= c.batchNumber {
c.indexer.Index(datas)
@@ -51,6 +56,7 @@ func (c *ChannelQueue) Run() error {
}
// Push will push the indexer data to queue
-func (c *ChannelQueue) Push(data *IndexerData) {
+func (c *ChannelQueue) Push(data *IndexerData) error {
c.queue <- data
+ return nil
}
diff --git a/modules/indexer/issues/queue_disk.go b/modules/indexer/issues/queue_disk.go
index 97bacdf99d..be6867f9ca 100644
--- a/modules/indexer/issues/queue_disk.go
+++ b/modules/indexer/issues/queue_disk.go
@@ -94,14 +94,10 @@ func (l *LevelQueue) Run() error {
}
// Push will push the indexer data to queue
-func (l *LevelQueue) Push(data *IndexerData) {
+func (l *LevelQueue) Push(data *IndexerData) error {
bs, err := json.Marshal(data)
if err != nil {
- log.Error(4, "Marshal: %v", err)
- return
- }
- err = l.queue.LPush(bs)
- if err != nil {
- log.Error(4, "LPush: %v", err)
+ return err
}
+ return l.queue.LPush(bs)
}