refactor: storage 中的 Delete 要求返回 error
This commit is contained in:
parent
3befe645b7
commit
a43fb4faea
|
@ -16,7 +16,7 @@ type IndexDataStorage[I generic.Ordered, T IndexDataItem[I]] interface {
|
|||
// SaveAll 保存所有数据
|
||||
SaveAll(name string, data map[I]T) error
|
||||
// Delete 删除特定索引数据
|
||||
Delete(name string, index I)
|
||||
Delete(name string, index I) error
|
||||
// DeleteAll 删除所有数据
|
||||
DeleteAll(name string)
|
||||
DeleteAll(name string) error
|
||||
}
|
||||
|
|
|
@ -109,19 +109,22 @@ func (slf *IndexDataFileStorage[I, T]) SaveAll(name string, data map[I]T) error
|
|||
return nil
|
||||
}
|
||||
|
||||
func (slf *IndexDataFileStorage[I, T]) Delete(name string, index I) {
|
||||
_ = os.Remove(filepath.Join(slf.dir, fmt.Sprintf(indexNameFormat, name, index, slf.suffix)))
|
||||
func (slf *IndexDataFileStorage[I, T]) Delete(name string, index I) error {
|
||||
return os.Remove(filepath.Join(slf.dir, fmt.Sprintf(indexNameFormat, name, index, slf.suffix)))
|
||||
}
|
||||
|
||||
func (slf *IndexDataFileStorage[I, T]) DeleteAll(name string) {
|
||||
func (slf *IndexDataFileStorage[I, T]) DeleteAll(name string) error {
|
||||
files, err := os.ReadDir(slf.dir)
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
for _, entry := range files {
|
||||
if entry.IsDir() || !strings.HasPrefix(entry.Name(), name) || !strings.HasSuffix(entry.Name(), slf.suffix) {
|
||||
continue
|
||||
}
|
||||
_ = os.Remove(filepath.Join(slf.dir, entry.Name()))
|
||||
if err := os.Remove(filepath.Join(slf.dir, entry.Name())); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue