-
Notifications
You must be signed in to change notification settings - Fork 5
/
indexers.go
184 lines (155 loc) · 5.49 KB
/
indexers.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package backends
import (
"fmt"
"math"
"strings"
"github.com/ghetzel/go-stockutil/log"
"github.com/ghetzel/go-stockutil/sliceutil"
"github.com/ghetzel/pivot/v3/dal"
"github.com/ghetzel/pivot/v3/filter"
)
var IndexerPageSize int = 100
var MaxFacetCardinality int = 10000
var DefaultCompoundJoiner = `:`
type IndexPage struct {
Page int
TotalPages int
Limit int
Offset int
TotalResults int64
}
type IndexResultFunc func(record *dal.Record, err error, page IndexPage) error // {}
type Indexer interface {
IndexConnectionString() *dal.ConnectionString
IndexInitialize(Backend) error
IndexExists(collection *dal.Collection, id interface{}) bool
IndexRetrieve(collection *dal.Collection, id interface{}) (*dal.Record, error)
IndexRemove(collection *dal.Collection, ids []interface{}) error
Index(collection *dal.Collection, records *dal.RecordSet) error
QueryFunc(collection *dal.Collection, filter *filter.Filter, resultFn IndexResultFunc) error
Query(collection *dal.Collection, filter *filter.Filter, resultFns ...IndexResultFunc) (*dal.RecordSet, error)
ListValues(collection *dal.Collection, fields []string, filter *filter.Filter) (map[string][]interface{}, error)
DeleteQuery(collection *dal.Collection, f *filter.Filter) error
FlushIndex() error
GetBackend() Backend
}
func MakeIndexer(connection dal.ConnectionString) (Indexer, error) {
log.Infof("Creating indexer: %v", connection.String())
switch connection.Backend() {
case `bleve`:
return NewBleveIndexer(connection), nil
case `elasticsearch`:
return NewElasticsearchIndexer(connection), nil
default:
return nil, fmt.Errorf("Unknown indexer type %q", connection.Backend())
}
}
func PopulateRecordSetPageDetails(recordset *dal.RecordSet, f *filter.Filter, page IndexPage) {
// result count is whatever we were told it was for this query
if page.TotalResults >= 0 {
recordset.KnownSize = true
recordset.ResultCount = page.TotalResults
} else {
recordset.ResultCount = int64(len(recordset.Records))
}
if page.TotalPages > 0 {
recordset.TotalPages = page.TotalPages
} else if recordset.ResultCount >= 0 && f.Limit > 0 {
// total pages = ceil(result count / page size)
recordset.TotalPages = int(math.Ceil(float64(recordset.ResultCount) / float64(f.Limit)))
} else {
recordset.TotalPages = 1
}
if recordset.RecordsPerPage == 0 {
recordset.RecordsPerPage = page.Limit
}
// page is the last page number set
if page.Limit > 0 {
recordset.Page = int(math.Ceil(float64(f.Offset+1) / float64(page.Limit)))
}
}
func DefaultQueryImplementation(indexer Indexer, collection *dal.Collection, f *filter.Filter, resultFns ...IndexResultFunc) (*dal.RecordSet, error) {
var recordset = dal.NewRecordSet()
if err := indexer.QueryFunc(collection, f, func(indexRecord *dal.Record, err error, page IndexPage) error {
defer PopulateRecordSetPageDetails(recordset, f, page)
var parent = indexer.GetBackend()
var forceIndexRecord bool
// look for a filter option that specifies that we should explicitly NOT attempt to retrieve the
// record from the parent by ID, but rather always use the index record as-is.
if f != nil {
if v, ok := f.Options[`ForceIndexRecord`].(bool); ok {
forceIndexRecord = v
}
}
// index compound field processing
if parent != nil {
if len(collection.IndexCompoundFields) > 1 {
var joiner = collection.IndexCompoundFieldJoiner
if joiner == `` {
joiner = DefaultCompoundJoiner
}
var values = sliceutil.Sliceify(
strings.Split(fmt.Sprintf("%v", indexRecord.ID), joiner),
)
if len(values) != len(collection.IndexCompoundFields) {
// if the index record's ID isn't holding _all_ of the field components,
// attempt to retrieve the rest of the values from the record itself.
if diff := len(collection.IndexCompoundFields) - len(values); diff >= 1 {
for _, cf := range collection.IndexCompoundFields[diff:] {
if v := indexRecord.Get(cf); v != nil {
values = append(values, v)
}
}
}
if len(values) != len(collection.IndexCompoundFields) {
return fmt.Errorf(
"Index item %v: expected %d compound field components, got %d",
indexRecord.ID,
len(collection.IndexCompoundFields),
len(values),
)
}
}
for i, field := range collection.IndexCompoundFields {
if i == 0 {
indexRecord.ID = values
} else {
indexRecord.Set(field, values[i])
}
}
}
}
var emptyRecord = dal.NewRecord(indexRecord.ID)
emptyRecord.Error = err
if len(resultFns) > 0 {
var resultFn = resultFns[0]
if f.IdOnly() {
return resultFn(emptyRecord, err, page)
} else if parent != nil && !forceIndexRecord {
if record, err := parent.Retrieve(collection.Name, indexRecord.ID, f.Fields...); err == nil {
return resultFn(record, err, page)
} else {
return resultFn(emptyRecord, err, page)
}
} else {
return resultFn(indexRecord, err, page)
}
} else {
if f.IdOnly() {
recordset.Records = append(recordset.Records, dal.NewRecord(indexRecord.ID))
} else if parent != nil && !forceIndexRecord {
if record, err := parent.Retrieve(collection.Name, indexRecord.ID, f.Fields...); err == nil {
recordset.Records = append(recordset.Records, record)
} else {
recordset.Records = append(recordset.Records, dal.NewRecordErr(indexRecord.ID, err))
}
} else {
recordset.Records = append(recordset.Records, indexRecord)
}
return nil
}
}); err != nil {
return nil, err
}
return recordset, nil
}