read-op-impl.c 7.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * (C) 2017 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

#include <stdlib.h>
#include <string.h>
#include "mobject-store-config.h"
#include "utlist.h"
#include "libmobject-store.h"
#include "log.h"
13
#include "read-op-impl.h"
14 15 16 17 18 19 20
#include "completion.h"

mobject_store_read_op_t mobject_store_create_read_op(void)
{
	mobject_store_read_op_t read_op = 
		(mobject_store_read_op_t)calloc(1, sizeof(*read_op));
	MOBJECT_ASSERT(read_op != MOBJECT_READ_OP_NULL, "Could not allocate read_op");
21
	read_op->actions     = (rd_action_base_t)0;
22
	read_op->bulk_handle = HG_BULK_NULL;
23
	read_op->ready       = 0;
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	return read_op;
}

void mobject_store_release_read_op(mobject_store_read_op_t read_op)
{
	if(read_op == MOBJECT_READ_OP_NULL) return;
	
	rd_action_base_t action, tmp;

	DL_FOREACH_SAFE(read_op->actions, action, tmp) {
		DL_DELETE(read_op->actions, action);
		free(action);
	}

	free(read_op);
}

void mobject_store_read_op_stat(mobject_store_read_op_t read_op,
                                uint64_t *psize,
                                time_t *pmtime,
                                int *prval)
{
	MOBJECT_ASSERT(read_op != MOBJECT_READ_OP_NULL, "invalid mobject_store_read_op_t obect");
47
	MOBJECT_ASSERT(!(read_op->ready), "can't modify a read_op that is ready to be processed");
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

	rd_action_stat_t action = (rd_action_stat_t)calloc(1, sizeof(*action));
	action->base.type       = READ_OPCODE_STAT;
	action->psize           = psize;
	action->pmtime          = pmtime;
    action->prval           = prval;

	READ_ACTION_UPCAST(base, action);
	DL_APPEND(read_op->actions, base);
}

void mobject_store_read_op_read(mobject_store_read_op_t read_op,
                                uint64_t offset,
                                size_t len,
                                char *buffer,
                                size_t *bytes_read,
                                int *prval)
{
	MOBJECT_ASSERT(read_op != MOBJECT_READ_OP_NULL, "invalid mobject_store_read_op_t obect");
67
	MOBJECT_ASSERT(!(read_op->ready), "can't modify a read_op that is ready to be processed");
68

69 70 71 72 73 74 75
	rd_action_read_t action   = (rd_action_read_t)calloc(1, sizeof(*action));
	action->base.type         = READ_OPCODE_READ;
	action->offset            = offset;
	action->len               = len;
	action->buffer.as_pointer = buffer;
	action->bytes_read        = bytes_read;
	action->prval             = prval;
76 77 78 79 80

	READ_ACTION_UPCAST(base, action);
	DL_APPEND(read_op->actions, base);
}

81 82 83 84 85 86 87
void mobject_store_read_op_omap_get_keys(mobject_store_read_op_t read_op,
				                         const char *start_after,
				                         uint64_t max_return,
				                         mobject_store_omap_iter_t *iter,
                                         int *prval)
{
	MOBJECT_ASSERT(read_op != MOBJECT_READ_OP_NULL, "invalid mobject_store_read_op_t obect");
88
	MOBJECT_ASSERT(!(read_op->ready), "can't modify a read_op that is ready to be processed");
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

	size_t strl = strlen(start_after);
	
	rd_action_omap_get_keys_t action = (rd_action_omap_get_keys_t)calloc(1, sizeof(*action)+strl);
	action->base.type                = READ_OPCODE_OMAP_GET_KEYS;
	action->start_after              = action->data;
	action->max_return               = max_return;
	action->iter                     = iter;
	action->prval                    = prval;
	action->data_size                = strl+1;
	strcpy(action->data, start_after);

	READ_ACTION_UPCAST(base, action);
    DL_APPEND(read_op->actions, base);
}

