Navigation Menu

Skip to content

Commit

Permalink
Add components of grnxx::map::DoubleArray.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yata committed Jun 6, 2013
1 parent 5379a50 commit 5d30b5d
Show file tree
Hide file tree
Showing 7 changed files with 425 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/grnxx/map/double_array/Makefile.am
Expand Up @@ -8,5 +8,8 @@ libgrnxx_map_double_array_la_SOURCES = \

libgrnxx_map_double_array_includedir = ${includedir}/grnxx/map/double_array
libgrnxx_map_double_array_include_HEADERS = \
block.hpp \
dummy.hpp \
header.hpp
entry.hpp \
header.hpp \
node.hpp
122 changes: 122 additions & 0 deletions lib/grnxx/map/double_array/block.hpp
@@ -0,0 +1,122 @@
/*
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_BLOCK_HPP
#define GRNXX_MAP_DOUBLE_ARRAY_BLOCK_HPP

#include "grnxx/features.hpp"

#include "grnxx/types.hpp"

namespace grnxx {
namespace map {
namespace double_array {

constexpr uint64_t BLOCK_MAX_FAILURE_COUNT = 4;
constexpr uint64_t BLOCK_MAX_LEVEL = 5;
constexpr uint64_t BLOCK_INVALID_ID = (1ULL << 40) - 1;

// The internal structure is as follows:
// - values_[0]
// 0-15 (16): first_phantom
// 16-23 ( 8): level
// 24-63 (40): next
// - values_[1]
// 0-15 (16): num_phantoms
// 16-23 ( 8): failure_count
// 24-63 (40): prev
// where 0 is the LSB and 63 is the MSB.

class Block {
// For values_[0].
static constexpr uint64_t FIRST_PHANTOM_MASK = (1ULL << 16) - 1;
static constexpr uint8_t FIRST_PHANTOM_SHIFT = 0;
static constexpr uint64_t LEVEL_MASK = (1ULL << 8) - 1;
static constexpr uint8_t LEVEL_SHIFT = 16;
static constexpr uint64_t NEXT_MASK = (1ULL << 40) - 1;
static constexpr uint8_t NEXT_SHIFT = 24;

// For values_[1].
static constexpr uint64_t NUM_PHANTOMS_MASK = (1ULL << 16) - 1;
static constexpr uint8_t NUM_PHANTOMS_SHIFT = 0;
static constexpr uint64_t FAILURE_COUNT_MASK = (1ULL << 8) - 1;
static constexpr uint8_t FAILURE_COUNT_SHIFT = 16;
static constexpr uint64_t PREV_MASK = (1ULL << 40) - 1;
static constexpr uint8_t PREV_SHIFT = 24;

public:
// Return the first phantom node.
uint64_t first_phantom() const {
return (values_[0] >> FIRST_PHANTOM_SHIFT) & FIRST_PHANTOM_MASK;
}
// Return the level.
uint64_t level() const {
return (values_[0] >> LEVEL_SHIFT) & LEVEL_MASK;
}
// Return the next block ID of the same level.
uint64_t next() const {
return (values_[0] >> NEXT_SHIFT) & NEXT_MASK;
}

// Return the number of phantom nodes.
uint64_t num_phantoms() const {
return (values_[1] >> NUM_PHANTOMS_SHIFT) & NUM_PHANTOMS_MASK;
}
// Return the failure count.
uint64_t failure_count() const {
return (values_[1] >> FAILURE_COUNT_SHIFT) & FAILURE_COUNT_MASK;
}
// Return the previous block ID of the same level.
uint64_t prev() const {
return (values_[1] >> PREV_SHIFT) & PREV_MASK;
}

void set_first_phantom(uint64_t first_phantom) {
values_[0] = (values_[0] & ~(FIRST_PHANTOM_MASK << FIRST_PHANTOM_SHIFT)) |
((first_phantom & FIRST_PHANTOM_MASK) << FIRST_PHANTOM_SHIFT);
}
void set_level(uint64_t level) {
values_[0] = (values_[0] & ~(LEVEL_MASK << LEVEL_SHIFT)) |
((level & LEVEL_MASK) << LEVEL_SHIFT);
}
void set_next(uint64_t next) {
values_[0] = (values_[0] & ~(NEXT_MASK << NEXT_SHIFT)) |
((next & NEXT_MASK) << NEXT_SHIFT);
}

void set_num_phantoms(uint64_t num_phantoms) {
values_[1] = (values_[1] & ~(NUM_PHANTOMS_MASK << NUM_PHANTOMS_SHIFT)) |
((num_phantoms & NUM_PHANTOMS_MASK) << NUM_PHANTOMS_SHIFT);
}
void set_failure_count(uint64_t failure_count) {
values_[1] = (values_[1] & ~(FAILURE_COUNT_MASK << FAILURE_COUNT_SHIFT)) |
((failure_count & FAILURE_COUNT_MASK) << FAILURE_COUNT_SHIFT);
}
void set_prev(uint64_t prev) {
values_[1] = (values_[1] & ~(PREV_MASK << PREV_SHIFT)) |
((prev & PREV_MASK) << PREV_SHIFT);
}

private:
uint64_t values_[2];
};

} // namespace double_array
} // namespace map
} // namespace grnxx

#endif // GRNXX_MAP_DOUBLE_ARRAY_BLOCK_HPP
3 changes: 3 additions & 0 deletions lib/grnxx/map/double_array/dummy.cpp
Expand Up @@ -17,7 +17,10 @@
*/
#include "grnxx/map/double_array/dummy.hpp"

