File: u1db_sync_target.c

package info (click to toggle)
u1db 13.10-6.2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 1,872 kB
  • ctags: 2,932
  • sloc: python: 11,859; ansic: 6,278; makefile: 151; sql: 40; sh: 1
file content (754 lines) | stat: -rw-r--r-- 26,910 bytes parent folder | download | duplicates (3)
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*
 * Copyright 2012 Canonical Ltd.
 *
 * This file is part of u1db.
 *
 * u1db is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.
 *
 * u1db is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with u1db.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "u1db/u1db_internal.h"
#include <string.h>
#include <json-c/linkhash.h>


static int st_get_sync_info(u1db_sync_target *st,
                            const char *source_replica_uid,
                            const char **st_replica_uid, int *st_gen,
                            char **st_trans_id, int *source_gen,
                            char **source_trans_id);

static int st_record_sync_info(u1db_sync_target *st,
        const char *source_replica_uid, int source_gen, const char *trans_id);

static int st_sync_exchange(u1db_sync_target *st,
                            const char *source_replica_uid, int n_docs,
                            u1db_document **docs, int *generations,
                            const char **trans_ids, int *target_gen,
                            char **target_trans_id, void *context,
                            u1db_doc_gen_callback cb,
                            const char **autocreated_replica_uid);
static int st_sync_exchange_doc_ids(u1db_sync_target *st,
                                    u1database *source_db, int n_doc_ids,
                                    const char **doc_ids, int *generations,
                                    const char **trans_ids, int *target_gen,
                                    char **target_trans_id, void *context,
                                    u1db_doc_gen_callback cb,
                                    const char **autocreated_replica_uid);
static int st_get_sync_exchange(u1db_sync_target *st,
                         const char *source_replica_uid,
                         int source_gen,
                         u1db_sync_exchange **exchange);

static void st_finalize_sync_exchange(u1db_sync_target *st,
                               u1db_sync_exchange **exchange);
static int st_set_trace_hook(u1db_sync_target *st,
                             void *context, u1db__trace_callback cb);
static void st_finalize(u1db_sync_target *st);
static void se_free_seen_id(struct lh_entry *e);


struct _get_docs_to_doc_gen_context {
    int doc_offset;
    void *user_context;
    u1db_doc_gen_callback user_cb;
    int *gen_for_doc_ids;
    const char **trans_ids_for_doc_ids;
    int free_when_done;
};

// A wrapper to change a 'u1db_doc_callback' into a 'u1db_doc_gen_callback'.
static int get_docs_to_gen_docs(void *context, u1db_document *doc);

int
u1db__get_sync_target(u1database *db, u1db_sync_target **sync_target)
{
    int status = U1DB_OK;

    if (db == NULL || sync_target == NULL) {
        return U1DB_INVALID_PARAMETER;
    }
    *sync_target = (u1db_sync_target *)calloc(1, sizeof(u1db_sync_target));
    if (*sync_target == NULL) {
        return U1DB_NOMEM;
    }
    (*sync_target)->implementation = db;
    (*sync_target)->get_sync_info = st_get_sync_info;
    (*sync_target)->record_sync_info = st_record_sync_info;
    (*sync_target)->sync_exchange = st_sync_exchange;
    (*sync_target)->sync_exchange_doc_ids = st_sync_exchange_doc_ids;
    (*sync_target)->get_sync_exchange = st_get_sync_exchange;
    (*sync_target)->finalize_sync_exchange = st_finalize_sync_exchange;
    (*sync_target)->_set_trace_hook = st_set_trace_hook;
    (*sync_target)->finalize = st_finalize;
    return status;
}


void
u1db__free_sync_target(u1db_sync_target **sync_target)
{
    if (sync_target == NULL || *sync_target == NULL) {
        return;
    }
    (*sync_target)->finalize(*sync_target);
    free(*sync_target);
    *sync_target = NULL;
}


