Skip to content

Commit

Permalink
Add an alpha version of Array implementations.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yata committed Jul 9, 2013
1 parent dc5fd18 commit cf70a4c
Show file tree
Hide file tree
Showing 5 changed files with 962 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/grnxx/Makefile.am
Expand Up @@ -15,6 +15,8 @@ libgrnxx_la_LDFLAGS = @AM_LTLDFLAGS@
libgrnxx_la_SOURCES = \
array.cpp \
array_impl.cpp \
array2.cpp \
array2_impl.cpp \
backtrace.cpp \
broken_down_time.cpp \
charset.cpp \
Expand All @@ -41,6 +43,8 @@ libgrnxx_includedir = ${includedir}/grnxx
libgrnxx_include_HEADERS = \
array.hpp \
array_impl.hpp \
array2.hpp \
array2_impl.hpp \
backtrace.hpp \
broken_down_time.hpp \
bytes.hpp \
Expand Down
32 changes: 32 additions & 0 deletions lib/grnxx/array2.cpp
@@ -0,0 +1,32 @@
/*
Copyright (C) 2012-2013 Brazil, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
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/array2.hpp"

#include "grnxx/exception.hpp"
#include "grnxx/logger.hpp"

namespace grnxx {
namespace alpha {

void ArrayErrorHandler::throw_memory_error() {
GRNXX_ERROR() << "new grnxx::Array failed";
throw MemoryError();
}

} // namespace alpha
} // namespace grnxx
124 changes: 124 additions & 0 deletions lib/grnxx/array2.hpp
@@ -0,0 +1,124 @@
/*
Copyright (C) 2012-2013 Brazil, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
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_ARRAY2_HPP
#define GRNXX_ARRAY2_HPP

#include "grnxx/features.hpp"

#include <memory>
#include <new>

#include "grnxx/array2_impl.hpp"
#include "grnxx/types.hpp"

namespace grnxx {

class Storage;

namespace alpha {

struct ArrayErrorHandler {
static void throw_memory_error();
};

template <typename T, uint64_t PAGE_SIZE = 0, uint64_t TABLE_SIZE = 0>
class Array {
// Test template parameters.
static_assert((PAGE_SIZE != 0) || (TABLE_SIZE == 0),
"TABLE_SIZE must be zero if PAGE_SIZE is zero");
static_assert((PAGE_SIZE & (PAGE_SIZE - 1)) == 0,
"PAGE_SIZE must be zero or a power of two");
static_assert((TABLE_SIZE & (TABLE_SIZE - 1)) == 0,
"TABLE_SIZE must be zero or a power of two");

using Impl = ArrayImpl<T, PAGE_SIZE, TABLE_SIZE>;

public:
using Value = typename Impl::Value;
using ValueArg = typename Impl::ValueArg;

~Array() {}

// Create an array.
static Array *create(Storage *storage, uint32_t storage_node_id,
uint64_t size) {
std::unique_ptr<Array> array(create_instance());
array->impl_.create(storage, storage_node_id, size);
return array.release();
}
// Create an array with default value.
static Array *create(Storage *storage, uint32_t storage_node_id,
uint64_t size, ValueArg default_value) {
std::unique_ptr<Array> array(create_instance());
array->impl_.create(storage, storage_node_id, size, default_value);
return array.release();
}

// Open an array.
static Array *open(Storage *storage, uint32_t storage_node_id) {
std::unique_ptr<Array> array(create_instance());
array->impl_.open(storage, storage_node_id);
return array.release();
}

// Unlink an array.
static bool unlink(Storage *storage, uint32_t storage_node_id) {
return Impl::unlink(storage, storage_node_id);
}

// Return the storage node ID.
uint32_t storage_node_id() const {
return impl_.storage_node_id();
}
// Return the number of values.
uint64_t size() const {
return impl_.size();
}

// Get a reference to a value.
Value &operator[](uint64_t value_id) {
return *impl_.get_value(value_id);
}
// Get a value.
Value get(uint64_t value_id) {
return *impl_.get_value(value_id);
}
// Set a value.
void set(uint64_t value_id, ValueArg value) {
*impl_.get_value(value_id) = value;
}

private:
Impl impl_;

Array() : impl_() {}

// Create an instance or throw an exception on failure.
static Array *create_instance() {
Array * const array = new (std::nothrow) Array;
if (!array) {
ArrayErrorHandler::throw_memory_error();
}
return array;
}
};

} // namespace alpha
} // namespace grnxx

#endif // GRNXX_ARRAY2_HPP

0 comments on commit cf70a4c

Please sign in to comment.