| 576 | } |
| 577 | |
| 578 | std::string PickRandomKey(Random* rnd, const std::vector<std::string>& keys) { |
| 579 | if (keys.empty()) { |
| 580 | return "foo"; |
| 581 | } else { |
| 582 | const int index = rnd->Uniform(keys.size()); |
| 583 | std::string result = keys[index]; |
| 584 | switch (rnd->Uniform(3)) { |
| 585 | case 0: |
| 586 | // Return an existing key |
| 587 | break; |
| 588 | case 1: { |
| 589 | // Attempt to return something smaller than an existing key |
| 590 | if (!result.empty() && result[result.size() - 1] > '\0') { |
| 591 | result[result.size() - 1]--; |
| 592 | } |
| 593 | break; |
| 594 | } |
| 595 | case 2: { |
| 596 | // Return something larger than an existing key |
| 597 | Increment(options_.comparator, &result); |
| 598 | break; |
| 599 | } |
| 600 | } |
| 601 | return result; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | // Returns nullptr if not running against a DB |
| 606 | DB* db() const { return constructor_->db(); } |