gauc 0.3.0

Couchbase Rust Adapter / CLI
Documentation
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 *     Copyright 2012 Couchbase, Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

#define LCB_IOPS_V12_NO_DEPRECATE

#include "internal.h"
#include "select_io_opts.h"
#include <libcouchbase/plugins/io/bsdio-inl.c>

#if defined(_WIN32) && !defined(usleep)
#define usleep(n) Sleep((n) / 1000)
#endif

typedef struct sel_EVENT sel_EVENT;
struct sel_EVENT {
    lcb_list_t list;
    lcb_socket_t sock;
    short flags;
    short eflags; /* effective flags */
    void *cb_data;
    lcb_ioE_callback handler;
    sel_EVENT *next; /* for chaining active events */
};

typedef struct sel_TIMER sel_TIMER;
struct sel_TIMER {
    lcb_list_t list;
    int active;
    hrtime_t exptime;
    void *cb_data;
    lcb_ioE_callback handler;
};

typedef struct {
    sel_EVENT events;
    lcb_list_t timers;
    int event_loop;
} sel_LOOP;

static int
timer_cmp_asc(lcb_list_t *a, lcb_list_t *b)
{
    sel_TIMER *ta = LCB_LIST_ITEM(a, sel_TIMER, list);
    sel_TIMER *tb = LCB_LIST_ITEM(b, sel_TIMER, list);
    if (ta->exptime > tb->exptime) {
        return 1;
    } else if (ta->exptime < tb->exptime) {
        return -1;
    } else {
        return 0;
    }
}

static void *
sel_event_new(lcb_io_opt_t iops)
{
    sel_LOOP *io = iops->v.v2.cookie;
    sel_EVENT *ret = calloc(1, sizeof(sel_EVENT));
    if (ret != NULL) {
        lcb_list_append(&io->events.list, &ret->list);
    }
    return ret;
}

static int
sel_event_update(lcb_io_opt_t iops, lcb_socket_t sock, void *event, short flags,
    void *cb_data, lcb_ioE_callback handler)
{
    sel_EVENT *ev = event;
    ev->sock = sock;
    ev->handler = handler;
    ev->cb_data = cb_data;
    ev->flags = flags;
    (void)iops;
    return 0;
}

static void
sel_event_free(lcb_io_opt_t iops, void *event)
{
    sel_EVENT *ev = event;
    lcb_list_delete(&ev->list);
    free(ev);
    (void)iops;
}

static void
sel_event_cancel(lcb_io_opt_t iops, lcb_socket_t sock, void *event)
{
    sel_EVENT *ev = event;
    ev->flags = 0;
    ev->cb_data = NULL;
    ev->handler = NULL;
    (void)iops;
    (void)sock;
}

static void *
sel_timer_new(lcb_io_opt_t iops)
{
    sel_TIMER *ret = calloc(1, sizeof(sel_TIMER));
    (void)iops;
    return ret;
}

static void
sel_timer_cancel(lcb_io_opt_t iops, void *timer)
{
    sel_TIMER *tm = timer;
    if (tm->active) {
        tm->active = 0;
        lcb_list_delete(&tm->list);
    }
    (void)iops;
}


static void sel_timer_free(lcb_io_opt_t iops, void *timer)
{
    sel_timer_cancel(iops, timer);
    free(timer);
    (void)iops;
}

static int
sel_timer_schedule(lcb_io_opt_t iops, void *timer, lcb_U32 usec, void *cb_data,
    lcb_ioE_callback handler)
{
    sel_TIMER *tm = timer;
    sel_LOOP *cookie = iops->v.v2.cookie;
    lcb_assert(!tm->active);
    tm->exptime = gethrtime() + (usec * (hrtime_t)1000);
    tm->cb_data = cb_data;
    tm->handler = handler;
    tm->active = 1;
    lcb_list_add_sorted(&cookie->timers, &tm->list, timer_cmp_asc);

    (void)iops;
    return 0;
}

static void
sel_stop_loop(struct lcb_io_opt_st *iops)
{
    sel_LOOP *io = iops->v.v2.cookie;
    io->event_loop = 0;
}

