/
sql.go
104 lines (90 loc) · 3.36 KB
/
sql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package db
import (
"database/sql"
"fmt"
"strings"
"github.com/Filecoin-Titan/titan/node/modules/dtypes"
"github.com/jmoiron/sqlx"
logging "github.com/ipfs/go-log/v2"
)
var log = logging.Logger("db")
// SQLDB represents a scheduler sql database.
type SQLDB struct {
db *sqlx.DB
}
// NewSQLDB creates a new SQLDB instance.
func NewSQLDB(db *sqlx.DB) (*SQLDB, error) {
s := &SQLDB{db}
return s, nil
}
const (
// Database table names.
assetRecordTable = "asset_record"
replicaInfoTable = "replica_info"
edgeUpdateTable = "edge_update_info"
nodeInfoTable = "node_info"
validatorsTable = "validators"
nodeRegisterTable = "node_register_info"
validationResultTable = "validation_result"
assetsViewTable = "asset_view"
bucketTable = "bucket"
workloadRecordTable = "workload_record"
userAssetTable = "user_asset"
userInfoTable = "user_info"
replicaEventTable = "replica_event"
retrieveEventTable = "retrieve_event"
assetVisitCountTable = "asset_visit_count"
replenishBackupTable = "replenish_backup"
userAssetGroupTable = "user_asset_group"
awsDataTable = "aws_data"
// Default limits for loading table entries.
loadNodeInfosDefaultLimit = 1000
loadValidationResultsDefaultLimit = 100
loadAssetRecordsDefaultLimit = 100
loadExpiredAssetRecordsDefaultLimit = 100
loadWorkloadDefaultLimit = 100
loadReplicaEventDefaultLimit = 500
loadRetrieveDefaultLimit = 100
loadReplicaDefaultLimit = 100
loadUserDefaultLimit = 100
)
// assetStateTable returns the asset state table name for the given serverID.
func assetStateTable(serverID dtypes.ServerID) string {
str := strings.ReplaceAll(string(serverID), "-", "")
return fmt.Sprintf("asset_state_%s", str)
}
// InitTables initializes data tables.
func InitTables(d *SQLDB, serverID dtypes.ServerID) error {
// init table
tx, err := d.db.Beginx()
if err != nil {
return err
}
defer func() {
err = tx.Rollback()
if err != nil && err != sql.ErrTxDone {
log.Errorf("InitTables Rollback err:%s", err.Error())
}
}()
// Execute table creation statements
tx.MustExec(fmt.Sprintf(cAssetStateTable, assetStateTable(serverID)))
tx.MustExec(fmt.Sprintf(cReplicaInfoTable, replicaInfoTable))
tx.MustExec(fmt.Sprintf(cNodeInfoTable, nodeInfoTable))
tx.MustExec(fmt.Sprintf(cValidationResultsTable, validationResultTable))
tx.MustExec(fmt.Sprintf(cNodeRegisterTable, nodeRegisterTable))
tx.MustExec(fmt.Sprintf(cAssetRecordTable, assetRecordTable))
tx.MustExec(fmt.Sprintf(cEdgeUpdateTable, edgeUpdateTable))
tx.MustExec(fmt.Sprintf(cValidatorsTable, validatorsTable))
tx.MustExec(fmt.Sprintf(cAssetViewTable, assetsViewTable))
tx.MustExec(fmt.Sprintf(cBucketTable, bucketTable))
tx.MustExec(fmt.Sprintf(cWorkloadTable, workloadRecordTable))
tx.MustExec(fmt.Sprintf(cUserAssetTable, userAssetTable))
tx.MustExec(fmt.Sprintf(cUserInfoTable, userInfoTable))
tx.MustExec(fmt.Sprintf(cReplicaEventTable, replicaEventTable))
tx.MustExec(fmt.Sprintf(cRetrieveEventTable, retrieveEventTable))
tx.MustExec(fmt.Sprintf(cAssetVisitCountTable, assetVisitCountTable))
tx.MustExec(fmt.Sprintf(cReplenishBackupTable, replenishBackupTable))
tx.MustExec(fmt.Sprintf(cUserAssetGroupTable, userAssetGroupTable))
tx.MustExec(fmt.Sprintf(cAWSDataTable, awsDataTable))
return tx.Commit()
}