Compare 9f34602 ... +1 ... 073dff2

Coverage Reach
tx.go sdb.go options.go backup.go errors.go

No flags found

Use flags to group coverage reports by test type, project and/or folders.
Then setup custom commit statuses and notifications for each flag.

e.g., #unittest #integration

#production #enterprise

#frontend #backend

Learn more about Codecov Flags here.


@@ -4,7 +4,6 @@
Loading
4 4
package sdb
5 5
6 6
import (
7 -
	"github.com/blevesearch/bleve"
8 7
	"github.com/dgraph-io/badger/v2"
9 8
)
10 9
@@ -41,16 +40,12 @@
Loading
41 40
}
42 41
43 42
func (db *DB) cleanIndex() error {
44 -
	bq := bleve.NewMatchAllQuery()
45 -
	req := bleve.NewSearchRequest(bq)
46 -
47 -
	res, err := db.si.Search(req)
48 -
	if err != nil {
49 -
		return bleveError(err)
50 -
	}
43 +
	tx := db.NewTx(RO)
44 +
	defer tx.Discard()
51 45
52 -
	for _, hit := range res.Hits {
53 -
		if err = db.si.Delete(hit.ID); err != nil {
46 +
	result, err := tx.Find("")
47 +
	for _, id := range result {
48 +
		if err = db.si.Delete(string(id)); err != nil {
54 49
			return bleveError(err)
55 50
		}
56 51
	}

@@ -15,6 +15,7 @@
Loading
15 15
	ErrValMustBePointer = errors.New("can't encode data, must be a pointer")
16 16
17 17
	ErrKeyNotFound = badgerError(badger.ErrKeyNotFound)
18 +
	ErrTxnTooBig   = badgerError(badger.ErrTxnTooBig)
18 19
)
19 20
20 21
// IsBadgerError returns true if the given error is from Badger.

@@ -7,6 +7,7 @@
Loading
7 7
	"errors"
8 8
9 9
	"github.com/blevesearch/bleve"
10 +
	"github.com/blevesearch/bleve/search/query"
10 11
	"github.com/dgraph-io/badger/v2"
11 12
	"github.com/niubaoshu/gotiny"
12 13
)
@@ -94,20 +95,26 @@
Loading
94 95
// the query language syntax. sort is a list of field names used for sorting,
95 96
// any field prefixed by a hyphen (-) will user reverse order.
96 97
func (tx *Tx) Find(q string, sort ...string) ([][]byte, error) {
97 -
	if q == "" && len(sort) == 0 {
98 -
		return nil, nil
98 +
	var bq query.Query
99 +
100 +
	if q == "" {
101 +
		bq = bleve.NewMatchAllQuery()
102 +
	} else {
103 +
		bq = bleve.NewQueryStringQuery(q)
99 104
	}
100 105
101 -
	result := [][]byte{}
102 -
	bq := bleve.NewQueryStringQuery(q)
103 106
	req := bleve.NewSearchRequest(bq)
104 -
	req.SortBy(sort)
107 +
108 +
	if len(sort) > 0 {
109 +
		req.SortBy(sort)
110 +
	}
105 111
106 112
	res, err := tx.si.Search(req)
107 113
	if err != nil {
108 114
		return nil, bleveError(err)
109 115
	}
110 116
117 +
	result := [][]byte{}
111 118
	for _, hit := range res.Hits {
112 119
		result = append(result, []byte(hit.ID))
113 120
	}
@@ -138,6 +145,21 @@
Loading
138 145
	return nil
139 146
}
140 147
148 +
// Prefix fetches all the keys from the database with the given prefix.
149 +
func (tx *Tx) Prefix(prefix []byte) [][]byte {
150 +
	it := tx.dbTx.NewIterator(badger.DefaultIteratorOptions)
151 +
	defer it.Close()
152 +
153 +
	result := [][]byte{}
154 +
	for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
155 +
		buf := tx.db.buffers.Get()
156 +
		result = append(result, it.Item().KeyCopy(buf.Bytes()))
157 +
		tx.db.buffers.Add(buf)
158 +
	}
159 +
160 +
	return result
161 +
}
162 +
141 163
// Set set v as value of the given key. This operation happens in memory, it
142 164
// will be written to the database once Commit is called. v must be a pointer.
143 165
func (tx *Tx) Set(key []byte, v interface{}) (err error) {

@@ -20,16 +20,10 @@
Loading
20 20
	SearchIndexDir = "search-index"
21 21
)
22 22
23 -
type BleveOptions struct {
24 -
	Dir          string
25 -
	DoctypeField string
26 -
	DocMappings  map[string]*mapping.DocumentMapping
27 -
}
28 -
29 23
// Options are parameters for initializing a database.
30 24
type Options struct {
31 25
	// Database location.
32 -
	Directory string
26 +
	Dir string
33 27
34 28
	Badger badger.Options
35 29
	Bleve  BleveOptions
@@ -49,8 +43,8 @@
Loading
49 43
	badgerOpts.Compression = options.None
50 44
51 45
	return Options{
52 -
		Directory: dir,
53 -
		Badger:    badgerOpts,
46 +
		Dir:    dir,
47 +
		Badger: badgerOpts,
54 48
55 49
		Bleve: BleveOptions{
56 50
			Dir:          filepath.Join(dir, SearchIndexDir),
@@ -84,3 +78,9 @@
Loading
84 78
85 79
	return opts, nil
86 80
}
81 +
82 +
type BleveOptions struct {
83 +
	Dir          string
84 +
	DoctypeField string
85 +
	DocMappings  map[string]*mapping.DocumentMapping
86 +
}

Learn more Showing 1 files with coverage changes found.

Changes in pkg/sdb/tx.go
-1
-1
+2
Loading file...
Files Coverage
pkg/sdb +3.20% 82.98%
Project Totals (5 files) 82.98%
Loading