Navigation Menu

Skip to content

Commit

Permalink
Add skeletons of Patricia and DoubleArray.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yata committed Jun 5, 2013
1 parent 4f5ba2c commit 10d9ce7
Show file tree
Hide file tree
Showing 7 changed files with 490 additions and 6 deletions.
10 changes: 6 additions & 4 deletions lib/grnxx/map.cpp
Expand Up @@ -26,9 +26,11 @@
#include "grnxx/string_builder.hpp"
#include "grnxx/map/array_map.hpp"
#include "grnxx/map/cursor_impl.hpp"
#include "grnxx/map/double_array.hpp"
#include "grnxx/map/hash_table.hpp"
#include "grnxx/map/header.hpp"
#include "grnxx/map/helper.hpp"
#include "grnxx/map/patricia.hpp"
#include "grnxx/map/scanner_impl.hpp"

namespace grnxx {
Expand Down Expand Up @@ -73,10 +75,10 @@ Map<T> *Map<T>::create(MapType type, Storage *storage,
return map::ArrayMap<T>::create(storage, storage_node_id, options);
}
case MAP_DOUBLE_ARRAY: {
// TODO: Not supported yet.
return map::DoubleArray<T>::create(storage, storage_node_id, options);
}
case MAP_PATRICIA: {
// TODO: Not supported yet.
return map::Patricia<T>::create(storage, storage_node_id, options);
}
case MAP_HASH_TABLE: {
return map::HashTable<T>::create(storage, storage_node_id, options);
Expand Down Expand Up @@ -105,10 +107,10 @@ Map<T> *Map<T>::open(Storage *storage, uint32_t storage_node_id) {
return map::ArrayMap<T>::open(storage, storage_node_id);
}
case MAP_DOUBLE_ARRAY: {
// TODO: Not supported yet.
return map::DoubleArray<T>::open(storage, storage_node_id);
}
case MAP_PATRICIA: {
// TODO: Not supported yet.
return map::Patricia<T>::open(storage, storage_node_id);
}
case MAP_HASH_TABLE: {
return map::HashTable<T>::open(storage, storage_node_id);
Expand Down
4 changes: 2 additions & 2 deletions lib/grnxx/map.hpp
Expand Up @@ -38,9 +38,9 @@ constexpr int64_t MAP_INVALID_KEY_ID = MAP_MAX_KEY_ID + 1;

enum MapType : uint32_t {
MAP_ARRAY = 0, // Array-based implementation.
MAP_DOUBLE_ARRAY = 1, // TODO: DoubleArray-based implementation.
MAP_HASH_TABLE = 1, // HashTable-based implementation.
MAP_PATRICIA = 2, // TODO: Patricia-based implementation.
MAP_HASH_TABLE = 3 // TODO: HashTable-based implementation.
MAP_DOUBLE_ARRAY = 3 // TODO: DoubleArray-based implementation.
};

StringBuilder &operator<<(StringBuilder &builder, MapType type);
Expand Down
4 changes: 4 additions & 0 deletions lib/grnxx/map/Makefile.am
Expand Up @@ -15,7 +15,9 @@ libgrnxx_map_la_SOURCES = \
bytes_array.cpp \
bytes_store.cpp \
cursor_impl.cpp \
double_array.cpp \
hash_table.cpp \
patricia.cpp \
scanner_impl.cpp

libgrnxx_map_includedir = ${includedir}/grnxx/map
Expand All @@ -24,7 +26,9 @@ libgrnxx_map_include_HEADERS = \
bytes_array.hpp \
bytes_store.hpp \
cursor_impl.hpp \
double_array.hpp \
hash_table.hpp \
header.hpp \
helper.hpp \
patricia.hpp \
scanner_impl.hpp
149 changes: 149 additions & 0 deletions lib/grnxx/map/double_array.cpp
@@ -0,0 +1,149 @@
/*
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/map/double_array.hpp"

#include <new>

#include "grnxx/bytes.hpp"
#include "grnxx/geo_point.hpp"
#include "grnxx/logger.hpp"
#include "grnxx/map/helper.hpp"
#include "grnxx/storage.hpp"

namespace grnxx {
namespace map {

struct DoubleArrayHeader {
// TODO
int64_t max_key_id;
uint64_t num_keys;

DoubleArrayHeader();
};

DoubleArrayHeader::DoubleArrayHeader()
: max_key_id(MAP_MIN_KEY_ID - 1),
num_keys(0) {}

template <typename T>
Map<T> *DoubleArray<T>::create(Storage *, uint32_t, const MapOptions &) {
GRNXX_ERROR() << "invalid combination";
return nullptr;
}

template <typename T>
Map<T> *DoubleArray<T>::open(Storage *, uint32_t) {
GRNXX_ERROR() << "invalid combination";
return nullptr;
}

template class DoubleArray<int8_t>;
template class DoubleArray<uint8_t>;
template class DoubleArray<int16_t>;
template class DoubleArray<uint16_t>;
template class DoubleArray<int32_t>;
template class DoubleArray<uint32_t>;
template class DoubleArray<int64_t>;
template class DoubleArray<uint64_t>;
template class DoubleArray<double>;
template class DoubleArray<GeoPoint>;

DoubleArray<Bytes>::DoubleArray()
: storage_(nullptr),
storage_node_id_(STORAGE_INVALID_NODE_ID),
header_(nullptr) {}

DoubleArray<Bytes>::~DoubleArray() {}

DoubleArray<Bytes> *DoubleArray<Bytes>::create(Storage *storage,
uint32_t storage_node_id,
const MapOptions &options) {
std::unique_ptr<DoubleArray> map(new (std::nothrow) DoubleArray);
if (!map) {
GRNXX_ERROR() << "new grnxx::map::DoubleArray failed";
return nullptr;
}
if (!map->create_map(storage, storage_node_id, options)) {
return nullptr;
}
return map.release();
}

DoubleArray<Bytes> *DoubleArray<Bytes>::open(Storage *storage,
uint32_t storage_node_id) {
std::unique_ptr<DoubleArray> map(new (std::nothrow) DoubleArray);
if (!map) {
GRNXX_ERROR() << "new grnxx::map::DoubleArray failed";
return nullptr;
}
if (!map->open_map(storage, storage_node_id)) {
return nullptr;
}
return map.release();
}

uint32_t DoubleArray<Bytes>::storage_node_id() const {
return storage_node_id_;
}

MapType DoubleArray<Bytes>::type() const {
return MAP_DOUBLE_ARRAY;
}

int64_t DoubleArray<Bytes>::max_key_id() const {
return header_->max_key_id;
}

uint64_t DoubleArray<Bytes>::num_keys() const {
return header_->num_keys;
}

bool DoubleArray<Bytes>::create_map(Storage *storage, uint32_t storage_node_id,
const MapOptions &) {
storage_ = storage;
StorageNode storage_node =
storage->create_node(storage_node_id, sizeof(DoubleArrayHeader));
if (!storage_node) {
return false;
}
storage_node_id_ = storage_node.id();
header_ = static_cast<DoubleArrayHeader *>(storage_node.body());
*header_ = DoubleArrayHeader();
// TODO
return false;
}

bool DoubleArray<Bytes>::open_map(Storage *storage, uint32_t storage_node_id) {
storage_ = storage;
StorageNode storage_node = storage->open_node(storage_node_id);
if (!storage_node) {
return false;
}
if (storage_node.size() < sizeof(DoubleArrayHeader)) {
GRNXX_ERROR() << "invalid format: size = " << storage_node.size()
<< ", header_size = " << sizeof(DoubleArrayHeader);
return false;
}
storage_node_id_ = storage_node_id;
header_ = static_cast<DoubleArrayHeader *>(storage_node.body());
// TODO
return false;
}

} // namespace map
} // namespace grnxx
103 changes: 103 additions & 0 deletions lib/grnxx/map/double_array.hpp
@@ -0,0 +1,103 @@
/*
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_MAP_DOUBLE_ARRAY_HPP
#define GRNXX_MAP_DOUBLE_ARRAY_HPP

#include "grnxx/features.hpp"

#include <memory>

#include "grnxx/bytes.hpp"
#include "grnxx/map.hpp"
#include "grnxx/map_cursor.hpp"
#include "grnxx/map_cursor_query.hpp"
#include "grnxx/types.hpp"

namespace grnxx {
namespace map {

struct DoubleArrayHeader;

template <typename T>
class DoubleArray {
public:
static Map<T> *create(Storage *storage, uint32_t storage_node_id,
const MapOptions &options = MapOptions());
static Map<T> *open(Storage *storage, uint32_t storage_node_id);
};

template <>
class DoubleArray<Bytes> : public Map<Bytes> {
public:
using Key = typename Map<Bytes>::Key;
using KeyArg = typename Map<Bytes>::KeyArg;
using Cursor = typename Map<Bytes>::Cursor;

DoubleArray();
~DoubleArray();

static DoubleArray *create(Storage *storage, uint32_t storage_node_id,
const MapOptions &options = MapOptions());
static DoubleArray *open(Storage *storage, uint32_t storage_node_id);

uint32_t storage_node_id() const;
MapType type() const;

int64_t max_key_id() const;
uint64_t num_keys() const;

// TODO
// bool get(int64_t key_id, Key *key = nullptr);
// bool unset(int64_t key_id);
// bool reset(int64_t key_id, KeyArg dest_key);

// bool find(KeyArg key, int64_t *key_id = nullptr);
// bool add(KeyArg key, int64_t *key_id = nullptr);
// bool remove(KeyArg key);
// bool replace(KeyArg src_key, KeyArg dest_key, int64_t *key_id = nullptr);

// bool truncate();

// bool find_longest_prefix_match(KeyArg query,
// int64_t *key_id = nullptr,
// Key *key = nullptr);

// Cursor *create_cursor(
// MapCursorAllKeys<T> query,
// const MapCursorOptions &options = MapCursorOptions());
// Cursor *create_cursor(
// const MapCursorKeyIDRange<T> &query,
// const MapCursorOptions &options = MapCursorOptions());
// Cursor *create_cursor(
// const MapCursorKeyRange<T> &query,
// const MapCursorOptions &options = MapCursorOptions());

private:
Storage *storage_;
uint32_t storage_node_id_;
DoubleArrayHeader *header_;

bool create_map(Storage *storage, uint32_t storage_node_id,
const MapOptions &options);
bool open_map(Storage *storage, uint32_t storage_node_id);
};

} // namespace map
} // namespace grnxx

#endif // GRNXX_MAP_DOUBLE_ARRAY_HPP

0 comments on commit 10d9ce7

Please sign in to comment.