blob: 2474e19f81aba6e779a96c7b87187095da04129d (
plain)
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
|
package db
import (
"os"
"path/filepath"
"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/mapping"
)
// Search tracks all search indices to use throughout system
var Search map[string]bleve.Index
// Searchable ...
type Searchable interface {
SearchMapping() *mapping.IndexMappingImpl
}
func init() {
Search = make(map[string]bleve.Index)
}
// MapIndex creates the mapping for a type and tracks the index to be used within
// the system for adding/deleting/checking data
func MapIndex(typeName string) error {
// TODO: type assert for Searchable, get configuration (which can be overridden)
// by Ponzu user if defines own SearchMapping()
mapping := bleve.NewIndexMapping()
mapping.StoreDynamic = false
idxName := typeName + ".index"
var idx bleve.Index
// check if index exists, use it or create new one
pwd, err := os.Getwd()
if err != nil {
return err
}
searchPath := filepath.Join(pwd, "search")
err = os.MkdirAll(searchPath, os.ModeDir|os.ModePerm)
if err != nil {
return err
}
idxPath := filepath.Join(searchPath, idxName)
if _, err = os.Stat(idxPath); os.IsNotExist(err) {
idx, err = bleve.New(idxPath, mapping)
if err != nil {
return err
}
} else {
idx, err = bleve.Open(idxPath)
if err != nil {
return err
}
}
// add the type name to the index and track the index
Search[typeName] = idx
return nil
}
|