Motr  M0
example2.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 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  * Example Motr application to create index, put key&val into index,
24  * get val from index, and then delete the index.
25  */
26 
27 /*
28  * Please change the dir according to you development environment.
29  *
30  * How to build:
31  * gcc -I/work/cortx-motr -DM0_EXTERN=extern -DM0_INTERNAL= -Wno-attributes \
32  * -L/work/cortx-motr/motr/.libs -lmotr \
33  * example2.c -o example2
34  *
35  * Please change the configuration according to you development environment.
36  *
37  * How to run:
38  * LD_LIBRARY_PATH=/work/cortx-motr/motr/.libs/ \
39  * ./example2 172.16.154.179@tcp:12345:34:1 172.16.154.179@tcp:12345:33:1000 \
40  * "<0x7000000000000001:0>" "<0x7200000000000001:64>" 12345670
41  */
42 
43 #include "motr/client.h"
44 #include "lib/assert.h"
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <errno.h>
48 
49 enum {
50  KV_COUNT = 10,
51  KV_LEN = 32,
52 };
53 
54 static void op_entity_fini(struct m0_entity *e, struct m0_op **ops)
55 {
56  m0_op_fini(ops[0]);
57  m0_op_free(ops[0]);
58  ops[0] = NULL;
59  m0_entity_fini(e);
60 }
61 
62 static int op_launch_wait_fini(struct m0_entity *e,
63  struct m0_op **ops,
64  int *sm_rc)
65 {
66  int rc;
67 
68  m0_op_launch(ops, 1);
71  printf("rc=%d op_rc=%d\n", rc, ops[0]->op_rc);
72  if (sm_rc != NULL) {
73  /* Save retcodes. */
74  *sm_rc = ops[0]->op_rc;
75  }
76  op_entity_fini(e, ops);
77  return rc;
78 }
79 
81 {
82  struct m0_op *ops[1] = { NULL };
83  struct m0_idx idx;
84  int rc;
85 
87 
88  rc = m0_entity_create(NULL, &idx.in_entity, &ops[0]);
89  if (rc == 0)
91 
92  printf("index create rc: %i\n", rc);
93  return rc;
94 }
95 
97 {
98  struct m0_op *ops[1] = { NULL };
99  struct m0_idx idx;
100  int rc;
101 
102  m0_idx_init(&idx, &container->co_realm, fid);
103 
104  rc = m0_entity_open(&idx.in_entity, &ops[0]);
105  if (rc == 0) {
106  rc = m0_entity_delete(&idx.in_entity, &ops[0]);
107  if (rc == 0)
109  else
111  }
112 
113  printf("index delete rc: %i\n", rc);
114  return rc;
115 }
116 
118 {
119  struct m0_op *ops[1] = { NULL };
120  struct m0_idx idx;
121  struct m0_bufvec keys;
122  struct m0_bufvec vals;
123  int i;
124  int32_t rcs[KV_COUNT];
125  int rc;
126 
127  rc = m0_bufvec_alloc(&keys, KV_COUNT, KV_LEN);
128  M0_ASSERT(rc == 0);
129 
130  rc= m0_bufvec_alloc(&vals, KV_COUNT, KV_LEN);
131  M0_ASSERT(rc == 0);
132 
133  for (i = 0; i < KV_COUNT; i++) {
134  memset(keys.ov_buf[i], 'A' + i, KV_LEN - 1);
135  memset(vals.ov_buf[i], 'a' + i, KV_LEN - 1);
136  }
137 
138  m0_idx_init(&idx, &container->co_realm, fid);
139  rc = m0_idx_op(&idx, M0_IC_PUT, &keys, &vals, rcs, 0, &ops[0]);
140  if (rc == 0)
142  for (i = 0; rc == 0 && i < KV_COUNT; i++) {
143  printf("PUT %d: key=%s val=%s\n",
144  i, (char*)keys.ov_buf[i], (char*)vals.ov_buf[i]);
145  }
146  m0_bufvec_free(&keys);
147  m0_bufvec_free(&vals);
148 
149  printf("index put rc: %i\n", rc);
150  return rc;
151 }
152 
154 {
155  struct m0_op *ops[1] = { NULL };
156  struct m0_idx idx;
157  struct m0_bufvec keys;
158  struct m0_bufvec vals;
159  int i;
160  int32_t rcs[KV_COUNT];
161  int rc;
162 
163  rc = m0_bufvec_alloc(&keys, KV_COUNT, KV_LEN);
164  M0_ASSERT(rc == 0);
165  /* For GET operation, we don't alloc actual buf */
167  M0_ASSERT(rc == 0);
168 
169  for (i = 0; i < KV_COUNT; i++) {
170  memset(keys.ov_buf[i], 'A' + i, KV_LEN - 1);
171  }
172 
173  m0_idx_init(&idx, &container->co_realm, fid);
174  rc = m0_idx_op(&idx, M0_IC_GET, &keys, &vals, rcs, 0, &ops[0]);
175  if (rc == 0)
177  for (i = 0; rc == 0 && i < KV_COUNT; i++) {
178  printf("GOT %d: key=%s val=%s\n",
179  i, (char*)keys.ov_buf[i], (char*)vals.ov_buf[i]);
180  }
181 
182  m0_bufvec_free(&keys);
183  m0_bufvec_free(&vals);
184 
185  printf("index get rc: %i\n", rc);
186  return rc;
187 }
188 
189 int main(int argc, char *argv[])
190 {
191  static struct m0_client *m0_instance = NULL;
192  static struct m0_container motr_container;
193  static struct m0_config motr_conf;
194  static struct m0_idx_dix_config motr_dix_conf;
195  struct m0_uint128 index_id = { 0, 0 };
196  int rc;
197 
198  if (argc != 6) {
199  printf("%s HA_ADDR LOCAL_ADDR Profile_fid Process_fid obj_id\n",
200  argv[0]);
201  exit(-1);
202  }
203  index_id.u_lo = atoll(argv[5]);
204  if (index_id.u_lo < M0_ID_APP.u_lo) {
205  printf("obj_id invalid. Please refer to M0_ID_APP "
206  "in motr/client.c\n");
207  exit(-EINVAL);
208  }
209 
211 
212  motr_conf.mc_is_oostore = true;
214  motr_conf.mc_ha_addr = argv[1];
215  motr_conf.mc_local_addr = argv[2];
216  motr_conf.mc_profile = argv[3];
217  motr_conf.mc_process_fid = argv[4];
222 
224  if (rc != 0) {
225  printf("error in m0_client_init: %d\n", rc);
226  exit(rc);
227  }
228 
231  if (rc != 0) {
232  printf("error in m0_container_init: %d\n", rc);
233  goto out;
234  }
235 
236  /* index FID must be m0_dix_fid_type */
237  m0_fid_tassume((struct m0_fid*)&index_id, &m0_dix_fid_type);
238 
239  rc = index_create(&motr_container, &index_id);
240  if (rc == 0) {
241  rc = index_put(&motr_container, &index_id);
242  if (rc == 0) {
243  printf("index put succeeded\n");
244  rc = index_get(&motr_container, &index_id);
245  if (rc == 0)
246  printf("index get succeeded\n");
247  }
248  rc = index_delete(&motr_container, &index_id);
249  }
250 
251 out:
253  printf("app completed: %d\n", rc);
254  return rc;
255 }
256 
257 /*
258  * Local variables:
259  * c-indentation-style: "K&R"
260  * c-basic-offset: 8
261  * tab-width: 8
262  * fill-column: 80
263  * scroll-step: 1
264  * End:
265  */
266 /*
267  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
268  */
Definition: client.h:835
void m0_entity_fini(struct m0_entity *entity)
Definition: client.c:438
int index_create(struct m0_container *container, struct m0_uint128 *fid)
Definition: example2.c:80
#define NULL
Definition: misc.h:38
const char * mc_process_fid
Definition: client.h:937
const m0_time_t M0_TIME_NEVER
Definition: time.c:108
void m0_op_fini(struct m0_op *op)
Definition: client.c:847
static void op_entity_fini(struct m0_entity *e, struct m0_op **ops)
Definition: example2.c:54
struct m0_container container
void m0_client_fini(struct m0_client *m0c, bool fini_m0)
Definition: client_init.c:1711
Definition: idx.h:70
#define M0_BITS(...)
Definition: misc.h:236
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
void ** ov_buf
Definition: vec.h:149
const struct m0_uint128 M0_ID_APP
Definition: client.c:92
struct m0_fid fid
Definition: di.c:46
M0_INTERNAL int m0_bufvec_alloc(struct m0_bufvec *bufvec, uint32_t num_segs, m0_bcount_t seg_size)
Definition: vec.c:220
int32_t m0_op_wait(struct m0_op *op, uint64_t bits, m0_time_t to)
Definition: client.c:739
int m0_idx_op(struct m0_idx *idx, enum m0_idx_opcode opcode, struct m0_bufvec *keys, struct m0_bufvec *vals, int32_t *rcs, uint32_t flags, struct m0_op **op)
Definition: idx.c:554
static struct m0_idx_dix_config motr_dix_conf
Definition: example1.c:51
M0_INTERNAL void m0_bufvec_free(struct m0_bufvec *bufvec)
Definition: vec.c:395
struct m0_entity in_entity
Definition: client.h:836
int main(int argc, char *argv[])
Definition: example2.c:189
int i
Definition: dir.c:1033
struct m0_realm co_realm
Definition: client.h:881
const char * mc_ha_addr
Definition: client.h:935
Definition: client.h:641
struct m0_entity re_entity
Definition: client.h:871
M0_INTERNAL const struct m0_fid_type m0_dix_fid_type
Definition: cas.c:168
#define M0_ASSERT(cond)
int32_t op_rc
Definition: client.h:652
static struct m0_container motr_container
Definition: example1.c:49
bool mc_is_oostore
Definition: client.h:920
void m0_op_launch(struct m0_op **op, uint32_t nr)
Definition: client.c:725
int index_get(struct m0_container *container, struct m0_uint128 *fid)
Definition: example2.c:153
static struct m0_config motr_conf
Definition: example1.c:50
int32_t sm_rc
Definition: sm.h:336
static int op_launch_wait_fini(struct m0_entity *e, struct m0_op **ops, int *sm_rc)
Definition: example2.c:62
bool mc_is_read_verify
Definition: client.h:925
void * mc_idx_service_conf
Definition: client.h:957
int m0_entity_create(struct m0_fid *pool, struct m0_entity *entity, struct m0_op **op)
Definition: obj.c:801
uint32_t mc_max_rpc_msg_size
Definition: client.h:949
const char * mc_local_addr
Definition: client.h:933
int mc_idx_service_id
Definition: client.h:956
Definition: fid.h:38
bool kc_create_meta
Definition: idx.h:183
uint32_t mc_tm_recv_queue_min_len
Definition: client.h:944
int m0_entity_delete(struct m0_entity *entity, struct m0_op **op)
Definition: obj.c:824
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
const char * mc_profile
Definition: client.h:938
int index_put(struct m0_container *container, struct m0_uint128 *fid)
Definition: example2.c:117
#define out(...)
Definition: gen.c:41
void m0_op_free(struct m0_op *op)
Definition: client.c:885
struct m0_sm en_sm
Definition: client.h:732
int m0_entity_open(struct m0_entity *entity, struct m0_op **op)
Definition: obj.c:885
struct m0_fom_ops ops
Definition: io_foms.c:623
uint64_t u_lo
Definition: types.h:37
int32_t rc
Definition: trigger_fop.h:47
int index_delete(struct m0_container *container, struct m0_uint128 *fid)
Definition: example2.c:96
void m0_idx_init(struct m0_idx *idx, struct m0_realm *parent, const struct m0_uint128 *id)
Definition: idx.c:626
Definition: vec.h:145
M0_INTERNAL int m0_bufvec_empty_alloc(struct m0_bufvec *bufvec, uint32_t num_segs)
Definition: vec.c:213
M0_INTERNAL void m0_fid_tassume(struct m0_fid *fid, const struct m0_fid_type *ft)
Definition: fid.c:146