static int
st_get_sync_info(u1db_sync_target *st, const char *source_replica_uid,
                 const char **st_replica_uid, int *st_gen, char **st_trans_id,
                 int *source_gen, char **source_trans_id)
{
    int status = U1DB_OK;
    u1database *db;
    if (st == NULL || source_replica_uid == NULL || st_replica_uid == NULL
            || st_gen == NULL || source_gen == NULL)
    {
        return U1DB_INVALID_PARAMETER;
    }
    // TODO: This really feels like it should be done inside some sort of
    //       transaction, so that the sync information is consistent with the
    //       current db generation. (at local generation X we are synchronized
    //       with remote generation Y.)
    //       At the very least, though, we check the sync generation *first*,
    //       so that we should only be getting the same data again, if for some
    //       reason we are currently synchronizing with the remote object.
    db = (u1database *)st->implementation;
    status = u1db_get_replica_uid(db, st_replica_uid);
    if (status != U1DB_OK) { goto finish; }
    status = u1db__get_replica_gen_and_trans_id(
        db, source_replica_uid, source_gen, source_trans_id);
    if (status != U1DB_OK) { goto finish; }
    status = u1db__get_generation_info(db, st_gen, st_trans_id);
finish:
    return status;
}


static int
st_record_sync_info(u1db_sync_target *st, const char *source_replica_uid,
                    int source_gen, const char *trans_id)
{
    int status;
    u1database *db;
    if (st == NULL || source_replica_uid == NULL) {
        return U1DB_INVALID_PARAMETER;
    }
    if (st->trace_cb) {
        status = st->trace_cb(st->trace_context, "record_sync_info");
        if (status != U1DB_OK) { goto finish; }
    }
    db = (u1database *)st->implementation;
    status = u1db__set_replica_gen_and_trans_id(
        db, source_replica_uid, source_gen, trans_id);
finish:
    return status;
}


static int
st_get_sync_exchange(u1db_sync_target *st, const char *source_replica_uid,
                     int target_gen_known_by_source,
                     u1db_sync_exchange **exchange)
{
    u1db_sync_exchange *tmp;
    if (st == NULL || source_replica_uid == NULL || exchange == NULL) {
        return U1DB_INVALID_PARAMETER;
    }
    tmp = (u1db_sync_exchange *)calloc(1, sizeof(u1db_sync_exchange));
    if (tmp == NULL) {
        return U1DB_NOMEM;
    }
    tmp->db = (u1database *)st->implementation;
    tmp->source_replica_uid = source_replica_uid;
    tmp->target_gen = target_gen_known_by_source;
    // Note: lh_table is overkill for what we need. We only need a set, not a
    //       mapping, and we don't need the prev/next pointers. But it is
    //       already available, and doesn't require us to implement and debug
    //       another set() implementation.
    tmp->seen_ids = lh_kchar_table_new(100, "seen_ids",
            se_free_seen_id);
    tmp->trace_context = st->trace_context;
    tmp->trace_cb = st->trace_cb;
    *exchange = tmp;
    return U1DB_OK;
}


static void
st_finalize_sync_exchange(u1db_sync_target *st, u1db_sync_exchange **exchange)
{
    int i;
    if (exchange == NULL || *exchange == NULL) {
        return;
    }
    if ((*exchange)->seen_ids != NULL) {
        lh_table_free((*exchange)->seen_ids);
        (*exchange)->seen_ids = NULL;
    }
    if ((*exchange)->doc_ids_to_return != NULL) {
        for (i = 0; i < (*exchange)->num_doc_ids; ++i) {
            free((*exchange)->doc_ids_to_return[i]);
        }
        free((*exchange)->doc_ids_to_return);
        (*exchange)->doc_ids_to_return = NULL;
        (*exchange)->num_doc_ids = 0;
    }
    if ((*exchange)->gen_for_doc_ids != NULL) {
        free((*exchange)->gen_for_doc_ids);
        (*exchange)->gen_for_doc_ids = NULL;
    }
    if ((*exchange)->trans_ids_for_doc_ids != NULL) {
        free((*exchange)->trans_ids_for_doc_ids);
        (*exchange)->trans_ids_for_doc_ids = NULL;
    }
    if ((*exchange)->target_trans_id != NULL) {
        free((*exchange)->target_trans_id);
        (*exchange)->target_trans_id = NULL;
    }
    free(*exchange);
    *exchange = NULL;
}


