getListFromDB retrieves data from the database. When isKeyCanNotExist is true, it returns an empty slice if the key doesn't exist instead of an error.
(key string, isKeyCanNotExist bool)
| 572 | |
| 573 | // getListFromDB retrieves data from the database. When isKeyCanNotExist is true, it returns an empty slice if the key doesn't exist instead of an error. |
| 574 | func (l *ListStructure) getListFromDB(key string, isKeyCanNotExist bool) (*list, int64, error) { |
| 575 | if isKeyCanNotExist { |
| 576 | // Get data corresponding to the key from the database |
| 577 | dbData, err := l.db.Get([]byte(key)) |
| 578 | |
| 579 | // Since the key might not exist, we need to handle ErrKeyNotFound separately as it is a valid case |
| 580 | if err != nil && err != _const.ErrKeyNotFound { |
| 581 | return nil, 0, err |
| 582 | } |
| 583 | |
| 584 | // Deserialize the data into a DecodedList |
| 585 | decodedList, err := l.decodeList(dbData) |
| 586 | if err != nil { |
| 587 | if len(dbData) != 0 { |
| 588 | return nil, 0, err |
| 589 | } else { |
| 590 | decodedList = &DecodedList{List: &list{nil, 0}, Expiration: 0} |
| 591 | } |
| 592 | } |
| 593 | return decodedList.List, decodedList.Expiration, nil |
| 594 | } else { |
| 595 | // Get data corresponding to the key from the database |
| 596 | dbData, err := l.db.Get([]byte(key)) |
| 597 | if err != nil { |
| 598 | return nil, 0, err |
| 599 | } |
| 600 | |
| 601 | // Deserialize the data into a DecodedList |
| 602 | decodedList, err := l.decodeList(dbData) |
| 603 | if err != nil { |
| 604 | return nil, 0, err |
| 605 | } |
| 606 | |
| 607 | return decodedList.List, decodedList.Expiration, nil |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | // setListToDB stores the data into the database. |
| 612 | func (l *ListStructure) setListToDB(key string, lst *list, ttl time.Duration) error { |