MCPcopy Create free account
hub / github.com/HDT3213/rdb / writeHashEncodingEx

Method writeHashEncodingEx

core/hash.go:381–437  ·  view source on GitHub ↗
(key string, hash map[string][]byte, expire map[string]int64, options ...interface{})

Source from the content-addressed store, hash-verified

379}
380
381func (enc *Encoder) writeHashEncodingEx(key string, hash map[string][]byte, expire map[string]int64, options ...interface{}) error {
382 err := enc.write([]byte{typeHashWithHfe})
383 if err != nil {
384 return err
385 }
386 err = enc.writeString(key)
387 if err != nil {
388 return err
389 }
390 // Hash with HFEs. min TTL at start (7.4+), 7.4RC not included
391 // minExpire is the minimum non-zero expiration time across all fields,
392 // used as a base for relative TTL encoding. Matches Redis hashTypeGetMinExpire().
393 var minExpire int64 = EB_EXPIRE_TIME_INVALID
394 for _, e := range expire {
395 if e > 0 && e < minExpire {
396 minExpire = e
397 }
398 }
399 if minExpire == EB_EXPIRE_TIME_INVALID {
400 // No field has expiration, use 0
401 minExpire = 0
402 }
403 minExpireBytes := make([]byte, 8)
404 binary.LittleEndian.PutUint64(minExpireBytes[:], uint64(minExpire))
405 err = enc.write(minExpireBytes)
406 if err != nil {
407 return err
408 }
409 err = enc.writeLength(uint64(len(hash)))
410 if err != nil {
411 return err
412 }
413 for field, value := range hash {
414 fieldExpire := expire[field]
415 var ttl uint64
416 if fieldExpire == 0 {
417 // 0 indicates no TTL
418 ttl = 0
419 } else {
420 // TTL is relative to minExpire (with +1 to avoid 0 that already taken)
421 ttl = uint64(fieldExpire - minExpire + 1)
422 }
423 err = enc.writeLength(ttl)
424 if err != nil {
425 return err
426 }
427 err = enc.writeString(field)
428 if err != nil {
429 return err
430 }
431 err = enc.writeString(unsafeBytes2Str(value))
432 if err != nil {
433 return err
434 }
435 }
436 return nil
437}
438

Callers 1

WriteHashMapObjectExMethod · 0.95

Calls 4

writeMethod · 0.95
writeStringMethod · 0.95
writeLengthMethod · 0.95
unsafeBytes2StrFunction · 0.70

Tested by

no test coverage detected