static int
st_set_trace_hook(u1db_sync_target *st, void *context, u1db__trace_callback cb)
{
    st->trace_context = context;
    st->trace_cb = cb;
    return U1DB_OK;
}


static void
st_finalize(u1db_sync_target *st)
{
    return;
}


static void
se_free_seen_id(struct lh_entry *e)
{
    if (e == NULL) {
        return;
    }
    if (e->k != NULL) {
        free((void *)e->k);
        e->k = NULL;
    }
    /* v is a (void*)int */
}


int
u1db__sync_exchange_seen_ids(u1db_sync_exchange *se, int *n_ids,
                             const char ***doc_ids)
{
    int i;
    struct lh_entry *entry;
    if (se == NULL || n_ids == NULL || doc_ids == NULL) {
        return U1DB_INVALID_PARAMETER;
    }
    if (se->seen_ids == NULL || se->seen_ids->count == 0) {
        *n_ids = 0;
        *doc_ids = NULL;
        return U1DB_OK;
    }
    *n_ids = se->seen_ids->count;
    (*doc_ids) = (const char **)calloc(*n_ids, sizeof(char *));
    i = 0;
    lh_foreach(se->seen_ids, entry) {
        if (entry->k != NULL) {
            if (i >= (*n_ids)) {
                // TODO: Better error? For some reason we found more than
                //       'count' valid entries
                return U1DB_INVALID_PARAMETER;
            }
            (*doc_ids)[i] = entry->k;
            i++;
        }
    }
    return U1DB_OK;
}

int
u1db__sync_exchange_insert_doc_from_source(u1db_sync_exchange *se,
        u1db_document *doc, int source_gen, const char *trans_id)
{
    int status = U1DB_OK;
    int insert_state;
    int at_gen;
    if (se == NULL || se->db == NULL || doc == NULL) {
        return U1DB_INVALID_PARAMETER;
    }
    // fprintf(stderr, "Inserting %s from source\n", doc->doc_id);
    status = u1db__put_doc_if_newer(se->db, doc, 0, se->source_replica_uid,
                                    source_gen, trans_id, &insert_state,
                                    &at_gen);
    if (insert_state == U1DB_INSERTED || insert_state == U1DB_CONVERGED) {
        lh_table_insert(se->seen_ids, strdup(doc->doc_id),
        (void *)(intptr_t)at_gen);
    } else {
        // state should be either U1DB_SUPERSEDED or U1DB_CONFLICTED, in either
        // case, we don't count this as a 'seen_id' because we will want to be
        // returning a document with this identifier back to the user.
        // fprintf(stderr, "Not inserting %s, %d\n", doc->doc_id, insert_state);
    }
    return status;
}


struct _whats_changed_doc_ids_state {
    int num_doc_ids;
    int max_doc_ids;
    struct lh_table *exclude_ids;
    char **doc_ids_to_return;
    int *gen_for_doc_ids;
    const char **trans_ids_for_doc_ids;
};

