memcache/memcache.c
author Tero Marttila <terom@fixme.fi>
Sat, 30 Aug 2008 19:13:15 +0300
changeset 49 10c7dce1a043
parent 46 8a832c0e01ee
permissions -rw-r--r--
autogenerate the memcache_test help output, and pipeline memcache requests
38
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
#include <stdlib.h>
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
#include "memcache.h"
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
#include "server.h"
41
540737bf6bac sending requests, and partial support for receiving -- incomplete, not tested
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
     6
#include "request.h"
38
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
#include "../common.h"
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
49
10c7dce1a043 autogenerate the memcache_test help output, and pipeline memcache requests
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
     9
struct memcache *memcache_alloc (memcache_cb cb_fn, char pipeline_requests) {
38
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
    struct memcache *mc = NULL;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    if ((mc = calloc(1, sizeof(*mc))) == NULL)
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
        ERROR("calloc");
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    
49
10c7dce1a043 autogenerate the memcache_test help output, and pipeline memcache requests
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
    15
    // store attributes
38
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    mc->cb_fn = cb_fn;
49
10c7dce1a043 autogenerate the memcache_test help output, and pipeline memcache requests
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
    17
    mc->pipeline_requests = pipeline_requests;
38
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
    // init server list
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
    LIST_INIT(&mc->server_list);
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
    // success
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
    return mc;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
error:
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
    if (mc)
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
        free(mc);
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
    
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
    return NULL;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
}
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
int memcache_add_server (struct memcache *mc, struct config_endpoint *endpoint, int max_connections) {
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
    struct memcache_server *server = NULL;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
    
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
    // alloc the server
49
10c7dce1a043 autogenerate the memcache_test help output, and pipeline memcache requests
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
    36
    if ((server = memcache_server_alloc(mc, endpoint, max_connections)) == NULL)
38
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
        goto error;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
    // enlist it
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
    LIST_INSERT_HEAD(&mc->server_list, server, serverlist_node);
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
    // done
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
    return 0;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
error:
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
    return -1;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
}
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
39
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    49
struct memcache_server *memcache_choose_server (struct memcache *mc, const struct memcache_key *key) {
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    50
    // XXX: support multiple servers
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    51
    return mc->server_list.lh_first;
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    52
}
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    53
44
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    54
static struct memcache_req *_memcache_req (struct memcache *mc, enum memcache_command cmd, const struct memcache_key *key, const struct memcache_obj *obj, const struct memcache_buf *buf, void *cb_arg) {
39
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    55
    struct memcache_req *req = NULL;
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    56
    struct memcache_server *server = NULL;
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    57
    
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    58
    // alloc the request
46
8a832c0e01ee bugfixed, enum->string mappings, test does requests
Tero Marttila <terom@fixme.fi>
parents: 44
diff changeset
    59
    if ((req = memcache_req_alloc(mc, cmd, key, obj, buf, cb_arg)) == NULL)
39
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    60
        ERROR("failed to allocate request");
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    61
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    62
    // pick a server
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    63
    if ((server = memcache_choose_server(mc, key)) == NULL)
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    64
        ERROR("failed to find a server to use");
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    65
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    66
    // enqueue it!
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    67
    if (memcache_server_add_req(server, req))
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    68
        ERROR("failed to hand over the request for processing");
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    69
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    70
    // success!
41
540737bf6bac sending requests, and partial support for receiving -- incomplete, not tested
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    71
    return req;
39
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    72
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    73
error:
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    74
    if (req)
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    75
        memcache_req_free(req);
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    76
41
540737bf6bac sending requests, and partial support for receiving -- incomplete, not tested
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    77
    return NULL;
39
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    78
}  
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    79
44
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    80
struct memcache_req *memcache_fetch (struct memcache *mc, const struct memcache_key *key, void *cb_arg) {
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    81
    return _memcache_req(mc, MEMCACHE_CMD_FETCH_GET, key, NULL, NULL, cb_arg);
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    82
}
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    83
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    84
struct memcache_req *memcache_store (struct memcache *mc, enum memcache_command cmd, const struct memcache_key *key, const struct memcache_obj *obj, const struct memcache_buf *buf, void *cb_arg) {
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    85
    if (
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    86
            (cmd != MEMCACHE_CMD_STORE_SET)
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    87
        &&  (cmd != MEMCACHE_CMD_STORE_ADD)
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    88
        &&  (cmd != MEMCACHE_CMD_STORE_REPLACE)
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    89
        &&  (cmd != MEMCACHE_CMD_STORE_APPEND)
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    90
        &&  (cmd != MEMCACHE_CMD_STORE_PREPEND)
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    91
        &&  (cmd != MEMCACHE_CMD_STORE_CAS)
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    92
    )
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    93
        ERROR("invalid command for store");
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    94
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    95
    return _memcache_req(mc, cmd, key, obj, buf, cb_arg);
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    96
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    97
error:
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    98
    return NULL;
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    99
}
03a7e064f833 stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   100