MCPcopy Create free account
hub / github.com/IceFireDB/IceFireDB / cmdEXISTS

Function cmdEXISTS

strings.go:388–439  ·  view source on GitHub ↗

cmdEXISTS checks the existence of keys. no concurrent Syntax: EXISTS key [key ...] Time complexity: O(N) where N is the number of keys to check. Returns the number of keys that exist from those specified as arguments. If the same existing key is mentioned multiple times in the arguments, it will be

(m uhaha.Machine, args []string)

Source from the content-addressed store, hash-verified

386// Returns the number of keys that exist from those specified as arguments.
387// If the same existing key is mentioned multiple times in the arguments, it will be counted multiple times.
388// The function uses a concurrency limit to check keys in parallel, which can reduce the overall execution time.
389func cmdEXISTS(m uhaha.Machine, args []string) (interface{}, error) {
390 if len(args) < 2 {
391 return nil, uhaha.ErrWrongNumArgs
392 }
393
394 keys := args[1:]
395 n := len(keys)
396
397 if n == 0 {
398 return redcon.SimpleInt(0), nil
399 }
400
401 results := make(chan bool, n)
402 var wg sync.WaitGroup
403 var mu sync.Mutex
404 var errret error
405
406 for _, key := range keys {
407 wg.Add(1)
408 go func(key string) {
409 defer wg.Done()
410 exists, err := existsKey(key)
411 if err != nil {
412 mu.Lock()
413 if errret == nil {
414 errret = err
415 }
416 mu.Unlock()
417 return
418 }
419 results <- exists
420 }(key)
421 }
422
423 go func() {
424 wg.Wait()
425 close(results)
426 }()
427
428 counter := 0
429 for res := range results {
430 if res {
431 counter++
432 }
433 }
434
435 if errret != nil {
436 return nil, errret
437 }
438
439 return redcon.SimpleInt(counter), nil
440}
441
442func existsKey(key string) (bool, error) {

Callers

nothing calls this directly

Calls 2

existsKeyFunction · 0.85
DoneMethod · 0.80

Tested by

no test coverage detected