JCS-pub/common/pkgs/storage/pubshards/shard_store.go

101 lines
2.5 KiB
Go

package pubshards
import (
"context"
"fmt"
stgglb "gitlink.org.cn/cloudream/jcs-pub/common/globals"
hubrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc/hub"
"gitlink.org.cn/cloudream/jcs-pub/common/pkgs/storage/types"
jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
)
type ShardStore struct {
detail *jcstypes.UserSpaceDetail
stgType *jcstypes.PubShardsType
hubCli *hubrpc.Client
}
func NewShardStore(detail *jcstypes.UserSpaceDetail, stgType *jcstypes.PubShardsType) (*ShardStore, error) {
if stgglb.StandaloneMode {
return nil, fmt.Errorf("pub shards only support online mode")
}
return &ShardStore{
detail: detail,
stgType: stgType,
}, nil
}
func (s *ShardStore) Start(ch *types.StorageEventChan) {
s.hubCli = stgglb.HubRPCPool.GetByID(s.stgType.MasterHub)
}
func (s *ShardStore) Stop() {
if s.hubCli != nil {
s.hubCli.Release()
s.hubCli = nil
}
}
func (s *ShardStore) Store(path jcstypes.JPath, hash jcstypes.FileHash, size int64) (types.FileInfo, error) {
resp, cerr := s.hubCli.PubShardsStore(context.Background(), &hubrpc.PubShardsStore{
PubShardsID: s.stgType.PubShardsID,
Password: s.stgType.Password,
Path: path,
Hash: hash,
Size: size,
})
if cerr != nil {
return types.FileInfo{}, cerr.ToError()
}
return resp.Info, nil
}
func (s *ShardStore) Info(hash jcstypes.FileHash) (types.FileInfo, error) {
resp, cerr := s.hubCli.PubShardsInfo(context.Background(), &hubrpc.PubShardsInfo{
PubShardsID: s.stgType.PubShardsID,
Password: s.stgType.Password,
FileHash: hash,
})
if cerr != nil {
return types.FileInfo{}, cerr.ToError()
}
return resp.Info, nil
}
func (s *ShardStore) ListAll() ([]types.FileInfo, error) {
resp, cerr := s.hubCli.PubShardsListAll(context.Background(), &hubrpc.PubShardsListAll{
PubShardsID: s.stgType.PubShardsID,
Password: s.stgType.Password,
})
if cerr != nil {
return nil, cerr.ToError()
}
return resp.Infos, nil
}
func (s *ShardStore) GC(avaiables []jcstypes.FileHash) error {
_, cerr := s.hubCli.PubShardsGC(context.Background(), &hubrpc.PubShardsGC{
PubShardsID: s.stgType.PubShardsID,
Password: s.stgType.Password,
FileHashes: avaiables,
})
if cerr != nil {
return cerr.ToError()
}
return nil
}
func (s *ShardStore) Stats() types.Stats {
resp, cerr := s.hubCli.PubShardsStats(context.Background(), &hubrpc.PubShardsStats{
PubShardsID: s.stgType.PubShardsID,
Password: s.stgType.Password,
})
if cerr != nil {
return types.Stats{}
}
return resp.Stats
}