Skip to content

Commit

Permalink
refactor(jobdb): add jobdb v2 interface, objectID as primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
will-2012 committed Feb 3, 2023
1 parent 6b6059d commit 563e76b
Show file tree
Hide file tree
Showing 4 changed files with 472 additions and 8 deletions.
19 changes: 19 additions & 0 deletions store/jobdb/job_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type PieceJob struct {
Done bool
}

// JobDB use txhash as primary key
type JobDB interface {
CreateUploadPayloadJob(txHash []byte, info *ptypesv1pb.ObjectInfo) (uint64, error)
SetObjectCreateHeightAndObjectID(txHash []byte, height uint64, objectID uint64) error
Expand All @@ -38,3 +39,21 @@ type JobDB interface {
SetPrimaryPieceJobDone(txHash []byte, piece *PieceJob) error
SetSecondaryPieceJobDone(txHash []byte, piece *PieceJob) error
}

// JobDBV2 use objectID as primary key
type JobDBV2 interface {
CreateUploadPayloadJobV2(info *ptypesv1pb.ObjectInfo) (uint64, error)
// SetObjectCreateHeightAndObjectID maybe useless
// SetObjectCreateHeightAndObjectID(txHash []byte, height uint64, objectID uint64) error

GetObjectInfoV2(objectID uint64) (*ptypesv1pb.ObjectInfo, error)
GetJobContextV2(jobId uint64) (*ptypesv1pb.JobContext, error)

SetUploadPayloadJobStateV2(jobId uint64, state string, timestamp int64) error
SetUploadPayloadJobJobErrorV2(jobID uint64, jobState string, jobErr string, timestamp int64) error

GetPrimaryJobV2(objectID uint64) ([]*PieceJob, error)
GetSecondaryJobV2(objectID uint64) ([]*PieceJob, error)
SetPrimaryPieceJobDoneV2(objectID uint64, piece *PieceJob) error
SetSecondaryPieceJobDoneV2(objectID uint64, piece *PieceJob) error
}
77 changes: 76 additions & 1 deletion store/jobdb/jobsql/db_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/bnb-chain/greenfield-storage-provider/util/log"
)

// v1 schema

// DBJob table schema
type DBJob struct {
JobID uint64 `gorm:"primary_key;autoIncrement"`
Expand Down Expand Up @@ -65,6 +67,64 @@ func (DBPieceJob) TableName() string {
return "piece_job"
}

// v2 schema

// DBJobV2 table schema
type DBJobV2 struct {
JobID uint64 `gorm:"primary_key;autoIncrement"`
JobType uint32
JobState uint32
JobErr string
CreateTime time.Time
ModifyTime time.Time
}

// TableName is used to set Job Schema's table name in database
func (DBJobV2) TableName() string {
return "job_v2"
}

// DBObjectV2 table schema
type DBObjectV2 struct {
ObjectID uint64 `gorm:"primary_key"`
JobID uint64 // Job.JobID
CreateHash string
SealHash string
Owner string
BucketName string
ObjectName string
Size uint64
Checksum string
IsPrivate bool
ContentType string
PrimarySP string
Height uint64
RedundancyType uint32
}

// TableName is used to set Object Schema's table name in database
func (DBObjectV2) TableName() string {
return "object_v2"
}

// DBPieceJobV2 table schema
type DBPieceJobV2 struct {
ObjectID uint64 `gorm:"index:idx_piece_group"`
PieceType uint32 `gorm:"index:idx_piece_group"`
PieceIdx uint32
PieceState uint32
Checksum string
StorageProvider string
IntegrityHash string
Signature string
}

// TableName is used to set PieceJob Schema's table name in database
func (DBPieceJobV2) TableName() string {
return "piece_job_v2"
}

// DBOption is mysql config options
type DBOption struct {
User string
Passwd string
Expand All @@ -77,7 +137,7 @@ var DefaultDBOption = &DBOption{
User: "root",
Passwd: "test_pwd",
Address: "127.0.0.1:3306",
Database: "job_context",
Database: "job_db",
}

func InitDB(opt *DBOption) (*gorm.DB, error) {
Expand All @@ -93,6 +153,7 @@ func InitDB(opt *DBOption) (*gorm.DB, error) {
}

// create if not exist
// v1 table
if err := db.AutoMigrate(&DBJob{}); err != nil {
log.Warnw("failed to create job table", "err", err)
return nil, err
Expand All @@ -105,5 +166,19 @@ func InitDB(opt *DBOption) (*gorm.DB, error) {
log.Warnw("failed to create piece job table", "err", err)
return nil, err
}
// v2 table
if err := db.AutoMigrate(&DBJobV2{}); err != nil {
log.Warnw("failed to create job table v2", "err", err)
return nil, err
}
if err := db.AutoMigrate(&DBObjectV2{}); err != nil {
log.Warnw("failed to create object table v2", "err", err)
return nil, err
}
if err := db.AutoMigrate(&DBPieceJobV2{}); err != nil {
log.Warnw("failed to create piece job table v2", "err", err)
return nil, err
}

return db, nil
}
Loading

0 comments on commit 563e76b

Please sign in to comment.