This repository was 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 pathfetch.go
111 lines (91 loc) · 2.48 KB
/
fetch.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
package dataloading
import (
"sort"
"io/ioutil"
"gopkg.in/yaml.v2"
"github.com/spf13/viper"
"github.com/gocodo/bloomdb"
)
// - Get current versions of all files
// - Compare this to existing versions of files -- removed existing
// - Order sources based on version
// - Run inserter on all files to be inserted
// - Record new files that have been imported
func Fetch(desc Description) error {
file, err := ioutil.ReadFile("dbmapping.yaml")
if err != nil {
return err
}
mapping := SourceMapping{}
err = yaml.Unmarshal(file, &mapping)
if err != nil {
return err
}
sources, err := desc.Available()
if err != nil {
return err
}
bdb := bloomdb.DBFromConfig(viper.GetString("sqlConnStr"), viper.GetStringSlice("searchHosts"))
conn, err := bdb.SqlConnection()
if err != nil {
return err
}
sourcesByName := make(map[string][]Source)
var count int
for _, source := range sources {
if sourcesByName[source.Name] == nil {
sourcesByName[source.Name] = []Source{}
}
row := conn.QueryRow("SELECT COUNT(*) FROM source_versions JOIN sources ON sources.id = source_versions.source_id WHERE sources.name = $1 AND source_versions.version = $2", source.Name, source.Version)
err := row.Scan(&count)
if err != nil {
return err
}
if count == 0 {
sourcesByName[source.Name] = append(sourcesByName[source.Name], source)
}
}
for sourceName, sources := range sourcesByName {
sort.Sort(ByVersion(sources))
var currentMappingSource Mapping
for _, mappingSource := range mapping.Sources {
if mappingSource.Name == sourceName {
currentMappingSource = mappingSource
break
}
}
for _, source := range sources {
reader, err := desc.Reader(source)
if err != nil {
return err
}
fields, err := desc.FieldNames(source.Name)
if err != nil {
return err
}
var action string
if source.Action == "" {
action = "sync"
} else {
action = source.Action
}
err = Insert(reader, currentMappingSource, fields, action)
if err != nil {
return err
}
source_id := ""
err = conn.QueryRow("SELECT id FROM sources WHERE name = $1", source.Name).Scan(&source_id)
if err != nil {
return err
}
source_version_id := bloomdb.MakeKey(source_id, source.Version)
_, err = conn.Exec("INSERT INTO source_versions (id, source_id, version) VALUES ($1, $2, $3)", source_version_id, source_id, source.Version)
if err != nil {
return err
}
}
}
downloader := NewDownloader("./data", nil)
downloader.Clear()
return nil
}