[Groonga-commit] groonga/grnxx at 5f0d115 [master] Add grnxx::map::KeyPool::defrag().

Back to archive index

susumu.yata null+****@clear*****
Thu Jul 25 17:41:55 JST 2013


susumu.yata	2013-07-25 17:41:55 +0900 (Thu, 25 Jul 2013)

  New Revision: 5f0d115a46fe2364c6e0c090eb3c1a176a6131c3
  https://github.com/groonga/grnxx/commit/5f0d115a46fe2364c6e0c090eb3c1a176a6131c3

  Message:
    Add grnxx::map::KeyPool::defrag().

  Modified files:
    lib/grnxx/map/key_pool.cpp
    lib/grnxx/map/key_pool.hpp

  Modified: lib/grnxx/map/key_pool.cpp (+23 -0)
===================================================================
--- lib/grnxx/map/key_pool.cpp    2013-07-25 17:20:40 +0900 (d135fa4)
+++ lib/grnxx/map/key_pool.cpp    2013-07-25 17:41:55 +0900 (54624ca)
@@ -212,6 +212,11 @@ int64_t KeyPool<T>::add(KeyArg key) {
 }
 
 template <typename T>
+void KeyPool<T>::defrag(double) {
+  // Nothing to do.
+}
+
+template <typename T>
 void KeyPool<T>::truncate() {
   header_->max_key_id = MIN_KEY_ID - 1;
   header_->num_keys = 0;
@@ -357,6 +362,24 @@ int64_t KeyPool<Bytes>::add(KeyArg key) {
   return entry_id;
 }
 
+void KeyPool<Bytes>::defrag(double usage_rate_threshold) {
+  const uint64_t page_size_in_use_threshold =
+      uint64_t(pool_->page_size() * usage_rate_threshold);
+  for (int64_t key_id = MIN_KEY_ID; key_id <= header_->max_key_id; ++key_id) {
+    Entry &entry = entries_->get_value(key_id);
+    if (entry) {
+      // Reallocate a key if it belongs to a page whose usage rate is less than
+      // "usage_rate_threshold".
+      const uint64_t bytes_id = entry.bytes_id();
+      const uint64_t page_id = bytes_id / pool_->page_size();
+      if (pool_->get_page_size_in_use(page_id) < page_size_in_use_threshold) {
+        entry.set_bytes_id(pool_->add(pool_->get(bytes_id)));
+        pool_->unset(bytes_id);
+      }
+    }
+  }
+}
+
 void KeyPool<Bytes>::truncate() {
   header_->max_key_id = MIN_KEY_ID - 1;
   header_->num_keys = 0;

  Modified: lib/grnxx/map/key_pool.hpp (+6 -0)
===================================================================
--- lib/grnxx/map/key_pool.hpp    2013-07-25 17:20:40 +0900 (5b138dc)
+++ lib/grnxx/map/key_pool.hpp    2013-07-25 17:41:55 +0900 (9a62887)
@@ -160,6 +160,9 @@ class KeyPool {
   // Add "key" and return its ID.
   int64_t add(KeyArg key);
 
+  // Defrag a pool.
+  void defrag(double usage_rate_threshold);
+
   // Remove all the keys.
   void truncate();
 
@@ -277,6 +280,9 @@ class KeyPool<Bytes> {
   // Add "key" and return its ID.
   int64_t add(KeyArg key);
 
+  // Defrag a pool.
+  void defrag(double usage_rate_threshold);
+
   // Remove all the keys.
   void truncate();
 
-------------- next part --------------
HTML����������������������������...
Download 



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