// Callback for whats_changed to map the callback into the sync_exchange
// doc_ids_to_return array.
static int
whats_changed_to_doc_ids(void *context, const char *doc_id, int gen,
                         const char *trans_id)
{
    struct lh_entry *e;
    struct _whats_changed_doc_ids_state *state;
    state = (struct _whats_changed_doc_ids_state *)context;
    if (state->exclude_ids != NULL
        && (e = lh_table_lookup_entry(state->exclude_ids, doc_id)) != NULL
        && (intptr_t)e->v >= gen)
    {
        // This document was already seen at this gen,
        // so we don't need to return it
        return 0;
    }
    if (state->num_doc_ids >= state->max_doc_ids) {
        state->max_doc_ids = (state->max_doc_ids * 2) + 10;
        if (state->doc_ids_to_return == NULL) {
            state->doc_ids_to_return = (char **)calloc(
                state->max_doc_ids, sizeof(char*));
            state->gen_for_doc_ids = (int *)calloc(
                state->max_doc_ids, sizeof(int));
            state->trans_ids_for_doc_ids = (const char **)calloc(
                state->max_doc_ids, sizeof(char*));
        } else {
            state->doc_ids_to_return = (char **)realloc(
                state->doc_ids_to_return, state->max_doc_ids * sizeof(char*));
            state->gen_for_doc_ids = (int *)realloc(
                state->gen_for_doc_ids, state->max_doc_ids * sizeof(int));
            state->trans_ids_for_doc_ids = (const char **)realloc(
                state->gen_for_doc_ids, state->max_doc_ids * sizeof(char*));
        }
        if (state->doc_ids_to_return == NULL || state->gen_for_doc_ids == NULL
                || state->trans_ids_for_doc_ids == NULL)
        {
            return U1DB_NOMEM;
        }
    }
    state->doc_ids_to_return[state->num_doc_ids] = strdup(doc_id);
    state->gen_for_doc_ids[state->num_doc_ids] = gen;
    state->trans_ids_for_doc_ids[state->num_doc_ids] = trans_id;
    state->num_doc_ids++;
    return 0;
}


int
u1db__sync_exchange_find_doc_ids_to_return(u1db_sync_exchange *se)
{
    int status;
    struct _whats_changed_doc_ids_state state = {0};
    if (se == NULL) {
        return U1DB_INVALID_PARAMETER;
    }
    if (se->trace_cb) {
        status = se->trace_cb(se->trace_context, "before whats_changed");
        if (status != U1DB_OK) { goto finish; }
    }
    state.exclude_ids = se->seen_ids;
    status = u1db_whats_changed(se->db, &se->target_gen, &se->target_trans_id,
            (void*)&state, whats_changed_to_doc_ids);
    if (status != U1DB_OK) {
        free(state.doc_ids_to_return);
        free(state.gen_for_doc_ids);
        free(state.trans_ids_for_doc_ids);
        goto finish;
    }
    if (se->trace_cb) {
        status = se->trace_cb(se->trace_context, "after whats_changed");
        if (status != U1DB_OK) { goto finish; }
    }
    se->num_doc_ids = state.num_doc_ids;
    se->doc_ids_to_return = state.doc_ids_to_return;
    se->gen_for_doc_ids = state.gen_for_doc_ids;
    se->trans_ids_for_doc_ids = state.trans_ids_for_doc_ids;
finish:
    return status;
}


// A wrapper to change a 'u1db_doc_callback' into a 'u1db_doc_gen_callback'.
static int
get_docs_to_gen_docs(void *context, u1db_document *doc)
{
    struct _get_docs_to_doc_gen_context *ctx;
    int status;
    ctx = (struct _get_docs_to_doc_gen_context *)context;
    // Note: using doc_offset in this way assumes that u1db_get_docs will
    //       always return them in exactly the order we requested. This is
    //       probably true, though.
    status = ctx->user_cb(
        ctx->user_context, doc, ctx->gen_for_doc_ids[ctx->doc_offset],
        ctx->trans_ids_for_doc_ids[ctx->doc_offset]);
    ctx->doc_offset++;
    if (ctx->free_when_done) {
        u1db_free_doc(&doc);
    }
    return status;
}


int
u1db__sync_exchange_return_docs(u1db_sync_exchange *se, void *context,
                                int (*cb)(void *context, u1db_document *doc,
                                          int gen, const char *trans_id))
{
    int status = U1DB_OK;
    struct _get_docs_to_doc_gen_context state = {0};
    if (se == NULL || cb == NULL) {
        return U1DB_INVALID_PARAMETER;
    }
    state.user_context = context;
    state.user_cb = cb;
    state.doc_offset = 0;
    state.gen_for_doc_ids = se->gen_for_doc_ids;
    state.trans_ids_for_doc_ids = se->trans_ids_for_doc_ids;
    if (se->trace_cb) {
        status = se->trace_cb(se->trace_context, "before get_docs");
        if (status != U1DB_OK) { goto finish; }
    }
    if (se->num_doc_ids > 0) {
        status = u1db_get_docs(se->db, se->num_doc_ids,
                (const char **)se->doc_ids_to_return,
                0, 1, &state, get_docs_to_gen_docs);
    }
finish:
    return status;
}

