Skip to content

Commit

Permalink
🎨 Improve data repo purging
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Nov 27, 2023
1 parent edddcb1 commit 0079250
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ func NewStore(path string, aesKey []byte) (ret *Store, err error) {
}

func (store *Store) Purge() (ret *PurgeStat, err error) {
logging.LogInfof("purging data repo [%s]", store.Path)

objectsDir := filepath.Join(store.Path, "objects")
if !gulu.File.IsDir(objectsDir) {
logging.LogWarnf("objects dir [%s] is not a dir", objectsDir)
return
}

Expand Down Expand Up @@ -187,42 +190,43 @@ func (store *Store) Purge() (ret *PurgeStat, err error) {
entries, err = os.ReadDir(checkIndexesDir)
if nil != err {
logging.LogErrorf("read check indexes dir [%s] failed: %s", checkIndexesDir, err)
return
}

for _, entry := range entries {
id := entry.Name()
if 40 != len(id) {
continue
}
} else {
for _, entry := range entries {
id := entry.Name()
if 40 != len(id) {
continue
}

data, readErr := os.ReadFile(filepath.Join(checkIndexesDir, id))
if nil != readErr {
logging.LogErrorf("read check index [%s] failed: %s", id, readErr)
continue
}
data, readErr := os.ReadFile(filepath.Join(checkIndexesDir, id))
if nil != readErr {
logging.LogErrorf("read check index [%s] failed: %s", id, readErr)
continue
}

if data, readErr = store.compressDecoder.DecodeAll(data, nil); nil != readErr {
logging.LogErrorf("decode check index [%s] failed: %s", id, readErr)
continue
}
if data, readErr = store.compressDecoder.DecodeAll(data, nil); nil != readErr {
logging.LogErrorf("decode check index [%s] failed: %s", id, readErr)
continue
}

checkIndex := &entity.CheckIndex{}
if readErr = gulu.JSON.UnmarshalJSON(data, checkIndex); nil != readErr {
logging.LogErrorf("unmarshal check index [%s] failed: %s", id, readErr)
continue
}
checkIndex := &entity.CheckIndex{}
if readErr = gulu.JSON.UnmarshalJSON(data, checkIndex); nil != readErr {
logging.LogErrorf("unmarshal check index [%s] failed: %s", id, readErr)
continue
}

if _, err = os.Stat(filepath.Join(store.Path, "indexes", checkIndex.IndexID)); os.IsNotExist(err) {
if removeErr := os.RemoveAll(filepath.Join(store.Path, "check", "indexes", checkIndex.ID)); nil != removeErr {
logging.LogErrorf("remove check index [%s] failed: %s", checkIndex.ID, removeErr)
if _, err = os.Stat(filepath.Join(store.Path, "indexes", checkIndex.IndexID)); os.IsNotExist(err) {
if removeErr := os.RemoveAll(filepath.Join(store.Path, "check", "indexes", checkIndex.ID)); nil != removeErr {
logging.LogErrorf("remove check index [%s] failed: %s", checkIndex.ID, removeErr)
}
}
}
}
}

fileCache.Clear()
indexCache.Clear()

logging.LogInfof("purged data repo [%s], [%d] indexes, [%d] objects, [%d] bytes", store.Path, ret.Indexes, ret.Objects, ret.Size)
return
}

Expand Down

0 comments on commit 0079250

Please sign in to comment.