susumu.yata
null+****@clear*****
Wed Apr 24 17:33:19 JST 2013
susumu.yata 2013-04-24 17:33:19 +0900 (Wed, 24 Apr 2013) New Revision: c01d791a3a86eb7ce717ba7f42440aa9582bad24 https://github.com/groonga/grnxx/commit/c01d791a3a86eb7ce717ba7f42440aa9582bad24 Message: Rename grnxx::storage::View to grnxx::storage::Chunk. Modified files: lib/grnxx/storage/Makefile.am lib/grnxx/storage/storage_impl.cpp lib/grnxx/storage/storage_impl.hpp test/test_storage.cpp Renamed files: lib/grnxx/storage/chunk-posix.cpp (from lib/grnxx/storage/view-posix.cpp) lib/grnxx/storage/chunk-posix.hpp (from lib/grnxx/storage/view-posix.hpp) lib/grnxx/storage/chunk-windows.cpp (from lib/grnxx/storage/view-windows.cpp) lib/grnxx/storage/chunk-windows.hpp (from lib/grnxx/storage/view-windows.hpp) lib/grnxx/storage/chunk.cpp (from lib/grnxx/storage/view.cpp) lib/grnxx/storage/chunk.hpp (from lib/grnxx/storage/view.hpp) Modified: lib/grnxx/storage/Makefile.am (+8 -8) =================================================================== --- lib/grnxx/storage/Makefile.am 2013-04-24 17:19:23 +0900 (f192041) +++ lib/grnxx/storage/Makefile.am 2013-04-24 17:33:19 +0900 (937f1a9) @@ -3,6 +3,9 @@ noinst_LTLIBRARIES = libgrnxx_storage.la libgrnxx_storage_la_LDFLAGS = @AM_LTLDFLAGS@ libgrnxx_storage_la_SOURCES = \ + chunk.cpp \ + chunk-posix.cpp \ + chunk-windows.cpp \ chunk_index.cpp \ file.cpp \ file-posix.cpp \ @@ -10,13 +13,13 @@ libgrnxx_storage_la_SOURCES = \ header.cpp \ node_header.cpp \ path.cpp \ - storage_impl.cpp \ - view.cpp \ - view-posix.cpp \ - view-windows.cpp + storage_impl.cpp libgrnxx_storage_includedir = ${includedir}/grnxx/storage libgrnxx_storage_include_HEADERS = \ + chunk.hpp \ + chunk-posix.hpp \ + chunk-windows.hpp \ chunk_index.hpp \ file.hpp \ file-posix.hpp \ @@ -24,7 +27,4 @@ libgrnxx_storage_include_HEADERS = \ header.hpp \ node_header.hpp \ path.hpp \ - storage_impl.hpp \ - view.hpp \ - view-posix.hpp \ - view-windows.hpp + storage_impl.hpp Renamed: lib/grnxx/storage/chunk-posix.cpp (+32 -29) 71% =================================================================== --- lib/grnxx/storage/view-posix.cpp 2013-04-24 17:19:23 +0900 (bcdac9c) +++ lib/grnxx/storage/chunk-posix.cpp 2013-04-24 17:33:19 +0900 (91c4ce4) @@ -15,7 +15,7 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "grnxx/storage/view-posix.hpp" +#include "grnxx/storage/chunk-posix.hpp" #ifndef GRNXX_WINDOWS @@ -35,44 +35,47 @@ namespace grnxx { namespace storage { -ViewImpl::ViewImpl() : flags_(VIEW_DEFAULT), address_(MAP_FAILED), size_(0) {} +ChunkImpl::ChunkImpl() + : flags_(CHUNK_DEFAULT), + address_(MAP_FAILED), + size_(0) {} -ViewImpl::~ViewImpl() { +ChunkImpl::~ChunkImpl() { if (address_ != MAP_FAILED) { if (::munmap(address_, static_cast<size_t>(size_)) != 0) { - GRNXX_ERROR() << "failed to unmap view: '::munmap' " << Error(errno); + GRNXX_ERROR() << "failed to unmap chunk: '::munmap' " << Error(errno); } } } -View *ViewImpl::create(File *file, int64_t offset, int64_t size, - ViewFlags flags) { - std::unique_ptr<ViewImpl> view(new (std::nothrow) ViewImpl); - if (!view) { +ChunkImpl *ChunkImpl::create(File *file, int64_t offset, int64_t size, + ChunkFlags flags) { + std::unique_ptr<ChunkImpl> chunk(new (std::nothrow) ChunkImpl); + if (!chunk) { GRNXX_ERROR() << "new grnxx::storage::FileImpl failed"; return nullptr; } if (file) { - if (!view->create_file_backed_view(file, offset, size, flags)) { + if (!chunk->create_file_backed_chunk(file, offset, size, flags)) { return nullptr; } } else { - if (!view->create_anonymous_view(size, flags)) { + if (!chunk->create_anonymous_chunk(size, flags)) { return nullptr; } } - return view.release(); + return chunk.release(); } -bool ViewImpl::sync(int64_t offset, int64_t size) { - if ((flags_ & VIEW_ANONYMOUS) || (flags_ & VIEW_READ_ONLY)) { +bool ChunkImpl::sync(int64_t offset, int64_t size) { + if ((flags_ & CHUNK_ANONYMOUS) || (flags_ & CHUNK_READ_ONLY)) { GRNXX_WARNING() << "invalid operation: flags = " << flags_; return false; } if ((offset < 0) || (offset > size_) || (size > size_) || ((size >= 0) && (size > (size_ - offset)))) { GRNXX_ERROR() << "invalid argument: offset = " << offset - << ", size = " << size << ", view_size = " << size_; + << ", size = " << size << ", chunk_size = " << size_; return false; } if (size < 0) { @@ -80,7 +83,7 @@ bool ViewImpl::sync(int64_t offset, int64_t size) { } if (size > 0) { if (::msync(static_cast<char *>(address_) + offset, size, MS_SYNC) != 0) { - GRNXX_ERROR() << "failed to sync view: offset = " << offset + GRNXX_ERROR() << "failed to sync chunk: offset = " << offset << ", size = " << size << ": '::msync' " << Error(errno); return false; } @@ -88,20 +91,20 @@ bool ViewImpl::sync(int64_t offset, int64_t size) { return true; } -ViewFlags ViewImpl::flags() const { +ChunkFlags ChunkImpl::flags() const { return flags_; } -void *ViewImpl::address() const { +void *ChunkImpl::address() const { return address_; } -int64_t ViewImpl::size() const { +int64_t ChunkImpl::size() const { return size_; } -bool ViewImpl::create_file_backed_view(File *file, int64_t offset, int64_t size, - ViewFlags flags) { +bool ChunkImpl::create_file_backed_chunk(File *file, int64_t offset, + int64_t size, ChunkFlags flags) { const int64_t file_size = file->size(); if ((offset < 0) || (offset >= file_size) || (size == 0) || (size > file_size) || @@ -114,19 +117,19 @@ bool ViewImpl::create_file_backed_view(File *file, int64_t offset, int64_t size, size = file_size - offset; } if (file->flags() & FILE_READ_ONLY) { - flags_ |= VIEW_READ_ONLY; + flags_ |= CHUNK_READ_ONLY; } size_ = size; int protection_flags = PROT_READ | PROT_WRITE; - if (flags_ & VIEW_READ_ONLY) { - flags_ |= VIEW_READ_ONLY; + if (flags_ & CHUNK_READ_ONLY) { + flags_ |= CHUNK_READ_ONLY; protection_flags = PROT_READ; } const int mmap_flags = MAP_SHARED; address_ = ::mmap(nullptr, size, protection_flags, mmap_flags, *static_cast<const int *>(file->handle()), offset); if (address_ == MAP_FAILED) { - GRNXX_ERROR() << "failed to map file-backed view: " + GRNXX_ERROR() << "failed to map file-backed chunk: " << "file_path = " << file->path() << ", offset = " << offset << ", size = " << size << ", flags = " << flags << ": '::mmap' " << Error(errno); @@ -135,28 +138,28 @@ bool ViewImpl::create_file_backed_view(File *file, int64_t offset, int64_t size, return true; } -bool ViewImpl::create_anonymous_view(int64_t size, ViewFlags flags) { +bool ChunkImpl::create_anonymous_chunk(int64_t size, ChunkFlags flags) { if (size <= 0) { GRNXX_ERROR() << "invalid argument: size = " << size; return false; } - flags_ = VIEW_ANONYMOUS; + flags_ = CHUNK_ANONYMOUS; size_ = size; const int protection_flags = PROT_READ | PROT_WRITE; const int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS; #ifdef MAP_HUGETLB - if (flags & VIEW_HUGE_TLB) { + if (flags & CHUNK_HUGE_TLB) { address_ = ::mmap(nullptr, size, protection_flags, mmap_flags | MAP_HUGETLB, -1, 0); if (address_ != MAP_FAILED) { - flags_ |= VIEW_HUGE_TLB; + flags_ |= CHUNK_HUGE_TLB; } } #endif // MAP_HUGETLB if (address_ == MAP_FAILED) { address_ = ::mmap(nullptr, size, protection_flags, mmap_flags, -1, 0); if (address_ == MAP_FAILED) { - GRNXX_ERROR() << "failed to map anonymous view: size = " << size + GRNXX_ERROR() << "failed to map anonymous chunk: size = " << size << ", flags = " << flags << ": '::mmap' " << Error(errno); return false; } Renamed: lib/grnxx/storage/chunk-posix.hpp (+14 -14) 65% =================================================================== --- lib/grnxx/storage/view-posix.hpp 2013-04-24 17:19:23 +0900 (c954ee0) +++ lib/grnxx/storage/chunk-posix.hpp 2013-04-24 17:33:19 +0900 (3bba508) @@ -15,38 +15,38 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef GRNXX_STORAGE_VIEW_POSIX_HPP -#define GRNXX_STORAGE_VIEW_POSIX_HPP +#ifndef GRNXX_STORAGE_CHUNK_POSIX_HPP +#define GRNXX_STORAGE_CHUNK_POSIX_HPP -#include "grnxx/storage/view.hpp" +#include "grnxx/storage/chunk.hpp" #ifndef GRNXX_WINDOWS namespace grnxx { namespace storage { -class ViewImpl : public View { +class ChunkImpl : public Chunk { public: - ViewImpl(); - ~ViewImpl(); + ChunkImpl(); + ~ChunkImpl(); - static View *create(File *file, int64_t offset, int64_t size, - ViewFlags flags); + static ChunkImpl *create(File *file, int64_t offset, int64_t size, + ChunkFlags flags); bool sync(int64_t offset, int64_t size); - ViewFlags flags() const; + ChunkFlags flags() const; void *address() const; int64_t size() const; private: - ViewFlags flags_; + ChunkFlags flags_; void *address_; int64_t size_; - bool create_file_backed_view(File *file, int64_t offset, int64_t size, - ViewFlags flags); - bool create_anonymous_view(int64_t size, ViewFlags flags); + bool create_file_backed_chunk(File *file, int64_t offset, int64_t size, + ChunkFlags flags); + bool create_anonymous_chunk(int64_t size, ChunkFlags flags); }; } // namespace storage @@ -54,4 +54,4 @@ class ViewImpl : public View { #endif // GRNXX_WINDOWS -#endif // GRNXX_STORAGE_VIEW_POSIX_HPP +#endif // GRNXX_STORAGE_CHUNK_POSIX_HPP Renamed: lib/grnxx/storage/chunk-windows.cpp (+32 -32) 70% =================================================================== --- lib/grnxx/storage/view-windows.cpp 2013-04-24 17:19:23 +0900 (34e8a27) +++ lib/grnxx/storage/chunk-windows.cpp 2013-04-24 17:33:19 +0900 (7df07c8) @@ -15,7 +15,7 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "grnxx/storage/view-windows.hpp" +#include "grnxx/storage/chunk-windows.hpp" #ifdef GRNXX_WINDOWS @@ -26,17 +26,17 @@ namespace grnxx { namespace storage { -ViewImpl::ViewImpl() - : flags_(VIEW_DEFAULT), +ChunkImpl::ChunkImpl() + : flags_(CHUNK_DEFAULT), handle_(nullptr), address_(nullptr), size_(0) {} -ViewImpl::~ViewImpl() { +ChunkImpl::~ChunkImpl() { if (address_) { - if (!::UnmapViewOfFile(address_)) { - GRNXX_ERROR() << "failed to unmap view" - << ": '::UnmapViewOfFile' " << Error(::GetLastError()); + if (!::UnmapChunkOfFile(address_)) { + GRNXX_ERROR() << "failed to unmap chunk" + << ": '::UnmapChunkOfFile' " << Error(::GetLastError()); } } if (handle_) { @@ -47,52 +47,52 @@ ViewImpl::~ViewImpl() { } } -View *ViewImpl::create(File *file, int64_t offset, int64_t size, - ViewFlags flags) { - std::unique_ptr<ViewImpl> view(new (std::nothrow) ViewImpl); - if (!view) { +ChunkImpl *ChunkImpl::create(File *file, int64_t offset, int64_t size, + ChunkFlags flags) { + std::unique_ptr<ChunkImpl> chunk(new (std::nothrow) ChunkImpl); + if (!chunk) { GRNXX_ERROR() << "new grnxx::storage::FileImpl failed"; return nullptr; } if (file) { - if (!view->create_file_backed_view(file, offset, size, flags)) { + if (!chunk->create_file_backed_chunk(file, offset, size, flags)) { return nullptr; } } else { - if (!view->create_anonymous_view(size, flags)) { + if (!chunk->create_anonymous_chunk(size, flags)) { return nullptr; } } - return view.release(); + return chunk.release(); } -bool ViewImpl::sync(int64_t offset, int64_t size) { - if ((flags_ & VIEW_ANONYMOUS) || (flags_ & VIEW_READ_ONLY)) { +bool ChunkImpl::sync(int64_t offset, int64_t size) { + if ((flags_ & CHUNK_ANONYMOUS) || (flags_ & CHUNK_READ_ONLY)) { GRNXX_WARNING() << "invalid operation: flags = " << flags_; return false; } if ((offset < 0) || (offset > size_) || (size > size_) || ((size >= 0) && (size > (size_ - offset)))) { GRNXX_ERROR() << "invalid argument: offset = " << offset - << ", size = " << size << ", view_size = " << size_; + << ", size = " << size << ", chunk_size = " << size_; return false; } if (size < 0) { size = size_ - offset; } if (size > 0) { - if (!::FlushViewOfFile(static_cast<char *>(address_) + offset, size)) { - GRNXX_ERROR() << "failed to sync view: offset = " << offset + if (!::FlushChunkOfFile(static_cast<char *>(address_) + offset, size)) { + GRNXX_ERROR() << "failed to sync chunk: offset = " << offset << ", size = " << size - << ": '::FlushViewOfFile' " << Error(::GetLastError()); + << ": '::FlushChunkOfFile' " << Error(::GetLastError()); return false; } } return true; } -bool ViewImpl::create_file_backed_view(File *file, int64_t offset, int64_t size, - ViewFlags flags) { +bool ChunkImpl::create_file_backed_chunk(File *file, int64_t offset, + int64_t size, ChunkFlags flags) { const int64_t file_size = file->size(); if ((offset < 0) || (offset >= file_size) || (size == 0) || (size > file_size) || @@ -105,12 +105,12 @@ bool ViewImpl::create_file_backed_view(File *file, int64_t offset, int64_t size, size = file_size - offset; } if (file->flags() & FILE_READ_ONLY) { - flags_ |= VIEW_READ_ONLY; + flags_ |= CHUNK_READ_ONLY; } size_ = size; int protection_mode = PAGE_READWRITE; DWORD desired_access = FILE_MAP_WRITE; - if (flags_ & VIEW_READ_ONLY) { + if (flags_ & CHUNK_READ_ONLY) { protection_mode = PAGE_READONLY; desired_access = FILE_MAP_READ; } @@ -128,24 +128,24 @@ bool ViewImpl::create_file_backed_view(File *file, int64_t offset, int64_t size, } 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, + address_ = ::MapChunkOfFile(handle_, desired_access, offset_high, offset_low, static_cast<SIZE_T>(size)); if (!address_) { - GRNXX_ERROR() << "failed to map view: " + GRNXX_ERROR() << "failed to map chunk: " << "file_path = " << file->path() << ", offset = " << offset << ", size = " << size << ", flags = " << flags - << ": '::MapViewOfFile' " << Error(::GetLastError()); + << ": '::MapChunkOfFile' " << Error(::GetLastError()); return false; } return true; } -bool ViewImpl::create_anonymous_view(int64_t size, ViewFlags flags) { +bool ChunkImpl::create_anonymous_chunk(int64_t size, ChunkFlags flags) { if (size <= 0) { GRNXX_ERROR() << "invalid argument: size = " << size; return false; } - flags_ = VIEW_ANONYMOUS; + flags_ = CHUNK_ANONYMOUS; size_ = size; const DWORD size_high = static_cast<DWORD>(size >> 32); const DWORD size_low = static_cast<DWORD>(size); @@ -157,11 +157,11 @@ bool ViewImpl::create_anonymous_view(int64_t size, ViewFlags flags) { << ": '::CreateFileMapping' " << Error(::GetLastError()); return false; } - address_ = ::MapViewOfFile(handle_, FILE_MAP_WRITE, 0, 0, 0); + address_ = ::MapChunkOfFile(handle_, FILE_MAP_WRITE, 0, 0, 0); if (!address_) { - GRNXX_ERROR() << "failed to map anonymous view: " + GRNXX_ERROR() << "failed to map anonymous chunk: " << "size = " << size << ", flags = " << flags - << ": '::MapViewOfFile' " << Error(::GetLastError()); + << ": '::MapChunkOfFile' " << Error(::GetLastError()); return false; } return true; Renamed: lib/grnxx/storage/chunk-windows.hpp (+14 -14) 68% =================================================================== --- lib/grnxx/storage/view-windows.hpp 2013-04-24 17:19:23 +0900 (0152902) +++ lib/grnxx/storage/chunk-windows.hpp 2013-04-24 17:33:19 +0900 (eeaae09) @@ -15,10 +15,10 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef GRNXX_STORAGE_VIEW_WINDOWS_HPP -#define GRNXX_STORAGE_VIEW_WINDOWS_HPP +#ifndef GRNXX_STORAGE_CHUNK_WINDOWS_HPP +#define GRNXX_STORAGE_CHUNK_WINDOWS_HPP -#include "grnxx/storage/view.hpp" +#include "grnxx/storage/chunk.hpp" #ifdef GRNXX_WINDOWS @@ -32,29 +32,29 @@ namespace grnxx { namespace storage { -class ViewImpl : public View { +class ChunkImpl : public Chunk { public: - ViewImpl(); - ~ViewImpl(); + ChunkImpl(); + ~ChunkImpl(); - static View *create(File *file, int64_t offset, int64_t size, - ViewFlags flags); + static ChunkImpl *create(File *file, int64_t offset, int64_t size, + ChunkFlags flags); bool sync(int64_t offset, int64_t size); - ViewFlags flags() const; + ChunkFlags flags() const; void *address() const; int64_t size() const; private: - ViewFlags flags_; + ChunkFlags flags_; HANDLE handle_; void *address_; uint64_t size_; - bool create_file_backed_view(File *file, int64_t offset, int64_t size, - ViewFlags flags); - bool create_anonymous_view(int64_t size, ViewFlags flags); + bool create_file_backed_chunk(File *file, int64_t offset, int64_t size, + ChunkFlags flags); + bool create_anonymous_chunk(int64_t size, ChunkFlags flags); }; } // namespace storage @@ -62,4 +62,4 @@ class ViewImpl : public View { #endif // GRNXX_WINDOWS -#endif // GRNXX_STORAGE_VIEW_WINDOWS_HPP +#endif // GRNXX_STORAGE_CHUNK_WINDOWS_HPP Renamed: lib/grnxx/storage/chunk.cpp (+13 -12) 68% =================================================================== --- lib/grnxx/storage/view.cpp 2013-04-24 17:19:23 +0900 (0132c83) +++ lib/grnxx/storage/chunk.cpp 2013-04-24 17:33:19 +0900 (ce27354) @@ -15,10 +15,10 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "grnxx/storage/view.hpp" +#include "grnxx/storage/chunk.hpp" -#include "grnxx/storage/view-posix.hpp" -#include "grnxx/storage/view-windows.hpp" +#include "grnxx/storage/chunk-posix.hpp" +#include "grnxx/storage/chunk-windows.hpp" #include "grnxx/string_builder.hpp" namespace grnxx { @@ -34,22 +34,23 @@ namespace storage { } \ } while (false) -StringBuilder &operator<<(StringBuilder &builder, ViewFlags flags) { +StringBuilder &operator<<(StringBuilder &builder, ChunkFlags flags) { bool is_first = true; - GRNXX_FLAGS_WRITE(VIEW_ANONYMOUS); - GRNXX_FLAGS_WRITE(VIEW_HUGE_TLB); - GRNXX_FLAGS_WRITE(VIEW_READ_ONLY); + GRNXX_FLAGS_WRITE(CHUNK_ANONYMOUS); + GRNXX_FLAGS_WRITE(CHUNK_HUGE_TLB); + GRNXX_FLAGS_WRITE(CHUNK_READ_ONLY); if (is_first) { - builder << "VIEW_DEFAULT"; + builder << "CHUNK_DEFAULT"; } return builder; } -View::View() {} -View::~View() {} +Chunk::Chunk() {} +Chunk::~Chunk() {} -View *View::create(File *file, int64_t offset, int64_t size, ViewFlags flags) { - return ViewImpl::create(file, offset, size, flags); +Chunk *Chunk::create(File *file, int64_t offset, int64_t size, + ChunkFlags flags) { + return ChunkImpl::create(file, offset, size, flags); } } // namespace storage Renamed: lib/grnxx/storage/chunk.hpp (+17 -17) 70% =================================================================== --- lib/grnxx/storage/view.hpp 2013-04-24 17:19:23 +0900 (fcd9d8e) +++ lib/grnxx/storage/chunk.hpp 2013-04-24 17:33:19 +0900 (eb030f0) @@ -15,8 +15,8 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef GRNXX_STORAGE_VIEW_HPP -#define GRNXX_STORAGE_VIEW_HPP +#ifndef GRNXX_STORAGE_CHUNK_HPP +#define GRNXX_STORAGE_CHUNK_HPP #include "grnxx/basic.hpp" #include "grnxx/flags_impl.hpp" @@ -29,40 +29,40 @@ namespace storage { class File; -class View; -using ViewFlags = FlagsImpl<View>; +class Chunk; +using ChunkFlags = FlagsImpl<Chunk>; // Use the default settings. -constexpr ViewFlags VIEW_DEFAULT = ViewFlags::define(0x00); +constexpr ChunkFlags CHUNK_DEFAULT = ChunkFlags::define(0x00); // Create an anonymous memory mapping. // This flag is implicitly enabled if "file" == nullptr. -constexpr ViewFlags VIEW_ANONYMOUS = ViewFlags::define(0x01); +constexpr ChunkFlags CHUNK_ANONYMOUS = ChunkFlags::define(0x01); // Use huge pages if available, or use regular pages. -constexpr ViewFlags VIEW_HUGE_TLB = ViewFlags::define(0x02); +constexpr ChunkFlags CHUNK_HUGE_TLB = ChunkFlags::define(0x02); // Create a read-only memory mapping. // This flag is implicitly enabled if "file" is read-only. -constexpr ViewFlags VIEW_READ_ONLY = ViewFlags::define(0x04); +constexpr ChunkFlags CHUNK_READ_ONLY = ChunkFlags::define(0x04); -StringBuilder &operator<<(StringBuilder &builder, ViewFlags flags); +StringBuilder &operator<<(StringBuilder &builder, ChunkFlags flags); -class View { +class Chunk { public: - View(); - virtual ~View(); + Chunk(); + virtual ~Chunk(); // Create a file-backed memory mapping on "file" if "file" != nullptr, or // create an anonymous memory mapping. - // The available flag is VIEW_HUGE_TLB. - static View *create(File *file, + // The available flag is CHUNK_HUGE_TLB. + static Chunk *create(File *file, int64_t offset = 0, int64_t size = -1, - ViewFlags flags = VIEW_DEFAULT); + ChunkFlags flags = CHUNK_DEFAULT); // Flush modified pages. virtual bool sync(int64_t offset = 0, int64_t size = -1) = 0; // Return the enabled flags. - virtual ViewFlags flags() const = 0; + virtual ChunkFlags flags() const = 0; // Return the starting address. virtual void *address() const = 0; // Return the size. @@ -72,4 +72,4 @@ class View { } // namespace storage } // namespace grnxx -#endif // GRNXX_STORAGE_VIEW_HPP +#endif // GRNXX_STORAGE_CHUNK_HPP Modified: lib/grnxx/storage/storage_impl.cpp (+46 -9) =================================================================== --- lib/grnxx/storage/storage_impl.cpp 2013-04-24 17:19:23 +0900 (669274b) +++ lib/grnxx/storage/storage_impl.cpp 2013-04-24 17:33:19 +0900 (c1edb55) @@ -18,10 +18,11 @@ #include "grnxx/storage/storage_impl.hpp" #include "grnxx/logger.hpp" +#include "grnxx/storage/chunk.hpp" +#include "grnxx/storage/chunk_index.hpp" #include "grnxx/storage/file.hpp" #include "grnxx/storage/header.hpp" #include "grnxx/storage/path.hpp" -#include "grnxx/storage/view.hpp" namespace grnxx { namespace storage { @@ -29,6 +30,9 @@ namespace { constexpr uint32_t MAX_NODE_ID = INVALID_NODE_ID - 1; +constexpr uint32_t MAX_NUM_NODE_HEADER_CHUNKS = 32; +constexpr uint32_t MAX_NUM_NODE_BODY_CHUNKS = 2048; + // TODO: Define constant values. } // namespace @@ -38,10 +42,12 @@ StorageImpl::StorageImpl() path_(), flags_(STORAGE_DEFAULT), header_(nullptr), + node_header_chunk_indexes_(nullptr), + node_body_chunk_indexes_(nullptr), files_(), - header_view_(), - node_header_views_(), - node_body_views_() {} + header_chunk_(), + node_header_chunks_(), + node_body_chunks_() {} StorageImpl::~StorageImpl() {} @@ -50,7 +56,7 @@ StorageImpl *StorageImpl::create(const char *path, const StorageOptions &options) { if (!options.is_valid()) { GRNXX_ERROR() << "invalid argument: options = " << options; - return false; + return nullptr; } std::unique_ptr<StorageImpl> storage(new (std::nothrow) StorageImpl); if (!storage) { @@ -58,11 +64,17 @@ StorageImpl *StorageImpl::create(const char *path, return nullptr; } if (path && (~flags & STORAGE_TEMPORARY)) { - storage->create_persistent_storage(path, flags, options); + if (!storage->create_persistent_storage(path, flags, options)) { + return nullptr; + } } else if (flags & STORAGE_TEMPORARY) { - storage->create_temporary_storage(path, flags, options); + if (!storage->create_temporary_storage(path, flags, options)) { + return nullptr; + } } else { - storage->create_anonymous_storage(flags, options); + if (!storage->create_anonymous_storage(flags, options)) { + return nullptr; + } } return storage.release(); } @@ -93,7 +105,7 @@ StorageImpl *StorageImpl::open_or_create(const char *path, } if (!options.is_valid()) { GRNXX_ERROR() << "invalid argument: options = " << options; - return false; + return nullptr; } std::unique_ptr<StorageImpl> storage(new (std::nothrow) StorageImpl); if (!storage) { @@ -240,5 +252,30 @@ bool StorageImpl::open_or_create_storage(const char *path, return false; } +bool StorageImpl::prepare_files_and_chunks(const StorageOptions &options) { + files_.reset( + new (std::nothrow) std::unique_ptr<File>[options.max_num_files]); + if (!files_) { + GRNXX_ERROR() << "new std::unique_ptr<grnxx::storage::File>[] failed: " + << "size = " << options.max_num_files; + return false; + } + node_header_chunks_.reset( + new (std::nothrow) std::unique_ptr<Chunk>[MAX_NUM_NODE_HEADER_CHUNKS]); + if (!node_header_chunks_) { + GRNXX_ERROR() << "new std::unique_ptr<grnxx::storage::Chunk>[] failed: " + << "size = " << MAX_NUM_NODE_HEADER_CHUNKS; + return false; + } + node_body_chunks_.reset( + new (std::nothrow) std::unique_ptr<Chunk>[MAX_NUM_NODE_BODY_CHUNKS]); + if (!node_header_chunks_) { + GRNXX_ERROR() << "new std::unique_ptr<grnxx::storage::Chunk>[] failed: " + << "size = " << MAX_NUM_NODE_BODY_CHUNKS; + return false; + } + return true; +} + } // namespace storage } // namespace grnxx Modified: lib/grnxx/storage/storage_impl.hpp (+10 -5) =================================================================== --- lib/grnxx/storage/storage_impl.hpp 2013-04-24 17:19:23 +0900 (895de63) +++ lib/grnxx/storage/storage_impl.hpp 2013-04-24 17:33:19 +0900 (ca14564) @@ -25,7 +25,8 @@ namespace storage { struct Header; class File; -class View; +class Chunk; +struct ChunkIndex; class StorageImpl : public Storage { public: @@ -60,10 +61,12 @@ class StorageImpl : public Storage { std::unique_ptr<char[]> path_; StorageFlags flags_; Header *header_; - std::unique_ptr<std::unique_ptr<File>> files_; - std::unique_ptr<View> header_view_; - std::unique_ptr<std::unique_ptr<View>> node_header_views_; - std::unique_ptr<std::unique_ptr<View>> node_body_views_; + ChunkIndex *node_header_chunk_indexes_; + ChunkIndex *node_body_chunk_indexes_; + std::unique_ptr<std::unique_ptr<File>[]> files_; + std::unique_ptr<Chunk> header_chunk_; + std::unique_ptr<std::unique_ptr<Chunk>[]> node_header_chunks_; + std::unique_ptr<std::unique_ptr<Chunk>[]> node_body_chunks_; bool create_persistent_storage(const char *path, StorageFlags flags, const StorageOptions &options); @@ -74,6 +77,8 @@ class StorageImpl : public Storage { bool open_storage(const char *path, StorageFlags flags); bool open_or_create_storage(const char *path, StorageFlags flags, const StorageOptions &options); + + bool prepare_files_and_chunks(const StorageOptions &options); }; } // namespace storage Modified: test/test_storage.cpp (+91 -91) =================================================================== --- test/test_storage.cpp 2013-04-24 17:19:23 +0900 (a73ee89) +++ test/test_storage.cpp 2013-04-24 17:33:19 +0900 (713570b) @@ -20,7 +20,7 @@ #include "grnxx/storage/file.hpp" #include "grnxx/storage/path.hpp" -#include "grnxx/storage/view.hpp" +#include "grnxx/storage/chunk.hpp" #include "grnxx/logger.hpp" namespace { @@ -234,138 +234,138 @@ void test_file_handle() { assert(file->handle()); } -void test_view_create() { +void test_chunk_create() { std::unique_ptr<grnxx::storage::File> file; - std::unique_ptr<grnxx::storage::View> view; + std::unique_ptr<grnxx::storage::Chunk> chunk; file.reset(grnxx::storage::File::create(nullptr)); assert(file); - view.reset(grnxx::storage::View::create(file.get())); - assert(!view); + chunk.reset(grnxx::storage::Chunk::create(file.get())); + assert(!chunk); assert(file->resize(1 << 20)); - view.reset(grnxx::storage::View::create(file.get())); - assert(view); - view.reset(grnxx::storage::View::create(file.get(), 0)); - assert(view); - view.reset(grnxx::storage::View::create(file.get(), 0, -1)); - assert(view); - view.reset(grnxx::storage::View::create(file.get(), 0, file->size())); - assert(view); - view.reset(grnxx::storage::View::create(file.get(), 0, 10)); - assert(view); - - view.reset(grnxx::storage::View::create(file.get(), -1)); - assert(!view); - view.reset(grnxx::storage::View::create(file.get(), file->size() + 1)); - assert(!view); - view.reset(grnxx::storage::View::create(file.get(), 0, 0)); - assert(!view); - view.reset(grnxx::storage::View::create(file.get(), 0, file->size() + 1)); - assert(!view); - view.reset(grnxx::storage::View::create(file.get(), file->size() / 2, - file->size())); - assert(!view); - - view.reset(grnxx::storage::View::create(nullptr, 0, 1 << 20)); - assert(view); - - view.reset(grnxx::storage::View::create(nullptr, 0, 0)); - assert(!view); - view.reset(grnxx::storage::View::create(nullptr, 0, -1)); - assert(!view); + 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, -1)); + 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, 0)); + 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_view_sync() { +void test_chunk_sync() { std::unique_ptr<grnxx::storage::File> file; - std::unique_ptr<grnxx::storage::View> view; + std::unique_ptr<grnxx::storage::Chunk> chunk; file.reset(grnxx::storage::File::create(nullptr)); assert(file); assert(file->resize(1 << 20)); - view.reset(grnxx::storage::View::create(file.get())); - assert(view); - assert(view->sync()); - assert(view->sync(0)); - assert(view->sync(0, -1)); - assert(view->sync(0, 0)); - assert(view->sync(0, file->size())); - - assert(!view->sync(-1)); - assert(!view->sync(file->size() + 1)); - assert(!view->sync(0, file->size() + 1)); - assert(!view->sync(file->size() / 2, file->size())); - - view.reset(grnxx::storage::View::create(nullptr, 0, 1 << 20)); - assert(view); - assert(!view->sync()); + chunk.reset(grnxx::storage::Chunk::create(file.get())); + assert(chunk); + assert(chunk->sync()); + assert(chunk->sync(0)); + assert(chunk->sync(0, -1)); + assert(chunk->sync(0, 0)); + assert(chunk->sync(0, file->size())); + + assert(!chunk->sync(-1)); + 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()); } -void test_view_flags() { +void test_chunk_flags() { const char FILE_PATH[] = "temp.grn"; grnxx::storage::File::unlink(FILE_PATH); std::unique_ptr<grnxx::storage::File> file; - std::unique_ptr<grnxx::storage::View> view; + std::unique_ptr<grnxx::storage::Chunk> chunk; file.reset(grnxx::storage::File::create(FILE_PATH)); assert(file); assert(file->resize(1 << 20)); - view.reset(grnxx::storage::View::create(file.get())); - assert(view); - assert(view->flags() == grnxx::storage::VIEW_DEFAULT); + 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); - view.reset(grnxx::storage::View::create(file.get())); - assert(view); - assert(view->flags() == grnxx::storage::VIEW_READ_ONLY); + chunk.reset(grnxx::storage::Chunk::create(file.get())); + assert(chunk); + assert(chunk->flags() == grnxx::storage::CHUNK_READ_ONLY); file.reset(); assert(grnxx::storage::File::unlink(FILE_PATH)); } -void test_view_address() { +void test_chunk_address() { std::unique_ptr<grnxx::storage::File> file; - std::unique_ptr<grnxx::storage::View> view; + std::unique_ptr<grnxx::storage::Chunk> chunk; file.reset(grnxx::storage::File::create(nullptr)); assert(file); assert(file->resize(10)); - view.reset(grnxx::storage::View::create(file.get())); - assert(view); - std::memcpy(view->address(), "0123456789", 10); - view.reset(grnxx::storage::View::create(file.get())); - assert(view); - assert(std::memcmp(view->address(), "0123456789", 10) == 0); + 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); } -void test_view_size() { +void test_chunk_size() { std::unique_ptr<grnxx::storage::File> file; - std::unique_ptr<grnxx::storage::View> view; + std::unique_ptr<grnxx::storage::Chunk> chunk; file.reset(grnxx::storage::File::create(nullptr)); assert(file); assert(file->resize(1 << 20)); - view.reset(grnxx::storage::View::create(file.get())); - assert(view); - assert(view->size() == file->size()); - view.reset(grnxx::storage::View::create(file.get(), file->size() / 2)); - assert(view); - assert(view->size() == (file->size() / 2)); - view.reset(grnxx::storage::View::create(file.get(), 0, file->size() / 2)); - assert(view); - assert(view->size() == (file->size() / 2)); - - view.reset(grnxx::storage::View::create(nullptr, 0, 1 << 20)); - assert(view); - assert(view->size() == (1 << 20)); + 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)); } void test_path() { @@ -386,12 +386,12 @@ void test_file() { test_file_handle(); } -void test_view() { - test_view_create(); - test_view_sync(); - test_view_flags(); - test_view_address(); - test_view_size(); +void test_chunk() { + test_chunk_create(); + test_chunk_sync(); + test_chunk_flags(); + test_chunk_address(); + test_chunk_size(); } } // namespace @@ -403,7 +403,7 @@ int main() { test_path(); test_file(); - test_view(); + test_chunk(); return 0; } -------------- next part -------------- HTML����������������������������...Download