susumu.yata
null+****@clear*****
Mon Feb 25 10:40:58 JST 2013
susumu.yata 2013-02-25 10:40:58 +0900 (Mon, 25 Feb 2013) New Revision: c1293db75be4f9ab54d9c91732b90c554f200dd4 https://github.com/groonga/grnxx/commit/c1293db75be4f9ab54d9c91732b90c554f200dd4 Log: Update grnxx::Map to use grnxx::MapKey. Modified files: lib/Makefile.am lib/map.hpp lib/map/da/basic_trie.cpp lib/map/da/basic_trie.hpp lib/map/da/large_trie.cpp lib/map/da/large_trie.hpp lib/map/da/trie.hpp lib/map/double_array.cpp lib/map/double_array.hpp lib/slice.hpp test/test_map.cpp test/test_map_da_basic_trie.cpp test/test_map_da_large_trie.cpp test/test_map_double_array.cpp Modified: lib/Makefile.am (+1 -0) =================================================================== --- lib/Makefile.am 2013-02-22 17:14:56 +0900 (496a9bd) +++ lib/Makefile.am 2013-02-25 10:40:58 +0900 (6cbbc67) @@ -20,6 +20,7 @@ libgrnxx_la_SOURCES = \ mutex.cpp \ os.cpp \ recycler.cpp \ + slice.cpp \ string.cpp \ string_builder.cpp \ thread.cpp \ Modified: lib/map.hpp (+85 -3) =================================================================== --- lib/map.hpp 2013-02-22 17:14:56 +0900 (596a902) +++ lib/map.hpp 2013-02-25 10:40:58 +0900 (8a12364) @@ -27,7 +27,7 @@ namespace grnxx { enum MapType : int32_t { MAP_UNKNOWN = 0, - MAP_DOUBLE_ARRAY = 1 + MAP_DOUBLE_ARRAY = 1 // grnxx::map::DoubleArray. }; struct MapOptions { @@ -42,6 +42,88 @@ struct MapHeader { MapHeader(); }; +// TODO: Dynamic memory allocation will be supported. +class MapKey { + public: + // Create an empty (zero-size) key. + MapKey() : slice_() {} + // Create a key that refers to a zero-terminated string. + MapKey(const char *cstr) : slice_(cstr) {} + // Create a key. + MapKey(const void *ptr, size_t size) : slice_(ptr, size) {} + + // Disable copy. + MapKey(const MapKey &) = delete; + MapKey &operator=(const MapKey &) = delete; + + MapKey &operator=(const Slice &s) { + slice_ = s; + return *this; + } + + // Return true iff "*this" is not empty. + explicit operator bool() const { + return static_cast<bool>(slice_); + } + + // Make "*this" empty. + void clear() { + slice_.clear(); + } + + // Compare "*this" and "s" and return a negative value if "*this" < "s", + // zero if "*this" == "s", or a positive value otherwise (if "*this" > "s"). + int compare(const Slice &s) const { + return slice_.compare(s); + } + + // Return true iff "s" is a prefix of "*this". + bool starts_with(const Slice &s) const { + return slice_.starts_with(s); + } + // Return true iff "s" is a suffix of "*this". + bool ends_with(const Slice &s) const { + return slice_.ends_with(s); + } + + // Return the "i"-th byte of "*this". + uint8_t operator[](size_t i) const { + return slice_[i]; + } + + // Return the slice of "*this". + const Slice &slice() const { + return slice_; + } + // Return the starting address of "*this". + const void *address() const { + return slice_.address(); + } + // Return a pointer that refers to the starting address of "*this". + const uint8_t *ptr() const { + return slice_.ptr(); + } + // Return the size of "*this". + size_t size() const { + return slice_.size(); + } + + private: + Slice slice_; +}; + +inline bool operator==(const MapKey &lhs, const Slice &rhs) { + return lhs.slice() == rhs; +} + +inline bool operator!=(const MapKey &lhs, const Slice &rhs) { + return lhs.slice() != rhs; +} + +inline std::ostream &operator<<(std::ostream &stream, const MapKey &key) { + return stream << key.slice(); +} + class Map { public: Map(); @@ -54,11 +136,11 @@ class Map { virtual uint32_t block_id() const = 0; - virtual bool search(int64_t key_id, Slice *key = nullptr) = 0; + virtual bool search(int64_t key_id, MapKey *key = nullptr) = 0; virtual bool search(const Slice &key, int64_t *key_id = nullptr) = 0; virtual bool lcp_search(const Slice &query, int64_t *key_id = nullptr, - Slice *key = nullptr) = 0; + MapKey *key = nullptr) = 0; virtual bool insert(const Slice &key, int64_t *key_id = nullptr) = 0; Modified: lib/map/da/basic_trie.cpp (+2 -2) =================================================================== --- lib/map/da/basic_trie.cpp 2013-02-22 17:14:56 +0900 (af7345d) +++ lib/map/da/basic_trie.cpp 2013-02-25 10:40:58 +0900 (9bbab58) @@ -104,7 +104,7 @@ uint32_t Trie::block_id() const { return block_info_->id(); } -bool Trie::search(int64_t key_id, Slice *key) { +bool Trie::search(int64_t key_id, MapKey *key) { if ((key_id < MIN_KEY_ID) || (key_id > header_->max_key_id)) { return false; } @@ -147,7 +147,7 @@ bool Trie::search(const Slice &key, int64_t *key_id) { return false; } -bool Trie::lcp_search(const Slice &query, int64_t *key_id, Slice *key) { +bool Trie::lcp_search(const Slice &query, int64_t *key_id, MapKey *key) { bool found = false; uint32_t node_id = ROOT_NODE_ID; uint32_t query_pos = 0; Modified: lib/map/da/basic_trie.hpp (+2 -2) =================================================================== --- lib/map/da/basic_trie.hpp 2013-02-22 17:14:56 +0900 (ce826c5) +++ lib/map/da/basic_trie.hpp 2013-02-25 10:40:58 +0900 (32554b6) @@ -431,11 +431,11 @@ class Trie : public da::Trie { uint32_t block_id() const; - bool search(int64_t key_id, Slice *key = nullptr); + bool search(int64_t key_id, MapKey *key = nullptr); bool search(const Slice &key, int64_t *key_id = nullptr); bool lcp_search(const Slice &query, int64_t *key_id = nullptr, - Slice *key = nullptr); + MapKey *key = nullptr); bool insert(const Slice &key, int64_t *key_id = nullptr); Modified: lib/map/da/large_trie.cpp (+2 -2) =================================================================== --- lib/map/da/large_trie.cpp 2013-02-22 17:14:56 +0900 (1c3f7d5) +++ lib/map/da/large_trie.cpp 2013-02-25 10:40:58 +0900 (2d6a1b5) @@ -115,7 +115,7 @@ uint32_t Trie::block_id() const { return block_info_->id(); } -bool Trie::search(int64_t key_id, Slice *key) { +bool Trie::search(int64_t key_id, MapKey *key) { if ((key_id < MIN_KEY_ID) || (key_id > header_->max_key_id)) { return false; } @@ -158,7 +158,7 @@ bool Trie::search(const Slice &key, int64_t *key_id) { return false; } -bool Trie::lcp_search(const Slice &query, int64_t *key_id, Slice *key) { +bool Trie::lcp_search(const Slice &query, int64_t *key_id, MapKey *key) { bool found = false; uint64_t node_id = ROOT_NODE_ID; uint64_t query_pos = 0; Modified: lib/map/da/large_trie.hpp (+2 -2) =================================================================== --- lib/map/da/large_trie.hpp 2013-02-22 17:14:56 +0900 (4f45878) +++ lib/map/da/large_trie.hpp 2013-02-25 10:40:58 +0900 (95b4483) @@ -426,11 +426,11 @@ class Trie : public da::Trie { uint32_t block_id() const; - bool search(int64_t key_id, Slice *key = nullptr); + bool search(int64_t key_id, MapKey *key = nullptr); bool search(const Slice &key, int64_t *key_id = nullptr); bool lcp_search(const Slice &query, int64_t *key_id = nullptr, - Slice *key = nullptr); + MapKey *key = nullptr); bool insert(const Slice &key, int64_t *key_id = nullptr); Modified: lib/map/da/trie.hpp (+2 -2) =================================================================== --- lib/map/da/trie.hpp 2013-02-22 17:14:56 +0900 (f1aeca7) +++ lib/map/da/trie.hpp 2013-02-25 10:40:58 +0900 (d9326dd) @@ -68,11 +68,11 @@ class Trie { virtual uint32_t block_id() const = 0; - virtual bool search(int64_t key_id, Slice *key = nullptr) = 0; + virtual bool search(int64_t key_id, MapKey *key = nullptr) = 0; virtual bool search(const Slice &key, int64_t *key_id = nullptr) = 0; virtual bool lcp_search(const Slice &query, int64_t *key_id = nullptr, - Slice *key = nullptr) = 0; + MapKey *key = nullptr) = 0; virtual bool insert(const Slice &key, int64_t *key_id = nullptr) = 0; Modified: lib/map/double_array.cpp (+2 -2) =================================================================== --- lib/map/double_array.cpp 2013-02-22 17:14:56 +0900 (66b9642) +++ lib/map/double_array.cpp 2013-02-25 10:40:58 +0900 (f514a80) @@ -69,7 +69,7 @@ uint32_t DoubleArray::block_id() const { return block_info_->id(); } -bool DoubleArray::search(int64_t key_id, Slice *key) { +bool DoubleArray::search(int64_t key_id, MapKey *key) { open_trie_if_needed(); if (!front_) { return false; @@ -78,7 +78,7 @@ bool DoubleArray::search(int64_t key_id, Slice *key) { } bool DoubleArray::lcp_search(const Slice &query, int64_t *key_id, - Slice *key) { + MapKey *key) { open_trie_if_needed(); if (!front_) { return false; Modified: lib/map/double_array.hpp (+2 -2) =================================================================== --- lib/map/double_array.hpp 2013-02-22 17:14:56 +0900 (49038b8) +++ lib/map/double_array.hpp 2013-02-25 10:40:58 +0900 (8d6b60e) @@ -43,11 +43,11 @@ class DoubleArray : public Map { uint32_t block_id() const; - bool search(int64_t key_id, Slice *key = nullptr); + bool search(int64_t key_id, MapKey *key = nullptr); bool search(const Slice &key, int64_t *key_id = nullptr); bool lcp_search(const Slice &query, int64_t *key_id = nullptr, - Slice *key = nullptr); + MapKey *key = nullptr); bool insert(const Slice &key, int64_t *key_id = nullptr); Modified: lib/slice.hpp (+2 -0) =================================================================== --- lib/slice.hpp 2013-02-22 17:14:56 +0900 (7a2fd1d) +++ lib/slice.hpp 2013-02-25 10:40:58 +0900 (64ece56) @@ -122,6 +122,8 @@ inline bool operator!=(const Slice &lhs, const Slice &rhs) { return !(lhs == rhs); } +std::ostream &operator<<(std::ostream &stream, const Slice &s); + } // namespace grnxx #endif // GRNXX_SLICE_HPP Modified: test/test_map.cpp (+1 -1) =================================================================== --- test/test_map.cpp 2013-02-22 17:14:56 +0900 (6aca248) +++ test/test_map.cpp 2013-02-25 10:40:58 +0900 (d358b62) @@ -105,7 +105,7 @@ void test_lcp_search() { assert(map->insert("ABE")); std::int64_t key_id; - grnxx::Slice key; + grnxx::MapKey key; assert(!map->lcp_search("", &key_id, &key)); assert(!map->lcp_search("A", &key_id, &key)); Modified: test/test_map_da_basic_trie.cpp (+1 -1) =================================================================== --- test/test_map_da_basic_trie.cpp 2013-02-22 17:14:56 +0900 (2f9922f) +++ test/test_map_da_basic_trie.cpp 2013-02-25 10:40:58 +0900 (d921d57) @@ -105,7 +105,7 @@ void test_lcp_search() { assert(trie->insert("ABE")); std::int64_t key_id; - grnxx::Slice key; + grnxx::MapKey key; assert(!trie->lcp_search("", &key_id, &key)); assert(!trie->lcp_search("A", &key_id, &key)); Modified: test/test_map_da_large_trie.cpp (+1 -1) =================================================================== --- test/test_map_da_large_trie.cpp 2013-02-22 17:14:56 +0900 (95b807b) +++ test/test_map_da_large_trie.cpp 2013-02-25 10:40:58 +0900 (9601952) @@ -105,7 +105,7 @@ void test_lcp_search() { assert(trie->insert("ABE")); std::int64_t key_id; - grnxx::Slice key; + grnxx::MapKey key; assert(!trie->lcp_search("", &key_id, &key)); assert(!trie->lcp_search("A", &key_id, &key)); Modified: test/test_map_double_array.cpp (+1 -1) =================================================================== --- test/test_map_double_array.cpp 2013-02-22 17:14:56 +0900 (0e589a1) +++ test/test_map_double_array.cpp 2013-02-25 10:40:58 +0900 (0fb7534) @@ -105,7 +105,7 @@ void test_lcp_search() { assert(da->insert("ABE")); std::int64_t key_id; - grnxx::Slice key; + grnxx::MapKey key; assert(!da->lcp_search("", &key_id, &key)); assert(!da->lcp_search("A", &key_id, &key)); -------------- next part -------------- HTML����������������������������...Download