}
}
+ /*
+ * Clears unreferenced elements from the lru set [from, to]
+ */
+ void clear_range(
+ const K& from,
+ const K& to) {
+ auto from_iter = lru_set.lower_bound(from);
+ auto to_iter = lru_set.upper_bound(to);
+ for (auto i = from_iter; i != to_iter; ) {
+ if (!(*i).lru) {
+ unreferenced_list.erase(lru_list_t::s_iterator_to(*i));
+ i = lru_set.erase_and_dispose(i, [](auto *p)
+ { delete p; } );
+ } else {
+ i++;
+ }
+ }
+ }
+
/**
* Returns the TRef corresponding to k if it exists or
* nullptr otherwise.
return obc_lru.get(hoid);
}
+ void clear_range(const hobject_t &from,
+ const hobject_t &to) {
+ obc_lru.clear_range(from, to);
+ }
+
const char** get_tracked_conf_keys() const final;
void handle_conf_change(const crimson::common::ConfigProxy& conf,
const std::set <std::string> &changed) final;
}
}
}
+
+TEST(LRU, clear_range) {
+ LRUTest cache;
+ const unsigned SIZE = 10;
+ cache.set_target_size(SIZE);
+ {
+ auto [ref, existed] = cache.add(1, 4);
+ ASSERT_FALSE(existed);
+ }
+ {
+ auto [ref, existed] = cache.add(2, 4);
+ ASSERT_FALSE(existed);
+ }
+ {
+ auto [ref, existed] = cache.add(3, 4);
+ ASSERT_FALSE(existed);
+ }
+ // Unlike above, the reference is not being destroyed
+ auto [live_ref1, existed1] = cache.add(4, 4);
+ ASSERT_FALSE(existed1);
+
+ auto [live_ref2, existed2] = cache.add(5, 4);
+ ASSERT_FALSE(existed2);
+
+ cache.clear_range(0,4);
+
+ // Should not exists (Unreferenced):
+ {
+ auto [ref, existed] = cache.add(1, 4);
+ ASSERT_FALSE(existed);
+ }
+ {
+ auto [ref, existed] = cache.add(2, 4);
+ ASSERT_FALSE(existed);
+ }
+ {
+ auto [ref, existed] = cache.add(3, 4);
+ ASSERT_FALSE(existed);
+ }
+ // Should exist (Still being referenced):
+ {
+ auto [ref, existed] = cache.add(4, 4);
+ ASSERT_TRUE(existed);
+ }
+ // Should exists (Still being referenced and wasn't removed)
+ {
+ auto [ref, existed] = cache.add(5, 4);
+ ASSERT_TRUE(existed);
+ }
+ // Test out of bound deletion:
+ {
+ cache.clear_range(3,8);
+ auto [ref, existed] = cache.add(4, 4);
+ ASSERT_TRUE(existed);
+ }
+ {
+ auto [ref, existed] = cache.add(3, 4);
+ ASSERT_FALSE(existed);
+ }
+}