Skip to content

Commit

Permalink
🎨 Improve purge repo
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Nov 20, 2024
1 parent c29870b commit 9de3833
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
5 changes: 2 additions & 3 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,8 @@ func (repo *Repo) PurgeCloud() (ret *entity.PurgeStat, err error) {
for refID := range refIndexIDs {
index, getErr := repo.cloud.GetIndex(refID)
if nil != getErr {
err = getErr
logging.LogErrorf("get index [%s] failed: %s", refID, err)
return
logging.LogWarnf("get index [%s] failed: %s", refID, err)
continue
}

for _, fileID := range index.Files {
Expand Down
40 changes: 22 additions & 18 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,6 @@ func (store *Store) Purge(retentionIndexIDs ...string) (ret *entity.PurgeStat, e
ret = &entity.PurgeStat{}
ret.Indexes = len(unreferencedIndexIDs)

// 清理未引用的数据对象
for unreferencedObjID := range unreferencedObjIDs {
stat, statErr := store.Stat(unreferencedObjID)
if nil != statErr {
logging.LogErrorf("stat [%s] failed: %s", unreferencedObjID, statErr)
continue
}

ret.Size += stat.Size()
ret.Objects++

if err = store.Remove(unreferencedObjID); nil != err {
logging.LogErrorf("remove unreferenced object [%s] failed: %s", unreferencedObjID, err)
return
}
}

// 清理未引用的索引对象
for unreferencedIndexID := range unreferencedIndexIDs {
indexPath := filepath.Join(store.Path, "indexes", unreferencedIndexID)
Expand All @@ -192,7 +175,7 @@ func (store *Store) Purge(retentionIndexIDs ...string) (ret *entity.PurgeStat, e
}
}

// 清理完索引后最后再清理校验索引
// 清理校验索引
// Clear check index when purging data repo https://github.com/siyuan-note/siyuan/issues/9665
checkIndexesDir := filepath.Join(store.Path, "check", "indexes")
if gulu.File.IsDir(checkIndexesDir) {
Expand Down Expand Up @@ -223,6 +206,10 @@ func (store *Store) Purge(retentionIndexIDs ...string) (ret *entity.PurgeStat, e
continue
}

if !unreferencedIndexIDs[checkIndex.IndexID] {
continue
}

if _, statErr := os.Stat(filepath.Join(store.Path, "indexes", checkIndex.IndexID)); os.IsNotExist(statErr) {
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)
Expand All @@ -232,6 +219,23 @@ func (store *Store) Purge(retentionIndexIDs ...string) (ret *entity.PurgeStat, e
}
}

// 清理未引用的数据对象
for unreferencedObjID := range unreferencedObjIDs {
stat, statErr := store.Stat(unreferencedObjID)
if nil != statErr {
logging.LogErrorf("stat [%s] failed: %s", unreferencedObjID, statErr)
continue
}

ret.Size += stat.Size()
ret.Objects++

if err = store.Remove(unreferencedObjID); nil != err {
logging.LogErrorf("remove unreferenced object [%s] failed: %s", unreferencedObjID, err)
return
}
}

fileCache.Clear()
indexCache.Clear()

Expand Down

0 comments on commit 9de3833

Please sign in to comment.