From aed0154bb2c760ad406c680f576ece7358228600 Mon Sep 17 00:00:00 2001 From: michael-grunder Date: Mon, 5 Aug 2024 18:51:12 -0700 Subject: [PATCH] Fix typo, add additional context --- README.md | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a40e4c7a..825961c7 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Libvalkey is the official C client for the [Valkey](https://valkey.io) database. ## Features - Commands are executed in a generic way, with printf-like invocation. -- Supports both `RESP2` and `RESP3 protocol versions. +- Supports both `RESP2` and `RESP3` protocol versions. - Supports both synchronous and asynchronous operation. - Optional support for `SSL` and `RDMA` connections. - Asynchronous API with several event libraries to choose from. @@ -71,7 +71,7 @@ sudo make install # Build with TLS and RDMA support mkdir build && cd build -cmakd -DCMAKE_BUILD_TYPE=RelWithDebInfo -DENABLE_SSL=1 -DENABLE_RDMA=1 .. +cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DENABLE_SSL=1 -DENABLE_RDMA=1 .. sudo make install ``` @@ -269,10 +269,18 @@ Libvalkey has a few other mechanisms worth detailing in the README. libvalkey uses a buffer to hold incomming bytes, which is typically restored to the configurable max buffer size (`16KB`) when it is empty. To avoid continually reallocating this buffer you can set the value higher, or to zero which means "no limit". +```c +context->reader->maxbuf = 0; +``` + ##### Maxelements By default, libvalkey will refuse to parse array-like replies if they have more than 2^32-1 or 4,294,967,295 elements. This value can be set to any arbitrary 64-bit value or zero which just means "no limit". +```c +context->reader->maxelements = 0; +``` + ##### RESP3 Push Replies The `RESP` protocol introduced out-of-band "push" replies in the third version of the specification. These replies may come at any point in the data stream. By default, libvalkey will simply process these messages and discard them. @@ -323,7 +331,7 @@ valkeyAllocatorFuncs my_allocators = { valkeyAllocFuncs old = valkeySetAllocators(&my_allocators); ``` -They can also be reset to GLIBC/MUSL +They can also be reset to the glibc/musl defaults ```c valkeyResetAllocators(); @@ -352,6 +360,7 @@ valkeyLibevAttach(EV_DEFAULT_ ac); valkeySetConnectCallback(ac, my_connect_callback); valkeySetDisconnectCallback(ac, my_disconnect_callback); + ev_run(EV_DEFAULT_ 0); ``` @@ -360,16 +369,36 @@ ev_run(EV_DEFAULT_ 0); Executing commands in an asynchronous context work similarly to the synchronous context, except that you can pass a callback that will be invoked when the reply is received. ```c +struct my_app_data { + size_t incrby_replies; + size_t get_replies; +}; + +void my_incrby_callback(valkeyAsyncContext *ac, void *r, void *privdata) { + struct my_app_data *data = privdata; + valkeyReply *reply = r; + + assert(reply != NULL && reply->type == VALKEY_REPLY_INTEGER); + + printf("Incremented value: %lld\n", reply->integer); + data->incrby_replies++; +} + void my_get_callback(valkeyAsyncContext *ac, void *r, void *privdata) { + struct my_app_data *data = privdata; valkeyReply *reply = r; - if (r == NULL) { - fprintf(stderr, "Error: %s\n", ac->errstr); - } else { - printf("Got reply: %s\n", r->str); - } + + assert(reply != NULL && reply->type == VALKEY_REPLY_STRING); + + printf("Key value: %s\n", reply->str); + data->get_replies++; +} + +int exec_some_commands(struct my_app_data *data) { + valkeyAsyncCommand(ac, my_incrby_callback, data, "INCRBY mykey %d", 42); + valkeyAsyncCommand(ac, my_get_callback, data, "GET %s", "mykey"); } -valkeyAsyncCommand(ac, my_get_callback, NULL, "GET foo"); ``` See the [examples](examples) directory for specific information on each supported event library.