static sel_TIMER *
pop_next_timer(sel_LOOP *cookie, hrtime_t now)
{
    sel_TIMER *ret;

    if (LCB_LIST_IS_EMPTY(&cookie->timers)) {
        return NULL;
    }

    ret = LCB_LIST_ITEM(cookie->timers.next, sel_TIMER, list);
    if (ret->exptime > now) {
        return NULL;
    }
    lcb_list_shift(&cookie->timers);
    ret->active = 0;
    return ret;
}

static int
get_next_timeout(sel_LOOP *cookie, struct timeval *tmo, hrtime_t now)
{
    sel_TIMER *first;
    hrtime_t delta;

    if (LCB_LIST_IS_EMPTY(&cookie->timers)) {
        tmo->tv_sec = 0;
        tmo->tv_usec = 0;
        return 0;
    }

    first = LCB_LIST_ITEM(cookie->timers.next, sel_TIMER, list);
    if (now < first->exptime) {
        delta = first->exptime - now;
    } else {
        delta = 0;
    }


    if (delta) {
        delta /= 1000;
        tmo->tv_sec = (long)(delta / 1000000);
        tmo->tv_usec = delta % 1000000;
    } else {
        tmo->tv_sec = 0;
        tmo->tv_usec = 0;
    }
    return 1;
}

static void
run_loop(sel_LOOP *io, int is_tick)
{
    sel_EVENT *ev;
    lcb_list_t *ii;

    fd_set readfds, writefds, exceptfds;

    io->event_loop = !is_tick;
    do {
        struct timeval tmo, *t;
        int ret;
        int nevents = 0;
        int has_timers;
        lcb_socket_t fdmax = 0;
        hrtime_t now;

        t = NULL;
        now = gethrtime();

        FD_ZERO(&readfds);
        FD_ZERO(&writefds);
        FD_ZERO(&exceptfds);

        LCB_LIST_FOR(ii, &io->events.list) {
            ev = LCB_LIST_ITEM(ii, sel_EVENT, list);
            if (ev->flags != 0) {
                if (ev->flags & LCB_READ_EVENT) {
                    FD_SET(ev->sock, &readfds);
                }

                if (ev->flags & LCB_WRITE_EVENT) {
                    FD_SET(ev->sock, &writefds);
                }

                FD_SET(ev->sock, &exceptfds);
                if (ev->sock > fdmax) {
                    fdmax = ev->sock;
                }
                ++nevents;
            }
        }

        has_timers = get_next_timeout(io, &tmo, now);
        if (has_timers && !is_tick) {
            t = &tmo;
        }


        if (nevents == 0 && has_timers == 0) {
            io->event_loop = 0;
            return;
        }

        if (nevents) {
            ret = select(fdmax + 1, &readfds, &writefds, &exceptfds, t);
            if (ret == SOCKET_ERROR) {
                return;
            }
        } else {
            ret = 0;
            if (!is_tick) {
                usleep((t->tv_sec * 1000000) + t->tv_usec);
            }
        }


        /** Always invoke the pending timers */
        if (has_timers) {
            sel_TIMER *tm;
            now = gethrtime();

            while ((tm = pop_next_timer(io, now))) {
                tm->handler(-1, 0, tm->cb_data);
            }
        }

        /* To be completely safe, we need to copy active events
         * before handing them. Iterating over the list of
         * registered events isn't safe, because one callback can
         * cancel all registered events before iteration will end
         */

        if (ret && nevents) {
            sel_EVENT *active = NULL;
            LCB_LIST_FOR(ii, &io->events.list) {
                ev = LCB_LIST_ITEM(ii, sel_EVENT, list);
                if (ev->flags != 0) {
                    ev->eflags = 0;
                    if (FD_ISSET(ev->sock, &readfds)) {
                        ev->eflags |= LCB_READ_EVENT;
                    }
                    if (FD_ISSET(ev->sock, &writefds)) {
                        ev->eflags |= LCB_WRITE_EVENT;
                    }
                    if (FD_ISSET(ev->sock, &exceptfds)) {
                        ev->eflags = LCB_ERROR_EVENT | LCB_RW_EVENT; /** It should error */
                    }
                    if (ev->eflags != 0) {
                        ev->next = active;
                        active = ev;
                    }
                }
            }
            ev = active;
            while (ev) {
                sel_EVENT *p = ev->next;
                ev->handler(ev->sock, ev->eflags, ev->cb_data);
                ev = p;
            }
        }
    } while (io->event_loop);
}