105 106 107 108 109 110 111 112
void mobject_store_read_op_omap_get_vals(mobject_store_read_op_t read_op,
                                         const char *start_after,
                                         const char *filter_prefix,
                                         uint64_t max_return,
                                         mobject_store_omap_iter_t *iter,
                                         int *prval)
{
	MOBJECT_ASSERT(read_op != MOBJECT_READ_OP_NULL, "invalid mobject_store_read_op_t obect");
113
	MOBJECT_ASSERT(!(read_op->ready), "can't modify a read_op that is ready to be processed");
114 115 116 117 118 119 120 121 122 123 124 125 126

	// compute required size for embedded data
	size_t strl1 = strlen(start_after)+1;
	size_t strl2 = strlen(filter_prefix)+1;
	size_t extra_mem = strl1+strl2;

	rd_action_omap_get_vals_t action = (rd_action_omap_get_vals_t)calloc(1, sizeof(*action)-1+extra_mem);
	action->base.type                = READ_OPCODE_OMAP_GET_VALS;
	action->start_after              = action->data;
	action->filter_prefix            = action->data + strl1;
	action->max_return               = max_return;
	action->iter                     = iter;
	action->prval                    = prval;
127
	action->data_size                = extra_mem;
128 129 130 131 132 133 134 135 136 137 138 139 140 141
	strcpy(action->data, start_after);
	strcpy(action->data + strl1, filter_prefix);

	READ_ACTION_UPCAST(base, action);
	DL_APPEND(read_op->actions, base);
}

void mobject_store_read_op_omap_get_vals_by_keys(mobject_store_read_op_t read_op,
                                                 char const* const* keys,
                                                 size_t keys_len,
                                                 mobject_store_omap_iter_t *iter,
                                                 int *prval)
{
	MOBJECT_ASSERT(read_op != MOBJECT_READ_OP_NULL, "invalid mobject_store_read_op_t obect");
142
	MOBJECT_ASSERT(!(read_op->ready), "can't modify a read_op that is ready to be processed");
143 144 145 146 147 148 149 150 151 152 153
	
	// computing extra memory required to hold keys
	size_t extra_mem = 0;
	size_t i;
	for(i = 0; i < keys_len; i++) {
		extra_mem += strlen(keys[i]) + 1;
	}

	rd_action_omap_get_vals_by_keys_t action =
		(rd_action_omap_get_vals_by_keys_t)calloc(1, sizeof(*action) - 1 + extra_mem);
	action->base.type = READ_OPCODE_OMAP_GET_VALS_BY_KEYS;
154
	action->num_keys  = keys_len;
155 156
	action->iter      = iter;
	action->prval     = prval;
157 158
	action->data_size = extra_mem;
	char* s = action->data;
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 187 188 189 190 191 192 193 194 195 196
	for(i = 0; i < keys_len; i++) {
		strcpy(s, keys[i]);
		s += strlen(keys[i]) + 1;
	}
	
	READ_ACTION_UPCAST(base, action);
	DL_APPEND(read_op->actions, base);
}

int mobject_store_read_op_operate(mobject_store_read_op_t read_op,
                                  mobject_store_ioctx_t io,
                                  const char *oid,
                                  int flags)
{
	int r;
	MOBJECT_ASSERT(read_op != MOBJECT_READ_OP_NULL, "invalid mobject_store_read_op_t obect");
	mobject_store_completion_t completion = MOBJECT_COMPLETION_NULL;
	r = mobject_store_aio_create_completion(NULL, NULL, NULL, &completion);
	MOBJECT_ASSERT(0 == r, "Could not create completion object");
	r = mobject_store_aio_read_op_operate(read_op, io, completion, oid, flags);
	MOBJECT_ASSERT(0 == r, "Call to mobject_store_aio_read_op_operate failed");
	r = mobject_store_aio_wait_for_complete(completion);
	MOBJECT_ASSERT(0 == r, "Could not wait for completion");
	int ret = mobject_store_aio_get_return_value(completion);
	mobject_store_aio_release(completion);
	return ret;
}

int mobject_store_aio_read_op_operate(mobject_store_read_op_t read_op,
                                      mobject_store_ioctx_t io,
                                      mobject_store_completion_t completion,
                                      const char *oid,
                                      int flags)
{
	MOBJECT_ASSERT(read_op != MOBJECT_READ_OP_NULL, "invalid mobject_store_read_op_t obect");
	// TODO
}