#include "grnxx/map/double_array/block.hpp"
#include "grnxx/map/double_array/entry.hpp"
#include "grnxx/map/double_array/header.hpp"
#include "grnxx/map/double_array/node.hpp"

namespace grnxx {
namespace map {
Expand Down
73 changes: 73 additions & 0 deletions lib/grnxx/map/double_array/entry.hpp
@@ -0,0 +1,73 @@
/*
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_ENTRY_HPP
#define GRNXX_MAP_DOUBLE_ARRAY_ENTRY_HPP

#include "grnxx/features.hpp"

#include "grnxx/types.hpp"

namespace grnxx {
namespace map {
namespace double_array {

// The internal structure is as follows:
// - Common
// 63 ( 1): is_valid
// - Valid: is_valid
// 0-62 (63): bytes_id
// - Invalid: !is_valid
// 0-62 (63): next
// where 0 is the LSB and 63 is the MSB.

class Entry {
static constexpr uint64_t IS_VALID_FLAG = 1ULL << 63;

public:
static Entry valid_entry(uint64_t bytes_id) {
return Entry(IS_VALID_FLAG | bytes_id);
}
static Entry invalid_entry(uint64_t next) {
return Entry(next);
}

// Return true iff the entry is associated with a key.
explicit operator bool() const {
return value_ & IS_VALID_FLAG;
}

// Return the ID of the associated byte sequence.
uint64_t bytes_id() const {
return value_ & ~IS_VALID_FLAG;
}
// Return the next invalid entry.
uint64_t next() const {
return value_;
}

private:
uint64_t value_;

explicit Entry(uint64_t value) : value_(value) {}
};

} // namespace double_array
} // namespace map
} // namespace grnxx

#endif // GRNXX_MAP_DOUBLE_ARRAY_ENTRY_HPP
16 changes: 15 additions & 1 deletion lib/grnxx/map/double_array/header.cpp
Expand Up @@ -26,7 +26,21 @@ namespace double_array {
Header::Header()
: map_type(MAP_ARRAY),
max_key_id(MAP_MIN_KEY_ID - 1),
num_keys(0) {}
num_keys(0),
nodes_storage_node_id(STORAGE_INVALID_NODE_ID),
siblings_storage_node_id(STORAGE_INVALID_NODE_ID),
blocks_storage_node_id(STORAGE_INVALID_NODE_ID),
entries_storage_node_id(STORAGE_INVALID_NODE_ID),
store_storage_node_id(STORAGE_INVALID_NODE_ID),
next_key_id(MAP_INVALID_KEY_ID),
num_blocks(0),
num_phantoms(0),
num_zombies(0),
latest_blocks() {
for (uint64_t i = 0; i <= BLOCK_MAX_LEVEL; ++i) {
latest_blocks[i] = BLOCK_INVALID_ID;
}
}

} // namespace double_array
} // namespace map
Expand Down
11 changes: 11 additions & 0 deletions lib/grnxx/map/double_array/header.hpp
Expand Up @@ -20,6 +20,7 @@

#include "grnxx/features.hpp"

#include "grnxx/map/double_array/block.hpp"
#include "grnxx/map.hpp"
#include "grnxx/types.hpp"

Expand All @@ -31,6 +32,16 @@ struct Header {
MapType map_type;
int64_t max_key_id;
uint64_t num_keys;
uint32_t nodes_storage_node_id;
uint32_t siblings_storage_node_id;
uint32_t blocks_storage_node_id;
uint32_t entries_storage_node_id;
uint32_t store_storage_node_id;
uint64_t next_key_id;
uint64_t num_blocks;
uint64_t num_phantoms;
uint64_t num_zombies;
uint64_t latest_blocks[BLOCK_MAX_LEVEL + 1];

Header();
};
Expand Down

0 comments on commit 5d30b5d

Please sign in to comment.