-
Notifications
You must be signed in to change notification settings - Fork 1
/
key.go
182 lines (148 loc) · 3.97 KB
/
key.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
package struct2csv
import (
"crypto/md5"
"reflect"
"sort"
"strconv"
"unsafe"
)
type KeyType interface {
String() string // get encode string
Int() uint64 // get the key map to uint64
}
// compile time checks
var _ KeyType = KeyAutoIncrementID(0)
var _ KeyType = KeyString("")
type KVs struct {
preSize int
preMappingSize int
kvs []*KeyValue
mapping map[string]KeyType
encodeHeaders []string // mapping's value call there's String()
unEncodeHeaders []string // mapping's key
}
func NewKVs(size, preMappingSize int) *KVs {
kvs := &KVs{
preSize: size,
kvs: make([]*KeyValue, 0, size),
mapping: make(map[string]KeyType, preMappingSize),
encodeHeaders: make([]string, 0, preMappingSize),
unEncodeHeaders: make([]string, 0, preMappingSize),
}
for i := 0; i < size; i++ {
kvs.kvs = append(kvs.kvs, newKeyValue(preMappingSize))
}
return kvs
}
func (kvs *KVs) Reset() {
enH := (*reflect.SliceHeader)(unsafe.Pointer(&kvs.encodeHeaders))
enH.Len = 0
unH := (*reflect.SliceHeader)(unsafe.Pointer(&kvs.unEncodeHeaders))
unH.Len = 0
kvs.mapping = make(map[string]KeyType, kvs.preSize)
}
func (kvs *KVs) getKVElem(index int) *KeyValue {
return kvs.kvs[index]
}
func (kvs *KVs) appendElem(kv *KeyValue) {
kvs.kvs = append(kvs.kvs, kv)
}
func (kvs *KVs) GetMapping() map[string]KeyType {
return kvs.mapping
}
func (kvs *KVs) GetSortMappingValues() []KeyType {
vs := make([]KeyType, 0, len(kvs.mapping))
for _, v := range kvs.mapping {
vs = append(vs, v)
}
sort.SliceStable(vs, func(i, j int) bool {
return vs[i].String() < vs[j].String()
})
return vs
}
func (kvs *KVs) GetUnEncodedSortHeader() []string {
if len(kvs.unEncodeHeaders) > 0 {
return kvs.unEncodeHeaders
}
for unen := range kvs.mapping {
kvs.unEncodeHeaders = append(kvs.unEncodeHeaders, unen)
}
sort.Strings(kvs.unEncodeHeaders)
return kvs.unEncodeHeaders
}
func (kvs *KVs) GetEncodedSortHeader() []string {
if len(kvs.encodeHeaders) > 0 {
return kvs.encodeHeaders
}
for _, en := range kvs.mapping {
kvs.encodeHeaders = append(kvs.encodeHeaders, en.String())
}
sort.Strings(kvs.encodeHeaders)
return kvs.encodeHeaders
}
func (kvs *KVs) SetEncodedSortHeader(header []string) {
kvs.encodeHeaders = header
}
type WrapperValue struct {
isValid bool // have use data
value interface{}
}
func (w *WrapperValue) clear() {
w.isValid = false
}
// KeyValue records the struct's key(containing the path) and its value
// consider the performance problems caused by map keys of the interface type
// uint64 is used as the map key, and wrapped by KeyValue.
// call Set and Get of map's read and write. KeyType can be extended to different types,
// then both performance and code extensibility are considered
type KeyValue struct {
kv map[uint64]*WrapperValue // all KeyType must be map to a unique uint64, use KeyType's Int() function
}
func newKeyValue(preSize int) *KeyValue {
return &KeyValue{kv: make(map[uint64]*WrapperValue, preSize)}
}
func (t *KeyValue) Set(k KeyType, v interface{}) {
t.kv[k.Int()] = &WrapperValue{
isValid: true,
value: v,
}
}
func (t *KeyValue) Get(k KeyType) (interface{}, bool) {
v, ok := t.kv[k.Int()]
if !ok || !v.isValid {
return nil, false
}
v.clear()
return v.value, true
}
func (t *KeyValue) Len() int {
return len(t.kv)
}
type KeyAutoIncrementID uint64
func newKeyAuto(max uint64) KeyAutoIncrementID {
max++
return KeyAutoIncrementID(max)
}
func (k KeyAutoIncrementID) String() string {
return strconv.Itoa(int(k))
}
func (k KeyAutoIncrementID) Int() uint64 {
return uint64(k)
}
type KeyString string
func newKeyString(s string) KeyString {
return KeyString(s)
}
func (k KeyString) String() string {
return string(k)
}
func (k KeyString) Int() uint64 {
sh := (*reflect.StringHeader)(unsafe.Pointer(&k))
sliceH := &reflect.SliceHeader{
Data: sh.Data,
Len: sh.Len,
Cap: sh.Len,
}
res := md5.Sum(*(*[]byte)(unsafe.Pointer(sliceH)))
return *(*uint64)(unsafe.Pointer(&res)) // only use 8 byte
}