[Groonga-commit] groonga/grnxx at 97fe3ef [master] Refine grnxx::alpha::Map.

Back to archive index

susumu.yata null+****@clear*****
Fri Apr 26 21:46:21 JST 2013


susumu.yata	2013-04-26 21:46:21 +0900 (Fri, 26 Apr 2013)

  New Revision: 97fe3ef0645c1eb88a7f85317a3d302db1e19ea4
  https://github.com/groonga/grnxx/commit/97fe3ef0645c1eb88a7f85317a3d302db1e19ea4

  Message:
    Refine grnxx::alpha::Map.

  Modified files:
    lib/grnxx/alpha/map.cpp
    lib/grnxx/alpha/map.hpp
    lib/grnxx/alpha/map/array.cpp
    lib/grnxx/alpha/map/array.hpp
    lib/grnxx/alpha/map/double_array-slice.cpp
    lib/grnxx/alpha/map/double_array.cpp
    lib/grnxx/alpha/map/double_array.hpp

  Modified: lib/grnxx/alpha/map.cpp (+41 -15)
===================================================================
--- lib/grnxx/alpha/map.cpp    2013-04-26 21:21:33 +0900 (9ff1324)
+++ lib/grnxx/alpha/map.cpp    2013-04-26 21:46:21 +0900 (0092d15)
@@ -37,13 +37,13 @@ MapCursor<T>::~MapCursor() {}
 
 template <typename T>
 bool MapCursor<T>::next() {
-  // TODO: Not supported!?
+  // Not supported.
   return false;
 }
 
 template <typename T>
 bool MapCursor<T>::remove() {
-  // TODO: Not supported.
+  // Not supported.
   return false;
 }
 
@@ -75,8 +75,15 @@ Map<T> *Map<T>::create(MapType type, io::Pool pool,
     case MAP_DOUBLE_ARRAY: {
       return map::DoubleArray<T>::create(pool, options);
     }
+    case MAP_PATRICIA: {
+      // TODO: Not supported yet.
+      return nullptr;
+    }
+    case MAP_HASH_TABLE: {
+      // TODO: Not supported yet.
+      return nullptr;
+    }
     default: {
-      // Not supported yet.
       return nullptr;
     }
   }
@@ -93,8 +100,15 @@ Map<T> *Map<T>::open(io::Pool pool, uint32_t block_id) {
     case MAP_DOUBLE_ARRAY: {
       return map::DoubleArray<T>::open(pool, block_id);
     }
+    case MAP_PATRICIA: {
+      // TODO: Not supported yet.
+      return nullptr;
+    }
+    case MAP_HASH_TABLE: {
+      // TODO: Not supported yet.
+      return nullptr;
+    }
     default: {
-      // Not supported yet.
       return nullptr;
     }
   }
