Skip to content

Commit b26dded

Browse files
committed
Added flush of a desired element in the cached_map.
The unit test is extended accordingly.
1 parent 0f7ec74 commit b26dded

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/util/cached_map.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,35 @@ class cached_mapt final
509509
base_map.flush();
510510
}
511511

512+
/*******************************************************************\
513+
514+
Function: cached_mapt::unload(const key_type &)
515+
516+
Inputs:
517+
key: A key whose corresponding value should be unload.
518+
519+
Outputs:
520+
True, if the referenced element was unload, and false otherwise.
521+
522+
Purpose:
523+
Unloads the element referenced by the passed key in the cache to allow the
524+
backing store to be externally manipulated.
525+
526+
Remarks:
527+
This function will not unload a locked element.
528+
529+
\*******************************************************************/
530+
bool unload(const key_type &key)
531+
{
532+
if(std::find(unloadable.cbegin(), unloadable.cend(), key)
533+
== unloadable.cend())
534+
{
535+
return false;
536+
}
537+
base_map.set(key, remove_cache(key));
538+
return true;
539+
}
540+
512541
private:
513542
std::shared_ptr<mapped_type> get(
514543
const key_type &key, std::function<mapped_type()> load_value) const

unit/serialization/cached_map.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ void test_cached_map()
122122
test_assert(map.cache_size() == 4, "The unloaded element can be reloaded");
123123
test_assert(map.size() == 4, "Map size is correct after element is reloaded");
124124

125-
// Now f4, f3 and f1 will be unlocked as they go out of scope
126-
// Now f4, f3 and f1 will be unloaded as map goes out of scope
125+
test_assert(map.locked_count() == 4,
126+
"All 4 elements must be locked before flush");
127+
test_assert(!map.unload(get_F_key(f2)),
128+
"Flush must fail as the element 7 is locked");
129+
// Now we unlock elements 1 and 7 respectively.
130+
f41 = nullptr;
131+
f22 = nullptr;
132+
test_assert(map.locked_count() == 2,
133+
"After unlocking 1 and 7 there must remain only 2 locked");
134+
test_assert(map.unload(get_F_key(f2)),
135+
"Flush of 7 must succeed as it was unlocked");
136+
137+
// Now 5, 9 and will be unloaded as map goes out of scope
127138
}

0 commit comments

Comments
 (0)