@@ -92,7 +92,6 @@ func (m *Migration) Apply(opts migrate.Options) error {
92
92
log .Error (err )
93
93
return err
94
94
}
95
- defer f .Close ()
96
95
buf := bufio .NewWriter (f )
97
96
98
97
swapCh := make (chan Swap , 1000 )
@@ -119,7 +118,7 @@ func (m *Migration) Apply(opts migrate.Options) error {
119
118
for _ , prefix := range migrationPrefixes {
120
119
log .VLog (" - Adding keys in prefix %s to backup file" , prefix )
121
120
cidSwapper := CidSwapper {Prefix : prefix , Store : m .dstore , SwapCh : swapCh }
122
- total , err := cidSwapper .Run ( true ) // DRY RUN
121
+ total , err := cidSwapper .Prepare ( ) // DRY RUN
123
122
if err != nil {
124
123
close (swapCh )
125
124
log .Error (err )
@@ -131,17 +130,12 @@ func (m *Migration) Apply(opts migrate.Options) error {
131
130
// Wait for our writing to finish before doing the flushing.
132
131
<- writingDone
133
132
buf .Flush ()
133
+ f .Close ()
134
134
135
- // MIGRATION: Run the real migration.
136
- for _ , prefix := range migrationPrefixes {
137
- log .VLog (" - Migrating keys in prefix %s" , prefix )
138
- cidSwapper := CidSwapper {Prefix : prefix , Store : m .dstore }
139
- total , err := cidSwapper .Run (false ) // NOT a Dry Run
140
- if err != nil {
141
- log .Error (err )
142
- return err
143
- }
144
- log .Log ("%d CIDv1 keys in %s have been migrated" , total , prefix )
135
+ err = m .scanAndSwap (filepath .Join (opts .Path , backupFile ), false ) // revert=false
136
+ if err != nil {
137
+ log .Error (err )
138
+ return err
145
139
}
146
140
147
141
// Wrap up, we are now in repo-version 12.
@@ -187,21 +181,48 @@ func (m *Migration) Revert(opts migrate.Options) error {
187
181
188
182
// Open revert path for reading
189
183
backupPath := filepath .Join (opts .Path , backupFile )
184
+ err = m .scanAndSwap (backupPath , true ) // revert = true
185
+ if err != nil {
186
+ log .Error (err )
187
+ return err
188
+ }
189
+
190
+ // Wrap up the Revert. We are back at version 11.
191
+ if err := repo .WriteVersion ("11" ); err != nil {
192
+ log .Error ("failed to write version file" )
193
+ return err
194
+ }
195
+
196
+ log .Log ("reverted version file to version 11" )
197
+
198
+ // Move the backup file out of the way.
199
+ err = os .Rename (backupPath , backupPath + ".reverted" )
200
+ if err != nil {
201
+ log .Error ("could not rename the backup file, but migration worked: %s" , err )
202
+ return err
203
+ }
204
+ return nil
205
+ }
206
+
207
+ // Receives a backup file which contains all the things that need to be
208
+ // migrated and reads every line, performing swaps in the needed direction.
209
+ func (m * Migration ) scanAndSwap (backupPath string , revert bool ) error {
190
210
f , err := getBackupFile (backupPath )
191
211
if err != nil {
192
212
log .Error (err )
193
213
return err
194
214
}
215
+ defer f .Close ()
195
216
196
- unswapCh := make (chan Swap , 1000 )
217
+ swapCh := make (chan Swap , 1000 )
197
218
scanner := bufio .NewScanner (f )
198
219
var scannerErr error
199
220
200
- // This will send swap objects to the Unswapper on unswapCh as they
221
+ // This will send swap objects to the swapping channel as they
201
222
// are read from the backup file on disk. It will also send MFS and
202
- // pinset pins for reversal.
223
+ // pinset pins for reversal when doing a revert .
203
224
go func () {
204
- defer close (unswapCh )
225
+ defer close (swapCh )
205
226
206
227
// Process backup file first.
207
228
for scanner .Scan () {
@@ -222,60 +243,53 @@ func (m *Migration) Revert(opts migrate.Options) error {
222
243
break
223
244
}
224
245
mhashPath := prefix .Child (dshelp .MultihashToDsKey (cid .Hash ()))
225
- // This is the original swap object which is what we
226
- // wanted to rebuild. Old is the old path and new is
227
- // the new path and the unswapper will revert this .
246
+
247
+ // The swapper will move cidPath to mhashPath, and the unswapper
248
+ // will do the opposite .
228
249
sw := Swap {Old : cidPath , New : mhashPath }
229
- unswapCh <- sw
250
+ swapCh <- sw
230
251
}
231
252
if err := scanner .Err (); err != nil {
232
253
log .Error (err )
233
254
return
234
255
}
235
256
236
- // Process MFS/pinset. We have to do this in cases the user
237
- // has been running with the migration for some time and made changes to
238
- // the pinset or the MFS root.
239
- if err := walkPinsAndMFS (unswapCh , m .dstore ); err != nil {
240
- log .Error (err )
241
- return
257
+ if revert {
258
+ // Process MFS/pinset. We have to do this in cases the
259
+ // user has been running with the migration for some
260
+ // time and made changes to the pinset or the MFS
261
+ // root.
262
+ if err := walkPinsAndMFS (swapCh , m .dstore ); err != nil {
263
+ log .Error (err )
264
+ return
265
+ }
242
266
}
243
-
244
267
}()
245
268
246
269
// The backup file contains prefixed keys, so we do not need to set
247
270
// Prefix in the CidSwapper.
248
271
cidSwapper := CidSwapper {Store : m .dstore }
249
- total , err := cidSwapper .Revert (unswapCh )
272
+ var total uint64
273
+ if revert {
274
+ total , err = cidSwapper .Revert (swapCh )
275
+ } else {
276
+ total , err = cidSwapper .Run (swapCh )
277
+ }
250
278
if err != nil {
251
279
log .Error (err )
252
280
return err
253
281
}
254
- // Revert will only return after unswapCh is closed, so we know
282
+
283
+ // The swapper will only return after swapCh is closed, so we know
255
284
// scannerErr is safe to read at this point.
256
285
if scannerErr != nil {
257
286
return err
258
287
}
259
288
260
- // Wrap up the Revert. We are back at version 11.
261
- log .Log ("%d multihashes reverted to CidV1s" , total )
262
- if err := repo .WriteVersion ("11" ); err != nil {
263
- log .Error ("failed to write version file" )
264
- return err
265
- }
266
-
267
- log .Log ("reverted version file to version 11" )
268
- err = f .Close ()
269
- if err != nil {
270
- log .Error ("could not close backup file" )
271
- return err
272
- }
273
-
274
- // Move the backup file out of the way.
275
- err = os .Rename (backupPath , backupPath + ".reverted" )
276
- if err != nil {
277
- log .Error ("could not rename the backup file, but migration worked: %s" , err )
278
- return err
289
+ if revert {
290
+ log .Log ("%d multihashes swapped to CidV1s" , total )
291
+ } else {
292
+ log .Log ("%d CidV1s swapped to multihashes" , total )
279
293
}
280
294
return nil
281
295
}
0 commit comments