From 318b0dee2b6d97b49c1ef1499c968eb4542d3db8 Mon Sep 17 00:00:00 2001 From: Olly Betts Date: Tue, 20 Mar 2018 12:00:06 +1300 Subject: [PATCH] [honey] Remove unless code copy The BufferedFile copy in honey_compact.cc isn't needed, so remove it and merge the few enhancements made to it into the version in honey_table.h. --- xapian-core/backends/honey/honey_compact.cc | 144 ---------------------------- xapian-core/backends/honey/honey_table.h | 24 ++--- 2 files changed, 6 insertions(+), 162 deletions(-) diff --git a/xapian-core/backends/honey/honey_compact.cc b/xapian-core/backends/honey/honey_compact.cc index 8f975c6a6..08b071fa8 100644 --- a/xapian-core/backends/honey/honey_compact.cc +++ b/xapian-core/backends/honey/honey_compact.cc @@ -153,150 +153,6 @@ key_type(const string& key) return static_cast(key[1]); } -class BufferedFile { - int fd = -1; - bool read_only = true; - mutable size_t buf_end = 0; - mutable char buf[4096]; - - public: - BufferedFile() { } - - ~BufferedFile() { - if (fd >= 0) close(fd); - } - - bool open(const std::string& path, bool read_only_) { - if (fd >= 0) close(fd); - read_only = read_only_; - if (read_only) { - // FIXME: add new io_open_stream_rd() etc? - fd = io_open_block_rd(path); - } else { - // FIXME: Always create anew for now... - fd = io_open_block_wr(path, true); - } - return fd >= 0; - } - - off_t get_pos() const { - return read_only ? - lseek(fd, 0, SEEK_CUR) - buf_end : - lseek(fd, 0, SEEK_CUR) + buf_end; - } - - bool empty() const { - if (buf_end) return false; - struct stat sbuf; - if (fd == -1 || fstat(fd, &sbuf) < 0) return true; - return (sbuf.st_size == 0); - } - - void write(unsigned char ch) { - if (buf_end == sizeof(buf)) { - // writev()? - io_write(fd, buf, buf_end); - buf_end = 0; - } - buf[buf_end++] = ch; - } - - void write(const char* p, size_t len) { - if (buf_end + len <= sizeof(buf)) { - memcpy(buf + buf_end, p, len); - buf_end += len; - return; - } - -#ifdef HAVE_WRITEV - while (true) { - struct iovec iov[2]; - iov[0].iov_base = buf; - iov[0].iov_len = buf_end; - iov[1].iov_base = const_cast(p); - iov[1].iov_len = len; - ssize_t n_ = writev(fd, iov, 2); - if (n_ < 0) abort(); - size_t n = n_; - if (n == buf_end + len) { - // Wrote everything. - buf_end = 0; - return; - } - if (n >= buf_end) { - // Wrote all of buf. - n -= buf_end; - p += n; - len -= n; - io_write(fd, p, len); - buf_end = 0; - return; - } - buf_end -= n; - memmove(buf, buf + n, buf_end); - } -#else - io_write(fd, buf, buf_end); - if (len >= sizeof(buf)) { - // If it's bigger than our buffer, just write it directly. - io_write(fd, p, len); - buf_end = 0; - return; - } - memcpy(buf, p, len); - buf_end = len; -#endif - } - - int read() const { - if (buf_end == 0) { -retry: - ssize_t r = ::read(fd, buf, sizeof(buf)); - if (r == 0) return EOF; - if (r < 0) { - if (errno == EINTR) goto retry; - throw Xapian::DatabaseError("read failed", errno); - } - if (size_t(r) < sizeof(buf)) { - memmove(buf + sizeof(buf) - r, buf, r); - } - buf_end = r; - } - return static_cast(buf[sizeof(buf) - buf_end--]); - } - - bool read(char* p, size_t len) const { - if (len <= buf_end) { - memcpy(p, buf + sizeof(buf) - buf_end, len); - buf_end -= len; - return true; - } - memcpy(p, buf + sizeof(buf) - buf_end, buf_end); - p += buf_end; - len -= buf_end; - buf_end = 0; - // FIXME: refill buffer if len < sizeof(buf) - return ::read(fd, p, len) == ssize_t(len); - } - - void flush() { - if (!read_only && buf_end) { - io_write(fd, buf, buf_end); - buf_end = 0; - } - } - - void sync() { - io_sync(fd); - } - - void rewind() { - read_only = true; - lseek(fd, 0, SEEK_SET); - buf_end = 0; - } -}; - template class PostlistCursor; #ifndef DISABLE_GPL_LIBXAPIAN diff --git a/xapian-core/backends/honey/honey_table.h b/xapian-core/backends/honey/honey_table.h index 9d65aed80..b7c2b16a0 100644 --- a/xapian-core/backends/honey/honey_table.h +++ b/xapian-core/backends/honey/honey_table.h @@ -131,9 +131,7 @@ class BufferedFile { } off_t get_pos() const { - return read_only ? - pos - buf_end : - pos + buf_end; + return read_only ? pos - buf_end : pos + buf_end; } void set_pos(off_t pos_) { @@ -171,9 +169,7 @@ class BufferedFile { void write(unsigned char ch) { if (buf_end == sizeof(buf)) { // writev()? - if (::write(fd, buf, buf_end)) { - // FIXME: retry short write - } + io_write(fd, buf, buf_end); pos += buf_end; buf_end = 0; } @@ -208,9 +204,7 @@ class BufferedFile { n -= buf_end; p += n; len -= n; - if (::write(fd, p, len)) { - // FIXME: retry short write - } + io_write(fd, p, len); buf_end = 0; return; } @@ -218,14 +212,10 @@ class BufferedFile { memmove(buf, buf + n, buf_end); } #else - if (::write(fd, buf, buf_end)) { - // FIXME: retry short write - } + io_write(fd, buf, buf_end); if (len >= sizeof(buf)) { // If it's bigger than our buffer, just write it directly. - if (::write(fd, p, len)) { - // FIXME: retry short write - } + io_write(fd, p, len); buf_end = 0; return; } @@ -282,9 +272,7 @@ class BufferedFile { void flush() { if (!read_only && buf_end) { - if (::write(fd, buf, buf_end)) { - // FIXME: retry short write - } + io_write(fd, buf, buf_end); pos += buf_end; buf_end = 0; } -- 2.11.4.GIT