| 514 | }; |
| 515 | |
| 516 | int readRangeInDb(PhysicalShard* shard, const KeyRangeRef& range, int rowLimit, int byteLimit, RangeResult* result) { |
| 517 | if (rowLimit == 0 || byteLimit == 0) { |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | int accumulatedRows = 0; |
| 522 | int accumulatedBytes = 0; |
| 523 | // TODO: Pass read timeout. |
| 524 | const int readRangeTimeout = SERVER_KNOBS->ROCKSDB_READ_RANGE_TIMEOUT; |
| 525 | rocksdb::Status s; |
| 526 | auto options = getReadOptions(); |
| 527 | // TODO: define single shard read timeout. |
| 528 | const uint64_t deadlineMircos = shard->db->GetEnv()->NowMicros() + readRangeTimeout * 1000000; |
| 529 | options.deadline = std::chrono::microseconds(deadlineMircos / 1000000); |
| 530 | |
| 531 | // When using a prefix extractor, ensure that keys are returned in order even if they cross |
| 532 | // a prefix boundary. |
| 533 | options.auto_prefix_mode = (SERVER_KNOBS->ROCKSDB_PREFIX_LEN > 0); |
| 534 | if (rowLimit >= 0) { |
| 535 | ReadIterator readIter = shard->readIterPool->getIterator(); |
| 536 | auto cursor = readIter.iter; |
| 537 | cursor->Seek(toSlice(range.begin)); |
| 538 | while (cursor->Valid() && toStringRef(cursor->key()) < range.end) { |
| 539 | KeyValueRef kv(toStringRef(cursor->key()), toStringRef(cursor->value())); |
| 540 | ++accumulatedRows; |
| 541 | accumulatedBytes += sizeof(KeyValueRef) + kv.expectedSize(); |
| 542 | result->push_back_deep(result->arena(), kv); |
| 543 | // Calling `cursor->Next()` is potentially expensive, so short-circut here just in case. |
| 544 | if (result->size() >= rowLimit || accumulatedBytes >= byteLimit) { |
| 545 | break; |
| 546 | } |
| 547 | cursor->Next(); |
| 548 | } |
| 549 | s = cursor->status(); |
| 550 | shard->readIterPool->returnIterator(readIter); |
| 551 | } else { |
| 552 | ReadIterator readIter = shard->readIterPool->getIterator(); |
| 553 | auto cursor = readIter.iter; |
| 554 | cursor->SeekForPrev(toSlice(range.end)); |
| 555 | if (cursor->Valid() && toStringRef(cursor->key()) == range.end) { |
| 556 | cursor->Prev(); |
| 557 | } |
| 558 | while (cursor->Valid() && toStringRef(cursor->key()) >= range.begin) { |
| 559 | KeyValueRef kv(toStringRef(cursor->key()), toStringRef(cursor->value())); |
| 560 | ++accumulatedRows; |
| 561 | accumulatedBytes += sizeof(KeyValueRef) + kv.expectedSize(); |
| 562 | result->push_back_deep(result->arena(), kv); |
| 563 | // Calling `cursor->Prev()` is potentially expensive, so short-circut here just in case. |
| 564 | if (result->size() >= -rowLimit || accumulatedBytes >= byteLimit) { |
| 565 | break; |
| 566 | } |
| 567 | cursor->Prev(); |
| 568 | } |
| 569 | s = cursor->status(); |
| 570 | shard->readIterPool->returnIterator(readIter); |
| 571 | } |
| 572 | |
| 573 | if (!s.ok()) { |
no test coverage detected