Example #1
0
void update(riak_config *cfg, riak_connection *cxn) {
    fprintf(stdout, "-------------------------------\n");
    fprintf(stdout, "Test Riak UPDATE\n");
    fprintf(stdout, "-------------------------------\n");

    // create "binaries" on the heap to store bucket and key
    // for the object we are going to GET
    // a well behaved client should check for NULL
    // when allocating new structs
    riak_binary *bucket_bin    = riak_binary_copy_from_string(cfg, "TestBucket");
    riak_binary *key_bin       = riak_binary_copy_from_string(cfg, "TestKey");
    riak_binary *new_value_bin = riak_binary_copy_from_string(cfg, "MyValue");

    // check this for errors after performing an operation
    riak_error err;

    // allocate a struct to set GET options, specifically
    // to set the basic_quorum & r options
    riak_get_options *get_options = riak_get_options_new(cfg);
    riak_get_options_set_basic_quorum(get_options, RIAK_TRUE);
    riak_get_options_set_r(get_options, 2);

    riak_get_response *get_response = NULL;
    err = riak_get(cxn, bucket_bin, key_bin, get_options, &get_response);
    if(err) {
      fprintf(stderr, "Error fetching object for update\n");
      exit(1);
    }
    riak_object *obj = riak_get_get_content(get_response)[0];
    riak_object_set_value(cfg, obj, new_value_bin);

    riak_put_response *put_response = NULL;
    riak_put_options *put_options = riak_put_options_new(cfg);
    err = riak_put(cxn, obj, put_options, &put_response);
    if(err) {
      fprintf(stderr, "Error storing updated object\n");
      exit(1);
    }

    // cleanup
    riak_get_response_free(cfg, &get_response);
    riak_put_response_free(cfg, &put_response);
    riak_binary_free(cfg, &bucket_bin);
    riak_binary_free(cfg, &key_bin);
    riak_get_options_free(cfg, &get_options);
    riak_put_options_free(cfg, &put_options);

    fprintf(stdout, "Ok\n");
}
Example #2
0
// main function that retrieves data from riak kvs 
static int namak_handler(request_rec *r)
{
    // check handler name
    if (strcmp(r->handler, "namak")) {
        return DECLINED;
    }
    // if method is not GET do not process
    if (r->method_number != M_GET)
        return DECLINED;
    // a riak_config serves as your per-thread state to interact with Riak
    riak_config *cfg;
    // use the default configuration
    riak_error err = riak_config_new_default(&cfg);
    if (err) {
        ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, r->server, "failed to initailize default config");
        return DECLINED;
    }
    // make a table to store GET requests
    apr_table_t *params = apr_table_make(r->pool, 5);
    // debug code
    // ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, r->server, r->args);
    // parse request parameters
    if (r->args) {
        char *args = apr_pstrdup(r->pool, r->args);
        if (args == NULL) {
            ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, r->server, "Can not allocate memory pool");
            return HTTP_BAD_REQUEST;
        }
        char *tok, *val;
        while (args && *args) {
            if ((val = ap_strchr(args, '='))) {
                *val++ = '\0';
                if ((tok = ap_strchr(val, '&')))
                    *tok++ = '\0';
                apr_table_setn(params, args, val);
                args = tok;
            }
            else
                return HTTP_BAD_REQUEST;
        }
    }
    // get params to send them to Riak
    const char *bucket = apr_table_get(params, "bucket");
    const char *key = apr_table_get(params, "key");
    if (bucket == NULL || key == NULL)
        return DECLINED;
    riak_binary *bucket_type_bin = riak_binary_copy_from_string(cfg, "default"); // only for Riak 2.0?
    riak_binary *bucket_bin   = riak_binary_copy_from_string(cfg, bucket);
    riak_binary *key_bin      = riak_binary_copy_from_string(cfg, key);
    // check for memory allocation problems
    if (bucket_bin == NULL || key_bin    == NULL) {
        riak_config_free(&cfg);
        return DECLINED;
    }
    // Supporting Options and outputs
    riak_get_options *get_options;
    // Every possible message response type
    riak_get_response *get_response = NULL;
    riak_connection  *cxn   = NULL;
    // get Riak host and port from httpd.conf
    namak_svr_cfg* svr = ap_get_module_config(r->per_dir_config, &namak_module);
    if (svr->riak_server == NULL || svr->riak_port == NULL)
    {
        riak_binary_free(cfg, &bucket_type_bin);
        riak_binary_free(cfg, &bucket_bin);
        riak_binary_free(cfg, &key_bin);
        riak_config_free(&cfg);
        return DECLINED;
    }
    // Create a connection with the default address resolver
    err = riak_connection_new(cfg, &cxn, svr->riak_server, svr->riak_port, NULL);
    if (err) {
        ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, r->server,
                "failed to connect Riak(%s:%s)", svr->riak_server, svr->riak_port);
        riak_binary_free(cfg, &bucket_type_bin);
        riak_binary_free(cfg, &bucket_bin);
        riak_binary_free(cfg, &key_bin);
        riak_config_free(&cfg);
        return HTTP_SERVICE_UNAVAILABLE;
    }
    // handle possible operations from the command line
    get_options = riak_get_options_new(cfg);
    if (get_options == NULL) {
        ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, r->server, "failed to initailize option config");
        riak_binary_free(cfg, &bucket_type_bin);
        riak_binary_free(cfg, &bucket_bin);
        riak_binary_free(cfg, &key_bin);
        riak_config_free(&cfg);
        return DECLINED;
    }
    riak_get_options_set_basic_quorum(get_options, RIAK_TRUE);
    riak_get_options_set_r(get_options, 1);
    // get data from Riak kvs
    err = riak_get(cxn, bucket_type_bin, bucket_bin, key_bin, get_options, &get_response);
    if (err == ERIAK_OK) {
        // if response does not have at least one object, just return 404
        if (riak_get_get_n_content(get_response) < 1)
        {
            riak_binary_free(cfg, &bucket_type_bin);
            riak_binary_free(cfg, &bucket_bin);
            riak_binary_free(cfg, &key_bin);
            riak_config_free(&cfg);
            return HTTP_NOT_FOUND;
        }
        // get the first object
        riak_object **objects = riak_get_get_content(get_response);
        int i = 0;
        for(i = 0; i < riak_get_get_n_content(get_response); i++) {
            riak_object *obj = objects[i];
            r->content_type = riak_binary_data(riak_object_get_content_type(obj));
            ap_rprintf(r,"%s\n",riak_binary_data(riak_object_get_value(obj)));
            break;
        }
    }
    riak_get_response_free(cfg, &get_response);
    riak_get_options_free(cfg, &get_options);
    if (err) {
        riak_binary_free(cfg, &bucket_type_bin);
        riak_binary_free(cfg, &bucket_bin);
        riak_binary_free(cfg, &key_bin);
        riak_config_free(&cfg);
        return HTTP_SERVICE_UNAVAILABLE;
    }
    // cleanup
    riak_binary_free(cfg, &bucket_type_bin);
    riak_binary_free(cfg, &bucket_bin);
    riak_binary_free(cfg, &key_bin);
    riak_config_free(&cfg);
    return OK;
}