static void
sel_run_loop(struct lcb_io_opt_st *iops)
{
    run_loop(iops->v.v2.cookie, 0);
}
static void
sel_tick_loop(struct lcb_io_opt_st *iops)
{
    run_loop(iops->v.v2.cookie, 1);
}

static void
sel_destroy_iops(struct lcb_io_opt_st *iops)
{
    sel_LOOP *io = iops->v.v2.cookie;
    lcb_list_t *nn, *ii;
    sel_EVENT *ev;
    sel_TIMER *tm;

    assert(io->event_loop == 0);
    LCB_LIST_SAFE_FOR(ii, nn, &io->events.list) {
        ev = LCB_LIST_ITEM(ii, sel_EVENT, list);
        sel_event_free(iops, ev);
    }
    assert(LCB_LIST_IS_EMPTY(&io->events.list));
    LCB_LIST_SAFE_FOR(ii, nn, &io->timers) {
        tm = LCB_LIST_ITEM(ii, sel_TIMER, list);
        sel_timer_free(iops, tm);
    }
    assert(LCB_LIST_IS_EMPTY(&io->timers));
    free(io);
    free(iops);
}

static lcb_socket_t
sel_socket_wrap(lcb_io_opt_t io, int domain, int type, int protocol)
{
    lcb_socket_t res = socket_impl(io, domain, type, protocol);
#ifndef _WIN32

    /* This only works on non-Windows where FD_SETSIZE is in effect wrt the
     * actual FD number. On Windows, FD_SETSIZE is the cap on the _total_
     * number of sockets to be used in select; not necessarily what their
     * FD values are.
     *
     * TODO: Just use poll() on POSIX in the future.
     */
    if (res != INVALID_SOCKET && res > FD_SETSIZE) {
        close_impl(io, res);
        fprintf(stderr, "COUCHBASE: too many FDs. Cannot have socket > FD_SETSIZE. Use other I/O plugin\n");
        io->v.v3.error = EINVAL;
        res = INVALID_SOCKET;
    }
#endif
    return res;
}

static void
procs2_sel_callback(int version, lcb_loop_procs *loop_procs,
    lcb_timer_procs *timer_procs, lcb_bsd_procs *bsd_procs,
    lcb_ev_procs *ev_procs, lcb_completion_procs *completion_procs,
    lcb_iomodel_t *iomodel)
{
    ev_procs->create = sel_event_new;
    ev_procs->destroy = sel_event_free;
    ev_procs->watch = sel_event_update;
    ev_procs->cancel = sel_event_cancel;

    timer_procs->create = sel_timer_new;
    timer_procs->destroy = sel_timer_free;
    timer_procs->schedule = sel_timer_schedule;
    timer_procs->cancel = sel_timer_cancel;

    loop_procs->start = sel_run_loop;
    loop_procs->stop = sel_stop_loop;
    loop_procs->tick = sel_tick_loop;

    *iomodel = LCB_IOMODEL_EVENT;
    wire_lcb_bsd_impl2(bsd_procs, version);

    /* Override */
    bsd_procs->socket0 = sel_socket_wrap;
    (void)completion_procs;
}

LIBCOUCHBASE_API
lcb_error_t
lcb_create_select_io_opts(int version, lcb_io_opt_t *io, void *arg)
{
    lcb_io_opt_t ret;
    sel_LOOP *cookie;

    if (version != 0) {
        return LCB_PLUGIN_VERSION_MISMATCH;
    }
    ret = calloc(1, sizeof(*ret));
    cookie = calloc(1, sizeof(*cookie));
    if (ret == NULL || cookie == NULL) {
        free(ret);
        free(cookie);
        return LCB_CLIENT_ENOMEM;
    }
    lcb_list_init(&cookie->events.list);
    lcb_list_init(&cookie->timers);

    /* setup io iops! */
    ret->version = 3;
    ret->dlhandle = NULL;
    ret->destructor = sel_destroy_iops;

    /* consider that struct isn't allocated by the library,
     * `need_cleanup' flag might be set in lcb_create() */
    ret->v.v3.need_cleanup = 0;
    ret->v.v3.get_procs = procs2_sel_callback;
    ret->v.v3.cookie = cookie;

    /* For backwards compatibility */
    wire_lcb_bsd_impl(ret);

    *io = ret;
    (void)arg;
    return LCB_SUCCESS;
}