From 5565a15f67fec49ed04b77eae4f053f537464ed1 Mon Sep 17 00:00:00 2001 From: songjc <969378911@qq.com> Date: Fri, 25 Aug 2023 09:30:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8E=A5=E5=8F=A3=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/models.go | 16 ++++++++++------ pkgs/db/object_block.go | 12 ++++++------ pkgs/db/object_rep.go | 7 +++---- pkgs/mq/coordinator/package.go | 18 +++++++++++++----- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/models/models.go b/models/models.go index f260816..114454c 100644 --- a/models/models.go +++ b/models/models.go @@ -1,5 +1,7 @@ package models +import "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" + /// TODO 将分散在各处的公共结构体定义集中到这里来 type RedundancyData interface{} @@ -59,25 +61,27 @@ func NewEc(id int, name string, ecK int, ecN int) EC { } 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{ - ObjectID: objectID, + Object: object, FileHash: fileHash, NodeIDs: nodeIDs, } } type ObjectECData struct { + Object model.Object `json:"object"` Blocks []ObjectBlockData `json:"blocks"` } -func NewObjectECData(blocks []ObjectBlockData) ObjectECData { +func NewObjectECData(object model.Object, blocks []ObjectBlockData) ObjectECData { return ObjectECData{ + Object: object, Blocks: blocks, } } diff --git a/pkgs/db/object_block.go b/pkgs/db/object_block.go index 7b4dd66..65e8a08 100644 --- a/pkgs/db/object_block.go +++ b/pkgs/db/object_block.go @@ -85,15 +85,15 @@ func (db *ObjectBlockDB) GetBatchBlocksNodes(ctx SQLContext, hashs [][]string) ( } 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 { 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 { Index int `db:"Index"` 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"+ " left join Cache on ObjectBlock.FileHash = Cache.FileHash"+ " where ObjectID = ? group by ObjectBlock.Index, ObjectBlock.FileHash", - objID, + obj.ObjectID, ) if err != nil { return nil, err @@ -124,7 +124,7 @@ func (db *ObjectBlockDB) GetWithNodeIDInPackage(ctx SQLContext, packageID int64) blocks = append(blocks, block) } - rets = append(rets, models.NewObjectECData(blocks)) + rets = append(rets, models.NewObjectECData(obj, blocks)) } return rets, nil diff --git a/pkgs/db/object_rep.go b/pkgs/db/object_rep.go index 6514021..48c2b7f 100644 --- a/pkgs/db/object_rep.go +++ b/pkgs/db/object_rep.go @@ -81,14 +81,14 @@ func (db *ObjectRepDB) GetFileMaxRepCount(ctx SQLContext, fileHash string) (int, func (db *ObjectRepDB) GetWithNodeIDInPackage(ctx SQLContext, packageID int64) ([]models.ObjectRepData, error) { var tmpRets []struct { - ObjectID int64 `db:"ObjectID"` + model.Object FileHash *string `db:"FileHash"` NodeIDs *string `db:"NodeIDs"` } err := sqlx.Select(ctx, &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 Cache on ObjectRep.FileHash = Cache.FileHash"+ " 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 { return nil, err } - rets := make([]models.ObjectRepData, 0, len(tmpRets)) for _, tmp := range tmpRets { var repData models.ObjectRepData - repData.ObjectID = tmp.ObjectID + repData.Object = tmp.Object if tmp.FileHash != nil { repData.FileHash = *tmp.FileHash diff --git a/pkgs/mq/coordinator/package.go b/pkgs/mq/coordinator/package.go index faccbe4..5cb43dd 100644 --- a/pkgs/mq/coordinator/package.go +++ b/pkgs/mq/coordinator/package.go @@ -212,9 +212,14 @@ type GetPackageCachedNodes struct { PackageID int64 `json:"packageID"` } +type PackageCachedNodeInfo struct { + NodeID int64 `json:"nodeID"` + FileSize int64 `json:"fileSize"` + ObjectCount int64 `json:"objectCount"` +} + type GetPackageCachedNodesResp struct { - NodeIDs []int64 `json:"nodeIDs"` - RedundancyType string `json:"redundancyType"` + models.PackageCachingInfo } 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{ - NodeIDs: nodeIDs, - RedundancyType: redundancyType, + PackageCachingInfo: models.PackageCachingInfo{ + NodeInfos: nodeInfos, + PackageSize: packageSize, + RedunancyType: redunancyType, + }, } }