Skip to content

Commit 30d0b41

Browse files
committed
chore: better index path validation of creation
1 parent a2ab4c7 commit 30d0b41

1 file changed

Lines changed: 39 additions & 32 deletions

File tree

store/store.go

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -90,29 +90,52 @@ func (s *IndexStore) CreateIndex(config *models.IndexConfig) error {
9090

9191
indexPath := filepath.Join(s.dataDir, config.ID)
9292

93-
// Create index mapping
94-
indexMapping := bleve.NewIndexMapping()
93+
var index bleve.Index
94+
var err error
9595

96-
// Apply exclude attributes if specified
96+
// Check if index directory already exists on disk
97+
if _, statErr := os.Stat(indexPath); statErr == nil {
98+
// Directory exists, try to open existing index
99+
index, err = bleve.Open(indexPath)
100+
if err != nil {
101+
// Failed to open, remove and recreate
102+
os.RemoveAll(indexPath)
103+
index, err = s.createNewIndex(indexPath, config)
104+
if err != nil {
105+
return err
106+
}
107+
}
108+
} else {
109+
// Directory doesn't exist, create new index
110+
index, err = s.createNewIndex(indexPath, config)
111+
if err != nil {
112+
return err
113+
}
114+
}
115+
116+
s.indexes[config.ID] = index
117+
s.configs[config.ID] = config
118+
s.indexLocks[config.ID] = &sync.RWMutex{}
119+
s.saveConfigs()
120+
121+
return nil
122+
}
123+
124+
// createNewIndex creates a new bleve index with the given config
125+
func (s *IndexStore) createNewIndex(indexPath string, config *models.IndexConfig) (bleve.Index, error) {
126+
indexMapping := bleve.NewIndexMapping()
97127
if len(config.ExcludeAttributes) > 0 {
98128
defaultMapping := indexMapping.DefaultMapping
99129
for _, attr := range config.ExcludeAttributes {
100130
disabledMapping := bleve.NewDocumentDisabledMapping()
101131
defaultMapping.AddSubDocumentMapping(attr, disabledMapping)
102132
}
103133
}
104-
105134
index, err := bleve.New(indexPath, indexMapping)
106135
if err != nil {
107-
return fmt.Errorf("failed to create index: %w", err)
136+
return nil, fmt.Errorf("failed to create index: %w", err)
108137
}
109-
110-
s.indexes[config.ID] = index
111-
s.configs[config.ID] = config
112-
s.indexLocks[config.ID] = &sync.RWMutex{}
113-
s.saveConfigs()
114-
115-
return nil
138+
return index, nil
116139
}
117140

118141
// GetIndex returns an index by ID
@@ -305,32 +328,16 @@ func (s *IndexStore) CreateIndexInternal(config *models.IndexConfig) error {
305328
if err != nil {
306329
// Failed to open, remove and recreate
307330
os.RemoveAll(indexPath)
308-
indexMapping := bleve.NewIndexMapping()
309-
if len(config.ExcludeAttributes) > 0 {
310-
defaultMapping := indexMapping.DefaultMapping
311-
for _, attr := range config.ExcludeAttributes {
312-
disabledMapping := bleve.NewDocumentDisabledMapping()
313-
defaultMapping.AddSubDocumentMapping(attr, disabledMapping)
314-
}
315-
}
316-
index, err = bleve.New(indexPath, indexMapping)
331+
index, err = s.createNewIndex(indexPath, config)
317332
if err != nil {
318-
return fmt.Errorf("failed to create index: %w", err)
333+
return err
319334
}
320335
}
321336
} else {
322337
// Directory doesn't exist, create new index
323-
indexMapping := bleve.NewIndexMapping()
324-
if len(config.ExcludeAttributes) > 0 {
325-
defaultMapping := indexMapping.DefaultMapping
326-
for _, attr := range config.ExcludeAttributes {
327-
disabledMapping := bleve.NewDocumentDisabledMapping()
328-
defaultMapping.AddSubDocumentMapping(attr, disabledMapping)
329-
}
330-
}
331-
index, err = bleve.New(indexPath, indexMapping)
338+
index, err = s.createNewIndex(indexPath, config)
332339
if err != nil {
333-
return fmt.Errorf("failed to create index: %w", err)
340+
return err
334341
}
335342
}
336343

0 commit comments

Comments
 (0)