Motr  M0
seg_dict.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2013-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_UT
24 #include "lib/trace.h"
25 #include "lib/types.h" /* m0_uint128_eq */
26 #include "lib/misc.h" /* M0_BITS */
27 #include "lib/memory.h" /* M0_ALLOC_PTR, m0_free */
28 #include "be/ut/helper.h" /* m0_be_ut_backend */
29 #include "ut/ut.h"
30 
31 
33 {
34  static struct m0_be_ut_backend ut_be;
35  struct m0_be_tx_credit credit = {};
36  struct m0_be_tx tx = {};
37  struct m0_be_ut_seg ut_seg;
38  struct m0_be_seg *seg;
39  const char *nk; /*next key */
40  void *p;
41  int i;
42  int rc;
43 #define OPT 7
44 #define END 11
45  struct {
46  const char *name;
47  void **value;
48  } dict[] = {
49  { "dead", (void*)0xdead },
50  { "beaf", (void*)0xbeaf },
51  { "cafe", (void*)0xcafe },
52  { "babe", (void*)0xbabe },
53  { "d00d", (void*)0xd00d },
54  { "8bad", (void*)0x8bad },
55  { "f00d", (void*)0xf00d },
56  [OPT] = { "M0_BE:opt1", (void*)0xf00d0001 },
57  { "M0_BE:opt2", (void*)0xf00d0002 },
58  { "M0_BE:opt3", (void*)0xf00d0003 },
59  { "M0_BE:opt4", (void*)0xf00d0004 },
60  [END] = { "M0_BE:end0", (void*)0xf00d0000 },
61  };
62 
63  M0_SET0(&ut_be);
65  m0_be_ut_seg_init(&ut_seg, &ut_be, 1 << 20);
66  seg = ut_seg.bus_seg;
67 #ifdef __KERNEL__
70  m0_be_tx_prep(&tx, &credit);
71  rc = m0_be_tx_open_sync(&tx);
72  M0_UT_ASSERT(rc == 0);
75  m0_be_tx_fini(&tx);
76 #endif
77  M0_SET0(&tx);
78  m0_be_ut_tx_init(&tx, &ut_be);
79 
80  credit = M0_BE_TX_CREDIT(0, 0);
81  for (i = 0; i < ARRAY_SIZE(dict); ++i) {
82  m0_be_seg_dict_insert_credit(seg, "....", &credit);
83  m0_be_seg_dict_delete_credit(seg, "....", &credit);
84  }
85  m0_be_tx_prep(&tx, &credit);
86  m0_be_tx_open_sync(&tx);
87 
88  for (i = 0; i < ARRAY_SIZE(dict); ++i) {
89  rc = m0_be_seg_dict_insert(seg, &tx, dict[i].name,
90  dict[i].value);
91  M0_UT_ASSERT(rc == 0);
92  }
93 
94  for (i = 0; i < ARRAY_SIZE(dict); ++i) {
95  rc = m0_be_seg_dict_lookup(seg, dict[i].name, &p);
96  M0_UT_ASSERT(rc == 0 && dict[i].value == p);
97  }
98 
99  for (i = 0, rc = m0_be_seg_dict_begin(seg, "M0_BE:opt", &nk, &p);
100  rc == 0;
101  i++, rc = m0_be_seg_dict_next(seg, "M0_BE:opt", nk, &nk, &p)) {
102  M0_UT_ASSERT(rc == 0 && p == dict[i + OPT].value &&
103  strcmp(nk, dict[i + OPT].name) == 0);
104  }
105  M0_UT_ASSERT(i == END-OPT);
106 
107  for (i = 0; i < ARRAY_SIZE(dict); i+=2) {
108  rc = m0_be_seg_dict_delete(seg, &tx, dict[i].name);
109  M0_UT_ASSERT(rc == 0);
110  }
111 
112  for (i = 1; i < ARRAY_SIZE(dict); i+=2) {
113  rc = m0_be_seg_dict_lookup(seg, dict[i].name, &p);
114  M0_UT_ASSERT(rc == 0 && dict[i].value == p);
115  }
116 
117  m0_be_tx_close_sync(&tx);
118  m0_be_tx_fini(&tx);
119  /* reload segment, check dictionary is persistent */
121 
123 
124  for (i = 1; i < ARRAY_SIZE(dict); i+=2) {
125  rc = m0_be_seg_dict_lookup(seg, dict[i].name, &p);
126  M0_UT_ASSERT(rc == 0 && dict[i].value == p);
127  }
128 
131 }
132 
133 #undef M0_TRACE_SUBSYSTEM
134 
135 /*
136  * Local variables:
137  * c-indentation-style: "K&R"
138  * c-basic-offset: 8
139  * tab-width: 8
140  * fill-column: 80
141  * scroll-step: 1
142  * End:
143  */
144 /*
145  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
146  */
#define END
void m0_be_ut_seg_fini(struct m0_be_ut_seg *ut_seg)
Definition: stubs.c:267
static struct m0_addb2_philter p
Definition: consumer.c:40
struct m0_be_domain * bs_domain
Definition: seg.h:82
#define OPT
M0_INTERNAL int m0_be_seg_dict_begin(struct m0_be_seg *seg, const char *start_key, const char **this_key, void **this_rec)
Definition: seg_dict.c:236
M0_INTERNAL int m0_be_seg_dict_insert(struct m0_be_seg *seg, struct m0_be_tx *tx, const char *name, void *value)
Definition: seg_dict.c:255
void m0_be_ut_seg_reload(struct m0_be_ut_seg *ut_seg)
Definition: stubs.c:272
#define NULL
Definition: misc.h:38
M0_INTERNAL void m0_be_seg_dict_init(struct m0_be_seg *seg)
Definition: seg_dict.c:160
M0_INTERNAL int m0_be_seg_dict_next(struct m0_be_seg *seg, const char *prefix, const char *start_key, const char **this_key, void **this_rec)
Definition: seg_dict.c:245
struct m0_be_seg * bus_seg
Definition: helper.h:119
M0_INTERNAL void m0_be_tx_fini(struct m0_be_tx *tx)
Definition: stubs.c:163
int const char const void * value
Definition: dir.c:325
M0_INTERNAL void m0_be_tx_prep(struct m0_be_tx *tx, const struct m0_be_tx_credit *credit)
Definition: stubs.c:175
struct m0_be_ut_seg ut_seg
Definition: ad.c:73
void m0_be_ut_seg_init(struct m0_be_ut_seg *ut_seg, struct m0_be_ut_backend *ut_be, m0_bcount_t size)
Definition: stubs.c:256
#define M0_SET0(obj)
Definition: misc.h:64
#define M0_BE_TX_CREDIT(nr, size)
Definition: tx_credit.h:94
int i
Definition: dir.c:1033
void m0_be_ut_seg_dict(void)
Definition: seg_dict.c:32
struct m0_be_ut_backend ut_be
Definition: ad.c:72
const char * name
Definition: trace.c:110
M0_INTERNAL int m0_be_seg_dict_delete(struct m0_be_seg *seg, struct m0_be_tx *tx, const char *name)
Definition: seg_dict.c:316
M0_INTERNAL int m0_be_tx_open_sync(struct m0_be_tx *tx)
Definition: stubs.c:199
void m0_be_ut_backend_init(struct m0_be_ut_backend *ut_be)
Definition: stubs.c:238
Definition: seg.h:66
M0_INTERNAL void m0_be_tx_init(struct m0_be_tx *tx, uint64_t tid, struct m0_be_domain *dom, struct m0_sm_group *sm_group, m0_be_tx_cb_t persistent, m0_be_tx_cb_t discarded, void(*filler)(struct m0_be_tx *tx, void *payload), void *datum)
Definition: stubs.c:150
M0_INTERNAL void m0_be_seg_dict_delete_credit(struct m0_be_seg *seg, const char *name, struct m0_be_tx_credit *accum)
Definition: seg_dict.c:131
M0_INTERNAL void m0_be_seg_dict_create_credit(struct m0_be_seg *seg, struct m0_be_tx_credit *accum)
Definition: seg_dict.c:144
void m0_be_ut_tx_init(struct m0_be_tx *tx, struct m0_be_ut_backend *ut_be)
Definition: stubs.c:286
void m0_be_ut_backend_fini(struct m0_be_ut_backend *ut_be)
Definition: stubs.c:242
static struct m0_be_seg * seg
Definition: btree.c:40
M0_INTERNAL void m0_be_seg_dict_create(struct m0_be_seg *seg, struct m0_be_tx *tx)
Definition: seg_dict.c:342
M0_INTERNAL void m0_be_seg_dict_insert_credit(struct m0_be_seg *seg, const char *name, struct m0_be_tx_credit *accum)
Definition: seg_dict.c:114
M0_INTERNAL int m0_be_seg_dict_lookup(struct m0_be_seg *seg, const char *name, void **out)
Definition: seg_dict.c:180
int32_t rc
Definition: trigger_fop.h:47
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define M0_UT_ASSERT(a)
Definition: ut.h:46
M0_INTERNAL void m0_be_tx_close_sync(struct m0_be_tx *tx)
Definition: stubs.c:205
Definition: tx.h:280