susumu.yata
null+****@clear*****
Tue Jul 2 14:43:39 JST 2013
susumu.yata 2013-07-02 14:43:39 +0900 (Tue, 02 Jul 2013) New Revision: cb5c31fde3a8b675313f5be73d2b42a891cc74a7 https://github.com/groonga/grnxx/commit/cb5c31fde3a8b675313f5be73d2b42a891cc74a7 Message: Use grnxx::Exception. Modified files: lib/grnxx/storage/chunk-posix.cpp lib/grnxx/storage/chunk-posix.hpp lib/grnxx/storage/chunk-windows.cpp lib/grnxx/storage/chunk-windows.hpp lib/grnxx/storage/chunk.hpp test/test_storage.cpp Modified: lib/grnxx/storage/chunk-posix.cpp (+29 -28) =================================================================== --- lib/grnxx/storage/chunk-posix.cpp 2013-07-02 13:38:59 +0900 (492688a) +++ lib/grnxx/storage/chunk-posix.cpp 2013-07-02 14:43:39 +0900 (b54c83b) @@ -27,6 +27,7 @@ #include <new> #include "grnxx/errno.hpp" +#include "grnxx/exception.hpp" #include "grnxx/logger.hpp" #include "grnxx/storage/file.hpp" @@ -47,7 +48,9 @@ ChunkImpl::ChunkImpl() ChunkImpl::~ChunkImpl() { if (address_ != MAP_FAILED) { if (::munmap(address_, static_cast<size_t>(size_)) != 0) { - GRNXX_ERROR() << "failed to unmap chunk: '::munmap' " << Errno(errno); + Errno errno_copy(errno); + GRNXX_WARNING() << "failed to unmap chunk: " + << "call = ::munmap, errno = " << errno_copy; } } } @@ -57,45 +60,42 @@ ChunkImpl *ChunkImpl::create(File *file, uint64_t offset, uint64_t size, std::unique_ptr<ChunkImpl> chunk(new (std::nothrow) ChunkImpl); if (!chunk) { GRNXX_ERROR() << "new grnxx::storage::FileImpl failed"; - return nullptr; + throw MemoryError(); } if (file) { - if (!chunk->create_file_backed_chunk(file, offset, size, flags)) { - return nullptr; - } + chunk->create_file_backed_chunk(file, offset, size, flags); } else { - if (!chunk->create_anonymous_chunk(size, flags)) { - return nullptr; - } + chunk->create_anonymous_chunk(size, flags); } return chunk.release(); } -bool ChunkImpl::sync(uint64_t offset, uint64_t size) { +void ChunkImpl::sync(uint64_t offset, uint64_t size) { if ((flags_ & CHUNK_ANONYMOUS) || (flags_ & CHUNK_READ_ONLY)) { - GRNXX_WARNING() << "invalid operation: flags = " << flags_; - return false; + GRNXX_ERROR() << "invalid operation: flags = " << flags_; + throw LogicError(); } if ((offset > size_) || (size > size_) || (size > (size_ - offset))) { GRNXX_ERROR() << "invalid argument: offset = " << offset << ", size = " << size << ", chunk_size = " << size_; - return false; + throw LogicError(); } if (size == 0) { size = size_ - offset; } if (size > std::numeric_limits<size_t>::max()) { GRNXX_ERROR() << "invalid argument: size = " << size; - return false; + throw LogicError(); } if (size > 0) { if (::msync(static_cast<char *>(address_) + offset, size, MS_SYNC) != 0) { + Errno errno_copy(errno); GRNXX_ERROR() << "failed to sync chunk: offset = " << offset - << ", size = " << size << ": '::msync' " << Errno(errno); - return false; + << ", size = " << size + << ", call = ::msync, errno = " << errno_copy; + throw SystemError(errno_copy); } } - return true; } ChunkFlags ChunkImpl::flags() const { @@ -110,15 +110,14 @@ uint64_t ChunkImpl::size() const { return size_; } -bool ChunkImpl::create_file_backed_chunk(File *file, uint64_t offset, +void ChunkImpl::create_file_backed_chunk(File *file, uint64_t offset, uint64_t size, ChunkFlags flags) { - // TODO: This may throw. const uint64_t file_size = file->get_size(); if ((offset >= file_size) || (size > file_size) || (size > (file_size - offset))) { GRNXX_ERROR() << "invalid argument: offset = " << offset << ", size = " << size << ", file_size = " << file_size; - return false; + throw LogicError(); } if (size == 0) { size = file_size - offset; @@ -127,7 +126,7 @@ bool ChunkImpl::create_file_backed_chunk(File *file, uint64_t offset, (size > std::numeric_limits<size_t>::max())) { GRNXX_ERROR() << "invalid argument: offset = " << offset << ", size = " << size; - return false; + throw LogicError(); } if (file->flags() & FILE_READ_ONLY) { flags_ |= CHUNK_READ_ONLY; @@ -142,20 +141,21 @@ bool ChunkImpl::create_file_backed_chunk(File *file, uint64_t offset, address_ = ::mmap(nullptr, size, protection_flags, mmap_flags, *static_cast<const int *>(file->handle()), offset); if (address_ == MAP_FAILED) { + Errno errno_copy(errno); GRNXX_ERROR() << "failed to map file-backed chunk: " << "file_path = " << file->path() << ", file_size = " << file_size << ", offset = " << offset << ", size = " << size - << ", flags = " << flags << ": '::mmap' " << Errno(errno); - return false; + << ", flags = " << flags + << ", call = ::mmap, errno = " << errno_copy; + throw SystemError(errno_copy); } - return true; } -bool ChunkImpl::create_anonymous_chunk(uint64_t size, ChunkFlags flags) { +void ChunkImpl::create_anonymous_chunk(uint64_t size, ChunkFlags flags) { if ((size == 0) || (size > std::numeric_limits<size_t>::max())) { GRNXX_ERROR() << "invalid argument: size = " << size; - return false; + throw LogicError(); } flags_ = CHUNK_ANONYMOUS; size_ = size; @@ -173,12 +173,13 @@ bool ChunkImpl::create_anonymous_chunk(uint64_t size, ChunkFlags flags) { if (address_ == MAP_FAILED) { address_ = ::mmap(nullptr, size, protection_flags, mmap_flags, -1, 0); if (address_ == MAP_FAILED) { + Errno errno_copy(errno); GRNXX_ERROR() << "failed to map anonymous chunk: size = " << size - << ", flags = " << flags << ": '::mmap' " << Errno(errno); - return false; + << ", flags = " << flags + << ", call = ::mmap, errno = " << errno_copy; + throw SystemError(errno_copy); } } - return true; } } // namespace storage Modified: lib/grnxx/storage/chunk-posix.hpp (+3 -3) =================================================================== --- lib/grnxx/storage/chunk-posix.hpp 2013-07-02 13:38:59 +0900 (8cf872e) +++ lib/grnxx/storage/chunk-posix.hpp 2013-07-02 14:43:39 +0900 (659bc7a) @@ -35,7 +35,7 @@ class ChunkImpl : public Chunk { static ChunkImpl *create(File *file, uint64_t offset, uint64_t size, ChunkFlags flags); - bool sync(uint64_t offset, uint64_t size); + void sync(uint64_t offset, uint64_t size); ChunkFlags flags() const; void *address() const; @@ -46,9 +46,9 @@ class ChunkImpl : public Chunk { void *address_; uint64_t size_; - bool create_file_backed_chunk(File *file, uint64_t offset, uint64_t size, + void create_file_backed_chunk(File *file, uint64_t offset, uint64_t size, ChunkFlags flags); - bool create_anonymous_chunk(uint64_t size, ChunkFlags flags); + void create_anonymous_chunk(uint64_t size, ChunkFlags flags); }; } // namespace storage Modified: lib/grnxx/storage/chunk-windows.cpp (+36 -38) =================================================================== --- lib/grnxx/storage/chunk-windows.cpp 2013-07-02 13:38:59 +0900 (0d0fc5f) +++ lib/grnxx/storage/chunk-windows.cpp 2013-07-02 14:43:39 +0900 (b05f028) @@ -22,6 +22,7 @@ #include <new> #include "grnxx/errno.hpp" +#include "grnxx/exception.hpp" #include "grnxx/logger.hpp" #include "grnxx/storage/file.hpp" @@ -37,14 +38,16 @@ ChunkImpl::ChunkImpl() ChunkImpl::~ChunkImpl() { if (address_) { if (!::UnmapViewOfFile(address_)) { - GRNXX_ERROR() << "failed to unmap chunk" - << ": '::UnmapViewOfFile' " << Errno(::GetLastError()); + Errno errno_copy(::GetLastError()); + GRNXX_ERROR() << "failed to unmap chunk: " + << "call = ::UnmapViewOfFile, errno = " << errno_copy; } } if (handle_) { if (!::CloseHandle(handle_)) { - GRNXX_ERROR() << "failed to close file mapping" - << ": '::CloseHandle' " << Errno(::GetLastError()); + Errno errno_copy(::GetLastError()); + GRNXX_ERROR() << "failed to close file mapping: " + << "call = ::CloseHandle, errno = " << errno_copy; } } } @@ -54,61 +57,54 @@ ChunkImpl *ChunkImpl::create(File *file, uint64_t offset, uint64_t size, std::unique_ptr<ChunkImpl> chunk(new (std::nothrow) ChunkImpl); if (!chunk) { GRNXX_ERROR() << "new grnxx::storage::FileImpl failed"; - return nullptr; + throw MemoryError(); } if (file) { - if (!chunk->create_file_backed_chunk(file, offset, size, flags)) { - return nullptr; - } + chunk->create_file_backed_chunk(file, offset, size, flags); } else { - if (!chunk->create_anonymous_chunk(size, flags)) { - return nullptr; - } + chunk->create_anonymous_chunk(size, flags); } return chunk.release(); } -bool ChunkImpl::sync(uint64_t offset, uint64_t size) { +void ChunkImpl::sync(uint64_t offset, uint64_t size) { if ((flags_ & CHUNK_ANONYMOUS) || (flags_ & CHUNK_READ_ONLY)) { - GRNXX_WARNING() << "invalid operation: flags = " << flags_; - return false; + GRNXX_ERROR() << "invalid operation: flags = " << flags_; + throw LogicError(); } if ((offset > size_) || (size > size_) || (size > (size_ - offset))) { GRNXX_ERROR() << "invalid argument: offset = " << offset << ", size = " << size << ", chunk_size = " << size_; - return false; + throw LogicError(); } if (size > std::numeric_limits<SIZE_T>::max()) { GRNXX_ERROR() << "invalid argument: size = " << size; - return false; + throw LogicError(); } if (!::FlushViewOfFile(static_cast<char *>(address_) + offset, size)) { + Errno errno_copy(::GetLastError()); GRNXX_ERROR() << "failed to sync chunk: offset = " << offset << ", size = " << size - << ": '::FlushViewOfFile' " << Errno(::GetLastError()); - return false; + << ", call = FlushViewOfFile;, errno = " << errno_copy; + throw SystemError(errno_copy); } - return true; } -bool ChunkImpl::create_file_backed_chunk(File *file, uint64_t offset, +void ChunkImpl::create_file_backed_chunk(File *file, uint64_t offset, uint64_t size, ChunkFlags flags) { - uint64_t file_size; - if (!file->get_size(&file_size)) { - return false; - } + uint64_t file_size = file->get_size(); if ((offset >= file_size) || (size > file_size) || (size > (file_size - offset))) { GRNXX_ERROR() << "invalid argument: offset = " << offset << ", size = " << size << ", file_size = " << file_size; - return false; + throw LogicError(); } if (size == 0) { size = file_size - offset; } if (size > std::numeric_limits<SIZE_T>::max()) { GRNXX_ERROR() << "invalid argument: size = " << size; - return false; + throw LogicError(); } if (file->flags() & FILE_READ_ONLY) { flags_ |= CHUNK_READ_ONLY; @@ -126,32 +122,33 @@ bool ChunkImpl::create_file_backed_chunk(File *file, uint64_t offset, nullptr, protection_mode, size_high, size_low, nullptr); if (!handle_) { + Errno errno_copy(::GetLastError()); GRNXX_ERROR() << "failed to create file mapping: " << "file_path = " << file->path() << ", file_size = " << file_size << ", offset = " << offset << ", size = " << size << ", flags = " << flags - << ": '::CreateFileMapping' " << Errno(::GetLastError()); - return false; + << ", call = ::CreateFileMapping, errno = " << errno_copy; + throw SystemError(errno_copy); } const DWORD offset_high = static_cast<DWORD>(offset >> 32); const DWORD offset_low = static_cast<DWORD>(offset); address_ = ::MapViewOfFile(handle_, desired_access, offset_high, offset_low, static_cast<SIZE_T>(size)); if (!address_) { + Errno errno_copy(::GetLastError()); GRNXX_ERROR() << "failed to map chunk: " << "file_path = " << file->path() << ", file_size = " << file_size << ", offset = " << offset << ", size = " << size << ", flags = " << flags - << ": '::MapViewOfFile' " << Errno(::GetLastError()); - return false; + << ", call = ::MapViewOfFile, errno = " << errno_copy; + throw SystemError(errno_copy); } - return true; } -bool ChunkImpl::create_anonymous_chunk(uint64_t size, ChunkFlags flags) { +void ChunkImpl::create_anonymous_chunk(uint64_t size, ChunkFlags flags) { if (size == 0) { GRNXX_ERROR() << "invalid argument: size = " << size; - return false; + throw LogicError(); } flags_ = CHUNK_ANONYMOUS; size_ = size; @@ -160,19 +157,20 @@ bool ChunkImpl::create_anonymous_chunk(uint64_t size, ChunkFlags flags) { handle_ = ::CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, size_high, size_low, nullptr); if (!handle_) { + Errno errno_copy(::GetLastError()); GRNXX_ERROR() << "failed to create anonymous file mapping: " << "size = " << size << ", flags = " << flags - << ": '::CreateFileMapping' " << Errno(::GetLastError()); - return false; + << ", call = ::CreateFileMapping, errno = " << errno_copy; + throw SystemError(errno_copy); } address_ = ::MapViewOfFile(handle_, FILE_MAP_WRITE, 0, 0, 0); if (!address_) { + Errno errno_copy(::GetLastError()); GRNXX_ERROR() << "failed to map anonymous chunk: " << "size = " << size << ", flags = " << flags - << ": '::MapViewOfFile' " << Errno(::GetLastError()); - return false; + << ", call = ::MapViewOfFile, errno = " << errno_copy; + throw SystemError(errno_copy); } - return true; } } // namespace storage Modified: lib/grnxx/storage/chunk-windows.hpp (+3 -3) =================================================================== --- lib/grnxx/storage/chunk-windows.hpp 2013-07-02 13:38:59 +0900 (4c543e2) +++ lib/grnxx/storage/chunk-windows.hpp 2013-07-02 14:43:39 +0900 (a6460ba) @@ -42,7 +42,7 @@ class ChunkImpl : public Chunk { static ChunkImpl *create(File *file, uint64_t offset, uint64_t size, ChunkFlags flags); - bool sync(uint64_t offset, uint64_t size); + void sync(uint64_t offset, uint64_t size); ChunkFlags flags() const; void *address() const; @@ -54,9 +54,9 @@ class ChunkImpl : public Chunk { void *address_; uint64_t size_; - bool create_file_backed_chunk(File *file, uint64_t offset, uint64_t size, + void create_file_backed_chunk(File *file, uint64_t offset, uint64_t size, ChunkFlags flags); - bool create_anonymous_chunk(uint64_t size, ChunkFlags flags); + void create_anonymous_chunk(uint64_t size, ChunkFlags flags); }; } // namespace storage Modified: lib/grnxx/storage/chunk.hpp (+1 -1) =================================================================== --- lib/grnxx/storage/chunk.hpp 2013-07-02 13:38:59 +0900 (9dce8f1) +++ lib/grnxx/storage/chunk.hpp 2013-07-02 14:43:39 +0900 (c852ed0) @@ -59,7 +59,7 @@ class Chunk { ChunkFlags flags = CHUNK_DEFAULT); // Flush modified pages. - virtual bool sync(uint64_t offset = 0, uint64_t size = 0) = 0; + virtual void sync(uint64_t offset = 0, uint64_t size = 0) = 0; // Return the enabled flags. virtual ChunkFlags flags() const = 0; Modified: test/test_storage.cpp (+4 -64) =================================================================== --- test/test_storage.cpp 2013-07-02 13:38:59 +0900 (f086d29) +++ test/test_storage.cpp 2013-07-02 14:43:39 +0900 (bb7312d) @@ -131,7 +131,6 @@ void test_file_lock_and_unlock() { std::unique_ptr<grnxx::storage::File> file_2; file_2.reset(grnxx::storage::File::open(FILE_PATH)); - assert(file_2); assert(file_1->lock(grnxx::storage::FILE_LOCK_SHARED)); assert(file_2->lock(grnxx::storage::FILE_LOCK_SHARED | @@ -156,7 +155,6 @@ void test_file_lock_and_unlock() { void test_file_sync() { std::unique_ptr<grnxx::storage::File> file( grnxx::storage::File::create(nullptr)); - assert(file); file->sync(); } @@ -164,7 +162,6 @@ void test_file_sync() { void test_file_resize_and_size() { std::unique_ptr<grnxx::storage::File> file( grnxx::storage::File::create(nullptr)); - assert(file); assert(file->get_size() == 0); file->resize(65536); @@ -179,12 +176,10 @@ void test_file_path() { std::unique_ptr<grnxx::storage::File> file; file.reset(grnxx::storage::File::create(FILE_PATH)); - assert(file); assert(std::strcmp(file->path(), FILE_PATH) == 0); file.reset(grnxx::storage::File::create(FILE_PATH, grnxx::storage::FILE_TEMPORARY)); - assert(file); assert(std::strcmp(file->path(), FILE_PATH) != 0); assert(grnxx::storage::File::unlink(FILE_PATH)); @@ -196,17 +191,14 @@ void test_file_flags() { std::unique_ptr<grnxx::storage::File> file; file.reset(grnxx::storage::File::create(FILE_PATH)); - assert(file); assert(file->flags() == grnxx::storage::FILE_DEFAULT); file.reset(grnxx::storage::File::open(FILE_PATH, grnxx::storage::FILE_READ_ONLY)); - assert(file); assert(file->flags() == grnxx::storage::FILE_READ_ONLY); file.reset(grnxx::storage::File::create(FILE_PATH, grnxx::storage::FILE_TEMPORARY)); - assert(file); assert(file->flags() == grnxx::storage::FILE_TEMPORARY); assert(grnxx::storage::File::unlink(FILE_PATH)); @@ -215,7 +207,6 @@ void test_file_flags() { void test_file_handle() { std::unique_ptr<grnxx::storage::File> file( grnxx::storage::File::create(nullptr)); - assert(file); assert(file->handle()); } @@ -226,40 +217,15 @@ void test_chunk_create() { std::unique_ptr<grnxx::storage::Chunk> chunk; file.reset(grnxx::storage::File::create(nullptr)); - assert(file); - chunk.reset(grnxx::storage::Chunk::create(file.get())); - assert(!chunk); - file->resize(FILE_SIZE); chunk.reset(grnxx::storage::Chunk::create(file.get())); - assert(chunk); chunk.reset(grnxx::storage::Chunk::create(file.get(), 0)); - assert(chunk); chunk.reset(grnxx::storage::Chunk::create(file.get(), 0, 0)); - assert(chunk); chunk.reset(grnxx::storage::Chunk::create(file.get(), 0, FILE_SIZE)); - assert(chunk); chunk.reset(grnxx::storage::Chunk::create(file.get(), 0, 10)); - assert(chunk); - - chunk.reset(grnxx::storage::Chunk::create(file.get(), -1)); - assert(!chunk); - chunk.reset(grnxx::storage::Chunk::create(file.get(), FILE_SIZE + 1)); - assert(!chunk); - chunk.reset(grnxx::storage::Chunk::create(file.get(), 0, FILE_SIZE + 1)); - assert(!chunk); - chunk.reset(grnxx::storage::Chunk::create(file.get(), FILE_SIZE / 2, - FILE_SIZE)); - assert(!chunk); chunk.reset(grnxx::storage::Chunk::create(nullptr, 0, 1 << 20)); - assert(chunk); - - chunk.reset(grnxx::storage::Chunk::create(nullptr, 0, 0)); - assert(!chunk); - chunk.reset(grnxx::storage::Chunk::create(nullptr, 0, -1)); - assert(!chunk); } void test_chunk_sync() { @@ -268,23 +234,13 @@ void test_chunk_sync() { std::unique_ptr<grnxx::storage::Chunk> chunk; file.reset(grnxx::storage::File::create(nullptr)); - assert(file); file->resize(FILE_SIZE); chunk.reset(grnxx::storage::Chunk::create(file.get())); - assert(chunk); - assert(chunk->sync()); - assert(chunk->sync(0)); - assert(chunk->sync(0, 0)); - assert(chunk->sync(0, FILE_SIZE)); - - assert(!chunk->sync(FILE_SIZE + 1)); - assert(!chunk->sync(0, FILE_SIZE + 1)); - assert(!chunk->sync(FILE_SIZE / 2, FILE_SIZE)); - - chunk.reset(grnxx::storage::Chunk::create(nullptr, 0, 1 << 20)); - assert(chunk); - assert(!chunk->sync()); + chunk->sync(); + chunk->sync(0); + chunk->sync(0, 0); + chunk->sync(0, FILE_SIZE); } void test_chunk_flags() { @@ -294,19 +250,15 @@ void test_chunk_flags() { std::unique_ptr<grnxx::storage::Chunk> chunk; file.reset(grnxx::storage::File::create(FILE_PATH)); - assert(file); file->resize(1 << 20); chunk.reset(grnxx::storage::Chunk::create(file.get())); - assert(chunk); assert(chunk->flags() == grnxx::storage::CHUNK_DEFAULT); file.reset(grnxx::storage::File::open(FILE_PATH, grnxx::storage::FILE_READ_ONLY)); - assert(file); chunk.reset(grnxx::storage::Chunk::create(file.get())); - assert(chunk); assert(chunk->flags() == grnxx::storage::CHUNK_READ_ONLY); file.reset(); @@ -320,22 +272,17 @@ void test_chunk_address() { std::unique_ptr<grnxx::storage::Chunk> chunk; file.reset(grnxx::storage::File::create(nullptr)); - assert(file); file->resize(10); chunk.reset(grnxx::storage::Chunk::create(file.get())); - assert(chunk); std::memcpy(chunk->address(), "0123456789", 10); chunk.reset(grnxx::storage::Chunk::create(file.get())); - assert(chunk); assert(std::memcmp(chunk->address(), "0123456789", 10) == 0); file.reset(grnxx::storage::File::create(FILE_PATH)); - assert(file); file->resize(1 << 16); chunk.reset(grnxx::storage::Chunk::create(file.get())); - assert(chunk); for (std::uint64_t i = 0; i < (1 << 16); ++i) { static_cast<std::uint8_t *>(chunk->address())[i] = static_cast<uint8_t>(i); } @@ -343,9 +290,7 @@ void test_chunk_address() { file.reset(); file.reset(grnxx::storage::File::open(FILE_PATH)); - assert(file); chunk.reset(grnxx::storage::Chunk::create(file.get())); - assert(chunk); for (std::uint64_t i = 0; i < (1 << 16); ++i) { assert(static_cast<std::uint8_t *>(chunk->address())[i] == static_cast<uint8_t>(i)); @@ -361,21 +306,16 @@ void test_chunk_size() { std::unique_ptr<grnxx::storage::Chunk> chunk; file.reset(grnxx::storage::File::create(nullptr)); - assert(file); file->resize(FILE_SIZE); chunk.reset(grnxx::storage::Chunk::create(file.get())); - assert(chunk); assert(chunk->size() == FILE_SIZE); chunk.reset(grnxx::storage::Chunk::create(file.get(), FILE_SIZE / 2)); - assert(chunk); assert(chunk->size() == (FILE_SIZE / 2)); chunk.reset(grnxx::storage::Chunk::create(file.get(), 0, FILE_SIZE / 2)); - assert(chunk); assert(chunk->size() == (FILE_SIZE / 2)); chunk.reset(grnxx::storage::Chunk::create(nullptr, 0, 1 << 20)); - assert(chunk); assert(chunk->size() == (1 << 20)); } -------------- next part -------------- HTML����������������������������... ダウンロード