-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
594 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package cache | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
|
||
"github.com/syndtr/goleveldb/leveldb" | ||
"github.com/syndtr/goleveldb/leveldb/util" | ||
) | ||
|
||
// 存储本地数据库连接 | ||
var db *leveldb.DB | ||
|
||
func GetDBName() string { | ||
if runtime.GOOS == "windows" { | ||
return filepath.Join(os.Getenv("USERPROFILE"), ".upx.db") | ||
} | ||
return filepath.Join(os.Getenv("HOME"), ".upx.db") | ||
} | ||
|
||
func GetClient() (*leveldb.DB, error) { | ||
var err error | ||
if db == nil { | ||
db, err = leveldb.OpenFile(GetDBName(), nil) | ||
} | ||
return db, err | ||
} | ||
|
||
func Delete(key string) error { | ||
db, err := GetClient() | ||
if err != nil { | ||
return err | ||
} | ||
return db.Delete([]byte(key), nil) | ||
} | ||
|
||
func Range(scoop string, fn func(key []byte, data []byte)) error { | ||
db, err := GetClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
iter := db.NewIterator( | ||
util.BytesPrefix([]byte(scoop)), | ||
nil, | ||
) | ||
|
||
for iter.Next() { | ||
fn(iter.Key(), iter.Value()) | ||
} | ||
|
||
iter.Release() | ||
return iter.Error() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package cache | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// 分片上传任务 | ||
type MutUpload struct { | ||
UploadID string | ||
|
||
// 文件总计大小 | ||
Size int64 | ||
|
||
// 分块大小 | ||
PartSize int64 | ||
|
||
// 本都文件路径 | ||
Path string | ||
|
||
// 云端文件路径 | ||
UpPath string | ||
|
||
// 上传时间 | ||
CreateAt time.Time | ||
} | ||
|
||
func (p *MutUpload) Key() string { | ||
fingerprint := fmt.Sprintf( | ||
"%s:%s:%d:%d", | ||
p.Path, | ||
p.UpPath, | ||
p.Size, | ||
p.PartSize, | ||
) | ||
|
||
return fmt.Sprintf( | ||
"mutupload-%x", | ||
md5.Sum([]byte(fingerprint)), | ||
) | ||
} | ||
|
||
// 查询分片上传任务 | ||
func FindMutUpload(fn func(key string, entity *MutUpload) bool) ([]*MutUpload, error) { | ||
var result []*MutUpload | ||
err := Range("mutupload-", func(key []byte, value []byte) { | ||
var item = &MutUpload{} | ||
if err := json.Unmarshal(value, item); err != nil { | ||
db.Delete(key, nil) | ||
return | ||
} | ||
|
||
// 删除过期的分片上传记录 | ||
if time.Since(item.CreateAt).Hours() > 12 { | ||
FindMutUploadPart(func(key string, part *MutUploadPart) bool { | ||
if part.UploadID == item.UploadID { | ||
db.Delete([]byte(key), nil) | ||
} | ||
return false | ||
}) | ||
db.Delete(key, nil) | ||
} | ||
|
||
if fn(string(key), item) { | ||
result = append(result, item) | ||
} | ||
}) | ||
return result, err | ||
} | ||
|
||
// 添加分片上传 | ||
func AddMutUpload(entity *MutUpload) error { | ||
db, err := GetClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
data, err := json.Marshal(entity) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return db.Put([]byte(entity.Key()), data, nil) | ||
} | ||
|
||
// 分片上传任务下的具体分片信息 | ||
type MutUploadPart struct { | ||
UploadID string | ||
PartId int64 | ||
Len int64 | ||
} | ||
|
||
func (p *MutUploadPart) Key() string { | ||
return fmt.Sprintf("part-%s-%d", p.UploadID, p.PartId) | ||
} | ||
|
||
// 获取已经上传的分片 | ||
func FindMutUploadPart(fn func(key string, entity *MutUploadPart) bool) ([]*MutUploadPart, error) { | ||
var result []*MutUploadPart | ||
err := Range("part-", func(key []byte, value []byte) { | ||
var item = &MutUploadPart{} | ||
if err := json.Unmarshal(value, item); err != nil { | ||
db.Delete(key, nil) | ||
return | ||
} | ||
|
||
if fn(string(key), item) { | ||
result = append(result, item) | ||
} | ||
}) | ||
return result, err | ||
} | ||
|
||
// 记录已经上传的分片 | ||
func AddMutUploadPart(entity *MutUploadPart) error { | ||
db, err := GetClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
data, err := json.Marshal(entity) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return db.Put([]byte(entity.Key()), data, nil) | ||
} | ||
|
||
func DeleteByUploadID(uploadID string) error { | ||
FindMutUpload(func(key string, entity *MutUpload) bool { | ||
if entity.UploadID == uploadID { | ||
Delete(key) | ||
} | ||
return false | ||
}) | ||
FindMutUploadPart(func(key string, entity *MutUploadPart) bool { | ||
if entity.UploadID == uploadID { | ||
Delete(key) | ||
} | ||
return false | ||
}) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package cache | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMutUpload(t *testing.T) { | ||
mutUpload := &MutUpload{ | ||
UploadID: "1", | ||
Size: 100 * 12, | ||
PartSize: 100, | ||
Path: "a.jpg", | ||
UpPath: "b.jpg", | ||
CreateAt: time.Now(), | ||
} | ||
assert.NoError(t, AddMutUpload(mutUpload)) | ||
assert.NoError(t, AddMutUpload(&MutUpload{ | ||
UploadID: "2", | ||
Size: 100 * 12, | ||
PartSize: 100, | ||
Path: "/c/a.jpg", | ||
UpPath: "b.jpg", | ||
CreateAt: time.Now(), | ||
})) | ||
results, err := FindMutUpload(func(key string, entity *MutUpload) bool { | ||
return key == mutUpload.Key() | ||
}) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, len(results), 1) | ||
assert.Equal( | ||
t, | ||
results[0].Key(), | ||
mutUpload.Key(), | ||
) | ||
} | ||
|
||
func TestMutUploadPart(t *testing.T) { | ||
part1s := []int64{} | ||
for i := 0; i < 100; i++ { | ||
part1s = append(part1s, int64(i)) | ||
} | ||
|
||
for _, v := range part1s { | ||
err := AddMutUploadPart(&MutUploadPart{ | ||
UploadID: "1", | ||
PartId: v, | ||
Len: 100, | ||
}) | ||
assert.NoError(t, err) | ||
} | ||
|
||
part2s := []int64{} | ||
records, err := FindMutUploadPart(func(key string, entity *MutUploadPart) bool { | ||
return entity.UploadID == "1" | ||
}) | ||
assert.NoError(t, err) | ||
for _, v := range records { | ||
part2s = append(part2s, v.PartId) | ||
} | ||
|
||
assert.ElementsMatch(t, part1s, part2s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.