| 447 | return n > 0, nil |
| 448 | } |
| 449 | |
| 450 | func cmdDECRBY(m uhaha.Machine, args []string) (interface{}, error) { |
| 451 | // 1. Validate the number of arguments |
| 452 | if len(args) != 3 { |
| 453 | return nil, uhaha.ErrWrongNumArgs |
| 454 | } |
| 455 | |
| 456 | // 2. Parse the decrement value |
| 457 | decrement, err := ledis.StrInt64([]byte(args[2]), nil) |
| 458 | if err != nil { |
| 459 | return nil, fmt.Errorf("ERR value is not an integer or out of range") |
| 460 | } |
| 461 | |
| 462 | // 3. Check if the key exists |
| 463 | key := []byte(args[1]) |
| 464 | exists, err := ldb.Exists(key) |
| 465 | if err != nil { |
| 466 | return nil, err |
| 467 | } |
| 468 | |
| 469 | // 4. If the key does not exist, initialize it to 0 |
| 470 | if exists == 0 { |
| 471 | if err := ldb.Set(key, []byte("0")); err != nil { |
| 472 | return nil, err |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | // 5. Perform the DECRBY operation |
| 477 | n, err := ldb.DecrBy(key, decrement) |
| 478 | if err != nil { |
| 479 | // Handle errors, such as the key containing a non-integer value |
| 480 | return nil, fmt.Errorf("ERR value is not an integer or out of range") |
| 481 | } |
| 482 | |
| 483 | // 6. Return the new value after decrementing |
| 484 | return redcon.SimpleInt(n), nil |
| 485 | } |
| 486 | |
| 487 | func cmdDECR(m uhaha.Machine, args []string) (interface{}, error) { |