@@ -39,8 +39,10 @@ const (
39
39
// ProcessingFileName is the extension of the lock/processing index file.
40
40
ProcessingFileName = ".processing"
41
41
42
- // MappingFileName is the extension of the mapping file.
43
- MappingFileName = "mapping.db"
42
+ // MappingFileNamePrefix is the prefix in mapping file <prefix>-<mappingKey><extension>
43
+ MappingFileNamePrefix = "map"
44
+ // MappingFileNameExtension is the extension in mapping file <prefix>-<mappingKey><extension>
45
+ MappingFileNameExtension = ".db"
44
46
)
45
47
46
48
const (
@@ -124,7 +126,6 @@ func (d *Driver) Create(
124
126
return nil , err
125
127
}
126
128
127
- mapping := newMapping (d .mappingFilePath (db , table , id ))
128
129
processingFile := d .processingFilePath (db , table , id )
129
130
if err := index .WriteProcessingFile (
130
131
processingFile ,
@@ -133,7 +134,7 @@ func (d *Driver) Create(
133
134
return nil , err
134
135
}
135
136
136
- return newPilosaIndex (idx , mapping , cfg ), nil
137
+ return newPilosaIndex (idx , cfg ), nil
137
138
}
138
139
139
140
// LoadAll loads all indexes for given db and table
@@ -187,7 +188,6 @@ func (d *Driver) loadIndex(db, table, id string) (*pilosaIndex, error) {
187
188
return nil , errCorruptedIndex .New (dir )
188
189
}
189
190
190
- mapping := d .mappingFilePath (db , table , id )
191
191
processing := d .processingFilePath (db , table , id )
192
192
ok , err := index .ExistsProcessingFile (processing )
193
193
if err != nil {
@@ -213,11 +213,23 @@ func (d *Driver) loadIndex(db, table, id string) (*pilosaIndex, error) {
213
213
if err != nil {
214
214
return nil , err
215
215
}
216
- if cfg .Driver (DriverID ) == nil {
216
+ cfgDriver := cfg .Driver (DriverID )
217
+ if cfgDriver == nil {
217
218
return nil , errCorruptedIndex .New (dir )
218
219
}
219
220
220
- return newPilosaIndex (idx , newMapping (mapping ), cfg ), nil
221
+ pilosaIndex := newPilosaIndex (idx , cfg )
222
+ for k , v := range cfgDriver {
223
+ if strings .HasPrefix (v , MappingFileNamePrefix ) && strings .HasSuffix (v , MappingFileNameExtension ) {
224
+ path := d .mappingFilePath (db , table , id , k )
225
+ if _ , err := os .Stat (path ); err != nil {
226
+ continue
227
+ }
228
+ pilosaIndex .mapping [k ] = newMapping (path )
229
+ }
230
+ }
231
+
232
+ return pilosaIndex , nil
221
233
}
222
234
223
235
func (d * Driver ) savePartition (
@@ -245,28 +257,33 @@ func (d *Driver) savePartition(
245
257
}
246
258
247
259
rollback := true
248
- if err := idx .mapping .openCreate (true ); err != nil {
260
+ mk := mappingKey (p )
261
+ mapping , ok := idx .mapping [mk ]
262
+ if ! ok {
263
+ return 0 , errMappingNotFound .New (mk )
264
+ }
265
+ if err := mapping .openCreate (true ); err != nil {
249
266
return 0 , err
250
267
}
251
268
252
269
defer func () {
253
270
if rollback {
254
- idx . mapping .rollback ()
271
+ mapping .rollback ()
255
272
} else {
256
- e := d .saveMapping (ctx , idx . mapping , colID , false , b )
273
+ e := d .saveMapping (ctx , mapping , colID , false , b )
257
274
if e != nil && err == nil {
258
275
err = e
259
276
}
260
277
}
261
278
262
- idx . mapping .close ()
279
+ mapping .close ()
263
280
kviter .Close ()
264
281
}()
265
282
266
283
for colID = 0 ; err == nil ; colID ++ {
267
284
// commit each batch of objects (pilosa and boltdb)
268
285
if colID % sql .IndexBatchSize == 0 && colID != 0 {
269
- if err = d .saveBatch (ctx , idx . mapping , colID , b ); err != nil {
286
+ if err = d .saveBatch (ctx , mapping , colID , b ); err != nil {
270
287
return 0 , err
271
288
}
272
289
}
@@ -287,15 +304,15 @@ func (d *Driver) savePartition(
287
304
continue
288
305
}
289
306
290
- rowID , err := idx . mapping .getRowID (field .Name (), values [i ])
307
+ rowID , err := mapping .getRowID (field .Name (), values [i ])
291
308
if err != nil {
292
309
return 0 , err
293
310
}
294
311
295
312
b .bitBatches [i ].Add (rowID , colID )
296
313
}
297
314
298
- err = idx . mapping .putLocation (pilosaIndex .Name (), p , colID , location )
315
+ err = mapping .putLocation (pilosaIndex .Name (), colID , location )
299
316
if err != nil {
300
317
return 0 , err
301
318
}
@@ -352,6 +369,13 @@ func (d *Driver) Save(
352
369
return err
353
370
}
354
371
372
+ cfgPath := d .configFilePath (i .Database (), i .Table (), i .ID ())
373
+ cfg , err := index .ReadConfigFile (cfgPath )
374
+ if err != nil {
375
+ return err
376
+ }
377
+ driverCfg := cfg .Driver (DriverID )
378
+
355
379
defer iter .Close ()
356
380
pilosaIndex := idx .index
357
381
@@ -382,6 +406,10 @@ func (d *Driver) Save(
382
406
wg .Wait ()
383
407
return err
384
408
}
409
+ mk := mappingKey (p )
410
+ driverCfg [mk ] = mappingFileName (mk )
411
+ mapping := newMapping (d .mappingFilePath (idx .Database (), idx .Table (), idx .ID (), mk ))
412
+ idx .mapping [mk ] = mapping
385
413
386
414
wg .Add (1 )
387
415
@@ -417,6 +445,9 @@ func (d *Driver) Save(
417
445
if len (errors ) > 0 {
418
446
return errors [0 ]
419
447
}
448
+ if err = index .WriteConfigFile (cfgPath , cfg ); err != nil {
449
+ return err
450
+ }
420
451
421
452
logrus .WithFields (logrus.Fields {
422
453
"duration" : time .Since (start ),
@@ -469,6 +500,8 @@ func (d *Driver) Delete(i sql.Index, partitions sql.PartitionIter) error {
469
500
return err
470
501
}
471
502
}
503
+ mk := mappingKey (p )
504
+ delete (idx .mapping , mk )
472
505
}
473
506
474
507
return partitions .Close ()
@@ -581,8 +614,13 @@ func (d *Driver) processingFilePath(db, table, id string) string {
581
614
return filepath .Join (d .root , db , table , id , ProcessingFileName )
582
615
}
583
616
584
- func (d * Driver ) mappingFilePath (db , table , id string ) string {
585
- return filepath .Join (d .root , db , table , id , MappingFileName )
617
+ func mappingFileName (key string ) string {
618
+ h := sha1 .New ()
619
+ io .WriteString (h , key )
620
+ return fmt .Sprintf ("%s-%x%s" , MappingFileNamePrefix , h .Sum (nil ), MappingFileNameExtension )
621
+ }
622
+ func (d * Driver ) mappingFilePath (db , table , id string , key string ) string {
623
+ return filepath .Join (d .root , db , table , id , mappingFileName (key ))
586
624
}
587
625
588
626
func (d * Driver ) newPilosaIndex (db , table string ) (* pilosa.Index , error ) {
0 commit comments