struct _return_doc_state {
    u1database *db;
    const char *target_uid;
    int num_inserted;
};

static int
return_doc_to_insert_from_target(void *context, u1db_document *doc, int gen,
                                 const char *trans_id)
{
    int status, insert_state;
    struct _return_doc_state *state;
    state = (struct _return_doc_state *)context;

    status = u1db__put_doc_if_newer(
        state->db, doc, 1, state->target_uid, gen, trans_id, &insert_state,
        NULL);
    u1db_free_doc(&doc);
    if (status == U1DB_OK) {
        if (insert_state == U1DB_INSERTED || insert_state == U1DB_CONFLICTED) {
            // Either it was directly inserted, or it was saved as a conflict
            state->num_inserted++;
        }
    } else {
    }
    return status;
}


static int
get_and_insert_docs(u1database *source_db, u1db_sync_exchange *se,
                    int n_doc_ids, const char **doc_ids, int *generations,
                    const char **trans_ids)
{
    struct _get_docs_to_doc_gen_context get_doc_state = {0};

    get_doc_state.free_when_done = 1;
    get_doc_state.user_context = se;
    // Note: user_cb takes a 'void *' as the first parameter, so we cast the
    //       u1db__sync_exchange_insert_doc_from_source to avoid the warning
    get_doc_state.user_cb =
        (u1db_doc_gen_callback)u1db__sync_exchange_insert_doc_from_source;
    get_doc_state.gen_for_doc_ids = generations;
    get_doc_state.trans_ids_for_doc_ids = trans_ids;
    return u1db_get_docs(source_db, n_doc_ids, doc_ids,
            0, 1, &get_doc_state, get_docs_to_gen_docs);
}


static int
st_sync_exchange(u1db_sync_target *st, const char *source_replica_uid,
                 int n_docs, u1db_document **docs, int *generations,
                 const char **trans_ids, int *target_gen,
                 char **target_trans_id, void *context,
                 u1db_doc_gen_callback cb,
                 const char **autocreated_replica_uid)
{
    int status, i;

    u1db_sync_exchange *exchange = NULL;
    if (st == NULL || generations == NULL || target_gen == NULL
            || target_trans_id == NULL || cb == NULL)
    {
        return U1DB_INVALID_PARAMETER;
    }
    if (n_docs > 0 && (docs == NULL || generations == NULL)) {
        return U1DB_INVALID_PARAMETER;
    }
    status = st->get_sync_exchange(st, source_replica_uid,
                                   *target_gen, &exchange);
    if (status != U1DB_OK) { goto finish; }
    status = u1db_validate_gen_and_trans_id(
        exchange->db, *target_gen, *target_trans_id);
    if (status != U1DB_OK) { goto finish; }
    for (i = 0; i < n_docs; ++i) {
        status = u1db__sync_exchange_insert_doc_from_source(
            exchange, docs[i], generations[i], trans_ids[i]);
        if (status != U1DB_OK) { goto finish; }
    }
    status = u1db__sync_exchange_find_doc_ids_to_return(exchange);
    if (status != U1DB_OK) { goto finish; }
    status = u1db__sync_exchange_return_docs(exchange, context, cb);
finish:
    *target_gen = exchange->target_gen;
    *target_trans_id = exchange->target_trans_id;
    // We set this to NULL, because the caller is now responsible for it
    exchange->target_trans_id = NULL;
    st->finalize_sync_exchange(st, &exchange);
    return status;
}


