Motr  M0
ns_iter.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-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 #include "lib/memory.h"
24 #include "lib/misc.h" /* SET0 */
25 #include "lib/errno.h"
26 #include "cob/ns_iter.h"
27 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_COB
28 #include "lib/trace.h"
29 #
30 
34 static bool ns_iter_invariant(const struct m0_cob_fid_ns_iter *iter)
35 {
36  return iter != NULL &&
37  iter->cni_cdom != NULL && m0_fid_is_set(&iter->cni_last_fid);
38 }
39 
40 M0_INTERNAL int m0_cob_ns_iter_init(struct m0_cob_fid_ns_iter *iter,
41  struct m0_fid *gfid,
42  struct m0_cob_domain *cdom)
43 {
44  M0_PRE(iter != NULL);
45 
46  M0_SET0(iter);
47 
48  iter->cni_cdom = cdom;
49  iter->cni_last_fid = *gfid;
50 
52 
53  return 0;
54 }
55 
56 M0_INTERNAL int m0_cob_ns_rec_of(struct m0_be_btree *cob_namespace,
57  const struct m0_fid *key_gfid,
58  struct m0_fid *next_gfid,
59  struct m0_cob_nsrec **nsrec)
60 {
61  struct m0_cob_nskey *key = NULL;
62  struct m0_buf kbuf;
63  struct m0_buf nbuf;
64  struct m0_be_btree_cursor it;
65  uint32_t cob_idx = 0;
66  char nskey_bs[UINT32_STR_LEN];
67  uint32_t nskey_bs_len;
68  int rc;
69 
70  m0_be_btree_cursor_init(&it, cob_namespace);
71  M0_SET0(&nskey_bs);
72  snprintf(nskey_bs, UINT32_STR_LEN, "%u", cob_idx);
73  nskey_bs_len = strlen(nskey_bs);
74 
75  rc = m0_cob_nskey_make(&key, key_gfid, nskey_bs,
76  nskey_bs_len);
77  if (rc != 0)
78  return M0_RC(rc);
79 
81  rc = m0_be_btree_cursor_get_sync(&it, &kbuf, true);
82  if (rc == 0) {
83  /*
84  * Assign the fetched value to gfid, which is treated as
85  * iterator output.
86  */
87  struct m0_cob_nskey* k;
88 
89  m0_be_btree_cursor_kv_get(&it, &kbuf, &nbuf);
90  k = (struct m0_cob_nskey *)kbuf.b_addr;
91  *nsrec = (struct m0_cob_nsrec *)nbuf.b_addr;
92  *next_gfid = k->cnk_pfid;
93  }
94  m0_free(key);
96 
97  return M0_RC(rc);
98 }
99 
100 M0_INTERNAL int m0_cob_ns_iter_next(struct m0_cob_fid_ns_iter *iter,
101  struct m0_fid *gfid,
102  struct m0_cob_nsrec **nsrec)
103 {
104  int rc;
105  struct m0_fid key_fid;
106 
107  M0_PRE(ns_iter_invariant(iter));
108  M0_PRE(gfid != NULL);
109 
110  key_fid = iter->cni_last_fid;
111  rc = m0_cob_ns_rec_of(&iter->cni_cdom->cd_namespace, &key_fid, gfid,
112  nsrec);
113  if (rc == 0) {
114  /* Container (f_container) value remains same, typically 0. */
116  /* Increment the f_key by 1, to exploit cursor_get() property.*/
117  iter->cni_last_fid.f_key = gfid->f_key + 1;
118  }
119 
120  return M0_RC(rc);
121 }
122 
123 M0_INTERNAL void m0_cob_ns_iter_fini(struct m0_cob_fid_ns_iter *iter)
124 {
125  M0_PRE(ns_iter_invariant(iter));
126  M0_SET0(iter);
127 }
128 
129 #undef M0_TRACE_SUBSYSTEM
130 
132 /*
133  * Local variables:
134  * c-indentation-style: "K&R"
135  * c-basic-offset: 8
136  * tab-width: 8
137  * fill-column: 80
138  * scroll-step: 1
139  * End:
140  */
struct m0_fid cni_last_fid
Definition: ns_iter.h:55
#define M0_PRE(cond)
#define NULL
Definition: misc.h:38
M0_INTERNAL int m0_cob_ns_iter_next(struct m0_cob_fid_ns_iter *iter, struct m0_fid *gfid, struct m0_cob_nsrec **nsrec)
Definition: ns_iter.c:100
void * b_addr
Definition: buf.h:39
M0_INTERNAL void m0_cob_ns_iter_fini(struct m0_cob_fid_ns_iter *iter)
Definition: ns_iter.c:123
M0_INTERNAL void m0_buf_init(struct m0_buf *buf, void *data, uint32_t nob)
Definition: buf.c:37
static struct m0_be_emap_cursor it
Definition: extmap.c:46
struct m0_cob_domain * cni_cdom
Definition: ns_iter.h:51
#define M0_SET0(obj)
Definition: misc.h:64
M0_INTERNAL bool m0_fid_is_set(const struct m0_fid *fid)
Definition: fid.c:106
static struct m0_cob_domain * cdom
Definition: xform.c:55
M0_INTERNAL void m0_be_btree_cursor_fini(struct m0_be_btree_cursor *cursor)
Definition: btree.c:2290
M0_INTERNAL int m0_cob_nskey_make(struct m0_cob_nskey **keyh, const struct m0_fid *pfid, const char *name, size_t namelen)
Definition: cob.c:148
return M0_RC(rc)
Definition: buf.h:37
static bool ns_iter_invariant(const struct m0_cob_fid_ns_iter *iter)
Definition: ns_iter.c:34
M0_INTERNAL int m0_cob_ns_iter_init(struct m0_cob_fid_ns_iter *iter, struct m0_fid *gfid, struct m0_cob_domain *cdom)
Definition: ns_iter.c:40
uint64_t f_container
Definition: fid.h:39
#define M0_POST(cond)
M0_INTERNAL void m0_be_btree_cursor_kv_get(struct m0_be_btree_cursor *cur, struct m0_buf *key, struct m0_buf *val)
Definition: btree.c:2485
Definition: fid.h:38
uint64_t f_key
Definition: fid.h:40
M0_INTERNAL int m0_cob_ns_rec_of(struct m0_be_btree *cob_namespace, const struct m0_fid *key_gfid, struct m0_fid *next_gfid, struct m0_cob_nsrec **nsrec)
Definition: ns_iter.c:56
M0_INTERNAL void m0_be_btree_cursor_init(struct m0_be_btree_cursor *cur, struct m0_be_btree *btree)
Definition: btree.c:2281
M0_INTERNAL int m0_be_btree_cursor_get_sync(struct m0_be_btree_cursor *cur, const struct m0_buf *key, bool slant)
Definition: btree.c:2331
M0_INTERNAL size_t m0_cob_nskey_size(const struct m0_cob_nskey *cnk)
Definition: cob.c:175
struct m0_fid gfid
Definition: dir.c:626
void m0_free(void *data)
Definition: memory.c:146
int32_t rc
Definition: trigger_fop.h:47
struct m0_fid cnk_pfid
Definition: cob.h:391
Definition: idx_mock.c:47
struct m0_be_btree cd_namespace
Definition: cob.h:271