Skip to content

Commit

Permalink
Towards a moveable FilePiece
Browse files Browse the repository at this point in the history
  • Loading branch information
kpu committed Jul 19, 2016
1 parent f6959f6 commit 784f0e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
14 changes: 7 additions & 7 deletions util/file_piece.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

namespace util {

const uint64_t kPageSize = SizePage();

ParseNumberException::ParseNumberException(StringPiece value) throw() {
*this << "Could not parse \"" << value << "\" into a ";
}
Expand All @@ -38,7 +40,7 @@ LineIterator &LineIterator::operator++() {
}

FilePiece::FilePiece(const char *name, std::ostream *show_progress, std::size_t min_buffer) :
file_(OpenReadOrThrow(name)), total_size_(SizeFile(file_.get())), page_(SizePage()),
file_(OpenReadOrThrow(name)), total_size_(SizeFile(file_.get())),
progress_(total_size_, total_size_ == kBadSize ? NULL : show_progress, std::string("Reading ") + name) {
Initialize(name, show_progress, min_buffer);
}
Expand All @@ -51,13 +53,13 @@ std::string NamePossiblyFind(int fd, const char *name) {
} // namespace

FilePiece::FilePiece(int fd, const char *name, std::ostream *show_progress, std::size_t min_buffer) :
file_(fd), total_size_(SizeFile(file_.get())), page_(SizePage()),
file_(fd), total_size_(SizeFile(file_.get())),
progress_(total_size_, total_size_ == kBadSize ? NULL : show_progress, std::string("Reading ") + NamePossiblyFind(fd, name)) {
Initialize(NamePossiblyFind(fd, name).c_str(), show_progress, min_buffer);
}

FilePiece::FilePiece(std::istream &stream, const char *name, std::size_t min_buffer) :
total_size_(kBadSize), page_(SizePage()) {
total_size_(kBadSize) {
InitializeNoRead("istream", min_buffer);

fallback_to_read_ = true;
Expand All @@ -68,8 +70,6 @@ FilePiece::FilePiece(std::istream &stream, const char *name, std::size_t min_buf
fell_back_.Reset(stream);
}

FilePiece::~FilePiece() {}

StringPiece FilePiece::ReadLine(char delim, bool strip_cr) {
std::size_t skip = 0;
while (true) {
Expand Down Expand Up @@ -120,7 +120,7 @@ unsigned long int FilePiece::ReadULong() {
void FilePiece::InitializeNoRead(const char *name, std::size_t min_buffer) {
file_name_ = name;

default_map_size_ = page_ * std::max<std::size_t>((min_buffer / page_ + 1), 2);
default_map_size_ = kPageSize * std::max<std::size_t>((min_buffer / kPageSize + 1), 2);
position_ = NULL;
position_end_ = NULL;
mapped_offset_ = 0;
Expand Down Expand Up @@ -264,7 +264,7 @@ void FilePiece::UpdateProgress() {

void FilePiece::MMapShift(uint64_t desired_begin) {
// Use mmap.
uint64_t ignore = desired_begin % page_;
uint64_t ignore = desired_begin % kPageSize;
// Duplicate request for Shift means give more data.
if (position_ == data_.begin() + ignore && position_) {
default_map_size_ *= 2;
Expand Down
3 changes: 1 addition & 2 deletions util/file_piece.hh
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class FilePiece {
*/
explicit FilePiece(std::istream &stream, const char *name = NULL, std::size_t min_buffer = 1048576);

~FilePiece();
~FilePiece() {}

LineIterator begin() {
return LineIterator(*this);
Expand Down Expand Up @@ -197,7 +197,6 @@ class FilePiece {

scoped_fd file_;
const uint64_t total_size_;
const uint64_t page_;

std::size_t default_map_size_;
uint64_t mapped_offset_;
Expand Down

0 comments on commit 784f0e7

Please sign in to comment.