ntrrg / usersd
1
// Copyright 2019 Miguel Angel Rivera Notararigo. All rights reserved.
2
// This source code was released under the MIT license.
3

4
package sdb
5

6
import (
7
	"github.com/blevesearch/bleve"
8
	"github.com/dgraph-io/badger/v2"
9
)
10

11 1
func (db *DB) ReloadIndex() error {
12 1
	if err := db.cleanIndex(); err != nil {
13 0
		return err
14
	}
15

16 1
	tx := db.NewTx(RO)
17 1
	defer tx.Discard()
18

19 1
	it := tx.dbTx.NewIterator(badger.DefaultIteratorOptions)
20 1
	defer it.Close()
21

22 1
	for it.Rewind(); it.Valid(); it.Next() {
23 1
		var (
24 1
			val interface{}
25 1
			key = it.Item().Key()
26 1
		)
27

28 1
		if err := tx.Get(key, &val); err != nil {
29 0
			return badgerError(err)
30
		}
31

32 1
		if err := tx.si.Index(string(key), val); err != nil {
33 0
			return bleveError(err)
34
		}
35
	}
36

37 1
	return nil
38
}
39

40 1
func (db *DB) cleanIndex() error {
41 1
	bq := bleve.NewMatchAllQuery()
42 1
	req := bleve.NewSearchRequest(bq)
43

44 1
	res, err := db.si.Search(req)
45 1
	if err != nil {
46 0
		return bleveError(err)
47
	}
48

49 1
	for _, hit := range res.Hits {
50 1
		if err = db.si.Delete(hit.ID); err != nil {
51 0
			return bleveError(err)
52
		}
53
	}
54

55 1
	return nil
56
}

Read our documentation on viewing source code .

Loading