-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsize.go
99 lines (85 loc) · 1.65 KB
/
size.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
package ddbrew
import (
"errors"
"strconv"
)
const (
WU_UNIT = 1000 // 1KB
RU_UNIT = 4000 // 4KB
)
type ItemResult struct {
Size int
ReadUnit int
WriteUnit int
}
func itemSizeRoundUp(size int, unitSize int) int {
remainder := size % unitSize
quotient := size / unitSize
if remainder > 0 {
return (quotient + 1) * unitSize
} else {
return quotient * unitSize
}
}
func itemReadSizeRoundUp(size int) int {
return itemSizeRoundUp(size, RU_UNIT)
}
func itemWriteSizeRoundUp(size int) int {
return itemSizeRoundUp(size, WU_UNIT)
}
func getRuSize(size int) int {
return itemReadSizeRoundUp(size) / RU_UNIT
}
func getWuSize(size int) int {
return itemWriteSizeRoundUp(size) / WU_UNIT
}
func GetItemSizeByJSON(json map[string]interface{}) (*ItemResult, error) {
size, err := getItemSizeByJSON(json)
if err != nil {
return nil, err
}
ru := getRuSize(size)
wu := getWuSize(size)
return &ItemResult{
Size: size,
ReadUnit: ru,
WriteUnit: wu,
}, nil
}
func getItemSizeByJSON(json interface{}) (int, error) {
var sum int
switch vt := json.(type) {
case map[string]interface{}:
for k, v := range vt {
glen, err := getItemSizeByJSON(v)
if err != nil {
return 0, nil
}
sum += glen
sum += len(k)
}
case string:
l := len(vt)
return l, nil
case float64:
l := len(strconv.FormatFloat(vt, 'f', -1, 64))
return l, nil
case bool:
l := 1
return l, nil
case nil:
l := 1
return l, nil
case []interface{}:
for _, v := range vt {
glen, err := getItemSizeByJSON(v)
if err != nil {
return 0, err
}
sum += glen
}
default:
return 0, errors.New("unexpected type error")
}
return sum, nil
}