summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/deps/libyrmcds/USAGE.md
blob: 408711937a59d76faa2976b7334e43ba81edc7f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
Usage
=====

`yc`
----

`yc` is a memcached/yrmcds client program.  It provides access to all
libyrmcds library functions therefore access to all server functions.

`yc` prints out its usage when invoked without command-line arguments.

Minimal example
---------------

```c
#include <yrmcds.h>

#include <errno.h>
#include <error.h>
#include <stdlib.h>
#include <stdio.h>

void check_error(yrmcds_error e) {
    if( e != 0 ) {
        if( e == YRMCDS_SYSTEM_ERROR ) {
            error(0, errno, "system error");
        } else {
            fprintf(stderr, "yrmcds error: %s\n", yrmcds_strerror(e));
        }
        exit(2);
    }
}

void check_response(const yrmcds_response* r) {
    if( r->status != YRMCDS_STATUS_OK ) {
        fprintf(stderr, "Command failed: 0x%04x %.*s\n",
                r->status, (int)r->data_len, r->data);
        exit(3);
    }
}

int main(int argc, char** argv) {
    yrmcds c;
    yrmcds_response r;

    check_error( yrmcds_connect(&c, "localhost", 11211) );
    check_error( yrmcds_noop(&c, NULL) );
    check_error( yrmcds_recv(&c, &r) );
    check_response(&r);
    check_error( yrmcds_close(&c) );

    return 0;
}
```

Compilation and linking
-----------------------

Link with `-lyrmcds -lpthread` as follows:

```
gcc -g -O2 -o foo foo.c -lyrmcds -lpthread
```

Request serial number
---------------------

All command sending functions can issue a unique serial number of the
command.  This can be used when you receive responses asynchronously
as described in the next section.

```c
uint32_t async_cas(yrmcds* c) {
    uint32_t serial;
    // try compare-and-swap
    check_error( yrmcds_set(&c, "abc", 3, "12345", 5, 0, 0, 5, 0, &serial) );
    return serial;
}
```

Multi-threading
---------------

All functions in libyrmcds are thread-safe as long as different `yrmcds`
structs are used.  Additionally, any number of threads can use command
sending functions such as `yrmcds_get()` or `yrmcds_set()` even when
they share the same `yrmcds` struct.

Further, `yrmcds_recv()` can be used in parallel with command sending
functions.  You can create a dedicated thread to receive server responses
asynchronously.  Use request serial numbers to identify a response's
request.

```c
void async_recv(yrmcds* c, void (*notify)(uint32_t, yrmcds_response* r)) {
    yrmcds_respone r;
    while( 1 ) {
        check_error( yrmcds_recv(&c, &r) );
        (*notify)(r.serial, &r);
    }
}
```

You can use `yrmcds_shutdown()` to interrupt the thread blocking in
`yrmcds_recv()` for graceful exit.


Transparent compression
-----------------------

libyrmcds provides optional transparent data compression by [LZ4][].

To use this feature, the library must be built with LZ4 as follows:

```
$ make lz4
$ make
```

If the library supports LZ4 compression, you can enable transparent
LZ4 (de)compression for large objects.  The threshold for compression
can be set by `yrmcds_set_compression()`.  The compression is disabled
by default.

Note that all clients must support and enable the compression to
properly handle compressed data.

Text protocol support
---------------------

Although libyrmcds is designed for binary protocol, yet it provides
limited access to text protocol commands.  Use `yrmcds_text_mode()`
to put the connection into the text protocol mode:

```c
    check_error( yrmcds_connect(&c, "localhost", 11211) );
    check_error( yrmcds_text_mode(&c) );

    // Use the library API just in the same way as binary protocol.
```

Limitations:

* Only subset of commands can be used.

    `yrmcds_get_touch`, lock related functions, `yrmcds_incr2` and
    `yrmcds_decr2`, stat functions, key dump extension are not supported.

* Quiet mutation cannot be used.

    `quiet` arguments must be 0.  You need to receive a response for
    every command.

* `yrmcds_response::cas_unique` is always 0 for storage commands.

    The response for `yrmcds_set`, `yrmcds_add`, or `yrmcds_replace`
    will not bring the correct CAS value.  Use `yrmcds_get` or
    `yrmcds_getk` to retrieve the correct value.

* `yrmcds_response::command` value is dummy.

    Because `command` is one of binary protocol command numbers.

Counter extension
-----------------

yrmcds has a distributed counter extension for resource management.
If the extension is enabled in server, you can access counters by `yrmcds_cnt_*` functions.
The usage of each function is very similar to the corresponding `yrmcds_` function.

The minimum example is:

```c
#include <yrmcds.h>

#include <errno.h>
#include <error.h>
#include <stdio.h>
#include <stdlib.h>

void check_error(yrmcds_error e) {
    if( e != YRMCDS_OK ) {
        if( e == YRMCDS_SYSTEM_ERROR ) {
            error(0, errno, "system error");
        } else {
            fprintf(stderr, "yrmcds error: %s\n", yrmcds_strerror(e));
        }
        exit(2);
    }
}

void check_response(const yrmcds_cnt_response* r) {
    if( r->status != YRMCDS_STATUS_OK ) {
        fprintf(stderr, "Command failed: 0x%02x %.*s\n",
                r->status, (int)r->body_length, r->body);
        exit(3);
    }
}

int main(void) {
    yrmcds_cnt c;
    yrmcds_cnt_response r;

    check_error( yrmcds_cnt_connect(&c, "localhost", 11215) );
    check_error( yrmcds_cnt_noop(&c, NULL) );
    check_error( yrmcds_cnt_recv(&c, &r) );
    check_response(&r);
    check_error( yrmcds_cnt_close(&c) );

    return 0;
}
```

API documents
-------------

HTML documents generated with Doxygen is available [here][api].


[api]: http://cybozu.github.io/libyrmcds/html/
[LZ4]: https://code.google.com/p/lz4/