Motr  M0
index.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2016-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 
30 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_CLIENT
31 #include <stdio.h> /* FILE */
32 #include <uuid/uuid.h> /* uuid_generate */
33 #include "lib/assert.h" /* M0_ASSERT */
34 #include "lib/errno.h"
35 #include "lib/memory.h"
36 #include "lib/vec.h"
37 #include "lib/trace.h" /* M0_ERR */
38 #include "index.h"
39 #include "index_parser.h"
40 #include "index_op.h"
41 #include "motr/client.h"
42 #include "motr/idx.h"
43 
44 struct m0_instance {
45  char *ci_laddr;
46  char *ci_ha_addr;
48  char *ci_prof;
50 };
51 
52 struct m0_ctx {
53  struct params *cc_params;
57 };
58 
59 static struct m0_ctx cc_ctx;
60 
62 {
64 }
65 
66 static struct m0_fid ifid(uint64_t x, uint64_t y)
67 {
68  return M0_FID_TINIT('x', x, y);
69 }
70 
71 static int instance_init(struct params *params)
72 {
73  int rc;
74  struct m0_client *instance = NULL;
75  struct m0_idx_dix_config config = { .kc_create_meta = false };
76 
77  M0_PRE(params != NULL);
78  cc_ctx = (struct m0_ctx) {
79  .cc_params = params,
80  .cc_conf = {
81  .mc_is_oostore = true,
82  .mc_is_read_verify = false,
83  .mc_local_addr = params->cp_local_addr,
84  .mc_ha_addr = params->cp_ha_addr,
85  .mc_profile = params->cp_prof,
86  .mc_process_fid = params->cp_proc_fid,
87 
88  .mc_tm_recv_queue_min_len = M0_NET_TM_RECV_QUEUE_DEF_LEN,
89  .mc_max_rpc_msg_size = M0_RPC_DEF_MAX_RPC_MSG_SIZE,
90 
91  .mc_idx_service_id = M0_IDX_DIX,
92  .mc_idx_service_conf = &config
93  }
94  };
96  if (rc == 0)
98  return M0_RC(rc);
99 }
100 
101 static void instance_fini(void)
102 {
103  struct m0_client *instance;
104 
105  instance = m0_instance();
106  m0_client_fini(instance, true);
107 }
108 
109 static int genf(char *filename, int cnt)
110 {
111  FILE *f;
112  int i;
113  struct m0_fid fid;
114 
115  M0_PRE(filename != NULL);
116  M0_PRE(cnt != 0);
117  f = fopen(filename, "w");
118  if (f == NULL)
119  return M0_ERR(-ENOENT);
120  for (i = 0; i < cnt; i++) {
121  fid = ifid(10, i + 1);
122  fprintf(f, FID_F"\n", FID_P(&fid));
123  }
124  fclose(f);
125  return 0;
126 }
127 
128 static int genv(char *filename, int cnt, int size)
129 {
130  FILE *f;
131  uint64_t i;
132  uint64_t j;
133  int len;
134  int val_size;
135  uuid_t uuid;
136  char str_uuid[16];
137  char buf[20];
138 
139  M0_PRE(filename != NULL);
140  M0_PRE(cnt != 0);
141 
142  len = sizeof(str_uuid);
143  f = fopen(filename, "w");
144  if (f == NULL)
145  return M0_ERR(-ENOENT);
146  val_size = sprintf(buf, "[0x%x:", size);
147  val_size += size * 5; /* len of all "0x%02x," and "0x%02x]" */
148  for (i = 0; i < cnt; ++i) {
149  uuid_generate_random(uuid);
150  uuid_unparse(uuid, str_uuid);
151  fprintf(f, "%d ", val_size);
152  fprintf(f, "[0x%x:", size);
153  m0_console_printf("%" PRIu64 "\n", i);
154  for (j = 0; j < len; ++j)
155  fprintf(f, "0x%02x,", str_uuid[j]);
156  for (j = 0; j < size - len - 1; j++)
157  fprintf(f, "0x%02x,", 1);
158  fprintf(f, "0x%02x]\n", 1);
159  }
160  fclose(f);
161  return 0;
162 }
163 
164 static void log_hex_val(const char *tag, void *buf, int size)
165 {
166  int i;
167 
168  m0_console_printf("/%s/ ", tag);
169  for (i = 0; i < size; i++)
170  m0_console_printf("0x%02x ", *((char *)buf+i));
171 }
172 static void log_keys_vals(struct m0_bufvec *keys, struct m0_bufvec *vals)
173 {
174  int i;
175 
176  for (i = 0; i < keys->ov_vec.v_nr; i++) {
177  if (is_str) {
178  m0_console_printf("[%d]:\n", i);
179  m0_console_printf("\tKEY: %.*s\n", (int)keys->ov_vec.v_count[i],
180  (char *)keys->ov_buf[i]);
181  m0_console_printf("\tVAL: %.*s\n", (int)vals->ov_vec.v_count[i],
182  (char *)vals->ov_buf[i]);
183  } else {
184  m0_console_printf("[%d]:\n", i);
185  log_hex_val("KEY", keys->ov_buf[i], keys->ov_vec.v_count[i]);
186  m0_console_printf("\n");
187  log_hex_val("VAL", vals->ov_buf[i], vals->ov_vec.v_count[i]);
188  m0_console_printf("\n");
189  }
190  }
191  m0_console_printf("selected %d records\n",vals->ov_vec.v_nr);
192 }
193 static void log_fids(struct m0_fid_arr *fids, struct m0_bufvec *vals)
194 {
195  int i;
196 
197  for (i = 0; i < fids->af_count; i++)
198  if (vals != NULL)
199  m0_console_printf(FID_F" found rc %i\n",
200  FID_P(&fids->af_elems[i]),
201  *(int*)vals->ov_buf[i]);
202  else
203  m0_console_printf(FID_F"\n", FID_P(&fids->af_elems[i]));
204 }
205 
206 static int cmd_exec(struct index_cmd *cmd)
207 {
208  int rc;
209 
210  switch (cmd->ic_cmd) {
211  case CRT:
213  &cmd->ic_fids);
214  m0_console_printf("create done, rc: %i\n", rc);
215  break;
216  case DRP:
218  &cmd->ic_fids);
219  m0_console_printf("drop done, rc: %i\n", rc);
220  break;
221  case LST:
223  &cmd->ic_fids.af_elems[0], cmd->ic_cnt,
224  &cmd->ic_keys);
225  if (rc == 0) {
226  int i;
227  struct m0_fid *f;
228 
229  for (i = 0; i < cmd->ic_keys.ov_vec.v_nr; i++) {
230  f = (struct m0_fid*)cmd->ic_keys.ov_buf[i];
232  }
233  }
234  m0_console_printf("list done, rc: %i\n", rc);
235  break;
236  case LKP:
238  &cmd->ic_fids, &cmd->ic_vals);
239  if (rc == 0)
240  log_fids(&cmd->ic_fids, &cmd->ic_vals);
241  m0_console_printf("lookup done, rc: %i\n", rc);
242  break;
243  case PUT:
245  &cmd->ic_fids, &cmd->ic_keys,
246  &cmd->ic_vals);
247  m0_console_printf("put done, rc: %i\n", rc);
248  break;
249  case DEL:
251  &cmd->ic_fids, &cmd->ic_keys);
252  m0_console_printf("del done, rc: %i\n", rc);
253  break;
254  case GET:
256  &cmd->ic_fids.af_elems[0], &cmd->ic_keys,
257  &cmd->ic_vals);
258  if (rc == 0)
259  log_keys_vals(&cmd->ic_keys, &cmd->ic_vals);
260  m0_console_printf("get done, rc: %i\n", rc);
261  break;
262  case NXT:
264  &cmd->ic_fids.af_elems[0],
265  &cmd->ic_keys, cmd->ic_cnt,
266  &cmd->ic_vals);
267  if (rc == 0)
268  log_keys_vals(&cmd->ic_keys, &cmd->ic_vals);
269  m0_console_printf("next done, rc: %i\n", rc);
270  break;
271  case GENF:
272  rc = genf(cmd->ic_filename, cmd->ic_cnt);
273  break;
274  case GENV:
275  rc = genv(cmd->ic_filename, cmd->ic_cnt, cmd->ic_len);
276  break;
277  default:
278  rc = M0_ERR(-EINVAL);
279  M0_ASSERT(0);
280  }
281  return rc;
282 }
283 
284 static void ctx_init(struct index_ctx *ctx)
285 {
286  M0_SET0(ctx);
287 }
288 
289 static void ctx_fini(struct index_ctx *ctx)
290 {
291  int i;
292 
293  for (i = 0; i < ctx->ictx_nr; ++i) {
294  m0_free(ctx->ictx_cmd[i].ic_fids.af_elems);
295  m0_bufvec_free(&ctx->ictx_cmd[i].ic_keys);
296  m0_bufvec_free(&ctx->ictx_cmd[i].ic_vals);
297  }
298 }
299 
300 int index_execute(int argc, char **argv)
301 {
302  struct index_ctx ctx;
303  int rc;
304  int i;
305 
306  ctx_init(&ctx);
307  rc = index_parser_args_process(&ctx, argc, argv);
308  if (rc == 0) {
309  for (i = 0; i < ctx.ictx_nr && rc == 0; i++)
310  rc = cmd_exec(&ctx.ictx_cmd[i]);
311  }
312  ctx_fini(&ctx);
313  return M0_RC(rc);
314 }
315 
317 {
318  int rc;
319 
320  M0_SET0(&cc_ctx);
322  if (rc == 0) {
325  m0_instance());
327  }
328  return M0_RC(rc);
329 }
330 
331 void index_fini(void)
332 {
333  instance_fini();
334 }
335 
336 void index_usage(void)
337 {
339  "\n\tINDEX subsystem\n"
340  "\t 'index [commands]'- name of subsystem\n"
341  "\t Commands:\n");
343 }
344 
345 #undef M0_TRACE_SUBSYSTEM
346 
349 /*
350  * Local variables:
351  * c-indentation-style: "K&R"
352  * c-basic-offset: 8
353  * tab-width: 8
354  * fill-column: 80
355  * scroll-step: 1
356  * End:
357  */
358 /*
359  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
360  */
int index_drop(struct m0_realm *parent, struct m0_fid_arr *fids)
Definition: index_op.c:98
void index_usage(void)
Definition: index.c:336
struct m0_client * ci_instance
Definition: index.c:49
struct m0_uint128 uuid[1000]
Definition: uuid.c:73
#define M0_PRE(cond)
struct m0_client * m0_instance()
Definition: index.c:61
Definition: index.c:52
Definition: index.h:38
int index_create(struct m0_container *container, struct m0_uint128 *fid)
Definition: example2.c:80
static void instance_fini(void)
Definition: index.c:101
#define NULL
Definition: misc.h:38
int index_del(struct m0_realm *parent, struct m0_fid_arr *fids, struct m0_bufvec *keys)
Definition: index_op.c:227
char * ci_prof
Definition: index.c:48
char * cp_local_addr
Definition: common.h:35
char * ci_ha_addr
Definition: index.c:46
char * ci_confd_addr
Definition: index.c:47
static bool x
Definition: sm.c:168
static FILE * f
Definition: adieu.c:79
static uint64_t tag(uint8_t code, uint64_t id)
Definition: addb2.c:1047
void m0_console_printf(const char *fmt,...)
Definition: trace.c:801
static int cmd_exec(struct index_cmd *cmd)
Definition: index.c:206
struct m0_vec ov_vec
Definition: vec.h:147
struct m0_fid * af_elems
Definition: fid.h:45
void m0_client_fini(struct m0_client *m0c, bool fini_m0)
Definition: client_init.c:1711
Definition: idx.h:70
int index_next(struct m0_realm *parent, struct m0_fid *fid, struct m0_bufvec *keys, int cnt, struct m0_bufvec *vals)
Definition: index_op.c:266
#define M0_SET0(obj)
Definition: misc.h:64
int m0_client_init(struct m0_client **m0c, struct m0_config *conf, bool init_m0)
Definition: client_init.c:1533
const struct m0_uint128 M0_UBER_REALM
Definition: client.c:85
Definition: config.py:1
Definition: index.h:37
void ** ov_buf
Definition: vec.h:149
static struct m0_fid ifid(uint64_t x, uint64_t y)
Definition: index.c:66
Definition: sock.c:887
struct m0_fid fid
Definition: di.c:46
static void log_keys_vals(struct m0_bufvec *keys, struct m0_bufvec *vals)
Definition: index.c:172
return M0_RC(rc)
M0_INTERNAL void m0_bufvec_free(struct m0_bufvec *bufvec)
Definition: vec.c:395
char * cp_ha_addr
Definition: common.h:36
int i
Definition: dir.c:1033
#define PRIu64
Definition: types.h:58
struct m0_realm co_realm
Definition: client.h:881
static struct nlx_ping_client_params * params
return M0_ERR(-EOPNOTSUPP)
Definition: cnt.h:36
static struct m0_ctx cc_ctx
Definition: index.c:59
#define M0_FID_TINIT(type, container, key)
Definition: fid.h:90
static int genf(char *filename, int cnt)
Definition: index.c:109
static void ctx_init(struct index_ctx *ctx)
Definition: index.c:284
Definition: parser.c:72
struct m0_entity re_entity
Definition: client.h:871
#define M0_ASSERT(cond)
bool is_str
Definition: cmd_main.c:64
Definition: index.h:45
struct m0_bufvec ic_keys
Definition: index.h:57
static void ctx_fini(struct index_ctx *ctx)
Definition: index.c:289
int ic_cnt
Definition: index.h:59
int ic_len
Definition: index.h:60
void index_fini(void)
Definition: index.c:331
static int instance_init(struct params *params)
Definition: index.c:71
char * ci_laddr
Definition: index.c:45
int index_get(struct m0_container *container, struct m0_uint128 *fid)
Definition: example2.c:153
Definition: fid.h:43
uint32_t v_nr
Definition: vec.h:51
int32_t sm_rc
Definition: sm.h:336
static const struct m0_fid fids[]
Definition: diter.c:76
m0_bcount_t * v_count
Definition: vec.h:53
int index_lookup(struct m0_realm *parent, struct m0_fid_arr *fids, struct m0_bufvec *rets)
Definition: index_op.c:146
#define FID_P(f)
Definition: fid.h:77
void index_parser_print_command_help(void)
Definition: index_parser.c:444
struct m0_config cc_conf
Definition: index.c:54
struct m0_instance cc_instance
Definition: index.c:55
static void log_fids(struct m0_fid_arr *fids, struct m0_bufvec *vals)
Definition: index.c:193
Definition: fid.h:38
Definition: index.h:46
Definition: common.h:34
struct m0_container cc_parent
Definition: index.c:56
m0_bcount_t size
Definition: di.c:39
Definition: index.h:40
static struct m0 instance
Definition: main.c:78
Definition: index.h:44
int index_list(struct m0_realm *parent, struct m0_fid *fid, int cnt, struct m0_bufvec *keys)
Definition: index_op.c:119
void m0_container_init(struct m0_container *con, struct m0_realm *parent, const struct m0_uint128 *id, struct m0_client *instance)
Definition: realm.c:31
char * cp_proc_fid
Definition: common.h:39
int index_put(struct m0_container *container, struct m0_uint128 *fid)
Definition: example2.c:117
Definition: index.h:39
char * ic_filename
Definition: index.h:61
Definition: nucleus.c:42
struct m0_sm en_sm
Definition: client.h:732
static int genv(char *filename, int cnt, int size)
Definition: index.c:128
int index_init(struct params *params)
Definition: index.c:316
void m0_free(void *data)
Definition: memory.c:146
int ic_cmd
Definition: index.h:55
struct params * cc_params
Definition: index.c:53
struct m0_bufvec ic_vals
Definition: index.h:58
static void log_hex_val(const char *tag, void *buf, int size)
Definition: index.c:164
int32_t rc
Definition: trigger_fop.h:47
int index_parser_args_process(struct index_ctx *ctx, int argc, char **argv)
Definition: index_parser.c:418
char * cp_prof
Definition: common.h:38
struct m0_fid_arr ic_fids
Definition: index.h:56
#define FID_F
Definition: fid.h:75
Definition: vec.h:145
int index_execute(int argc, char **argv)
Definition: index.c:300