@@ -108,6 +122,17 @@ bool Map<T>::unlink(io::Pool pool, uint32_t block_id) {
     case MAP_ARRAY: {
       return map::Array<T>::unlink(pool, block_id);
     }
+    case MAP_DOUBLE_ARRAY: {
+      return map::DoubleArray<T>::unlink(pool, block_id);
+    }
+    case MAP_PATRICIA: {
+      // TODO: Not supported yet.
+      return false;
+    }
+    case MAP_HASH_TABLE: {
+      // TODO: Not supported yet.
+      return false;
+    }
     default: {
       // Not supported yet.
       return false;
@@ -118,7 +143,7 @@ bool Map<T>::unlink(io::Pool pool, uint32_t block_id) {
 template <typename T>
 uint32_t Map<T>::block_id() const {
   // Not supported.
-  return 0;
+  return io::BLOCK_INVALID_ID;
 }
 
 template <typename T>
@@ -153,6 +178,7 @@ bool Map<T>::get(int64_t, T *) {
 
 template <typename T>
 bool Map<T>::get_next(int64_t, int64_t *, T *) {
+  // Not supported.
   return false;
 }
 
@@ -215,8 +241,9 @@ bool Map<Slice>::find_longest_prefix_match(Slice query, int64_t *key_id,
 }
 
 template <typename T>
-void Map<T>::truncate() {
+bool Map<T>::truncate() {
   // Not supported.
+  return false;
 }
 
 template <typename T>
@@ -271,8 +298,7 @@ MapCursor<Slice> *Map<Slice>::open_prefix_cursor(
 }
 
 template <typename T>
-MapCursor<T> *Map<T>::open_completion_cursor(
-    T, const MapCursorOptions &) {
+MapCursor<T> *Map<T>::open_completion_cursor(T, const MapCursorOptions &) {
   // Not supported.
   return nullptr;
 }
@@ -347,13 +373,13 @@ bool MapScan::next() {
 }
 
 MapScan::MapScan(Map<Slice> *map, const Slice &query, const Charset *charset)
-  : map_(map),
-    query_(query),
-    offset_(0),
-    size_(0),
-    key_id_(-1),
-    key_(),
-    charset_(charset) {}
+    : map_(map),
+      query_(query),
+      offset_(0),
+      size_(0),
+      key_id_(-1),
+      key_(),
+      charset_(charset) {}
 
 }  // namespace alpha
 }  // namespace grnxx

  Modified: lib/grnxx/alpha/map.hpp (+22 -16)
===================================================================
--- lib/grnxx/alpha/map.hpp    2013-04-26 21:21:33 +0900 (8cc535d)
+++ lib/grnxx/alpha/map.hpp    2013-04-26 21:46:21 +0900 (a0c3c43)
@@ -28,14 +28,16 @@ class Charset;
 
 namespace alpha {
 
+template <typename T> class MapCursor;
 class MapScan;
+template <typename T> class Map;
 
 enum MapType : int32_t {
   MAP_UNKNOWN      = 0,
-  MAP_ARRAY        = 1,  // Test implementation.
-  MAP_DOUBLE_ARRAY = 2,  // Partly implemented.
-  MAP_PATRICIA     = 3,  // TODO: Not supported yet.
-  MAP_HASH_TABLE   = 4   // TODO: Not supported yet.
+  MAP_ARRAY        = 1,  // Array-based implementation.
+  MAP_DOUBLE_ARRAY = 2,  // DoubleArray-based implementation.
+  MAP_PATRICIA     = 3,  // TODO: Patricia-based implementation.
+  MAP_HASH_TABLE   = 4   // TODO: HashTable-based implementation.
 };
 
 struct MapOptions {
@@ -106,6 +108,8 @@ class MapCursor {
 template <typename T>
 class Map {
  public:
+  using Cursor = MapCursor<T>;
+
   Map();
   virtual ~Map();
 
@@ -169,36 +173,38 @@ class Map {
   virtual bool find_longest_prefix_match(T query, int64_t *key_id = nullptr,
                                          T *key = nullptr);
 
-  // Remove all the keys in "*this".
-  virtual void truncate();
+  // Remove all the keys in "*this" and return true on success.
+  virtual bool truncate();
 
   // Create a cursor for accessing all the keys.
-  virtual MapCursor<T> *open_basic_cursor(
+  virtual Cursor *open_basic_cursor(
       const MapCursorOptions &options = MapCursorOptions());
   // Create a cursor for accessing keys in range [min, max].
-  virtual MapCursor<T> *open_id_cursor(int64_t min, int64_t max,
+  virtual Cursor *open_id_cursor(
+      int64_t min, int64_t max,
       const MapCursorOptions &options = MapCursorOptions());
   // Create a cursor for accessing keys in range [min, max].
-  virtual MapCursor<T> *open_key_cursor(T min, T max,
-      const MapCursorOptions &options = MapCursorOptions());
+  virtual Cursor *open_key_cursor(
+      T min, T max, const MapCursorOptions &options = MapCursorOptions());
 
   // Only for GeoPoint.
   // Create a cursor for accessing keys whose most significant "bit_size" bits
   // are same as the MSBs of "query".
-  virtual MapCursor<T> *open_bitwise_completion_cursor(
+  virtual Cursor *open_bitwise_completion_cursor(
       T query, size_t bit_size,
       const MapCursorOptions &options = MapCursorOptions());
 
   // Only for Slice.
   // Create a cursor for accessing keys matching a prefix of "query".
-  virtual MapCursor<T> *open_prefix_cursor(T query, size_t min_size,
+  virtual Cursor *open_prefix_cursor(
+      T query, size_t min_size,
       const MapCursorOptions &options = MapCursorOptions());
   // Create a cursor for accessing keys starting with "query".
-  virtual MapCursor<T> *open_completion_cursor(T query,
-      const MapCursorOptions &options = MapCursorOptions());
+  virtual Cursor *open_completion_cursor(
+      T query, const MapCursorOptions &options = MapCursorOptions());
   // Create a cursor for accessing keys ending with "query".
-  virtual MapCursor<T> *open_reverse_completion_cursor(T query,
-      const MapCursorOptions &options = MapCursorOptions());
+  virtual Cursor *open_reverse_completion_cursor(
+      T query, const MapCursorOptions &options = MapCursorOptions());
 
   // Only for Slice.
   // Create a MapScan object to find keys in "query".

  Modified: lib/grnxx/alpha/map/array.cpp (+4 -2)
===================================================================
--- lib/grnxx/alpha/map/array.cpp    2013-04-26 21:21:33 +0900 (fc7f873)
+++ lib/grnxx/alpha/map/array.cpp    2013-04-26 21:46:21 +0900 (c169eef)
@@ -287,13 +287,14 @@ bool Array<T>::update(T src_key, T dest_key, int64_t *key_id) {
 }
 
 template <typename T>
-void Array<T>::truncate() {
+bool Array<T>::truncate() {
   for (int64_t i = 0; i <= header_->max_key_id; ++i) {
     set_bit(i, false);
   }
   header_->max_key_id = -1;
   header_->next_key_id = 0;
   header_->num_keys = 0;
+  return true;
 }
 
 template <typename T>
@@ -514,13 +515,14 @@ bool Array<Slice>::update(Slice src_key, Slice dest_key, int64_t *key_id) {
   return true;
 }
 
-void Array<Slice>::truncate() {
+bool Array<Slice>::truncate() {
   for (int64_t i = 0; i <= header_->max_key_id; ++i) {
     keys_[i] = nullptr;
   }
   header_->max_key_id = -1;
   header_->next_key_id = 0;
   header_->num_keys = 0;
+  return true;
 }
 
 Array<Slice>::Array()

  Modified: lib/grnxx/alpha/map/array.hpp (+2 -2)
===================================================================
--- lib/grnxx/alpha/map/array.hpp    2013-04-26 21:21:33 +0900 (1ca7c18)
+++ lib/grnxx/alpha/map/array.hpp    2013-04-26 21:46:21 +0900 (4d48823)
@@ -66,7 +66,7 @@ class Array : public grnxx::alpha::Map<T> {
   bool remove(T key);
   bool update(T src_key, T dest_key, int64_t *key_id = nullptr);
 
-  void truncate();
+  bool truncate();
 
  private:
   io::Pool pool_;
@@ -118,7 +118,7 @@ class Array<Slice> : public grnxx::alpha::Map<Slice> {
   bool remove(Slice key);
   bool update(Slice src_key, Slice dest_key, int64_t *key_id = nullptr);
 
-  void truncate();
+  bool truncate();
 
  private:
   io::Pool pool_;

  Modified: lib/grnxx/alpha/map/double_array-slice.cpp (+2 -1)
===================================================================
--- lib/grnxx/alpha/map/double_array-slice.cpp    2013-04-26 21:21:33 +0900 (c403168)
+++ lib/grnxx/alpha/map/double_array-slice.cpp    2013-04-26 21:46:21 +0900 (522f218)
@@ -1532,12 +1532,13 @@ bool DoubleArray<Slice>::find_longest_prefix_match(
   return found;
 }
 
-void DoubleArray<Slice>::truncate() {
+bool DoubleArray<Slice>::truncate() {
   nodes_[ROOT_NODE_ID].set_child(INVALID_LABEL);
   nodes_[ROOT_NODE_ID].set_offset(INVALID_OFFSET);
   header_->next_key_id = 0;
   header_->max_key_id = -1;
   header_->num_keys = 0;
+  return true;
 }
 
 MapCursor<Slice> *DoubleArray<Slice>::open_basic_cursor(

  Modified: lib/grnxx/alpha/map/double_array.cpp (+2 -1)
===================================================================
--- lib/grnxx/alpha/map/double_array.cpp    2013-04-26 21:21:33 +0900 (ea7e7ce)
+++ lib/grnxx/alpha/map/double_array.cpp    2013-04-26 21:46:21 +0900 (92a78db)
@@ -1407,12 +1407,13 @@ bool DoubleArray<T>::update(T src_key, T dest_key, int64_t *key_id) {
 }
 
 template <typename T>
-void DoubleArray<T>::truncate() {
+bool DoubleArray<T>::truncate() {
   nodes_[ROOT_NODE_ID].set_child(INVALID_LABEL);
   nodes_[ROOT_NODE_ID].set_offset(INVALID_OFFSET);
   header_->next_key_id = 0;
   header_->max_key_id = -1;
   header_->num_keys = 0;
+  return true;
 }
 
 template <typename T>

  Modified: lib/grnxx/alpha/map/double_array.hpp (+2 -2)
===================================================================
--- lib/grnxx/alpha/map/double_array.hpp    2013-04-26 21:21:33 +0900 (ff0a011)
+++ lib/grnxx/alpha/map/double_array.hpp    2013-04-26 21:46:21 +0900 (5d11a6f)
@@ -95,7 +95,7 @@ class DoubleArray : public Map<T> {
   bool remove(T key);
   bool update(T src_key, T dest_key, int64_t *key_id = nullptr);
 
-  void truncate();
+  bool truncate();
 
   MapCursor<T> *open_basic_cursor(
       const MapCursorOptions &options = MapCursorOptions());
@@ -192,7 +192,7 @@ class DoubleArray<Slice> : public Map<Slice> {
   bool find_longest_prefix_match(Slice query, int64_t *key_id = nullptr,
                                  Slice *key = nullptr);
 
-  void truncate();
+  bool truncate();
 
   MapCursor<Slice> *open_basic_cursor(
       const MapCursorOptions &options = MapCursorOptions());
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index