decodeStringValue decodes the value format: [type][expire][value] type: 1 byte expire: 8 bytes value: n bytes
(value []byte)
| 584 | // expire: 8 bytes |
| 585 | // value: n bytes |
| 586 | func decodeStringValue(value []byte) ([]byte, int64, error) { |
| 587 | // Check the length of the value |
| 588 | if len(value) < 1 { |
| 589 | return nil, -1, ErrInvalidValue |
| 590 | } |
| 591 | |
| 592 | // Check the type of the value |
| 593 | if value[0] != String { |
| 594 | return nil, -1, ErrInvalidType |
| 595 | } |
| 596 | |
| 597 | // Use the variable bufIndex to keep track of the current index position in value, |
| 598 | // starting from 1 to indicate the number of bytes read so far. |
| 599 | var bufIndex = 1 |
| 600 | |
| 601 | // Decode the expiration time expire from the sub-slice of byte slice value |
| 602 | // starting from the current index position bufIndex. |
| 603 | expire, n := binary.Varint(value[bufIndex:]) |
| 604 | |
| 605 | // Check the number of bytes read |
| 606 | if n <= 0 { |
| 607 | return nil, -1, ErrInvalidValue |
| 608 | } |
| 609 | |
| 610 | // Update the current index position bufIndex by adding the number of bytes read n. |
| 611 | bufIndex += n |
| 612 | |
| 613 | // Check the expiration time expire |
| 614 | if expire != 0 && expire < time.Now().UnixNano() { |
| 615 | return nil, -1, _const.ErrKeyIsExpired |
| 616 | } |
| 617 | |
| 618 | // Return the original value value |
| 619 | return value[bufIndex:], expire, nil |
| 620 | } |
| 621 | |
| 622 | func (s *StringStructure) Stop() error { |
| 623 | err := s.db.Close() |