Navigation Menu

Skip to content

Commit

Permalink
Add a cache mechanism to grnxx::map::Patricia (but commented out).
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yata committed Jun 27, 2013
1 parent a9949d9 commit d55c7ad
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
42 changes: 39 additions & 3 deletions lib/grnxx/map/patricia.cpp
Expand Up @@ -22,6 +22,7 @@
#include "grnxx/bytes.hpp"
#include "grnxx/geo_point.hpp"
#include "grnxx/logger.hpp"
#include "grnxx/map/hash_table/hash.hpp"
#include "grnxx/map/helper.hpp"
#include "grnxx/map/patricia/header.hpp"
#include "grnxx/storage.hpp"
Expand Down Expand Up @@ -470,7 +471,8 @@ Patricia<Bytes>::Patricia()
storage_node_id_(STORAGE_INVALID_NODE_ID),
header_(nullptr),
nodes_(),
keys_() {}
keys_(),
cache_() {}

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

Expand Down Expand Up @@ -648,6 +650,22 @@ bool Patricia<Bytes>::find(KeyArg key, int64_t *key_id) {
}

bool Patricia<Bytes>::add(KeyArg key, int64_t *key_id) {
// int64_t * const cache =
// cache_->get_pointer(hash_table::Hash<Key>()(key) % cache_->size());
// {
// if ((*cache >= 0) && (*cache < keys_->max_key_id())) {
// bool bit;
// if (keys_->get_bit(*cache, &bit) && bit) {
// Key cached_key;
// if (keys_->get_key(*cache, &cached_key) && (key == cached_key)) {
// if (key_id) {
// *key_id = *cache;
// }
// return false;
// }
// }
// }
// }
uint64_t node_id = ROOT_NODE_ID;
Node *node = nodes_->get_pointer(node_id);
if (!node) {
Expand All @@ -665,6 +683,9 @@ bool Patricia<Bytes>::add(KeyArg key, int64_t *key_id) {
if (key_id) {
*key_id = next_key_id;
}
// if (cache) {
// *cache = next_key_id;
// }
return true;
}
const uint64_t bit_size = key.size() * 8;
Expand Down Expand Up @@ -719,6 +740,9 @@ bool Patricia<Bytes>::add(KeyArg key, int64_t *key_id) {
if (key_id) {
*key_id = node->key_id();
}
// if (cache) {
// *cache = node->key_id();
// }
return false;
}
node = history[depth % 8];
Expand Down Expand Up @@ -746,6 +770,9 @@ bool Patricia<Bytes>::add(KeyArg key, int64_t *key_id) {
if (key_id) {
*key_id = next_key_id;
}
// if (cache) {
// *cache = next_key_id;
// }
return true;
}
count = (count * 8) + 7 - bit_scan_reverse(key[count] ^ stored_key[count]);
Expand Down Expand Up @@ -785,6 +812,9 @@ bool Patricia<Bytes>::add(KeyArg key, int64_t *key_id) {
if (key_id) {
*key_id = next_key_id;
}
// if (cache) {
// *cache = next_key_id;
// }
return true;
}
// Find the branching point with the naive method.
Expand Down Expand Up @@ -832,6 +862,9 @@ bool Patricia<Bytes>::add(KeyArg key, int64_t *key_id) {
if (key_id) {
*key_id = next_key_id;
}
// if (cache) {
// *cache = next_key_id;
// }
return true;
}

Expand Down Expand Up @@ -992,12 +1025,14 @@ bool Patricia<Bytes>::create_map(Storage *storage, uint32_t storage_node_id,
*header_ = Header();
nodes_.reset(NodeArray::create(storage, storage_node_id_));
keys_.reset(KeyStore<Bytes>::create(storage, storage_node_id_));
if (!nodes_ || !keys_) {
cache_.reset(Cache::create(storage, storage_node_id_, -1));
if (!nodes_ || !keys_ || !cache_) {
storage->unlink_node(storage_node_id_);
return false;
}
header_->nodes_storage_node_id = nodes_->storage_node_id();
header_->keys_storage_node_id = keys_->storage_node_id();
header_->cache_storage_node_id = cache_->storage_node_id();
Node * const root_node = nodes_->get_pointer(ROOT_NODE_ID);
if (!root_node) {
storage->unlink_node(storage_node_id_);
Expand All @@ -1023,7 +1058,8 @@ bool Patricia<Bytes>::open_map(Storage *storage, uint32_t storage_node_id) {
// TODO: Check the format.
nodes_.reset(NodeArray::open(storage, header_->nodes_storage_node_id));
keys_.reset(KeyStore<Bytes>::open(storage, header_->keys_storage_node_id));
if (!nodes_ || !keys_) {
cache_.reset(Cache::open(storage, header_->cache_storage_node_id));
if (!nodes_ || !keys_ || !cache_) {
return false;
}
return true;
Expand Down
2 changes: 2 additions & 0 deletions lib/grnxx/map/patricia.hpp
Expand Up @@ -94,6 +94,7 @@ template <>
class Patricia<Bytes> : public Map<Bytes> {
using Node = patricia::Node;
using NodeArray = Array<Node>;
using Cache = Array<int64_t, 1 << 20, 1, 1>;

public:
using Header = patricia::Header;
Expand Down Expand Up @@ -134,6 +135,7 @@ class Patricia<Bytes> : public Map<Bytes> {
Header *header_;
std::unique_ptr<NodeArray> nodes_;
std::unique_ptr<KeyStore<Bytes>> keys_;
std::unique_ptr<Cache> cache_;

bool create_map(Storage *storage, uint32_t storage_node_id,
const MapOptions &options);
Expand Down
3 changes: 2 additions & 1 deletion lib/grnxx/map/patricia/header.cpp
Expand Up @@ -27,7 +27,8 @@ Header::Header()
: map_type(MAP_PATRICIA),
next_node_id(2),
nodes_storage_node_id(STORAGE_INVALID_NODE_ID),
keys_storage_node_id(STORAGE_INVALID_NODE_ID) {}
keys_storage_node_id(STORAGE_INVALID_NODE_ID),
cache_storage_node_id(STORAGE_INVALID_NODE_ID) {}

} // namespace patricia
} // namespace map
Expand Down
1 change: 1 addition & 0 deletions lib/grnxx/map/patricia/header.hpp
Expand Up @@ -32,6 +32,7 @@ struct Header {
uint64_t next_node_id;
uint32_t nodes_storage_node_id;
uint32_t keys_storage_node_id;
uint32_t cache_storage_node_id;

Header();
};
Expand Down

0 comments on commit d55c7ad

Please sign in to comment.