| @@ -1,5 +1,7 @@ | |||||
| package models | package models | ||||
| import "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" | |||||
| /// TODO 将分散在各处的公共结构体定义集中到这里来 | /// TODO 将分散在各处的公共结构体定义集中到这里来 | ||||
| type RedundancyData interface{} | type RedundancyData interface{} | ||||
| @@ -59,25 +61,27 @@ func NewEc(id int, name string, ecK int, ecN int) EC { | |||||
| } | } | ||||
| type ObjectRepData struct { | type ObjectRepData struct { | ||||
| ObjectID int64 `json:"objectID"` | |||||
| FileHash string `json:"fileHash"` | |||||
| NodeIDs []int64 `json:"nodeIDs"` | |||||
| Object model.Object `json:"object"` | |||||
| FileHash string `json:"fileHash"` | |||||
| NodeIDs []int64 `json:"nodeIDs"` | |||||
| } | } | ||||
| func NewObjectRepData(objectID int64, fileHash string, nodeIDs []int64) ObjectRepData { | |||||
| func NewObjectRepData(object model.Object, fileHash string, nodeIDs []int64) ObjectRepData { | |||||
| return ObjectRepData{ | return ObjectRepData{ | ||||
| ObjectID: objectID, | |||||
| Object: object, | |||||
| FileHash: fileHash, | FileHash: fileHash, | ||||
| NodeIDs: nodeIDs, | NodeIDs: nodeIDs, | ||||
| } | } | ||||
| } | } | ||||
| type ObjectECData struct { | type ObjectECData struct { | ||||
| Object model.Object `json:"object"` | |||||
| Blocks []ObjectBlockData `json:"blocks"` | Blocks []ObjectBlockData `json:"blocks"` | ||||
| } | } | ||||
| func NewObjectECData(blocks []ObjectBlockData) ObjectECData { | |||||
| func NewObjectECData(object model.Object, blocks []ObjectBlockData) ObjectECData { | |||||
| return ObjectECData{ | return ObjectECData{ | ||||
| Object: object, | |||||
| Blocks: blocks, | Blocks: blocks, | ||||
| } | } | ||||
| } | } | ||||
| @@ -85,15 +85,15 @@ func (db *ObjectBlockDB) GetBatchBlocksNodes(ctx SQLContext, hashs [][]string) ( | |||||
| } | } | ||||
| func (db *ObjectBlockDB) GetWithNodeIDInPackage(ctx SQLContext, packageID int64) ([]models.ObjectECData, error) { | func (db *ObjectBlockDB) GetWithNodeIDInPackage(ctx SQLContext, packageID int64) ([]models.ObjectECData, error) { | ||||
| var objectIDs []int64 | |||||
| err := sqlx.Select(ctx, &objectIDs, "select ObjectID from Object where PackageID = ? order by ObjectID asc", packageID) | |||||
| var objs []model.Object | |||||
| err := sqlx.Select(ctx, &objs, "select * from Object where PackageID = ? order by ObjectID asc", packageID) | |||||
| if err != nil { | if err != nil { | ||||
| return nil, fmt.Errorf("query objectIDs: %w", err) | return nil, fmt.Errorf("query objectIDs: %w", err) | ||||
| } | } | ||||
| rets := make([]models.ObjectECData, 0, len(objectIDs)) | |||||
| rets := make([]models.ObjectECData, 0, len(objs)) | |||||
| for _, objID := range objectIDs { | |||||
| for _, obj := range objs { | |||||
| var tmpRets []struct { | var tmpRets []struct { | ||||
| Index int `db:"Index"` | Index int `db:"Index"` | ||||
| FileHash string `db:"FileHash"` | FileHash string `db:"FileHash"` | ||||
| @@ -105,7 +105,7 @@ func (db *ObjectBlockDB) GetWithNodeIDInPackage(ctx SQLContext, packageID int64) | |||||
| "select ObjectBlock.Index, ObjectBlock.FileHash, group_concat(NodeID) as NodeIDs from ObjectBlock"+ | "select ObjectBlock.Index, ObjectBlock.FileHash, group_concat(NodeID) as NodeIDs from ObjectBlock"+ | ||||
| " left join Cache on ObjectBlock.FileHash = Cache.FileHash"+ | " left join Cache on ObjectBlock.FileHash = Cache.FileHash"+ | ||||
| " where ObjectID = ? group by ObjectBlock.Index, ObjectBlock.FileHash", | " where ObjectID = ? group by ObjectBlock.Index, ObjectBlock.FileHash", | ||||
| objID, | |||||
| obj.ObjectID, | |||||
| ) | ) | ||||
| if err != nil { | if err != nil { | ||||
| return nil, err | return nil, err | ||||
| @@ -124,7 +124,7 @@ func (db *ObjectBlockDB) GetWithNodeIDInPackage(ctx SQLContext, packageID int64) | |||||
| blocks = append(blocks, block) | blocks = append(blocks, block) | ||||
| } | } | ||||
| rets = append(rets, models.NewObjectECData(blocks)) | |||||
| rets = append(rets, models.NewObjectECData(obj, blocks)) | |||||
| } | } | ||||
| return rets, nil | return rets, nil | ||||
| @@ -81,14 +81,14 @@ func (db *ObjectRepDB) GetFileMaxRepCount(ctx SQLContext, fileHash string) (int, | |||||
| func (db *ObjectRepDB) GetWithNodeIDInPackage(ctx SQLContext, packageID int64) ([]models.ObjectRepData, error) { | func (db *ObjectRepDB) GetWithNodeIDInPackage(ctx SQLContext, packageID int64) ([]models.ObjectRepData, error) { | ||||
| var tmpRets []struct { | var tmpRets []struct { | ||||
| ObjectID int64 `db:"ObjectID"` | |||||
| model.Object | |||||
| FileHash *string `db:"FileHash"` | FileHash *string `db:"FileHash"` | ||||
| NodeIDs *string `db:"NodeIDs"` | NodeIDs *string `db:"NodeIDs"` | ||||
| } | } | ||||
| err := sqlx.Select(ctx, | err := sqlx.Select(ctx, | ||||
| &tmpRets, | &tmpRets, | ||||
| "select Object.ObjectID, ObjectRep.FileHash, group_concat(NodeID) as NodeIDs from Object"+ | |||||
| "select Object.*, ObjectRep.FileHash, group_concat(NodeID) as NodeIDs from Object"+ | |||||
| " left join ObjectRep on Object.ObjectID = ObjectRep.ObjectID"+ | " left join ObjectRep on Object.ObjectID = ObjectRep.ObjectID"+ | ||||
| " left join Cache on ObjectRep.FileHash = Cache.FileHash"+ | " left join Cache on ObjectRep.FileHash = Cache.FileHash"+ | ||||
| " where PackageID = ? group by Object.ObjectID order by Object.ObjectID asc", | " where PackageID = ? group by Object.ObjectID order by Object.ObjectID asc", | ||||
| @@ -97,11 +97,10 @@ func (db *ObjectRepDB) GetWithNodeIDInPackage(ctx SQLContext, packageID int64) ( | |||||
| if err != nil { | if err != nil { | ||||
| return nil, err | return nil, err | ||||
| } | } | ||||
| rets := make([]models.ObjectRepData, 0, len(tmpRets)) | rets := make([]models.ObjectRepData, 0, len(tmpRets)) | ||||
| for _, tmp := range tmpRets { | for _, tmp := range tmpRets { | ||||
| var repData models.ObjectRepData | var repData models.ObjectRepData | ||||
| repData.ObjectID = tmp.ObjectID | |||||
| repData.Object = tmp.Object | |||||
| if tmp.FileHash != nil { | if tmp.FileHash != nil { | ||||
| repData.FileHash = *tmp.FileHash | repData.FileHash = *tmp.FileHash | ||||
| @@ -212,9 +212,14 @@ type GetPackageCachedNodes struct { | |||||
| PackageID int64 `json:"packageID"` | PackageID int64 `json:"packageID"` | ||||
| } | } | ||||
| type PackageCachedNodeInfo struct { | |||||
| NodeID int64 `json:"nodeID"` | |||||
| FileSize int64 `json:"fileSize"` | |||||
| ObjectCount int64 `json:"objectCount"` | |||||
| } | |||||
| type GetPackageCachedNodesResp struct { | type GetPackageCachedNodesResp struct { | ||||
| NodeIDs []int64 `json:"nodeIDs"` | |||||
| RedundancyType string `json:"redundancyType"` | |||||
| models.PackageCachingInfo | |||||
| } | } | ||||
| func NewGetPackageCachedNodes(userID int64, packageID int64) GetPackageCachedNodes { | func NewGetPackageCachedNodes(userID int64, packageID int64) GetPackageCachedNodes { | ||||
| @@ -224,10 +229,13 @@ func NewGetPackageCachedNodes(userID int64, packageID int64) GetPackageCachedNod | |||||
| } | } | ||||
| } | } | ||||
| func NewGetPackageCachedNodesResp(nodeIDs []int64, redundancyType string) GetPackageCachedNodesResp { | |||||
| func NewGetPackageCachedNodesResp(nodeInfos []models.NodePackageCachingInfo, packageSize int64, redunancyType string) GetPackageCachedNodesResp { | |||||
| return GetPackageCachedNodesResp{ | return GetPackageCachedNodesResp{ | ||||
| NodeIDs: nodeIDs, | |||||
| RedundancyType: redundancyType, | |||||
| PackageCachingInfo: models.PackageCachingInfo{ | |||||
| NodeInfos: nodeInfos, | |||||
| PackageSize: packageSize, | |||||
| RedunancyType: redunancyType, | |||||
| }, | |||||
| } | } | ||||
| } | } | ||||