-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonUtil.go
303 lines (294 loc) · 8.16 KB
/
jsonUtil.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package golangUtil
import (
"fmt"
"reflect"
"strconv"
"strings"
"sync"
"sync/atomic"
"unsafe"
)
const MaxStorageBytes int64 = 1024
const MaxTolerateCacheFailedRate = 0.3
const RetryStoreCacheTimes = 10
const ArrangeCacheTimes = 1000
type nocopy uintptr
type jsonEncoder struct {
cache map[uintptr]*[]byte
obsolete heap
cacheAccessTimes map[uintptr]int64
capacity int64
nocopy nocopy
rw sync.RWMutex
cacheInValidTimes int
//the field to order the cache invalid info
cacheSuccessNums int64
cacheNums int64
}
func init() {
JsonEncoder = &jsonEncoder{
cache: make(map[uintptr]*[]byte),
capacity: 0,
obsolete: heap{
heapArr: make([]int, 100),
value: make([]uintptr, 100),
length: 0,
},
rw: sync.RWMutex{},
cacheAccessTimes: make(map[uintptr]int64),
}
JsonEncoder.nocopy = nocopy(unsafe.Pointer(JsonEncoder))
}
var (
JsonEncoder *jsonEncoder
)
func (e *jsonEncoder) store(address uintptr, data *[]byte) {
e.check()
e.rw.Lock()
defer e.rw.Unlock()
if _, ok := e.cache[address]; ok {
return
}
var cacheSuccessNums = e.cacheSuccessNums
var cacheNums = e.cacheNums
//cache drain strategy
if e.capacity+int64(len(*data)) > MaxStorageBytes {
if int64(e.obsolete.peekMax())+MaxStorageBytes-e.capacity < int64(len(*data)) && e.cacheInValidTimes < RetryStoreCacheTimes {
e.cacheInValidTimes++
return
} else if e.cacheInValidTimes >= RetryStoreCacheTimes {
value := e.obsolete.peekMax()
if int64(len(*e.cache[value])) > MaxStorageBytes/4 {
delete(e.cache, value)
e.obsolete.delete(e.obsolete.length)
delete(e.cacheAccessTimes, value)
} else if float64(cacheSuccessNums/cacheNums) < MaxTolerateCacheFailedRate {
var sum = int64(e.obsolete.length)
for i := 0; i > e.obsolete.length; i++ {
var temp = e.obsolete.value[e.obsolete.length]
//can't give up the hot data
if e.cacheAccessTimes[temp] > cacheSuccessNums/sum*2 {
continue
}
delete(e.cache, temp)
delete(e.cacheAccessTimes, temp)
e.obsolete.delete(i)
}
e.cacheNums = 0
e.cacheSuccessNums = 0
}
e.cacheInValidTimes = 0
} else if int64(e.obsolete.peekMax())+MaxStorageBytes-e.capacity >= int64(len(*data)) {
if e.cacheAccessTimes[e.obsolete.peekMax()] <= cacheSuccessNums/int64(len(e.cache))*2 {
var temp = e.obsolete.peekMax()
delete(e.cache, temp)
delete(e.cacheAccessTimes, temp)
e.obsolete.delete(1)
}
}
}
if e.cacheNums > ArrangeCacheTimes {
if float64(cacheSuccessNums/cacheNums) < MaxTolerateCacheFailedRate {
var sum = int64(e.obsolete.length)
for i := 0; i > e.obsolete.length; i++ {
var temp = e.obsolete.value[e.obsolete.length]
//can't give up the hot data
if e.cacheAccessTimes[temp] > cacheSuccessNums/sum {
continue
}
delete(e.cache, temp)
delete(e.cacheAccessTimes, temp)
e.obsolete.delete(i)
}
e.cacheNums = 0
e.cacheSuccessNums = 0
e.cacheInValidTimes = 0
}
}
atomic.AddInt64(&e.capacity, int64(len(*data)))
e.obsolete.insert(len(*data), address)
e.cacheAccessTimes[address] = 0
e.cache[address] = data
}
func (e *jsonEncoder) load(address uintptr) (*[]byte, bool) {
e.check()
e.rw.RLock()
defer e.rw.RUnlock()
atomic.AddInt64(&e.cacheNums, 1)
value, ok := e.cache[address]
if ok {
times := e.cacheAccessTimes[address]
atomic.AddInt64(×, 1)
atomic.AddInt64(&e.cacheSuccessNums, 1)
}
return value, ok
}
func (e *jsonEncoder) check() {
if uintptr(e.nocopy) != uintptr(unsafe.Pointer(e)) &&
!atomic.CompareAndSwapUintptr((*uintptr)(&e.nocopy), 0, uintptr(unsafe.Pointer(e))) &&
uintptr(e.nocopy) != uintptr(unsafe.Pointer(e)) {
panic(any("object has copyed"))
}
}
func Marshal(value any) ([]byte, error) {
var address uintptr
var load *[]byte
var ok bool
if value, ok := value.(unsafe.Pointer); ok {
address = uintptr(value)
} else {
address = uintptr(unsafe.Pointer(&value))
}
load, ok = JsonEncoder.load(address)
if ok {
return *load, nil
}
sb := strings.Builder{}
bytes, err := marshal(reflect.ValueOf(value))
if err != nil {
return nil, err
}
sb.WriteString("{")
sb.WriteString(string(bytes))
sb.WriteString("}")
bytes = []byte(sb.String())
JsonEncoder.store(address, &bytes)
return bytes, nil
}
func isInvalid(value reflect.Value) bool {
if value.Kind() == reflect.Pointer {
if value.IsNil() {
return true
}
} else {
if value.IsZero() {
return true
}
}
return false
}
func marshal(value reflect.Value) (result []byte, err error) {
defer func() {
if panicErr := recover(); panicErr != any(nil) {
result = nil
fmt.Println(panicErr)
err = fmt.Errorf("params analy error")
}
}()
if isInvalid(value) {
return nil, nil
}
result = make([]byte, 0, value.Type().Size()*2)
switch value.Kind() {
case reflect.String:
result = append(result, []byte(value.String())...)
case reflect.Int, reflect.Int64, reflect.Int8, reflect.Int16, reflect.Int32:
result = append(result, []byte(strconv.FormatInt(value.Int(), 10))...)
case reflect.Slice:
var tempArr = make([]byte, 0, value.Len()*value.Type().Elem().Align())
for i := 0; i < value.Len(); i++ {
temp, err := marshal(value.Index(i))
if err != nil {
return nil, err
}
if i != value.Len()-1 {
temp = append(temp, ',')
}
tempArr = append(tempArr, temp...)
}
result = append(result, combineToJson(value.Type().Name(), string(tempArr), value.Kind())...)
case reflect.Struct:
var number = value.NumField()
var temp = make([]byte, 0, value.Type().Size())
for i := 0; i < number; i++ {
var fieldValue = value.Field(i)
if isInvalid(fieldValue) {
continue
}
fieldName := value.Type().Field(i).Name
bytes, err := marshal(fieldValue)
if err != nil {
return nil, err
}
fieldType := fieldValue.Kind()
if fieldType == reflect.Struct {
temp = append(temp, bytes...)
if i != number-1 {
temp = append(temp, ',')
}
continue
}
if fieldType == reflect.Map {
fieldType = reflect.Int
}
temp = append(temp, combineToJson(fieldName, string(bytes), fieldType)...)
if i != number-1 {
temp = append(temp, ',')
}
}
result = append(result, combineToJson(value.Type().Name(), string(temp), value.Kind())...)
case reflect.Bool:
result = append(result, strconv.FormatBool(value.Bool())...)
case reflect.Pointer, reflect.Interface, reflect.Uintptr:
elem := value.Elem()
temp, err := marshal(elem)
if err != nil {
return nil, err
}
result = append(result, temp...)
case reflect.Float32, reflect.Float64:
result = append(result, strconv.FormatFloat(value.Float(), 'E', -1, 32)...)
case reflect.Complex64, reflect.Complex128:
result = append(result, strconv.FormatComplex(value.Complex(), 'E', -1, 32)...)
//maybe can improve
case reflect.Map:
keys := value.MapKeys()
if len(keys) <= 0 {
return
}
var tempArr = make([]byte, 0, 2*len(keys)*(keys[0].Type().Align())*value.MapIndex(keys[0]).Type().Align())
for i := 0; i < len(keys); i++ {
var keyValue = value.MapIndex(keys[i])
keyBytes, err := marshal(keys[i])
if err != nil {
return nil, err
}
valueBytes, err := marshal(keyValue)
if err != nil {
return nil, err
}
tempArr = append(tempArr, combineToJson(string(keyBytes), string(valueBytes), reflect.String)...)
}
result = append(result, combineToJson("", string(tempArr), reflect.Map)...)
}
return result, nil
}
func combineToJson(key string, value string, valueType reflect.Kind) []byte {
sb := strings.Builder{}
sb.WriteString("\"")
sb.WriteString(key)
sb.WriteString("\": ")
switch valueType {
case reflect.String:
sb.WriteString("\"")
sb.WriteString(value)
sb.WriteString("\"")
case reflect.Int, reflect.Int64, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, reflect.Bool:
sb.WriteString(value)
case reflect.Slice, reflect.Array:
sb.WriteString("[")
sb.WriteString(value)
sb.WriteString("]")
case reflect.Struct, reflect.Interface:
sb.WriteString("{")
sb.WriteString(value)
sb.WriteString("}")
case reflect.Map:
sb = strings.Builder{}
sb.WriteString("{")
sb.WriteString(value)
sb.WriteString("}")
}
sb.WriteString("\n")
return []byte(sb.String())
}