-
Notifications
You must be signed in to change notification settings - Fork 39
/
common.go
124 lines (106 loc) · 3.67 KB
/
common.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package bsdb
import (
"database/sql/driver"
"fmt"
"runtime"
"strconv"
"strings"
"time"
permtypes "github.com/bnb-chain/greenfield/x/permission/types"
"github.com/bnb-chain/greenfield-storage-provider/pkg/metrics"
)
// ListObjectsResult represents the result of a List Objects operation.
type ListObjectsResult struct {
PathName string
ResultType string
*Object
}
// ActionTypeMap db action value is a bitmap, traverse this map to get the corresponding action list
var ActionTypeMap = map[permtypes.ActionType]int{
permtypes.ACTION_TYPE_ALL: 0,
permtypes.ACTION_UPDATE_BUCKET_INFO: 1,
permtypes.ACTION_DELETE_BUCKET: 2,
permtypes.ACTION_CREATE_OBJECT: 3,
permtypes.ACTION_DELETE_OBJECT: 4,
permtypes.ACTION_COPY_OBJECT: 5,
permtypes.ACTION_GET_OBJECT: 6,
permtypes.ACTION_EXECUTE_OBJECT: 7,
permtypes.ACTION_LIST_OBJECT: 8,
permtypes.ACTION_UPDATE_GROUP_MEMBER: 9,
permtypes.ACTION_DELETE_GROUP: 10,
permtypes.ACTION_UPDATE_OBJECT_INFO: 11,
}
const (
// BsDBUser defines env variable name for block syncer db username.
BsDBUser = "BS_DB_USER"
// BsDBPasswd defines env variable name for block syncer db user passwd.
BsDBPasswd = "BS_DB_PASSWORD"
// BsDBAddress defines env variable name for block syncer db address.
BsDBAddress = "BS_DB_ADDRESS"
// BsDBDataBase defines env variable name for block syncer db database.
BsDBDataBase = "BS_DB_DATABASE"
// BsDBSwitchedUser defines env variable name for switched block syncer db username.
BsDBSwitchedUser = "BS_DB_SWITCHED_USER"
// BsDBSwitchedPasswd defines env variable name for switched block syncer db user passwd.
BsDBSwitchedPasswd = "BS_DB_SWITCHED_PASSWORD"
// BsDBSwitchedAddress defines env variable name for switched block syncer db address.
BsDBSwitchedAddress = "BS_DB_SWITCHED_ADDRESS"
// BsDBSwitchedDataBase defines env variable name for switched block syncer db database.
BsDBSwitchedDataBase = "BS_DB_SWITCHED_DATABASE"
)
// ObjectIDs represents the request of list object by ids
type ObjectIDs struct {
IDs []uint64 `json:"ids"`
}
// BucketIDs represents the request of list bucket by ids
type BucketIDs struct {
IDs []uint64 `json:"ids"`
}
type Uint32Array []uint32
func (a *Uint32Array) Scan(value interface{}) error {
if value == nil {
*a = nil
return nil
}
bytes, ok := value.([]byte)
if !ok {
return fmt.Errorf("failed to scan Uint32Array value: %v", value)
}
s := string(bytes)
fields := strings.Split(s, ",")
result := make([]uint32, len(fields))
for i, field := range fields {
v, err := strconv.ParseUint(field, 10, 32)
if err != nil {
return fmt.Errorf("failed to scan Uint32Array value: %v", err)
}
result[i] = uint32(v)
}
*a = result
return nil
}
func (a Uint32Array) Value() (driver.Value, error) {
if len(a) == 0 {
return nil, nil
}
values := make([]string, len(a))
for i, value := range a {
values[i] = strconv.FormatUint(uint64(value), 10)
}
return strings.Join(values, ","), nil
}
func MetadataDatabaseFailureMetrics(err error, startTime time.Time, methodName string) {
metrics.MetadataReqTime.WithLabelValues(DatabaseFailure, DatabaseLevel, methodName, err.Error()).Observe(time.Since(startTime).Seconds())
}
func MetadataDatabaseSuccessMetrics(startTime time.Time, methodName string) {
metrics.MetadataReqTime.WithLabelValues(DatabaseSuccess, DatabaseLevel, methodName, strconv.Itoa(0)).Observe(time.Since(startTime).Seconds())
}
func currentFunction() string {
counter, _, _, success := runtime.Caller(1)
if !success {
println("functionName: runtime.Caller: failed")
}
fullName := runtime.FuncForPC(counter).Name()
splitNames := strings.Split(fullName, ".")
return splitNames[len(splitNames)-1]
}