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