11// SPDX-License-Identifier: BSD-3-Clause
22/* Copyright 2020-2021, Intel Corporation */
33
4+ #include < functional>
5+ #include < libpmemobj.h>
6+ #include < random>
7+
48#include " transaction_helpers.hpp"
59#include " unittest.hpp"
610
711#include < libpmemobj++/experimental/inline_string.hpp>
812#include < libpmemobj++/experimental/radix_tree.hpp>
913
10- #include < functional>
11-
12- #include < libpmemobj.h>
14+ static std::mt19937_64 generator;
1315
1416namespace nvobj = pmem::obj;
1517namespace nvobjex = pmem::obj::experimental;
@@ -86,6 +88,7 @@ struct root {
8688 nvobj::persistent_ptr<container_inline_s_u8t_mt> radix_inline_s_u8t_mt;
8789};
8890
91+ /* Helper functions to access key/value of different types */
8992template <typename Container,
9093 typename Enable = typename std::enable_if<
9194 std::is_same<typename Container::mapped_type,
@@ -108,9 +111,9 @@ value(unsigned v, int repeats = 1)
108111{
109112 using CharT = typename Container::mapped_type::value_type;
110113
114+ auto str = std::to_string (v);
111115 auto s = std::basic_string<CharT>{};
112116 for (int i = 0 ; i < repeats; i++) {
113- auto str = std::to_string (v);
114117 s += std::basic_string<CharT>(str.begin (), str.end ());
115118 }
116119
@@ -178,6 +181,7 @@ operator!=(pmem::obj::experimental::basic_inline_string<CharT, Traits> &lhs,
178181 .compare (rhs) != 0 ;
179182}
180183
184+ /* verify all elements in container, using lower_/upper_bound and find */
181185template <typename Container, typename K, typename F>
182186void
183187verify_elements (nvobj::persistent_ptr<Container> ptr, unsigned count, K &&key_f,
@@ -216,6 +220,8 @@ verify_elements(nvobj::persistent_ptr<Container> ptr, unsigned count, K &&key_f,
216220 }
217221}
218222
223+ /* run 1 thread with modifications (erase/write/etc.) and multiple threads with
224+ * reads */
219225template <typename ModifyF, typename ReadF>
220226static void
221227parallel_modify_read (ModifyF modifier, std::vector<ReadF> &readers,
@@ -230,16 +236,30 @@ parallel_modify_read(ModifyF modifier, std::vector<ReadF> &readers,
230236 });
231237}
232238
239+ /* each test suite should initialize generator at the beginning */
240+ void
241+ init_random ()
242+ {
243+ std::random_device rd;
244+ auto seed = rd ();
245+ std::cout << " rand seed: " << seed << std::endl;
246+ generator = std::mt19937_64 (seed);
247+ }
248+
233249template <typename Container>
234250static void
235251init_container (nvobj::pool<root> &pop, nvobj::persistent_ptr<Container> &ptr,
236- const size_t initial_elements, const size_t value_repeats = 1 )
252+ const size_t initial_elements, const size_t value_repeats = 1 ,
253+ bool rand_keys = false )
237254{
238255 nvobj::transaction::run (
239256 pop, [&] { ptr = nvobj::make_persistent<Container>(); });
240257
241258 for (size_t i = 0 ; i < initial_elements; ++i) {
242- ptr->emplace (key<Container>(i),
243- value<Container>(i, value_repeats));
259+ auto k = key<Container>(i);
260+ if (rand_keys) {
261+ k = key<Container>(static_cast <unsigned >(generator ()));
262+ }
263+ ptr->emplace (k, value<Container>(i, value_repeats));
244264 }
245265}
0 commit comments