-
Notifications
You must be signed in to change notification settings - Fork 1
/
upgrade.go
256 lines (241 loc) · 4.78 KB
/
upgrade.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
package main
import (
"fmt"
"log"
)
var (
upgradeCmd = app.Command("upgrade", "upgrade dataset schema")
)
func upgradeGeocoderCache(path string) error {
cache, err := OpenCache(path)
if err != nil {
return err
}
defer cache.Close()
version, err := cache.Version()
if err != nil || version >= geocoderVersion {
return err
}
log.Printf("migrating geocoder from %d to %d", version, geocoderVersion)
err = cache.SetVersion(geocoderVersion)
if err != nil {
return err
}
return cache.Close()
}
func populateStoreLocations(geocoderDir, storeDir string) error {
store, err := UpgradeStore(storeDir)
if err != nil {
return err
}
defer store.Close()
version, err := store.Version()
if err != nil || version >= 2 {
return err
}
log.Printf("migrating store from %d to %d", version, 2)
if version < 2 {
err = store.DeleteLocations()
if err != nil {
return err
}
}
geocoder, err := NewGeocoder("", geocoderDir)
if err != nil {
return err
}
defer geocoder.Close()
ids, err := store.List()
if err != nil {
return err
}
for i, id := range ids {
if (i+1)%1000 == 0 {
fmt.Printf("%d offers location cached\n", i+1)
}
_, date, err := store.GetLocation(id)
if !date.IsZero() {
continue
}
offer, err := getStoreOffer(store, id)
if err != nil {
return err
}
loc, _, _, err := geocodeOffer(geocoder, offer.Location, true, 0)
if err != nil {
return err
}
err = store.PutLocation(id, loc, offer.Date)
if err != nil {
return err
}
}
err = store.SetVersion(2)
if err != nil {
return err
}
return store.Close()
}
/*
func migrateGeocoder(oldDir, newPath string) error {
oldCache, err := OpenOldCache(oldDir)
if err != nil {
return err
}
defer oldCache.Close()
newCache, err := OpenCache(newPath)
if err != nil {
return nil
}
defer newCache.Close()
keys, err := oldCache.List()
if err != nil {
return err
}
for _, key := range keys {
data, err := oldCache.Get(key)
if err != nil {
return err
}
res := &jstruct.Location{}
err = ffjson.Unmarshal(data, res)
if err != nil {
return err
}
err = newCache.Put(key, data, buildLocation(res))
if err != nil {
return err
}
}
return newCache.Close()
}
*/
/*
func removeMissing(oldDir, newPath string) error {
oldStore, err := OpenOldStore(oldDir)
if err != nil {
return err
}
defer oldStore.Close()
newStore, err := OpenStore(newPath)
if err != nil {
return err
}
defer newStore.Close()
newIds, err := newStore.List()
if err != nil {
return err
}
oldIds, err := oldStore.List()
if err != nil {
return err
}
oldMap := map[string]bool{}
for _, id := range oldIds {
oldMap[id] = true
}
oldIds = nil
n := 0
now := time.Now()
for _, id := range newIds {
n += 1
if (n % 1000) == 0 {
fmt.Printf("%d new offers deleted\n", n)
}
if _, ok := oldMap[id]; ok {
continue
}
err := newStore.Delete(id, now)
if err != nil {
return err
}
}
return newStore.Close()
}
func migrateStore(oldDir, newPath string) error {
oldStore, err := OpenOldStore(oldDir)
if err != nil {
return err
}
defer oldStore.Close()
newStore, err := OpenStore(newPath)
if err != nil {
return err
}
defer newStore.Close()
deletedIds, err := oldStore.ListDeletedIds()
if err != nil {
return err
}
n := 0
for _, deletedId := range deletedIds {
n += 1
if (n % 1000) == 0 {
fmt.Printf("%d deleted offers migrated\n", n)
}
deletedOffers, err := oldStore.ListDeletedOffers(deletedId)
if err != nil {
return err
}
for _, deleted := range deletedOffers {
offer, err := oldStore.GetDeleted(deleted.Id)
if err != nil {
return err
}
err = newStore.Put(deletedId, offer)
if err != nil {
return err
}
date, err := time.Parse(time.RFC3339, deleted.Date)
err = newStore.Delete(deletedId, date)
if err != nil {
return err
}
}
}
ids, err := oldStore.List()
if err != nil {
return err
}
n = 0
for _, id := range ids {
n += 1
if (n % 1000) == 0 {
fmt.Printf("%d offers migrated\n", n)
}
offer, err := oldStore.Get(id)
if err != nil {
return err
}
err = newStore.Put(id, offer)
if err != nil {
return err
}
}
return newStore.Close()
}
*/
func upgrade(cfg *Config) error {
/*
err := upgradeGeocoderCache(cfg.Geocoder())
if err != nil {
return fmt.Errorf("could not upgrade geocoder: %s", err)
}
err := migrateGeocoder(cfg.Geocoder(), "newgeocoder")
err = populateStoreLocations(cfg.Geocoder(), cfg.Store())
if err != nil {
return fmt.Errorf("could not upgrade store: %s", err)
}
err := migrateStore("offers-orig-old/offers", "newstore")
if err != nil {
return err
}
err = removeMissing("offers/offers", "newstore")
if err != nil {
return err
}
err = migrateStore("offers/offers", "newstore")
return err
*/
return nil
}