[Groonga-commit] groonga/grnxx at 4485649 [master] Implement grnxx::map::Pool::sweep().

Back to archive index

susumu.yata null+****@clear*****
Mon Aug 19 13:17:05 JST 2013


susumu.yata	2013-08-19 13:17:05 +0900 (Mon, 19 Aug 2013)

  New Revision: 448564994fb6f0a4c906bfab65d94f8d2af7601d
  https://github.com/groonga/grnxx/commit/448564994fb6f0a4c906bfab65d94f8d2af7601d

  Message:
    Implement grnxx::map::Pool::sweep().

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

  Modified: lib/grnxx/map/pool.cpp (+21 -9)
===================================================================
--- lib/grnxx/map/pool.cpp    2013-08-16 18:20:02 +0900 (e92b07a)
+++ lib/grnxx/map/pool.cpp    2013-08-19 13:17:05 +0900 (44f032e)
@@ -54,7 +54,8 @@ Pool<T>::Pool()
       pages_(),
       table_(nullptr),
       size_(0),
-      queue_() {}
+      queue_(),
+      clock_() {}
 
 template <typename T>
 Pool<T>::~Pool() {}
@@ -186,7 +187,13 @@ void Pool<T>::defrag() {
 
 template <typename T>
 void Pool<T>::sweep(Duration lifetime) {
-  // TODO
+  const Time threshold = clock_.now() - lifetime;
+  while (!queue_.empty()) {
+    QueueEntry &queue_entry = queue_.front();
+    if (queue_entry.time <= threshold) {
+      queue_.pop();
+    }
+  }
 }
 
 template <typename T>
@@ -382,8 +389,7 @@ void Pool<T>::refresh_table() {
   // Keep an old cache table because another thread may read it.
   if (new_pages) {
     try {
-      // TODO: Time must be added.
-      queue_.push(std::move(new_pages));
+      queue_.push(QueueEntry{ std::move(new_pages), clock_.now() });
     } catch (const std::exception &exception) {
       GRNXX_ERROR() << "std::queue::push() failed";
       throw StandardError(exception);
@@ -429,7 +435,8 @@ Pool<Bytes>::Pool()
       pages_(),
       table_(nullptr),
       size_(0),
-      queue_() {}
+      queue_(),
+      clock_() {}
 
 Pool<Bytes>::~Pool() {}
 
@@ -518,7 +525,7 @@ void Pool<Bytes>::defrag() {
   uint32_t prev_page_id = INVALID_PAGE_ID;
   uint8_t *page = nullptr;
   for (int64_t key_id = MIN_KEY_ID; key_id <= max_key_id; ++key_id) {
-    // TODO: "index_pool_->get/reset()" can be the bottleneck.
+    // FIXME: "index_pool_->get/reset()" can be the bottleneck.
     uint64_t bytes_id;
     if (!index_pool_->get(key_id, &bytes_id)) {
       continue;
@@ -552,7 +559,13 @@ void Pool<Bytes>::defrag() {
 }
 
 void Pool<Bytes>::sweep(Duration lifetime) {
-  // TODO
+  const Time threshold = clock_.now() - lifetime;
+  while (!queue_.empty()) {
+    QueueEntry &queue_entry = queue_.front();
+    if (queue_entry.time <= threshold) {
+      queue_.pop();
+    }
+  }
 }
 
 void Pool<Bytes>::create_pool(Storage *storage, uint32_t storage_node_id) {
@@ -798,8 +811,7 @@ void Pool<Bytes>::refresh_table() {
   // Keep an old cache table because another thread may read it.
   if (new_pages) {
     try {
-      // TODO: Time must be added.
-      queue_.push(std::move(new_pages));
+      queue_.push(QueueEntry{ std::move(new_pages), clock_.now() });
     } catch (const std::exception &exception) {
       GRNXX_ERROR() << "std::queue::push() failed";
       throw StandardError(exception);

  Modified: lib/grnxx/map/pool.hpp (+22 -6)
===================================================================
--- lib/grnxx/map/pool.hpp    2013-08-16 18:20:02 +0900 (101bc76)
+++ lib/grnxx/map/pool.hpp    2013-08-19 13:17:05 +0900 (fb06ea5)
@@ -26,6 +26,8 @@
 #include "grnxx/bytes.hpp"
 #include "grnxx/duration.hpp"
 #include "grnxx/mutex.hpp"
+#include "grnxx/periodic_clock.hpp"
+#include "grnxx/time.hpp"
 #include "grnxx/traits.hpp"
 #include "grnxx/types.hpp"
 
@@ -61,9 +63,16 @@ struct PoolUnit {
 };
 
 template <typename T>
+struct PoolQueueEntry {
+  std::unique_ptr<void *[]> page;
+  Time time;
+};
+
+template <typename T>
 class Pool {
-  using Header = PoolHeader<T>;
-  using Unit   = PoolUnit;
+  using Header     = PoolHeader<T>;
+  using Unit       = PoolUnit;
+  using QueueEntry = PoolQueueEntry<T>;
 
   static constexpr int64_t  MIN_KEY_ID     = POOL_MIN_KEY_ID;
   static constexpr int64_t  MAX_KEY_ID     = POOL_MAX_KEY_ID;
@@ -144,8 +153,8 @@ class Pool {
   std::unique_ptr<void *[]> pages_;
   uint32_t *table_;
   uint64_t size_;
-  // TODO: Time must be added.
-  std::queue<std::unique_ptr<void *[]>> queue_;
+  std::queue<QueueEntry> queue_;
+  PeriodicClock clock_;
 
   void create_pool(Storage *storage, uint32_t storage_node_id);
   void open_pool(Storage *storage, uint32_t storage_node_id);
@@ -193,10 +202,17 @@ struct PoolTableEntry {
 };
 
 template <>
+struct PoolQueueEntry<Bytes> {
+  std::unique_ptr<uint8_t *[]> page;
+  Time time;
+};
+
+template <>
 class Pool<Bytes> {
   using Header     = PoolHeader<Bytes>;
   using IndexPool  = Pool<uint64_t>;
   using TableEntry = PoolTableEntry;
+  using QueueEntry = PoolQueueEntry<Bytes>;
 
   static constexpr int64_t  MIN_KEY_ID     = POOL_MIN_KEY_ID;
   static constexpr int64_t  MAX_KEY_ID     = POOL_MAX_KEY_ID;
@@ -273,8 +289,8 @@ class Pool<Bytes> {
   std::unique_ptr<uint8_t *[]> pages_;
   TableEntry *table_;
   uint64_t size_;
-  // TODO: Time must be added.
-  std::queue<std::unique_ptr<uint8_t *[]>> queue_;
+  std::queue<QueueEntry> queue_;
+  PeriodicClock clock_;
 
   void create_pool(Storage *storage, uint32_t storage_node_id);
   void open_pool(Storage *storage, uint32_t storage_node_id);
-------------- next part --------------
HTML����������������������������...
Télécharger 



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