This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.go
342 lines (283 loc) · 7.51 KB
/
index.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package dataloading
import (
"os"
"fmt"
"log"
"time"
"io/ioutil"
"encoding/json"
"database/sql"
"gopkg.in/yaml.v2"
"github.com/gocodo/bloomdb"
"github.com/mattbaird/elastigo/lib"
"github.com/spf13/viper"
)
func deNull(doc map[string]interface{}) {
for k, v := range doc {
if v == nil {
delete(doc, k)
} else {
switch v.(type) {
case map[string]interface{}:
deNull(v.(map[string]interface{}))
case []interface{}:
for _, elm := range v.([]interface{}) {
deNull(elm.(map[string]interface{}))
}
}
}
}
}
func removeNulls(doc string) (string, error) {
var dat map[string]interface{}
err := json.Unmarshal([]byte(doc), &dat)
if err != nil {
return "", err
}
deNull(dat)
result, err := json.Marshal(dat)
if err != nil {
return "", err
}
return string(result), nil
}
type tableColumnInfo struct {
Name string
Type string
}
func tableColumns(conn *sql.DB, table string) ([]tableColumnInfo, error) {
columns := []tableColumnInfo{}
rows, err := conn.Query(` SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = '` + table + `';`)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var name, columnType string
if err := rows.Scan(&name, &columnType); err != nil {
return nil, err
}
columns = append(columns, tableColumnInfo{
name,
columnType,
})
}
if err := rows.Err(); err != nil {
return nil, err
}
return columns, nil
}
func defaultColumns(columns []tableColumnInfo) []SearchSelect {
filteredColumns := []SearchSelect{}
for _, column := range columns {
if column.Name != "id" && column.Name != "bloom_created_at" && column.Name != "revision" && column.Type != "uuid" {
filteredColumns = append(filteredColumns, SearchSelect{ Name: column.Name, Type: column.Type })
}
}
return filteredColumns
}
func fillSearchSourceBlanks(conn *sql.DB, mapping *SearchSource) error {
if len(mapping.Select) == 0 {
columns, err := tableColumns(conn, mapping.Pivot)
if err != nil {
return err
}
mapping.SelectTypes = defaultColumns(columns)
} else {
for _, s := range mapping.Select {
mapping.SelectTypes = append(mapping.SelectTypes, SearchSelect{
Name: s,
Type: "",
})
}
}
if mapping.SearchId == "" {
mapping.SearchId = mapping.Id
}
if mapping.SearchId == "id" {
mapping.SelectTypes = append(mapping.SelectTypes, SearchSelect{
Name: mapping.Pivot + ".id",
Type: "uuid",
})
}
if mapping.Joins == nil {
mapping.Joins = []SearchJoin{}
}
if mapping.Relationships == nil {
mapping.Relationships = []SearchRelationship{}
}
for i, join := range mapping.Joins {
if join.SourceId == "" {
mapping.Joins[i].SourceId = mapping.Id
}
if join.DestId == "" {
mapping.Joins[i].DestId = "id"
}
}
for i, relationship := range mapping.Relationships {
if relationship.SourceId == "" {
mapping.Relationships[i].SourceId = mapping.Id
}
if relationship.DestId == "" {
mapping.Relationships[i].DestId = "id"
}
if relationship.Name == "" {
mapping.Relationships[i].Name = relationship.Include
}
if len(relationship.Select) == 0 {
// Fill Select from schema of Include table
columns, err := tableColumns(conn, relationship.Include)
if err != nil {
return err
}
mapping.Relationships[i].SelectTypes = defaultColumns(columns)
} else {
for _, s := range relationship.Select {
mapping.Relationships[i].SelectTypes = append(mapping.Relationships[i].SelectTypes, SearchSelect{
Name: s,
Type: "",
})
}
}
}
return nil
}
func Index() error {
startTime := time.Now().UTC()
file, err := ioutil.ReadFile("searchmapping.yaml")
if err != nil {
return err
}
mappings := []SearchSource{}
err = yaml.Unmarshal(file, &mappings)
if err != nil {
return err
}
bdb := bloomdb.DBFromConfig(viper.GetString("sqlConnStr"), viper.GetStringSlice("searchHosts"))
conn, err := bdb.SqlConnection()
if err != nil {
return err
}
defer conn.Close()
for _, mapping := range mappings {
err = fillSearchSourceBlanks(conn, &mapping)
if err != nil {
return err
}
fmt.Println("Processing", mapping.Name)
searchHosts := viper.GetStringSlice("searchHosts")
c := elastigo.NewConn()
c.SetHosts(searchHosts)
var lastUpdated time.Time
err = conn.QueryRow("SELECT last_updated FROM search_types WHERE name = $1", mapping.Name).Scan(&lastUpdated)
if err == sql.ErrNoRows {
lastUpdated = time.Unix(0, 0)
typeId := bloomdb.MakeKey(mapping.Name)
_, err := conn.Exec("INSERT INTO search_types (id, name, last_updated, last_checked, public) VALUES ($1, $2, $3, $3, $4)", typeId, mapping.Name, lastUpdated, mapping.Public)
if err != nil {
return err
}
if _, err := os.Stat("searchmappings.json"); !os.IsNotExist(err) {
var properties map[string]interface{}
file, err := ioutil.ReadFile("searchmappings.json")
if err != nil {
return err
}
err = json.Unmarshal(file, &properties)
if err != nil {
return err
}
options := elastigo.MappingOptions{
Timestamp: elastigo.TimestampOptions{Enabled: true},
Properties: properties,
}
err = c.PutMapping(mapping.Name, "main", struct{}{}, options)
if err != nil {
return err
}
}
} else if err != nil {
return err
}
indexer := c.NewBulkIndexerErrors(10, 60)
indexer.BulkMaxBuffer = 10485760
indexer.Start()
indexCount := 0
deleteCount := 0
query := searchSourceToDeleteQuery(mapping, lastUpdated)
rows, err := conn.Query(query)
if err != nil {
log.Fatal("Failed to query for rows.", err)
}
defer rows.Close()
for rows.Next() {
var id string
err := rows.Scan(&id)
if err != nil {
log.Fatal(err)
}
deleteCount += 1
if deleteCount % 10000 == 0 {
fmt.Println(deleteCount, "Records Deleted in", time.Now().Sub(startTime))
}
indexer.Delete(mapping.Name, "main", id, false)
}
if err := rows.Err(); err != nil {
return err
}
indexer.Flush()
fmt.Println(deleteCount, "Records Deleted in", time.Now().Sub(startTime))
query = searchSourceToUpdateQuery(mapping, lastUpdated)
insertRows, err := conn.Query(query)
if err != nil {
fmt.Println("Error with query:", query)
return err
}
defer insertRows.Close()
for insertRows.Next() {
var doc, id string
err := insertRows.Scan(&doc, &id)
if err != nil {
return err
}
doc, err = removeNulls(doc)
if err != nil {
return err
}
indexCount += 1
if indexCount % 10000 == 0 {
fmt.Println(indexCount, "Records Indexed in", time.Now().Sub(startTime))
}
indexer.Index(mapping.Name, "main", id, "", nil, doc, false)
}
if err := insertRows.Err(); err != nil {
return err
}
indexer.Flush()
// There seems to be a bug in elastigo ... unsure why this sometimes blocks indefinitly
// Should be fixed at some point ... current fix is to time out after 20 seconds of trying to Stop
stopper := make(chan bool, 1)
go func() {
indexer.Stop()
stopper <- true
}()
select {
case _ = <- stopper:
fmt.Println("Indexer Stopped")
case <- time.After(time.Second * 20):
fmt.Println("Indexer Stop Timed out")
}
fmt.Println(indexCount, "Records Indexed in", time.Now().Sub(startTime))
if indexCount > 0 || deleteCount > 0 {
_, err = conn.Exec("UPDATE search_types SET last_updated = $1, last_checked = $1 WHERE name = $2", startTime, mapping.Name)
} else {
_, err = conn.Exec("UPDATE search_types SET last_checked = $1 WHERE name = $2", startTime, mapping.Name)
}
if err != nil {
return err
}
}
return nil
}