|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- 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"
- stgtypes "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 *stgtypes.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) (stgtypes.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 stgtypes.FileInfo{}, cerr.ToError()
- }
-
- return resp.Info, nil
- }
-
- func (s *ShardStore) Info(hash jcstypes.FileHash) (stgtypes.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 stgtypes.FileInfo{}, cerr.ToError()
- }
- return resp.Info, nil
- }
-
- func (s *ShardStore) ListAll() ([]stgtypes.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() stgtypes.ShardStoreStats {
- resp, cerr := s.hubCli.PubShardsStats(context.Background(), &hubrpc.PubShardsStats{
- PubShardsID: s.stgType.PubShardsID,
- Password: s.stgType.Password,
- })
- if cerr != nil {
- return stgtypes.ShardStoreStats{}
- }
- return resp.Stats
- }
|