GetSetFromDB retrieves a set from database given a key. If createIfNotExist is true, a new set will be created if the key is not found. It returns the file sets and any write error encountered.
(key []byte, createIfNotExist bool)
| 373 | // GetSetFromDB retrieves a set from database given a key. If createIfNotExist is true, |
| 374 | // a new set will be created if the key is not found. It returns the file sets and any write error encountered. |
| 375 | func (s *SetStructure) getSetFromDB(key []byte, createIfNotExist bool) (*FSets, int64, error) { |
| 376 | if s.db == nil { |
| 377 | return nil, 0, ErrSetNotInitialized |
| 378 | } |
| 379 | var zSetValueWithTTL FSetWithTTL |
| 380 | dbData, err := s.db.Get(key) |
| 381 | |
| 382 | // If key is not found, return nil for both; otherwise return the error. |
| 383 | if err != nil { |
| 384 | if errors.Is(err, _const.ErrKeyNotFound) && createIfNotExist { |
| 385 | return &FSets{}, 0, nil |
| 386 | } |
| 387 | return nil, 0, err |
| 388 | } else { |
| 389 | err = encoding.NewMessagePackDecoder(dbData).Decode(&zSetValueWithTTL) // Decode the value along with TTL |
| 390 | if err != nil { |
| 391 | return nil, 0, err |
| 392 | } |
| 393 | |
| 394 | expiration := zSetValueWithTTL.TTL |
| 395 | if expiration != 0 && expiration < time.Now().UnixNano() { |
| 396 | return nil, -1, _const.ErrKeyIsExpired |
| 397 | } |
| 398 | |
| 399 | return zSetValueWithTTL.ZSet, zSetValueWithTTL.TTL, nil // Return the zSetValue and TTL |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | type FSetWithTTL struct { |
| 404 | ZSet *FSets |
no test coverage detected