static int
st_sync_exchange_doc_ids(u1db_sync_target *st, u1database *source_db,
        int n_doc_ids, const char **doc_ids, int *generations,
        const char **trans_ids, int *target_gen, char **target_trans_id,
        void *context, u1db_doc_gen_callback cb,
        const char **autocreated_replica_uid)
{
    int status;
    const char *source_replica_uid = NULL;
    u1db_sync_exchange *exchange = NULL;
    if (st == NULL || source_db == NULL || target_gen == NULL
        || target_trans_id == NULL || cb == NULL)
    {
        return U1DB_INVALID_PARAMETER;
    }
    if (n_doc_ids > 0 && (doc_ids == NULL || generations == NULL)) {
        return U1DB_INVALID_PARAMETER;
    }
    status = u1db_get_replica_uid(source_db, &source_replica_uid);
    if (status != U1DB_OK) { goto finish; }
    status = st->get_sync_exchange(st, source_replica_uid,
                                   *target_gen, &exchange);
    if (status != U1DB_OK) { goto finish; }
    status = u1db_validate_gen_and_trans_id(
        exchange->db, *target_gen, *target_trans_id);
    if (status != U1DB_OK) { goto finish; }
    if (n_doc_ids > 0) {
        status = get_and_insert_docs(source_db, exchange,
            n_doc_ids, doc_ids, generations, trans_ids);
        if (status != U1DB_OK) { goto finish; }
    }
    status = u1db__sync_exchange_find_doc_ids_to_return(exchange);
    if (status != U1DB_OK) { goto finish; }
    status = u1db__sync_exchange_return_docs(exchange, context, cb);
finish:
    *target_gen = exchange->target_gen;
    *target_trans_id = exchange->target_trans_id;
    // We set this to NULL, because the caller is now responsible for it
    exchange->target_trans_id = NULL;
    st->finalize_sync_exchange(st, &exchange);
    return status;
}


