コード例 #1
0
ファイル: riakdrv.c プロジェクト: dhull/riak-c-driver
int riak_put(RIAK_CONN * connstruct, char * bucket, char * key, char * data) {
	RpbPutReq putReq;
	RpbContent content;
	RpbPutResp * putResp;
	RpbErrorResp * errorResp;
	int reqSize;
	char * buffer;
	RIAK_OP command, result;

	rpb_put_req__init(&putReq);
	rpb_content__init(&content);

	putReq.bucket.data = bucket;
	putReq.bucket.len = strlen(bucket);
	putReq.key.data = key;
	putReq.key.len = strlen(key);
	content.value.data = data;
	content.value.len = strlen(data);
	content.links = NULL;
	content.usermeta = NULL;
	putReq.content = &content;

	reqSize = rpb_put_req__get_packed_size(&putReq);
	buffer = malloc(reqSize);
	rpb_put_req__pack(&putReq,buffer);

	command.msgcode = RPB_PUT_REQ;
	command.msg = buffer;
	command.length = reqSize+1;
	result.msg = NULL;

	connstruct->last_error = RERR_OK;

	if(riak_exec_op(connstruct, &command, &result)!=0)
		return 1;

	/* Received correct response */
	if(result.msgcode == RPB_PUT_RESP) {
		/* not used right now */
		/*putResp = rpb_put_resp__unpack(NULL, result.length-1, result.msg);

		rpb_put_resp__free_unpacked(putResp, NULL);*/
		/* Riak reported an error */
	} else if(result.msgcode == RPB_ERROR_RESP) {
		errorResp = rpb_error_resp__unpack(NULL, result.length-1, result.msg);

		connstruct->last_error = RERR_BUCKET_LIST;
		riak_copy_error(connstruct, errorResp);

		rpb_error_resp__free_unpacked(errorResp, NULL);
		return 1;
		/* Something really bad happened. :( */
	} else {
		connstruct->last_error = RERR_UNKNOWN;
		return 1;
	}

	return 0;
}
コード例 #2
0
ファイル: pb_messages.c プロジェクト: denisphillips/debris
int riak_pb_put(struct riak_pb_transport *pbtransport,
                struct riak_object *riak_obj,
                struct riak_put_options *options,
                struct riak_response *response) {

   void *msgbuf;
   unsigned msglen;
   RpbPutReq putmsg = RPB_PUT_REQ__INIT;

   putmsg.bucket.data = malloc(riak_obj->bucket.len);
   memcpy(&putmsg.bucket.data, &riak_obj->bucket.data, riak_obj->bucket.len);
   putmsg.bucket.len  = riak_obj->bucket.len;

   // TODO: key may not be present
   putmsg.has_key=1;
   putmsg.key.data = malloc(riak_obj->key.len);
   memcpy(&putmsg.key.data, &riak_obj->key.data, riak_obj->key.len);
   putmsg.key.len  = riak_obj->key.len;

   RpbContent *content = malloc(sizeof(RpbContent));
   rpb_content__init(content);
   putmsg.content = content;

   content->value.len  = riak_obj->value.len;
   content->value.data = (uint8_t*)malloc(riak_obj->value.len);
   memcpy(&putmsg.content->value.data, &riak_obj->value.data, riak_obj->value.len);

   // process get options
   if(options != 0) {
     // TODO
   }
   msglen = rpb_put_req__get_packed_size (&putmsg);
   msgbuf = malloc (msglen);
   rpb_put_req__pack (&putmsg, msgbuf);

   struct pb_request request;
   request.reqid = MSG_RPBPUTREQ;
   request.msglength = msglen;
   request.reqdata = msgbuf;
   pbtransport->send_message(pbtransport->transport_data, &request);
   free(msgbuf);

   struct pb_response pbresponse;
   pbresponse.expected_respid = MSG_RPBPUTRESP;

   int pbr = pbtransport->receive_message(pbtransport->transport_data, &pbresponse);
   if(pbr >= 0) {
     // decode the PB response etc
     RpbPutResp *putresp = rpb_put_resp__unpack(NULL, pbresponse.msglength, pbresponse.respdata);
     rpb_put_resp__free_unpacked(putresp, NULL);
     free(pbresponse.respdata);
     return 0;
   } else {
     return -1;
   }
   /// TODO: errors!
   return 0;
}
コード例 #3
0
ファイル: riack_kv.c プロジェクト: iehoyeba/riack
int riack_put(struct RIACK_CLIENT *client,
			struct RIACK_OBJECT object,
			struct RIACK_OBJECT* preturned_object,
			struct RIACK_PUT_PROPERTIES* properties)
{
	int result;
	size_t packed_size;
	struct RIACK_PB_MSG msg_req, *msg_resp;
	ProtobufCAllocator pb_allocator;
	RpbPutReq put_req;
	RpbPutResp *put_resp;
	uint8_t *request_buffer;

    if (!client || !object.bucket.value) {
		return RIACK_ERROR_INVALID_INPUT;
	}

	pb_allocator = riack_pb_allocator(&client->allocator);
	result = RIACK_ERROR_COMMUNICATION;
	rpb_put_req__init(&put_req);
	riack_copy_object_to_rpbputreq(client, &object, &put_req);
    riack_set_object_properties(properties, &put_req);

	packed_size = rpb_put_req__get_packed_size(&put_req);
	request_buffer = (uint8_t*)RMALLOC(client, packed_size);
	if (request_buffer) {
		rpb_put_req__pack(&put_req, request_buffer);
		msg_req.msg_code = mc_RpbPutReq;
		msg_req.msg_len = packed_size;
		msg_req.msg = request_buffer;
		if ((riack_send_message(client, &msg_req) > 0)&&
			(riack_receive_message(client, &msg_resp) > 0))
		{
			if (msg_resp->msg_code == mc_RpbPutResp) {
				result = RIACK_SUCCESS;
				if (preturned_object) {
					put_resp = rpb_put_resp__unpack(&pb_allocator, msg_resp->msg_len, msg_resp->msg);
					if (put_resp) {
						riak_set_object_response_values(client, preturned_object, put_resp);
						rpb_put_resp__free_unpacked(put_resp, &pb_allocator);
					} else {
						result = RIACK_FAILED_PB_UNPACK;
					}
				}
			} else {
				riack_got_error_response(client, msg_resp);
				result = RIACK_ERROR_RESPONSE;
			}
			riack_message_free(client, &msg_resp);
		}
		RFREE(client, request_buffer);
	}
    riack_free_copied_rpb_put_req(client, &put_req);
	return result;
}