Files
EdgeNode/internal/caches/list_file_hash_map.go

115 lines
2.1 KiB
Go
Raw Normal View History

2022-08-20 11:47:57 +08:00
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package caches
import (
"github.com/TeaOSLab/EdgeNode/internal/utils"
2022-08-20 11:47:57 +08:00
"github.com/TeaOSLab/EdgeNode/internal/zero"
2022-08-22 09:44:09 +08:00
"math/big"
2022-08-20 11:47:57 +08:00
"sync"
)
// FileListHashMap 文件Hash列表
2022-08-20 11:47:57 +08:00
type FileListHashMap struct {
2022-08-22 09:44:09 +08:00
m map[uint64]zero.Zero
locker sync.RWMutex
isAvailable bool
isReady bool
2022-08-20 11:47:57 +08:00
}
func NewFileListHashMap() *FileListHashMap {
return &FileListHashMap{
2022-08-22 09:44:09 +08:00
m: map[uint64]zero.Zero{},
isAvailable: false,
isReady: false,
2022-08-20 11:47:57 +08:00
}
}
func (this *FileListHashMap) Load(db *FileListDB) error {
// 如果系统内存过小,我们不缓存
if utils.SystemMemoryGB() < 3 {
return nil
}
this.isAvailable = true
2022-08-20 11:47:57 +08:00
var lastId int64
for {
hashList, maxId, err := db.ListHashes(lastId)
if err != nil {
return err
}
if len(hashList) == 0 {
break
}
2022-08-22 09:44:09 +08:00
this.AddHashes(hashList)
2022-08-20 11:47:57 +08:00
lastId = maxId
}
this.isReady = true
return nil
}
func (this *FileListHashMap) Add(hash string) {
if !this.isAvailable {
return
}
2022-08-20 11:47:57 +08:00
this.locker.Lock()
2022-08-22 09:44:09 +08:00
this.m[this.bigInt(hash)] = zero.New()
this.locker.Unlock()
}
func (this *FileListHashMap) AddHashes(hashes []string) {
if !this.isAvailable {
return
}
this.locker.Lock()
for _, hash := range hashes {
this.m[this.bigInt(hash)] = zero.New()
}
2022-08-20 11:47:57 +08:00
this.locker.Unlock()
}
func (this *FileListHashMap) Delete(hash string) {
if !this.isAvailable {
return
}
2022-08-20 11:47:57 +08:00
this.locker.Lock()
2022-08-22 09:44:09 +08:00
delete(this.m, this.bigInt(hash))
2022-08-20 11:47:57 +08:00
this.locker.Unlock()
}
func (this *FileListHashMap) Exist(hash string) bool {
if !this.isAvailable {
return true
}
2022-08-20 11:47:57 +08:00
if !this.isReady {
// 只有完全Ready时才能判断是否为false
return true
}
this.locker.RLock()
2022-08-22 09:44:09 +08:00
_, ok := this.m[this.bigInt(hash)]
2022-08-20 11:47:57 +08:00
this.locker.RUnlock()
return ok
}
func (this *FileListHashMap) Clean() {
this.locker.Lock()
2022-08-22 09:44:09 +08:00
this.m = map[uint64]zero.Zero{}
2022-08-20 11:47:57 +08:00
this.locker.Unlock()
}
func (this *FileListHashMap) IsReady() bool {
return this.isReady
}
2022-08-22 09:44:09 +08:00
func (this *FileListHashMap) bigInt(hash string) uint64 {
var bigInt = big.NewInt(0)
bigInt.SetString(hash, 16)
return bigInt.Uint64()
}