int
u1db__sync_db_to_target(u1database *db, u1db_sync_target *target,
                        int *local_gen_before_sync)
{
    int status;
    struct _whats_changed_doc_ids_state to_send_state = {0};
    struct _return_doc_state return_doc_state = {0};
    const char *target_uid, *local_uid;
    char *local_trans_id = NULL;
    char *local_target_trans_id = NULL;
    char *target_trans_id_known_by_local = NULL;
    char *local_trans_id_known_by_target = NULL;
    char *target_trans_id = NULL;
    int target_gen, local_gen;
    int local_gen_known_by_target, target_gen_known_by_local;
    int request_ensure = 0;

    // fprintf(stderr, "Starting\n");
    if (db == NULL || target == NULL || local_gen_before_sync == NULL) {
        // fprintf(stderr, "DB, target, or local are NULL\n");
        status = U1DB_INVALID_PARAMETER;
        goto finish;
    }

    status = u1db_get_replica_uid(db, &local_uid);
    if (status != U1DB_OK) { goto finish; }
    // fprintf(stderr, "Local uid: %s\n", local_uid);
    status = target->get_sync_info(
        target, local_uid, &target_uid, &target_gen, &target_trans_id,
        &local_gen_known_by_target, &local_trans_id_known_by_target);
    if (status == 404) { // database does not exist
        target_uid = NULL;
        target_gen = 0;
        local_gen_known_by_target = 0;
        target_trans_id = strdup("");
        if (target_trans_id == NULL) {
            status = U1DB_NOMEM;
            goto finish;
        }
        local_trans_id_known_by_target = strdup("");
        if (local_trans_id_known_by_target == NULL) {
            status = U1DB_NOMEM;
            goto finish;
        }
        request_ensure = 1;
    } else if (status != U1DB_OK) { goto finish; }
    if (target_uid != NULL && strcmp(local_uid, target_uid) == 0) {
        status = U1DB_INVALID_REPLICA_UID;
        goto finish;
    }
    status = u1db_validate_gen_and_trans_id(
        db, local_gen_known_by_target, local_trans_id_known_by_target);
    if (status != U1DB_OK) { goto finish; }
    if (target_uid == NULL) {
        target_gen_known_by_local = 0;
        target_trans_id_known_by_local = strdup("");
        if (target_trans_id_known_by_local == NULL) {
            status = U1DB_NOMEM;
            goto finish;
        }
    } else {
        status = u1db__get_replica_gen_and_trans_id(
            db, target_uid, &target_gen_known_by_local,
            &target_trans_id_known_by_local);
        if (status != U1DB_OK) { goto finish; }
    }
    local_target_trans_id = target_trans_id_known_by_local;
    local_gen = local_gen_known_by_target;

    // Before we start the sync exchange, get the list of doc_ids that we want
    // to send. We have to do this first, so that local_gen_before_sync will
    // match exactly the list of doc_ids we send
    status = u1db_whats_changed(
        db, &local_gen, &local_trans_id, (void*)&to_send_state,
        whats_changed_to_doc_ids);
    if (status != U1DB_OK) { goto finish; }
    if (local_gen == local_gen_known_by_target
        && target_gen == target_gen_known_by_local)
    {
        if (strcmp(target_trans_id, target_trans_id_known_by_local) != 0) {
            status = U1DB_INVALID_TRANSACTION_ID;
        }
        // We know status == U1DB_OK, and we can shortcut the rest of the
        // logic, no need to look for more information.
        goto finish;
    }
    *local_gen_before_sync = local_gen;
    return_doc_state.db = db;
    return_doc_state.target_uid = target_uid;
    return_doc_state.num_inserted = 0;
    status = target->sync_exchange_doc_ids(target, db,
        to_send_state.num_doc_ids,
        (const char**)to_send_state.doc_ids_to_return,
        to_send_state.gen_for_doc_ids, to_send_state.trans_ids_for_doc_ids,
        &target_gen_known_by_local, &target_trans_id_known_by_local,
        &return_doc_state, return_doc_to_insert_from_target,
        request_ensure?&return_doc_state.target_uid:NULL);
    if (request_ensure) {
        if (return_doc_state.target_uid != NULL) {
            target_uid = return_doc_state.target_uid;
        } else {
            request_ensure = 0;
        }
    }
    if (status != U1DB_OK) { goto finish; }
    if (local_trans_id != NULL) {
        free(local_trans_id);
    }
    status = u1db__get_generation_info(db, &local_gen, &local_trans_id);
    if (status != U1DB_OK) { goto finish; }
    // Now we successfully sent and received docs, make sure we record the
    // current remote generation
    status = u1db__set_replica_gen_and_trans_id(
        db, target_uid, target_gen_known_by_local,
        target_trans_id_known_by_local);
    if (status != U1DB_OK) { goto finish; }
    if (return_doc_state.num_inserted > 0 &&
        ((*local_gen_before_sync + return_doc_state.num_inserted)
         == local_gen))
    {
        status = target->record_sync_info(
            target, local_uid, local_gen, local_trans_id);
        if (status != U1DB_OK) { goto finish; }
    }
finish:
    if (request_ensure && target_uid != NULL) {
        free((void *)target_uid);
    }
    if (local_trans_id != NULL) {
        free(local_trans_id);
    }
    if (local_trans_id_known_by_target != NULL) {
        free(local_trans_id_known_by_target);
    }
    if (target_trans_id != NULL) {
        free(target_trans_id);
    }
    if (local_target_trans_id != NULL) {
        if (target_trans_id_known_by_local == local_target_trans_id) {
            // Don't double free
            target_trans_id_known_by_local = NULL;
        }
        free(local_target_trans_id);
        local_target_trans_id = NULL;
    }
    if (target_trans_id_known_by_local != NULL) {
        free(target_trans_id_known_by_local);
        target_trans_id_known_by_local = NULL;
    }
    if (to_send_state.doc_ids_to_return != NULL) {
        int i;

        for (i = 0; i < to_send_state.num_doc_ids; ++i) {
            free(to_send_state.doc_ids_to_return[i]);
        }
        free(to_send_state.doc_ids_to_return);
    }
    if (to_send_state.gen_for_doc_ids != NULL) {
        free(to_send_state.gen_for_doc_ids);
    }
    if (to_send_state.trans_ids_for_doc_ids != NULL) {
        free(to_send_state.trans_ids_for_doc_ids);
    }
    return status;
}