v1.5.1 增强程序稳定性
This commit is contained in:
96
EdgeNode/internal/caches/list_file_migrate.go
Normal file
96
EdgeNode/internal/caches/list_file_migrate.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package caches
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||
"os"
|
||||
)
|
||||
|
||||
func MigrateSQLiteFileListDir(sqliteDir string, kvDir string) error {
|
||||
if len(sqliteDir) == 0 || len(kvDir) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err := os.Stat(sqliteDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
remotelogs.Println("CACHE", "migrating sqlite indexes from '"+sqliteDir+"' to '"+kvDir+"' ...")
|
||||
|
||||
src := NewSQLiteFileList(sqliteDir).(*SQLiteFileList)
|
||||
err = src.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
_ = src.Close()
|
||||
}()
|
||||
|
||||
dst := NewKVFileList(kvDir)
|
||||
err = dst.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
_ = dst.Close()
|
||||
}()
|
||||
|
||||
err = dst.CleanAll()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, db := range src.dbList {
|
||||
if db == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
var lastId int64
|
||||
for {
|
||||
hashes, maxId, listErr := db.ListHashes(lastId)
|
||||
if listErr != nil {
|
||||
return listErr
|
||||
}
|
||||
if len(hashes) == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
for _, hash := range hashes {
|
||||
item, readErr := db.ReadItem(hash)
|
||||
if readErr != nil {
|
||||
return readErr
|
||||
}
|
||||
if item == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
addErr := dst.Add(hash, item)
|
||||
if addErr != nil {
|
||||
return addErr
|
||||
}
|
||||
}
|
||||
|
||||
lastId = maxId
|
||||
}
|
||||
}
|
||||
|
||||
for _, store := range dst.stores {
|
||||
if store != nil && store.rawStore != nil {
|
||||
err = store.rawStore.Flush()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = os.RemoveAll(sqliteDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
remotelogs.Println("CACHE", "migrated sqlite indexes to pebble")
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user