4
4
"crypto/sha1"
5
5
"fmt"
6
6
"io"
7
+ "io/ioutil"
7
8
"os"
8
9
"path/filepath"
9
10
"strings"
@@ -24,6 +25,15 @@ const (
24
25
IndexNamePrefix = "idx"
25
26
// FrameNamePrefix the pilosa's frames prefix
26
27
FrameNamePrefix = "frm"
28
+
29
+ // ConfigFileName is the name of an index config file.
30
+ ConfigFileName = "config.yml"
31
+
32
+ // ProcessingFileName is the name of the lock/processing index file.
33
+ ProcessingFileName = ".processing"
34
+
35
+ // MappingFileName is the name of the mapping file.
36
+ MappingFileName = "mapping.db"
27
37
)
28
38
29
39
var (
@@ -65,7 +75,7 @@ func (*Driver) ID() string {
65
75
66
76
// Create a new index.
67
77
func (d * Driver ) Create (db , table , id string , expressions []sql.Expression , config map [string ]string ) (sql.Index , error ) {
68
- path , err := mkdir (d .root , db , table , id )
78
+ _ , err := mkdir (d .root , db , table , id )
69
79
if err != nil {
70
80
return nil , err
71
81
}
@@ -76,79 +86,88 @@ func (d *Driver) Create(db, table, id string, expressions []sql.Expression, conf
76
86
}
77
87
78
88
cfg := index .NewConfig (db , table , id , exprs , d .ID (), config )
79
- err = index .WriteConfigFile (path , cfg )
89
+ err = index .WriteConfigFile (d . configFilePath ( db , table , id ) , cfg )
80
90
if err != nil {
81
91
return nil , err
82
92
}
83
93
84
- return newPilosaIndex (path , d .client , cfg ), nil
94
+ return newPilosaIndex (d . mappingFilePath ( db , table , id ) , d .client , cfg ), nil
85
95
}
86
96
87
97
// LoadAll loads all indexes for given db and table
88
98
func (d * Driver ) LoadAll (db , table string ) ([]sql.Index , error ) {
89
- root := filepath .Join (d .root , db , table )
90
-
91
99
var (
92
100
indexes []sql.Index
93
101
errors []string
94
- err error
102
+ root = filepath . Join ( d . root , db , table )
95
103
)
96
- filepath .Walk (root , func (path string , info os.FileInfo , err error ) error {
97
- if err != nil {
98
- if path != root || ! os .IsNotExist (err ) {
99
- errors = append (errors , err .Error ())
100
- }
101
- return filepath .SkipDir
102
- }
103
104
104
- if info .IsDir () && path != root && info .Name () != "." && info .Name () != ".." {
105
- idx , err := d .loadIndex (path )
105
+ dirs , err := ioutil .ReadDir (root )
106
+ if err != nil {
107
+ if os .IsNotExist (err ) {
108
+ return indexes , nil
109
+ }
110
+ return nil , err
111
+ }
112
+ for _ , info := range dirs {
113
+ if info .IsDir () && ! strings .HasPrefix (info .Name (), "." ) {
114
+ idx , err := d .loadIndex (db , table , info .Name ())
106
115
if err != nil {
107
116
if ! errCorruptedIndex .Is (err ) {
108
117
errors = append (errors , err .Error ())
109
118
}
110
-
111
- return filepath .SkipDir
119
+ continue
112
120
}
113
121
114
122
indexes = append (indexes , idx )
115
123
}
116
-
117
- return nil
118
- })
124
+ }
119
125
120
126
if len (errors ) > 0 {
121
- err = fmt .Errorf (strings .Join (errors , "\n " ))
127
+ return nil , fmt .Errorf (strings .Join (errors , "\n " ))
122
128
}
123
- return indexes , err
129
+
130
+ return indexes , nil
124
131
}
125
132
126
- func (d * Driver ) loadIndex (path string ) (sql.Index , error ) {
127
- ok , err := index .ExistsProcessingFile (path )
133
+ func (d * Driver ) loadIndex (db , table , id string ) (sql.Index , error ) {
134
+ dir := filepath .Join (d .root , db , table , id )
135
+ config := d .configFilePath (db , table , id )
136
+ if _ , err := os .Stat (config ); err != nil {
137
+ return nil , errCorruptedIndex .New (dir )
138
+ }
139
+
140
+ mapping := d .mappingFilePath (db , table , id )
141
+ processing := d .processingFilePath (db , table , id )
142
+ ok , err := index .ExistsProcessingFile (processing )
128
143
if err != nil {
129
144
return nil , err
130
145
}
131
-
132
146
if ok {
133
147
log := logrus .WithFields (logrus.Fields {
134
- "err" : err ,
135
- "path" : path ,
148
+ "err" : err ,
149
+ "db" : db ,
150
+ "table" : table ,
151
+ "id" : id ,
152
+ "dir" : dir ,
136
153
})
137
154
log .Warn ("could not read index file, index is corrupt and will be deleted" )
138
-
139
- if err := os .RemoveAll (path ); err != nil {
140
- log .Warn ("unable to remove folder of corrupted index" )
155
+ if err := os .RemoveAll (dir ); err != nil {
156
+ log .Warn ("unable to remove corrupted index: " + dir )
141
157
}
142
158
143
- return nil , errCorruptedIndex .New (path )
159
+ return nil , errCorruptedIndex .New (dir )
144
160
}
145
161
146
- cfg , err := index .ReadConfigFile (path )
162
+ cfg , err := index .ReadConfigFile (config )
147
163
if err != nil {
148
164
return nil , err
149
165
}
166
+ if cfg .Driver (DriverID ) == nil {
167
+ return nil , errCorruptedIndex .New (dir )
168
+ }
150
169
151
- idx := newPilosaIndex (path , d .client , cfg )
170
+ idx := newPilosaIndex (mapping , d .client , cfg )
152
171
return idx , nil
153
172
}
154
173
@@ -166,12 +185,8 @@ func (d *Driver) Save(
166
185
return errInvalidIndexType .New (i )
167
186
}
168
187
169
- path , err := mkdir (d .root , i .Database (), i .Table (), i .ID ())
170
- if err != nil {
171
- return err
172
- }
173
-
174
- if err = index .CreateProcessingFile (path ); err != nil {
188
+ processingFile := d .processingFilePath (idx .Database (), idx .Table (), idx .ID ())
189
+ if err = index .CreateProcessingFile (processingFile ); err != nil {
175
190
return err
176
191
}
177
192
@@ -285,13 +300,12 @@ func (d *Driver) Save(
285
300
"id" : i .ID (),
286
301
}).Debugf ("finished pilosa indexing" )
287
302
288
- return index .RemoveProcessingFile (path )
303
+ return index .RemoveProcessingFile (processingFile )
289
304
}
290
305
291
306
// Delete the index with the given path.
292
307
func (d * Driver ) Delete (idx sql.Index ) error {
293
- path := filepath .Join (d .root , idx .Database (), idx .Table (), idx .ID ())
294
- if err := os .RemoveAll (path ); err != nil {
308
+ if err := os .RemoveAll (filepath .Join (d .root , idx .Database (), idx .Table (), idx .ID ())); err != nil {
295
309
return err
296
310
}
297
311
@@ -426,3 +440,15 @@ func mkdir(elem ...string) (string, error) {
426
440
path := filepath .Join (elem ... )
427
441
return path , os .MkdirAll (path , 0750 )
428
442
}
443
+
444
+ func (d * Driver ) configFilePath (db , table , id string ) string {
445
+ return filepath .Join (d .root , db , table , id , ConfigFileName )
446
+ }
447
+
448
+ func (d * Driver ) processingFilePath (db , table , id string ) string {
449
+ return filepath .Join (d .root , db , table , id , ProcessingFileName )
450
+ }
451
+
452
+ func (d * Driver ) mappingFilePath (db , table , id string ) string {
453
+ return filepath .Join (d .root , db , table , id , MappingFileName )
454
+ }
0 commit comments