MCPcopy Index your code
hub / github.com/HDT3213/rdb / tryWriteIntSetEncoding

Method tryWriteIntSetEncoding

core/set.go:130–188  ·  view source on GitHub ↗
(key string, values [][]byte)

Source from the content-addressed store, hash-verified

128}
129
130func (enc *Encoder) tryWriteIntSetEncoding(key string, values [][]byte) (bool, error) {
131 max := int64(math.MinInt64)
132 min := int64(math.MaxInt64)
133 intList := make([]int64, len(values))
134 for i, v := range values {
135 str := unsafeBytes2Str(v)
136 intV, ok := isEncodableUint64(str)
137 if !ok {
138 return false, nil
139 }
140 if intV < min {
141 min = intV
142 }
143 if intV > max {
144 max = intV
145 }
146 intList[i] = intV
147 }
148 intSize := uint32(8)
149 if min >= math.MinInt16 && max <= math.MaxInt16 {
150 intSize = 2
151 } else if min >= math.MinInt32 && max <= math.MaxInt32 {
152 intSize = 4
153 }
154 sort.Slice(intList, func(i, j int) bool {
155 return intList[i] < intList[j]
156 })
157
158 err := enc.write([]byte{typeSetIntSet})
159 if err != nil {
160 return true, err
161 }
162 err = enc.writeString(key)
163 if err != nil {
164 return true, err
165 }
166 buf := make([]byte, 8, 8+int(intSize)*len(values))
167 binary.LittleEndian.PutUint32(buf[0:4], intSize)
168 binary.LittleEndian.PutUint32(buf[4:8], uint32(len(values)))
169
170 for _, value := range intList {
171 switch intSize {
172 case 2:
173 binary.LittleEndian.PutUint16(enc.buffer[0:2], uint16(value))
174 buf = append(buf, enc.buffer[0:2]...)
175 case 4:
176 binary.LittleEndian.PutUint32(enc.buffer[0:4], uint32(value))
177 buf = append(buf, enc.buffer[0:4]...)
178 case 8:
179 binary.LittleEndian.PutUint64(enc.buffer, uint64(value))
180 buf = append(buf, enc.buffer...)
181 }
182 }
183 err = enc.writeNanString(unsafeBytes2Str(buf))
184 if err != nil {
185 return true, err
186 }
187 return true, nil

Callers 1

WriteSetObjectMethod · 0.95

Calls 5

writeMethod · 0.95
writeStringMethod · 0.95
writeNanStringMethod · 0.95
isEncodableUint64Function · 0.85
unsafeBytes2StrFunction · 0.70

Tested by

no test coverage detected