MCPcopy Create free account
hub / github.com/ByteStorage/FlyDB / MSetNX

Method MSetNX

structure/string.go:482–526  ·  view source on GitHub ↗

MSetNX sets multiple key-value pairs only if none of the specified keys exist

(pairs ...interface{})

Source from the content-addressed store, hash-verified

480
481// MSetNX sets multiple key-value pairs only if none of the specified keys exist
482func (s *StringStructure) MSetNX(pairs ...interface{}) (bool, error) {
483 if len(pairs)%2 != 0 {
484 return false, errors.New("Wrong number of arguments")
485 }
486
487 // Check if any of the specified keys already exist
488 for i := 0; i < len(pairs); i += 2 {
489 key, ok := pairs[i].(string)
490 if !ok {
491 return false, errors.New("Invalid key")
492 }
493
494 exists, err := s.Exists(key)
495 if err != nil {
496 return false, err
497 }
498
499 if exists {
500 return false, nil
501 }
502 }
503
504 // Create a map to store the key-value pairs
505 data := make(map[string]interface{})
506
507 // Extract key-value pairs from the input arguments and store them in the map
508 for i := 0; i < len(pairs); i += 2 {
509 key, ok := pairs[i].(string)
510 if !ok {
511 return false, errors.New("Invalid key")
512 }
513
514 value := pairs[i+1]
515 data[key] = value
516 }
517
518 // Set each key-value pair in the map
519 for key, value := range data {
520 if err := s.Set(key, value, 0); err != nil {
521 return false, err
522 }
523 }
524
525 return true, nil
526}
527
528// encodeStringValue encodes the value
529// format: [type][expire][value]

Callers

nothing calls this directly

Calls 2

ExistsMethod · 0.95
SetMethod · 0.95

Tested by

no test coverage detected