forked from sqliteai/sqlite-sync
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcloudsync.c
More file actions
3530 lines (2920 loc) · 140 KB
/
cloudsync.c
File metadata and controls
3530 lines (2920 loc) · 140 KB
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// cloudsync.c
// cloudsync
//
// Created by Marco Bambini on 16/05/24.
//
#include <inttypes.h>
#include <stdbool.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include "cloudsync.h"
#include "lz4.h"
#include "pk.h"
#include "sql.h"
#include "utils.h"
#include "dbutils.h"
#include "block.h"
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h> // for htonl, htons, ntohl, ntohs
#include <netinet/in.h> // for struct sockaddr_in, INADDR_ANY, etc. (if needed)
#endif
#ifndef htonll
#if __BIG_ENDIAN__
#define htonll(x) (x)
#define ntohll(x) (x)
#else
#ifndef htobe64
#define htonll(x) ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32 | (uint64_t)htonl((x) >> 32))
#define ntohll(x) ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32 | (uint64_t)ntohl((x) >> 32))
#else
#define htonll(x) htobe64(x)
#define ntohll(x) be64toh(x)
#endif
#endif
#endif
#define CLOUDSYNC_INIT_NTABLES 64
#define CLOUDSYNC_MIN_DB_VERSION 0
#define CLOUDSYNC_PAYLOAD_MINBUF_SIZE (512*1024)
#define CLOUDSYNC_PAYLOAD_SIGNATURE 0x434C5359 /* 'C','L','S','Y' */
#define CLOUDSYNC_PAYLOAD_VERSION_ORIGNAL 1
#define CLOUDSYNC_PAYLOAD_VERSION_1 CLOUDSYNC_PAYLOAD_VERSION_ORIGNAL
#define CLOUDSYNC_PAYLOAD_VERSION_2 2
#define CLOUDSYNC_PAYLOAD_VERSION_LATEST CLOUDSYNC_PAYLOAD_VERSION_2
#define CLOUDSYNC_PAYLOAD_MIN_VERSION_WITH_CHECKSUM CLOUDSYNC_PAYLOAD_VERSION_2
#ifndef MAX
#define MAX(a, b) (((a)>(b))?(a):(b))
#endif
#define DEBUG_DBERROR(_rc, _fn, _data) do {if (_rc != DBRES_OK) printf("Error in %s: %s\n", _fn, database_errmsg(_data));} while (0)
typedef enum {
CLOUDSYNC_PK_INDEX_TBL = 0,
CLOUDSYNC_PK_INDEX_PK = 1,
CLOUDSYNC_PK_INDEX_COLNAME = 2,
CLOUDSYNC_PK_INDEX_COLVALUE = 3,
CLOUDSYNC_PK_INDEX_COLVERSION = 4,
CLOUDSYNC_PK_INDEX_DBVERSION = 5,
CLOUDSYNC_PK_INDEX_SITEID = 6,
CLOUDSYNC_PK_INDEX_CL = 7,
CLOUDSYNC_PK_INDEX_SEQ = 8
} CLOUDSYNC_PK_INDEX;
typedef enum {
DBVM_VALUE_ERROR = -1,
DBVM_VALUE_UNCHANGED = 0,
DBVM_VALUE_CHANGED = 1,
} DBVM_VALUE;
#define SYNCBIT_SET(_data) _data->insync = 1
#define SYNCBIT_RESET(_data) _data->insync = 0
// MARK: - Deferred column-batch merge -
typedef struct {
const char *col_name; // pointer into table_context->col_name[idx] (stable)
dbvalue_t *col_value; // duplicated via database_value_dup (owned)
int64_t col_version;
int64_t db_version;
uint8_t site_id[UUID_LEN];
int site_id_len;
int64_t seq;
} merge_pending_entry;
typedef struct {
cloudsync_table_context *table;
char *pk; // malloc'd copy, freed on flush
int pk_len;
int64_t cl;
bool sentinel_pending;
bool row_exists; // true when the PK already exists locally
int count;
int capacity;
merge_pending_entry *entries;
// Statement cache — reuse the prepared statement when the column
// combination and row_exists flag match between consecutive PK flushes.
dbvm_t *cached_vm;
bool cached_row_exists;
int cached_col_count;
const char **cached_col_names; // array of pointers into table_context (not owned)
} merge_pending_batch;
// MARK: -
struct cloudsync_pk_decode_bind_context {
dbvm_t *vm;
char *tbl;
int64_t tbl_len;
const void *pk;
int64_t pk_len;
char *col_name;
int64_t col_name_len;
int64_t col_version;
int64_t db_version;
const void *site_id;
int64_t site_id_len;
int64_t cl;
int64_t seq;
};
struct cloudsync_context {
void *db;
char errmsg[1024];
int errcode;
char *libversion;
uint8_t site_id[UUID_LEN];
int insync;
int debug;
bool merge_equal_values;
void *aux_data;
// stmts and context values
dbvm_t *schema_version_stmt;
dbvm_t *data_version_stmt;
dbvm_t *db_version_stmt;
dbvm_t *getset_siteid_stmt;
int data_version;
int schema_version;
uint64_t schema_hash;
// set at transaction start and reset on commit/rollback
int64_t db_version;
// version the DB would have if the transaction committed now
int64_t pending_db_version;
// used to set an order inside each transaction
int seq;
// optional schema_name to be set in the cloudsync_table_context
char *current_schema;
// augmented tables are stored in-memory so we do not need to retrieve information about
// col_names and cid from the disk each time a write statement is performed
// we do also not need to use an hash map here because for few tables the direct
// in-memory comparison with table name is faster
cloudsync_table_context **tables; // dense vector: [0..tables_count-1] are valid
int tables_count; // size
int tables_cap; // capacity
int skip_decode_idx; // -1 in sqlite, col_value index in postgresql
// deferred column-batch merge (active during payload_apply)
merge_pending_batch *pending_batch;
};
struct cloudsync_table_context {
table_algo algo; // CRDT algoritm associated to the table
char *name; // table name
char *schema; // table schema
char *meta_ref; // schema-qualified meta table name (e.g. "schema"."name_cloudsync")
char *base_ref; // schema-qualified base table name (e.g. "schema"."name")
char **col_name; // array of column names
dbvm_t **col_merge_stmt; // array of merge insert stmt (indexed by col_name)
dbvm_t **col_value_stmt; // array of column value stmt (indexed by col_name)
int *col_id; // array of column id
col_algo_t *col_algo; // per-column algorithm (normal or block)
char **col_delimiter; // per-column delimiter for block splitting (NULL = default "\n")
bool has_block_cols; // quick check: does this table have any block columns?
dbvm_t *block_value_read_stmt; // SELECT col_value FROM blocks table
dbvm_t *block_value_write_stmt; // INSERT OR REPLACE into blocks table
dbvm_t *block_value_delete_stmt; // DELETE from blocks table
dbvm_t *block_list_stmt; // SELECT block entries for materialization
char *blocks_ref; // schema-qualified blocks table name
int ncols; // number of non primary key cols
int npks; // number of primary key cols
bool enabled; // flag to check if a table is enabled or disabled
#if !CLOUDSYNC_DISABLE_ROWIDONLY_TABLES
bool rowid_only; // a table with no primary keys other than the implicit rowid
#endif
char **pk_name; // array of primary key names
// precompiled statements
dbvm_t *meta_pkexists_stmt; // check if a primary key already exist in the augmented table
dbvm_t *meta_sentinel_update_stmt; // update a local sentinel row
dbvm_t *meta_sentinel_insert_stmt; // insert a local sentinel row
dbvm_t *meta_row_insert_update_stmt; // insert/update a local row
dbvm_t *meta_row_drop_stmt; // delete rows from meta
dbvm_t *meta_update_move_stmt; // update rows in meta when pk changes
dbvm_t *meta_local_cl_stmt; // compute local cl value
dbvm_t *meta_winner_clock_stmt; // get the rowid of the last inserted/updated row in the meta table
dbvm_t *meta_merge_delete_drop;
dbvm_t *meta_zero_clock_stmt;
dbvm_t *meta_col_version_stmt;
dbvm_t *meta_site_id_stmt;
dbvm_t *real_col_values_stmt; // retrieve all column values based on pk
dbvm_t *real_merge_delete_stmt;
dbvm_t *real_merge_sentinel_stmt;
bool is_altering; // flag to track if a table alteration is in progress
// context
cloudsync_context *context;
};
struct cloudsync_payload_context {
char *buffer;
size_t bsize;
size_t balloc;
size_t bused;
uint64_t nrows;
uint16_t ncols;
};
#ifdef _MSC_VER
#pragma pack(push, 1) // For MSVC: pack struct with 1-byte alignment
#define PACKED
#else
#define PACKED __attribute__((__packed__))
#endif
typedef struct PACKED {
uint32_t signature; // 'CLSY'
uint8_t version; // protocol version
uint8_t libversion[3]; // major.minor.patch
uint32_t expanded_size;
uint16_t ncols;
uint32_t nrows;
uint64_t schema_hash;
uint8_t checksum[6]; // 48 bits checksum (to ensure struct is 32 bytes)
} cloudsync_payload_header;
#ifdef _MSC_VER
#pragma pack(pop)
#endif
#if CLOUDSYNC_UNITTEST
bool force_uncompressed_blob = false;
#define CHECK_FORCE_UNCOMPRESSED_BUFFER() if (force_uncompressed_blob) use_uncompressed_buffer = true
#else
#define CHECK_FORCE_UNCOMPRESSED_BUFFER()
#endif
// Internal prototypes
int local_mark_insert_or_update_meta (cloudsync_table_context *table, const void *pk, size_t pklen, const char *col_name, int64_t db_version, int seq);
// MARK: - CRDT algos -
table_algo cloudsync_algo_from_name (const char *algo_name) {
if (algo_name == NULL) return table_algo_none;
if ((strcasecmp(algo_name, "CausalLengthSet") == 0) || (strcasecmp(algo_name, "cls") == 0)) return table_algo_crdt_cls;
if ((strcasecmp(algo_name, "GrowOnlySet") == 0) || (strcasecmp(algo_name, "gos") == 0)) return table_algo_crdt_gos;
if ((strcasecmp(algo_name, "DeleteWinsSet") == 0) || (strcasecmp(algo_name, "dws") == 0)) return table_algo_crdt_dws;
if ((strcasecmp(algo_name, "AddWinsSet") == 0) || (strcasecmp(algo_name, "aws") == 0)) return table_algo_crdt_aws;
// if nothing is found
return table_algo_none;
}
const char *cloudsync_algo_name (table_algo algo) {
switch (algo) {
case table_algo_crdt_cls: return "cls";
case table_algo_crdt_gos: return "gos";
case table_algo_crdt_dws: return "dws";
case table_algo_crdt_aws: return "aws";
case table_algo_none: return NULL;
}
return NULL;
}
// MARK: - DBVM Utils -
DBVM_VALUE dbvm_execute (dbvm_t *stmt, cloudsync_context *data) {
if (!stmt) return DBVM_VALUE_ERROR;
int rc = databasevm_step(stmt);
if (rc != DBRES_ROW && rc != DBRES_DONE) {
if (data) DEBUG_DBERROR(rc, "stmt_execute", data);
databasevm_reset(stmt);
return DBVM_VALUE_ERROR;
}
DBVM_VALUE result = DBVM_VALUE_CHANGED;
if (stmt == data->data_version_stmt) {
int version = (int)database_column_int(stmt, 0);
if (version != data->data_version) {
data->data_version = version;
} else {
result = DBVM_VALUE_UNCHANGED;
}
} else if (stmt == data->schema_version_stmt) {
int version = (int)database_column_int(stmt, 0);
if (version > data->schema_version) {
data->schema_version = version;
} else {
result = DBVM_VALUE_UNCHANGED;
}
} else if (stmt == data->db_version_stmt) {
data->db_version = (rc == DBRES_DONE) ? CLOUDSYNC_MIN_DB_VERSION : database_column_int(stmt, 0);
}
databasevm_reset(stmt);
return result;
}
int dbvm_count (dbvm_t *stmt, const char *value, size_t len, int type) {
int result = -1;
int rc = DBRES_OK;
if (value) {
rc = (type == DBTYPE_TEXT) ? databasevm_bind_text(stmt, 1, value, (int)len) : databasevm_bind_blob(stmt, 1, value, len);
if (rc != DBRES_OK) goto cleanup;
}
rc = databasevm_step(stmt);
if (rc == DBRES_DONE) {
result = 0;
rc = DBRES_OK;
} else if (rc == DBRES_ROW) {
result = (int)database_column_int(stmt, 0);
rc = DBRES_OK;
}
cleanup:
databasevm_reset(stmt);
return result;
}
void dbvm_reset (dbvm_t *stmt) {
if (!stmt) return;
databasevm_clear_bindings(stmt);
databasevm_reset(stmt);
}
// MARK: - Database Version -
char *cloudsync_dbversion_build_query (cloudsync_context *data) {
// this function must be manually called each time tables changes
// because the query plan changes too and it must be re-prepared
// unfortunately there is no other way
// we need to execute a query like:
/*
SELECT max(version) as version FROM (
SELECT max(db_version) as version FROM "table1_cloudsync"
UNION ALL
SELECT max(db_version) as version FROM "table2_cloudsync"
UNION ALL
SELECT max(db_version) as version FROM "table3_cloudsync"
UNION
SELECT value as version FROM cloudsync_settings WHERE key = 'pre_alter_dbversion'
)
*/
// the good news is that the query can be computed in SQLite without the need to do any extra computation from the host language
char *value = NULL;
int rc = database_select_text(data, SQL_DBVERSION_BUILD_QUERY, &value);
return (rc == DBRES_OK) ? value : NULL;
}
int cloudsync_dbversion_rebuild (cloudsync_context *data) {
if (data->db_version_stmt) {
databasevm_finalize(data->db_version_stmt);
data->db_version_stmt = NULL;
}
int64_t count = dbutils_table_settings_count_tables(data);
if (count == 0) return DBRES_OK;
else if (count == -1) return cloudsync_set_dberror(data);
char *sql = cloudsync_dbversion_build_query(data);
if (!sql) return DBRES_NOMEM;
DEBUG_SQL("db_version_stmt: %s", sql);
int rc = databasevm_prepare(data, sql, (void **)&data->db_version_stmt, DBFLAG_PERSISTENT);
DEBUG_STMT("db_version_stmt %p", data->db_version_stmt);
cloudsync_memory_free(sql);
return rc;
}
int cloudsync_dbversion_rerun (cloudsync_context *data) {
DBVM_VALUE schema_changed = dbvm_execute(data->schema_version_stmt, data);
if (schema_changed == DBVM_VALUE_ERROR) return -1;
if (schema_changed == DBVM_VALUE_CHANGED) {
int rc = cloudsync_dbversion_rebuild(data);
if (rc != DBRES_OK) return -1;
}
if (!data->db_version_stmt) {
data->db_version = CLOUDSYNC_MIN_DB_VERSION;
return 0;
}
DBVM_VALUE rc = dbvm_execute(data->db_version_stmt, data);
if (rc == DBVM_VALUE_ERROR) return -1;
return 0;
}
int cloudsync_dbversion_check_uptodate (cloudsync_context *data) {
// perform a PRAGMA data_version to check if some other process write any data
DBVM_VALUE rc = dbvm_execute(data->data_version_stmt, data);
if (rc == DBVM_VALUE_ERROR) return -1;
// db_version is already set and there is no need to update it
if (data->db_version != CLOUDSYNC_VALUE_NOTSET && rc == DBVM_VALUE_UNCHANGED) return 0;
return cloudsync_dbversion_rerun(data);
}
int64_t cloudsync_dbversion_next (cloudsync_context *data, int64_t merging_version) {
int rc = cloudsync_dbversion_check_uptodate(data);
if (rc != DBRES_OK) return -1;
int64_t result = data->db_version + 1;
if (result < data->pending_db_version) result = data->pending_db_version;
if (merging_version != CLOUDSYNC_VALUE_NOTSET && result < merging_version) result = merging_version;
data->pending_db_version = result;
return result;
}
// MARK: - PK Context -
char *cloudsync_pk_context_tbl (cloudsync_pk_decode_bind_context *ctx, int64_t *tbl_len) {
*tbl_len = ctx->tbl_len;
return ctx->tbl;
}
void *cloudsync_pk_context_pk (cloudsync_pk_decode_bind_context *ctx, int64_t *pk_len) {
*pk_len = ctx->pk_len;
return (void *)ctx->pk;
}
char *cloudsync_pk_context_colname (cloudsync_pk_decode_bind_context *ctx, int64_t *colname_len) {
*colname_len = ctx->col_name_len;
return ctx->col_name;
}
int64_t cloudsync_pk_context_cl (cloudsync_pk_decode_bind_context *ctx) {
return ctx->cl;
}
int64_t cloudsync_pk_context_dbversion (cloudsync_pk_decode_bind_context *ctx) {
return ctx->db_version;
}
// MARK: - CloudSync Context -
int cloudsync_insync (cloudsync_context *data) {
return data->insync;
}
void *cloudsync_siteid (cloudsync_context *data) {
return (void *)data->site_id;
}
void cloudsync_reset_siteid (cloudsync_context *data) {
memset(data->site_id, 0, sizeof(uint8_t) * UUID_LEN);
}
int cloudsync_load_siteid (cloudsync_context *data) {
// check if site_id was already loaded
if (data->site_id[0] != 0) return DBRES_OK;
// load site_id
char *buffer = NULL;
int64_t size = 0;
int rc = database_select_blob(data, SQL_SITEID_SELECT_ROWID0, &buffer, &size);
if (rc != DBRES_OK) return rc;
if (!buffer || size != UUID_LEN) {
if (buffer) cloudsync_memory_free(buffer);
return cloudsync_set_error(data, "Unable to retrieve siteid", DBRES_MISUSE);
}
memcpy(data->site_id, buffer, UUID_LEN);
cloudsync_memory_free(buffer);
return DBRES_OK;
}
int64_t cloudsync_dbversion (cloudsync_context *data) {
return data->db_version;
}
int cloudsync_bumpseq (cloudsync_context *data) {
int value = data->seq;
data->seq += 1;
return value;
}
void cloudsync_update_schema_hash (cloudsync_context *data) {
database_update_schema_hash(data, &data->schema_hash);
}
void *cloudsync_db (cloudsync_context *data) {
return data->db;
}
int cloudsync_add_dbvms (cloudsync_context *data) {
DEBUG_DBFUNCTION("cloudsync_add_stmts");
if (data->data_version_stmt == NULL) {
int rc = databasevm_prepare(data, SQL_DATA_VERSION, (void **)&data->data_version_stmt, DBFLAG_PERSISTENT);
DEBUG_STMT("data_version_stmt %p", data->data_version_stmt);
if (rc != DBRES_OK) return rc;
DEBUG_SQL("data_version_stmt: %s", SQL_DATA_VERSION);
}
if (data->schema_version_stmt == NULL) {
int rc = databasevm_prepare(data, SQL_SCHEMA_VERSION, (void **)&data->schema_version_stmt, DBFLAG_PERSISTENT);
DEBUG_STMT("schema_version_stmt %p", data->schema_version_stmt);
if (rc != DBRES_OK) return rc;
DEBUG_SQL("schema_version_stmt: %s", SQL_SCHEMA_VERSION);
}
if (data->getset_siteid_stmt == NULL) {
// get and set index of the site_id
// in SQLite, we can’t directly combine an INSERT and a SELECT to both insert a row and return an identifier (rowid) in a single statement,
// however, we can use a workaround by leveraging the INSERT statement with ON CONFLICT DO UPDATE and then combining it with RETURNING rowid
int rc = databasevm_prepare(data, SQL_SITEID_GETSET_ROWID_BY_SITEID, (void **)&data->getset_siteid_stmt, DBFLAG_PERSISTENT);
DEBUG_STMT("getset_siteid_stmt %p", data->getset_siteid_stmt);
if (rc != DBRES_OK) return rc;
DEBUG_SQL("getset_siteid_stmt: %s", SQL_SITEID_GETSET_ROWID_BY_SITEID);
}
return cloudsync_dbversion_rebuild(data);
}
int cloudsync_set_error (cloudsync_context *data, const char *err_user, int err_code) {
// force err_code to be something different than OK
if (err_code == DBRES_OK) err_code = database_errcode(data);
if (err_code == DBRES_OK) err_code = DBRES_ERROR;
// compute a meaningful error message
if (err_user == NULL) {
snprintf(data->errmsg, sizeof(data->errmsg), "%s", database_errmsg(data));
} else {
const char *db_error = database_errmsg(data);
char db_error_copy[sizeof(data->errmsg)];
int rc = database_errcode(data);
if (rc == DBRES_OK) {
snprintf(data->errmsg, sizeof(data->errmsg), "%s", err_user);
} else {
if (db_error == data->errmsg) {
snprintf(db_error_copy, sizeof(db_error_copy), "%s", db_error);
db_error = db_error_copy;
}
snprintf(data->errmsg, sizeof(data->errmsg), "%s (%s)", err_user, db_error);
}
}
data->errcode = err_code;
return err_code;
}
int cloudsync_set_dberror (cloudsync_context *data) {
return cloudsync_set_error(data, NULL, DBRES_OK);
}
const char *cloudsync_errmsg (cloudsync_context *data) {
return data->errmsg;
}
int cloudsync_errcode (cloudsync_context *data) {
return data->errcode;
}
void cloudsync_reset_error (cloudsync_context *data) {
data->errmsg[0] = 0;
data->errcode = DBRES_OK;
}
void *cloudsync_auxdata (cloudsync_context *data) {
return data->aux_data;
}
void cloudsync_set_auxdata (cloudsync_context *data, void *xdata) {
data->aux_data = xdata;
}
void cloudsync_set_schema (cloudsync_context *data, const char *schema) {
if (data->current_schema && schema && strcmp(data->current_schema, schema) == 0) return;
if (data->current_schema) cloudsync_memory_free(data->current_schema);
data->current_schema = NULL;
if (schema) data->current_schema = cloudsync_string_dup_lowercase(schema);
}
const char *cloudsync_schema (cloudsync_context *data) {
return data->current_schema;
}
const char *cloudsync_table_schema (cloudsync_context *data, const char *table_name) {
cloudsync_table_context *table = table_lookup(data, table_name);
if (!table) return NULL;
return table->schema;
}
// MARK: - Table Utils -
void table_pknames_free (char **names, int nrows) {
if (!names) return;
for (int i = 0; i < nrows; ++i) {cloudsync_memory_free(names[i]);}
cloudsync_memory_free(names);
}
char *table_build_mergedelete_sql (cloudsync_table_context *table) {
#if !CLOUDSYNC_DISABLE_ROWIDONLY_TABLES
if (table->rowid_only) {
char *sql = memory_mprintf(SQL_DELETE_ROW_BY_ROWID, table->name);
return sql;
}
#endif
return sql_build_delete_by_pk(table->context, table->name, table->schema);
}
char *table_build_mergeinsert_sql (cloudsync_table_context *table, const char *colname) {
char *sql = NULL;
#if !CLOUDSYNC_DISABLE_ROWIDONLY_TABLES
if (table->rowid_only) {
if (colname == NULL) {
// INSERT OR IGNORE INTO customers (first_name,last_name) VALUES (?,?);
sql = memory_mprintf(SQL_INSERT_ROWID_IGNORE, table->name);
} else {
// INSERT INTO customers (first_name,last_name,age) VALUES (?,?,?) ON CONFLICT DO UPDATE SET age=?;
sql = memory_mprintf(SQL_UPSERT_ROWID_AND_COL_BY_ROWID, table->name, colname, colname);
}
return sql;
}
#endif
if (colname == NULL) {
// is sentinel insert
sql = sql_build_insert_pk_ignore(table->context, table->name, table->schema);
} else {
sql = sql_build_upsert_pk_and_col(table->context, table->name, colname, table->schema);
}
return sql;
}
char *table_build_value_sql (cloudsync_table_context *table, const char *colname) {
#if !CLOUDSYNC_DISABLE_ROWIDONLY_TABLES
if (table->rowid_only) {
char *colnamequote = "\"";
char *sql = memory_mprintf(SQL_SELECT_COLS_BY_ROWID_FMT, colnamequote, colname, colnamequote, table->name);
return sql;
}
#endif
// SELECT age FROM customers WHERE first_name=? AND last_name=?;
return sql_build_select_cols_by_pk(table->context, table->name, colname, table->schema);
}
cloudsync_table_context *table_create (cloudsync_context *data, const char *name, table_algo algo) {
DEBUG_DBFUNCTION("table_create %s", name);
cloudsync_table_context *table = (cloudsync_table_context *)cloudsync_memory_zeroalloc(sizeof(cloudsync_table_context));
if (!table) return NULL;
table->context = data;
table->algo = algo;
table->name = cloudsync_string_dup_lowercase(name);
// Detect schema from metadata table location. If metadata table doesn't
// exist yet (during initialization), fall back to cloudsync_schema() which
// returns the explicitly set schema or current_schema().
table->schema = database_table_schema(name);
if (!table->schema) {
const char *fallback_schema = cloudsync_schema(data);
if (fallback_schema) {
table->schema = cloudsync_string_dup(fallback_schema);
}
}
if (!table->name) {
cloudsync_memory_free(table);
return NULL;
}
table->meta_ref = database_build_meta_ref(table->schema, table->name);
table->base_ref = database_build_base_ref(table->schema, table->name);
table->enabled = true;
return table;
}
void table_free (cloudsync_table_context *table) {
DEBUG_DBFUNCTION("table_free %s", (table) ? (table->name) : "NULL");
if (!table) return;
if (table->ncols > 0) {
if (table->col_name) {
for (int i=0; i<table->ncols; ++i) {
cloudsync_memory_free(table->col_name[i]);
}
cloudsync_memory_free(table->col_name);
}
if (table->col_merge_stmt) {
for (int i=0; i<table->ncols; ++i) {
databasevm_finalize(table->col_merge_stmt[i]);
}
cloudsync_memory_free(table->col_merge_stmt);
}
if (table->col_value_stmt) {
for (int i=0; i<table->ncols; ++i) {
databasevm_finalize(table->col_value_stmt[i]);
}
cloudsync_memory_free(table->col_value_stmt);
}
if (table->col_id) {
cloudsync_memory_free(table->col_id);
}
if (table->col_algo) {
cloudsync_memory_free(table->col_algo);
}
if (table->col_delimiter) {
for (int i=0; i<table->ncols; ++i) {
if (table->col_delimiter[i]) cloudsync_memory_free(table->col_delimiter[i]);
}
cloudsync_memory_free(table->col_delimiter);
}
}
if (table->block_value_read_stmt) databasevm_finalize(table->block_value_read_stmt);
if (table->block_value_write_stmt) databasevm_finalize(table->block_value_write_stmt);
if (table->block_value_delete_stmt) databasevm_finalize(table->block_value_delete_stmt);
if (table->block_list_stmt) databasevm_finalize(table->block_list_stmt);
if (table->blocks_ref) cloudsync_memory_free(table->blocks_ref);
if (table->name) cloudsync_memory_free(table->name);
if (table->schema) cloudsync_memory_free(table->schema);
if (table->meta_ref) cloudsync_memory_free(table->meta_ref);
if (table->base_ref) cloudsync_memory_free(table->base_ref);
if (table->pk_name) table_pknames_free(table->pk_name, table->npks);
if (table->meta_pkexists_stmt) databasevm_finalize(table->meta_pkexists_stmt);
if (table->meta_sentinel_update_stmt) databasevm_finalize(table->meta_sentinel_update_stmt);
if (table->meta_sentinel_insert_stmt) databasevm_finalize(table->meta_sentinel_insert_stmt);
if (table->meta_row_insert_update_stmt) databasevm_finalize(table->meta_row_insert_update_stmt);
if (table->meta_row_drop_stmt) databasevm_finalize(table->meta_row_drop_stmt);
if (table->meta_update_move_stmt) databasevm_finalize(table->meta_update_move_stmt);
if (table->meta_local_cl_stmt) databasevm_finalize(table->meta_local_cl_stmt);
if (table->meta_winner_clock_stmt) databasevm_finalize(table->meta_winner_clock_stmt);
if (table->meta_merge_delete_drop) databasevm_finalize(table->meta_merge_delete_drop);
if (table->meta_zero_clock_stmt) databasevm_finalize(table->meta_zero_clock_stmt);
if (table->meta_col_version_stmt) databasevm_finalize(table->meta_col_version_stmt);
if (table->meta_site_id_stmt) databasevm_finalize(table->meta_site_id_stmt);
if (table->real_col_values_stmt) databasevm_finalize(table->real_col_values_stmt);
if (table->real_merge_delete_stmt) databasevm_finalize(table->real_merge_delete_stmt);
if (table->real_merge_sentinel_stmt) databasevm_finalize(table->real_merge_sentinel_stmt);
cloudsync_memory_free(table);
}
int table_add_stmts (cloudsync_table_context *table, int ncols) {
int rc = DBRES_OK;
char *sql = NULL;
cloudsync_context *data = table->context;
// META TABLE statements
// CREATE TABLE IF NOT EXISTS \"%w_cloudsync\" (pk BLOB NOT NULL, col_name TEXT NOT NULL, col_version INTEGER, db_version INTEGER, site_id INTEGER DEFAULT 0, seq INTEGER, PRIMARY KEY (pk, col_name));
// precompile the pk exists statement
// we do not need an index on the pk column because it is already covered by the fact that it is part of the prikeys
// EXPLAIN QUERY PLAN reports: SEARCH table_name USING PRIMARY KEY (pk=?)
sql = cloudsync_memory_mprintf(SQL_CLOUDSYNC_ROW_EXISTS_BY_PK, table->meta_ref);
if (!sql) {rc = DBRES_NOMEM; goto cleanup;}
DEBUG_SQL("meta_pkexists_stmt: %s", sql);
rc = databasevm_prepare(data, sql, (void **)&table->meta_pkexists_stmt, DBFLAG_PERSISTENT);
cloudsync_memory_free(sql);
if (rc != DBRES_OK) goto cleanup;
// precompile the update local sentinel statement
sql = cloudsync_memory_mprintf(SQL_CLOUDSYNC_UPDATE_COL_BUMP_VERSION, table->meta_ref, CLOUDSYNC_TOMBSTONE_VALUE);
if (!sql) {rc = DBRES_NOMEM; goto cleanup;}
DEBUG_SQL("meta_sentinel_update_stmt: %s", sql);
rc = databasevm_prepare(data, sql, (void **)&table->meta_sentinel_update_stmt, DBFLAG_PERSISTENT);
cloudsync_memory_free(sql);
if (rc != DBRES_OK) goto cleanup;
// precompile the insert local sentinel statement
sql = cloudsync_memory_mprintf(SQL_CLOUDSYNC_UPSERT_COL_INIT_OR_BUMP_VERSION, table->meta_ref, CLOUDSYNC_TOMBSTONE_VALUE, table->meta_ref, table->meta_ref, table->meta_ref);
if (!sql) {rc = DBRES_NOMEM; goto cleanup;}
DEBUG_SQL("meta_sentinel_insert_stmt: %s", sql);
rc = databasevm_prepare(data, sql, (void **)&table->meta_sentinel_insert_stmt, DBFLAG_PERSISTENT);
cloudsync_memory_free(sql);
if (rc != DBRES_OK) goto cleanup;
// precompile the insert/update local row statement
sql = cloudsync_memory_mprintf(SQL_CLOUDSYNC_UPSERT_RAW_COLVERSION, table->meta_ref, table->meta_ref);
if (!sql) {rc = DBRES_NOMEM; goto cleanup;}
DEBUG_SQL("meta_row_insert_update_stmt: %s", sql);
rc = databasevm_prepare(data, sql, (void **)&table->meta_row_insert_update_stmt, DBFLAG_PERSISTENT);
cloudsync_memory_free(sql);
if (rc != DBRES_OK) goto cleanup;
// precompile the delete rows from meta
sql = cloudsync_memory_mprintf(SQL_CLOUDSYNC_DELETE_PK_EXCEPT_COL, table->meta_ref, CLOUDSYNC_TOMBSTONE_VALUE);
if (!sql) {rc = DBRES_NOMEM; goto cleanup;}
DEBUG_SQL("meta_row_drop_stmt: %s", sql);
rc = databasevm_prepare(data, sql, (void **)&table->meta_row_drop_stmt, DBFLAG_PERSISTENT);
cloudsync_memory_free(sql);
if (rc != DBRES_OK) goto cleanup;
// precompile the update rows from meta when pk changes
// see https://github.com/sqliteai/sqlite-sync/blob/main/docs/PriKey.md for more details
sql = sql_build_rekey_pk_and_reset_version_except_col(data, table->name, CLOUDSYNC_TOMBSTONE_VALUE);
if (!sql) {rc = DBRES_NOMEM; goto cleanup;}
DEBUG_SQL("meta_update_move_stmt: %s", sql);
rc = databasevm_prepare(data, sql, (void **)&table->meta_update_move_stmt, DBFLAG_PERSISTENT);
cloudsync_memory_free(sql);
if (rc != DBRES_OK) goto cleanup;
// local cl
sql = cloudsync_memory_mprintf(SQL_CLOUDSYNC_GET_COL_VERSION_OR_ROW_EXISTS, table->meta_ref, CLOUDSYNC_TOMBSTONE_VALUE, table->meta_ref);
if (!sql) {rc = DBRES_NOMEM; goto cleanup;}
DEBUG_SQL("meta_local_cl_stmt: %s", sql);
rc = databasevm_prepare(data, sql, (void **)&table->meta_local_cl_stmt, DBFLAG_PERSISTENT);
cloudsync_memory_free(sql);
if (rc != DBRES_OK) goto cleanup;
// rowid of the last inserted/updated row in the meta table
sql = cloudsync_memory_mprintf(SQL_CLOUDSYNC_INSERT_RETURN_CHANGE_ID, table->meta_ref);
if (!sql) {rc = DBRES_NOMEM; goto cleanup;}
DEBUG_SQL("meta_winner_clock_stmt: %s", sql);
rc = databasevm_prepare(data, sql, (void **)&table->meta_winner_clock_stmt, DBFLAG_PERSISTENT);
cloudsync_memory_free(sql);
if (rc != DBRES_OK) goto cleanup;
sql = cloudsync_memory_mprintf(SQL_CLOUDSYNC_DELETE_PK_EXCEPT_COL, table->meta_ref, CLOUDSYNC_TOMBSTONE_VALUE);
if (!sql) {rc = DBRES_NOMEM; goto cleanup;}
DEBUG_SQL("meta_merge_delete_drop: %s", sql);
rc = databasevm_prepare(data, sql, (void **)&table->meta_merge_delete_drop, DBFLAG_PERSISTENT);
cloudsync_memory_free(sql);
if (rc != DBRES_OK) goto cleanup;
// zero clock
sql = cloudsync_memory_mprintf(SQL_CLOUDSYNC_TOMBSTONE_PK_EXCEPT_COL, table->meta_ref, CLOUDSYNC_TOMBSTONE_VALUE);
if (!sql) {rc = DBRES_NOMEM; goto cleanup;}
DEBUG_SQL("meta_zero_clock_stmt: %s", sql);
rc = databasevm_prepare(data, sql, (void **)&table->meta_zero_clock_stmt, DBFLAG_PERSISTENT);
cloudsync_memory_free(sql);
if (rc != DBRES_OK) goto cleanup;
// col_version
sql = cloudsync_memory_mprintf(SQL_CLOUDSYNC_SELECT_COL_VERSION_BY_PK_COL, table->meta_ref);
if (!sql) {rc = DBRES_NOMEM; goto cleanup;}
DEBUG_SQL("meta_col_version_stmt: %s", sql);
rc = databasevm_prepare(data, sql, (void **)&table->meta_col_version_stmt, DBFLAG_PERSISTENT);
cloudsync_memory_free(sql);
if (rc != DBRES_OK) goto cleanup;
// site_id
sql = cloudsync_memory_mprintf(SQL_CLOUDSYNC_SELECT_SITE_ID_BY_PK_COL, table->meta_ref);
if (!sql) {rc = DBRES_NOMEM; goto cleanup;}
DEBUG_SQL("meta_site_id_stmt: %s", sql);
rc = databasevm_prepare(data, sql, (void **)&table->meta_site_id_stmt, DBFLAG_PERSISTENT);
cloudsync_memory_free(sql);
if (rc != DBRES_OK) goto cleanup;
// REAL TABLE statements
// precompile the get column value statement
if (ncols > 0) {
sql = sql_build_select_nonpk_by_pk(data, table->name, table->schema);
if (!sql) {rc = DBRES_NOMEM; goto cleanup;}
DEBUG_SQL("real_col_values_stmt: %s", sql);
rc = databasevm_prepare(data, sql, (void **)&table->real_col_values_stmt, DBFLAG_PERSISTENT);
cloudsync_memory_free(sql);
if (rc != DBRES_OK) goto cleanup;
}
sql = table_build_mergedelete_sql(table);
if (!sql) {rc = DBRES_NOMEM; goto cleanup;}
DEBUG_SQL("real_merge_delete: %s", sql);
rc = databasevm_prepare(data, sql, (void **)&table->real_merge_delete_stmt, DBFLAG_PERSISTENT);
cloudsync_memory_free(sql);
if (rc != DBRES_OK) goto cleanup;
sql = table_build_mergeinsert_sql(table, NULL);
if (!sql) {rc = DBRES_NOMEM; goto cleanup;}
DEBUG_SQL("real_merge_sentinel: %s", sql);
rc = databasevm_prepare(data, sql, (void **)&table->real_merge_sentinel_stmt, DBFLAG_PERSISTENT);
cloudsync_memory_free(sql);
if (rc != DBRES_OK) goto cleanup;
cleanup:
if (rc != DBRES_OK) DEBUG_ALWAYS("table_add_stmts error: %d %s\n", rc, database_errmsg(data));
return rc;
}
cloudsync_table_context *table_lookup (cloudsync_context *data, const char *table_name) {
DEBUG_DBFUNCTION("table_lookup %s", table_name);
if (table_name) {
for (int i=0; i<data->tables_count; ++i) {
if ((strcasecmp(data->tables[i]->name, table_name) == 0)) return data->tables[i];
}
}
return NULL;
}
void *table_column_lookup (cloudsync_table_context *table, const char *col_name, bool is_merge, int *index) {
DEBUG_DBFUNCTION("table_column_lookup %s", col_name);
for (int i=0; i<table->ncols; ++i) {
if (strcasecmp(table->col_name[i], col_name) == 0) {
if (index) *index = i;
return (is_merge) ? table->col_merge_stmt[i] : table->col_value_stmt[i];
}
}
if (index) *index = -1;
return NULL;
}
int table_remove (cloudsync_context *data, cloudsync_table_context *table) {
const char *table_name = table->name;
DEBUG_DBFUNCTION("table_remove %s", table_name);
for (int i = 0; i < data->tables_count; ++i) {
cloudsync_table_context *t = data->tables[i];
// pointer compare is fastest but fallback to strcasecmp if not same pointer
if ((t == table) || ((strcasecmp(t->name, table_name) == 0))) {
int last = data->tables_count - 1;
data->tables[i] = data->tables[last]; // move last into the hole (keeps array dense)
data->tables[last] = NULL; // NULLify tail (as an extra security measure)
data->tables_count--;
return data->tables_count;
}
}
return -1;
}
int table_add_to_context_cb (void *xdata, int ncols, char **values, char **names) {
cloudsync_table_context *table = (cloudsync_table_context *)xdata;
cloudsync_context *data = table->context;
int index = table->ncols;
for (int i=0; i<ncols; i+=2) {
const char *name = values[i];
int cid = (int)strtol(values[i+1], NULL, 0);
table->col_id[index] = cid;
table->col_name[index] = cloudsync_string_dup_lowercase(name);
if (!table->col_name[index]) goto error;
char *sql = table_build_mergeinsert_sql(table, name);
if (!sql) goto error;