add AddFileList method for sync library

This commit is contained in:
xiaoyaofenfen
2022-05-17 19:48:39 +08:00
parent d7434d29a6
commit f9dbb6ffa2
4 changed files with 135 additions and 1 deletions
+35
View File
@@ -105,6 +105,41 @@ func (b *BoltDb) Add(item *BoltItem) (bool, error) {
return true, nil
}
// AddItems 增加数据项
func (b *BoltDb) AddItems(items []*BoltItem) (bool, error) {
b.locker.Lock()
defer b.locker.Unlock()
// add item
// Start a writable transaction.
tx, err := b.db.Begin(true)
if err != nil {
return false, err
}
defer tx.Rollback()
rootBucket, er := tx.CreateBucketIfNotExists([]byte("/"))
if er != nil {
return false, er
}
for _, item := range items {
item.FilePath = FormatFilePath(item.FilePath)
if _, err := b.addOneItem(rootBucket, item); err != nil {
// Commit the transaction and check for error.
if err := tx.Commit(); err != nil {
return false, err
}
return false, err
}
}
// Commit the transaction and check for error.
if err := tx.Commit(); err != nil {
return false, err
}
return true, nil
}
// Get 获取一个数据项,数据项不存在返回错误
func (b *BoltDb) Get(filePath string) (string, error) {
filePath = FormatFilePath(filePath)
+4
View File
@@ -46,6 +46,8 @@ type (
Open() (bool, error)
// Add 存储一个数据项
Add(item *PanFileItem) (bool, error)
// AddFileList 存储批量数据项
AddFileList(items PanFileList) (bool, error)
// Get 获取一个数据项
Get(filePath string) (*PanFileItem, error)
// GetFileList 获取文件夹下的所有的文件列表
@@ -84,6 +86,8 @@ type (
Open() (bool, error)
// Add 存储一个数据项
Add(item *LocalFileItem) (bool, error)
// AddFileList 存储批量数据项
AddFileList(items LocalFileList) (bool, error)
// Get 获取一个数据项
Get(filePath string) (*LocalFileItem, error)
// GetFileList 获取文件夹下的所有的文件列表
+46
View File
@@ -58,6 +58,29 @@ func (p *PanSyncDbBolt) Add(item *PanFileItem) (bool, error) {
})
}
// AddFileList 增加批量数据项
func (p *PanSyncDbBolt) AddFileList(items PanFileList) (bool, error) {
if items == nil {
return false, fmt.Errorf("item is nil")
}
p.locker.Lock()
defer p.locker.Unlock()
boltItems := []*BoltItem{}
for _, item := range items {
data, err := json.Marshal(item)
if err != nil {
return false, err
}
boltItems = append(boltItems, &BoltItem{
FilePath: item.Path,
IsFolder: item.IsFolder(),
Data: string(data),
})
}
return p.db.AddItems(boltItems)
}
// Get 获取一个数据项,数据项不存在返回错误
func (p *PanSyncDbBolt) Get(filePath string) (*PanFileItem, error) {
filePath = FormatFilePath(filePath)
@@ -169,6 +192,29 @@ func (p *LocalSyncDbBolt) Add(item *LocalFileItem) (bool, error) {
})
}
// AddFileList 增加批量数据项
func (p *LocalSyncDbBolt) AddFileList(items LocalFileList) (bool, error) {
if items == nil {
return false, fmt.Errorf("item is nil")
}
p.locker.Lock()
defer p.locker.Unlock()
boltItems := []*BoltItem{}
for _, item := range items {
data, err := json.Marshal(item)
if err != nil {
return false, err
}
boltItems = append(boltItems, &BoltItem{
FilePath: item.Path,
IsFolder: item.IsFolder(),
Data: string(data),
})
}
return p.db.AddItems(boltItems)
}
// Get 获取一个数据项,数据项不存在返回错误
func (p *LocalSyncDbBolt) Get(filePath string) (*LocalFileItem, error) {
filePath = FormatFilePath(filePath)
+50 -1
View File
@@ -16,7 +16,7 @@ import (
func TestPanSyncDb(t *testing.T) {
// get access token
refreshToken := "c2b11bfc07...f090dc07ea59"
refreshToken := "d77420e4daa...9d384d7c44508"
webToken, err := aliyunpan.GetAccessTokenFromRefreshToken(refreshToken)
if err != nil {
fmt.Println("get acccess token error")
@@ -132,6 +132,55 @@ func TestLocalSyncDb(t *testing.T) {
WalkAllFile("D:\\smb\\feny\\goprojects\\dl\\a761171495", walkFunc)
}
func TestLocalSyncDbAddFileList(t *testing.T) {
b := NewLocalSyncDb("D:\\smb\\feny\\goprojects\\dev\\tmp.db")
b.Open()
defer b.Close()
fl := LocalFileList{}
fl = append(fl, &LocalFileItem{
FileName: "aliyunpan_command_history.txt",
FileSize: 1542,
FileType: "file",
CreatedAt: "2020-12-12 12:51:12",
UpdatedAt: "2020-12-12 12:51:12",
FileExtension: "",
Sha1Hash: "",
Path: "D:\\smb\\feny\\goprojects\\dev\\aliyunpan_command_history.txt",
})
fl = append(fl, &LocalFileItem{
FileName: "f1.txt",
FileSize: 1542,
FileType: "file",
CreatedAt: "2020-12-12 12:51:12",
UpdatedAt: "2020-12-12 12:51:12",
FileExtension: "",
Sha1Hash: "",
Path: "D:\\smb\\feny\\goprojects\\dev\\f1.txt",
})
fl = append(fl, &LocalFileItem{
FileName: "f2.txt",
FileSize: 1542,
FileType: "file",
CreatedAt: "2020-12-12 12:51:12",
UpdatedAt: "2020-12-12 12:51:12",
FileExtension: "",
Sha1Hash: "",
Path: "D:\\smb\\feny\\goprojects\\dev\\f2.txt",
})
fl = append(fl, &LocalFileItem{
FileName: "f1.txt",
FileSize: 1542,
FileType: "file",
CreatedAt: "2020-12-12 12:51:12",
UpdatedAt: "2020-12-12 12:51:12",
FileExtension: "",
Sha1Hash: "",
Path: "D:\\smb\\feny\\goprojects\\dev\\fo\\f1.txt",
})
fmt.Println(b.AddFileList(fl))
}
func TestLocalGet(t *testing.T) {
b := NewLocalSyncDb("D:\\smb\\feny\\goprojects\\dev\\local.db")
b.Open()