| 484 | } |
| 485 | |
| 486 | func cmdDECR(m uhaha.Machine, args []string) (interface{}, error) { |
| 487 | // 1. Validate the number of arguments |
| 488 | if len(args) != 2 { |
| 489 | return nil, uhaha.ErrWrongNumArgs |
| 490 | } |
| 491 | |
| 492 | // 2. Get the key name |
| 493 | key := []byte(args[1]) |
| 494 | |
| 495 | // 3. Check if the key exists |
| 496 | exists, err := ldb.Exists(key) |
| 497 | if err != nil { |
| 498 | return nil, err |
| 499 | } |
| 500 | |
| 501 | // 4. If the key does not exist, set its value to 0 |
| 502 | if exists == 0 { |
| 503 | if err := ldb.Set(key, []byte("0")); err != nil { |
| 504 | return nil, err |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | // 5. Perform the decrement operation |
| 509 | n, err := ldb.Decr(key) |
| 510 | if err != nil { |
| 511 | // 6. If the key's value is not an integer or cannot be represented as a 64-bit signed integer, return an error |
| 512 | return nil, err |
| 513 | } |
| 514 | |
| 515 | // 7. Return the value after decrementing |
| 516 | return redcon.SimpleInt(n), nil |
| 517 | } |
| 518 | |
| 519 | func cmdBITPOS(m uhaha.Machine, args []string) (interface{}, error) { |
| 520 | // Validate the number of arguments provided by the client. |