| 791 | } |
| 792 | |
| 793 | void TFileTransport::seekToChunk(int32_t chunk) { |
| 794 | if (fd_ <= 0) { |
| 795 | throw TTransportException("File not open"); |
| 796 | } |
| 797 | |
| 798 | int32_t numChunks = getNumChunks(); |
| 799 | |
| 800 | // file is empty, seeking to chunk is pointless |
| 801 | if (numChunks == 0) { |
| 802 | return; |
| 803 | } |
| 804 | |
| 805 | // negative indicates reverse seek (from the end) |
| 806 | if (chunk < 0) { |
| 807 | chunk += numChunks; |
| 808 | } |
| 809 | |
| 810 | // too large a value for reverse seek, just seek to beginning |
| 811 | if (chunk < 0) { |
| 812 | T_DEBUG("%s", "Incorrect value for reverse seek. Seeking to beginning..."); |
| 813 | chunk = 0; |
| 814 | } |
| 815 | |
| 816 | // cannot seek past EOF |
| 817 | bool seekToEnd = false; |
| 818 | off_t minEndOffset = 0; |
| 819 | if (chunk >= numChunks) { |
| 820 | T_DEBUG("%s", "Trying to seek past EOF. Seeking to EOF instead..."); |
| 821 | seekToEnd = true; |
| 822 | chunk = numChunks - 1; |
| 823 | // this is the min offset to process events till |
| 824 | minEndOffset = ::THRIFT_LSEEK(fd_, 0, SEEK_END); |
| 825 | } |
| 826 | |
| 827 | off_t newOffset = off_t(chunk) * chunkSize_; |
| 828 | offset_ = ::THRIFT_LSEEK(fd_, newOffset, SEEK_SET); |
| 829 | readState_.resetAllValues(); |
| 830 | currentEvent_ = nullptr; |
| 831 | if (offset_ == -1) { |
| 832 | TOutput::instance()("TFileTransport: lseek error in seekToChunk"); |
| 833 | throw TTransportException("TFileTransport: lseek error in seekToChunk"); |
| 834 | } |
| 835 | |
| 836 | // seek to EOF if user wanted to go to last chunk |
| 837 | if (seekToEnd) { |
| 838 | uint32_t oldReadTimeout = getReadTimeout(); |
| 839 | setReadTimeout(NO_TAIL_READ_TIMEOUT); |
| 840 | // keep on reading unti the last event at point of seekChunk call |
| 841 | shared_ptr<eventInfo> event; |
| 842 | while ((offset_ + readState_.bufferPtr_) < minEndOffset) { |
| 843 | event.reset(readEvent()); |
| 844 | if (event.get() == nullptr) { |
| 845 | break; |
| 846 | } |
| 847 | } |
| 848 | setReadTimeout(oldReadTimeout); |
| 849 | } |
| 850 | } |
nothing calls this directly
no test coverage detected