File: defrag_vector.cpp

package info (click to toggle)
libpmemobj-cpp 1.12-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,388 kB
  • sloc: cpp: 136,076; sh: 1,022; perl: 381; ansic: 163; makefile: 13
file content (186 lines) | stat: -rw-r--r-- 4,225 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2020, Intel Corporation */

#include "unittest.hpp"

#include <libpmemobj++/container/vector.hpp>
#include <libpmemobj++/defrag.hpp>
#include <libpmemobj++/detail/common.hpp>
#include <libpmemobj++/make_persistent.hpp>
#include <libpmemobj++/persistent_ptr.hpp>
#include <libpmemobj++/pexceptions.hpp>
#include <libpmemobj++/pool.hpp>
#include <libpmemobj++/transaction.hpp>
#include <libpmemobj/atomic_base.h>

namespace nvobj = pmem::obj;

namespace
{

struct root {
	nvobj::persistent_ptr<nvobj::vector<int>> vi;
	nvobj::persistent_ptr<nvobj::vector<double>> vd;
};

void
test_vector_basic(nvobj::pool<root> &pop)
{
	nvobj::transaction::run(pop, [&] {
		pop.root()->vi = nvobj::make_persistent<nvobj::vector<int>>();
		pop.root()->vd =
			nvobj::make_persistent<nvobj::vector<double>>();
	});

	static_assert(!nvobj::is_defragmentable<
			      nvobj::persistent_ptr<nvobj::vector<int>>>(),
		      "should not assert");
	static_assert(nvobj::is_defragmentable<nvobj::vector<int>>(),
		      "should not assert");

	pop.root()->vi->push_back(5);
	pop.root()->vi->push_back(10);
	pop.root()->vi->push_back(15);

	pop.root()->vd->push_back(1);

	nvobj::defrag my_defrag(pop);
	my_defrag.add(pop.root()->vi);
	my_defrag.add(pop.root()->vd);

	pobj_defrag_result res;
	try {
		res = my_defrag.run();
	} catch (pmem::defrag_error &) {
		UT_ASSERT(0);
	}

	/* 2 pointers + 2 vector objects (each have only 1 internal pointer) */
	UT_ASSERTeq(res.total, 4);
}

/*
 * Adding only objects pointed by ptrs, without pointers themselves.
 * One of the vectors is empty, hence finally only 1 object was added
 * to the defragmentation.
 */
void
test_vector_add_no_ptrs(nvobj::pool<root> &pop)
{
	nvobj::transaction::run(pop, [&] {
		pop.root()->vi = nvobj::make_persistent<nvobj::vector<int>>();
		pop.root()->vd =
			nvobj::make_persistent<nvobj::vector<double>>();
	});

	pop.root()->vi->push_back(5);

	nvobj::defrag my_defrag(pop);
	my_defrag.add(*pop.root()->vi);
	my_defrag.add(*pop.root()->vd);

	pobj_defrag_result res;
	try {
		res = my_defrag.run();
	} catch (pmem::defrag_error &) {
		UT_ASSERT(0);
	}

	UT_ASSERTeq(res.total, 1);
}

/*
 * When trying to add any object from outside of the selected pool,
 * the 'std::runtime_error' exception should be thrown.
 */
void
test_vector_try_add_wrong_pointer(nvobj::pool<root> &pop, std::string path)
{
	nvobj::persistent_ptr<nvobj::vector<char>> vc;
	nvobj::pool<root> pop_test;

	try {
		pop_test = nvobj::pool<struct root>::create(
			path, "layout", PMEMOBJ_MIN_POOL, S_IWUSR | S_IRUSR);
	} catch (pmem::pool_error &pe) {
		UT_FATAL("!pool::create: %s %s", pe.what(), path.c_str());
	}

	nvobj::transaction::run(pop_test, [&] {
		pop_test.root()->vi =
			nvobj::make_persistent<nvobj::vector<int>>();

		vc = nvobj::make_persistent<nvobj::vector<char>>();
	});

	nvobj::defrag my_defrag(pop);
	try {
		my_defrag.add(pop_test.root()->vi);
		UT_ASSERT(0);
	} catch (std::runtime_error &e) {
		UT_ASSERT(e.what() != std::string());
	} catch (...) {
		UT_ASSERT(0);
	}
	try {
		my_defrag.add(vc);
		UT_ASSERT(0);
	} catch (std::runtime_error &e) {
		UT_ASSERT(e.what() != std::string());
	} catch (...) {
		UT_ASSERT(0);
	}
	try {
		my_defrag.add(*vc);
		UT_ASSERT(0);
	} catch (std::runtime_error &e) {
		UT_ASSERT(e.what() != std::string());
	} catch (...) {
		UT_ASSERT(0);
	}

	pobj_defrag_result res;
	try {
		res = my_defrag.run();
	} catch (pmem::defrag_error &) {
		UT_ASSERT(0);
	}

	UT_ASSERTeq(res.total, 0);

	nvobj::transaction::run(pop_test, [&] {
		nvobj::delete_persistent<nvobj::vector<char>>(vc);
	});
	pop_test.close();
}
}

static void
test(int argc, char *argv[])
{
	if (argc != 2)
		UT_FATAL("usage: %s file-name", argv[0]);

	const char *path = argv[1];

	nvobj::pool<root> pop;

	try {
		pop = nvobj::pool<struct root>::create(
			path, "layout", PMEMOBJ_MIN_POOL, S_IWUSR | S_IRUSR);
	} catch (pmem::pool_error &pe) {
		UT_FATAL("!pool::create: %s %s", pe.what(), path);
	}

	test_vector_basic(pop);
	test_vector_add_no_ptrs(pop);
	test_vector_try_add_wrong_pointer(pop, std::string(path) + "_tmp");

	pop.close();
}

int
main(int argc, char *argv[])
{
	return run_test([&] { test(argc, argv); });
}