Skip to content

Commit 436544b

Browse files
committed
Code refactoring
1 parent 048d74d commit 436544b

File tree

7 files changed

+108
-54
lines changed

7 files changed

+108
-54
lines changed

src/cloudsync.c

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <math.h>
1818

1919
#include "cloudsync.h"
20+
#include "cloudsync_private.h"
2021
#include "lz4.h"
2122
#include "pk.h"
2223
#include "vtab.h"
@@ -145,6 +146,22 @@ typedef struct {
145146

146147
} cloudsync_table_context;
147148

149+
struct cloudsync_pk_decode_bind_context {
150+
sqlite3_stmt *vm;
151+
char *tbl;
152+
int64_t tbl_len;
153+
const void *pk;
154+
int64_t pk_len;
155+
char *col_name;
156+
int64_t col_name_len;
157+
int64_t col_version;
158+
int64_t db_version;
159+
const void *site_id;
160+
int64_t site_id_len;
161+
int64_t cl;
162+
int64_t seq;
163+
};
164+
148165
struct cloudsync_context {
149166
char *libversion;
150167
uint8_t site_id[UUID_LEN];
@@ -423,6 +440,31 @@ void cloudsync_set_auxdata (sqlite3_context *context, void *xdata) {
423440
if (data) data->aux_data = xdata;
424441
}
425442

443+
// MARK: - PK Context -
444+
445+
char *cloudsync_pk_context_tbl (cloudsync_pk_decode_bind_context *ctx, int64_t *tbl_len) {
446+
*tbl_len = ctx->tbl_len;
447+
return ctx->tbl;
448+
}
449+
450+
void *cloudsync_pk_context_pk (cloudsync_pk_decode_bind_context *ctx, int64_t *pk_len) {
451+
*pk_len = ctx->pk_len;
452+
return (void *)ctx->pk;
453+
}
454+
455+
char *cloudsync_pk_context_colname (cloudsync_pk_decode_bind_context *ctx, int64_t *colname_len) {
456+
*colname_len = ctx->col_name_len;
457+
return ctx->col_name;
458+
}
459+
460+
int64_t cloudsync_pk_context_cl (cloudsync_pk_decode_bind_context *ctx) {
461+
return ctx->cl;
462+
}
463+
464+
int64_t cloudsync_pk_context_dbversion (cloudsync_pk_decode_bind_context *ctx) {
465+
return ctx->db_version;
466+
}
467+
426468
// MARK: - Table Utils -
427469

428470
char *table_build_values_sql (sqlite3 *db, cloudsync_table_context *table) {
@@ -1496,11 +1538,13 @@ void cloudsync_sync_key(cloudsync_context *data, const char *key, const char *va
14961538
}
14971539
}
14981540

1541+
#if 0
14991542
void cloudsync_sync_table_key(cloudsync_context *data, const char *table, const char *column, const char *key, const char *value) {
15001543
DEBUG_SETTINGS("cloudsync_sync_table_key table: %s column: %s key: %s value: %s", table, column, key, value);
1501-
// TODO: implement me
1544+
// Unused in this version
15021545
return;
15031546
}
1547+
#endif
15041548

15051549
int cloudsync_commit_hook (void *ctx) {
15061550
cloudsync_context *data = (cloudsync_context *)ctx;
@@ -2834,8 +2878,8 @@ int cloudsync_init_internal (sqlite3_context *context, const char *table_name, c
28342878
// It is safe to call the following function multiple times, if there is nothing to update nothing will be changed.
28352879
// After an alter table, in contrast, all the cloudsync triggers, tables and stmts must be recreated.
28362880

2837-
// sync algo with table
2838-
cloudsync_sync_table_key(data, table_name, "*", CLOUDSYNC_KEY_ALGO, crdt_algo_name(algo_new));
2881+
// sync algo with table (unused in this version)
2882+
// cloudsync_sync_table_key(data, table_name, "*", CLOUDSYNC_KEY_ALGO, crdt_algo_name(algo_new));
28392883

28402884
// check triggers
28412885
dbutils_check_triggers(db, table_name, algo_new);

src/cloudsync.h

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,8 @@
1616
#include "sqlite3.h"
1717
#endif
1818

19-
#define CLOUDSYNC_VERSION "0.7.3"
20-
#define CLOUDSYNC_TOMBSTONE_VALUE "__[RIP]__"
21-
#define CLOUDSYNC_RLS_RESTRICTED_VALUE "__[RLS]__"
22-
#define CLOUDSYNC_DISABLE_ROWIDONLY_TABLES 1
19+
#define CLOUDSYNC_VERSION "0.7.4"
2320

2421
int sqlite3_cloudsync_init (sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi);
2522

26-
// PRIVATE
27-
28-
typedef enum {
29-
CLOUDSYNC_PAYLOAD_APPLY_WILL_APPLY = 1,
30-
CLOUDSYNC_PAYLOAD_APPLY_DID_APPLY = 2,
31-
CLOUDSYNC_PAYLOAD_APPLY_CLEANUP = 3
32-
} CLOUDSYNC_PAYLOAD_APPLY_STEPS;
33-
34-
typedef struct {
35-
sqlite3_stmt *vm;
36-
char *tbl;
37-
int64_t tbl_len;
38-
const void *pk;
39-
int64_t pk_len;
40-
char *col_name;
41-
int64_t col_name_len;
42-
int64_t col_version;
43-
int64_t db_version;
44-
const void *site_id;
45-
int64_t site_id_len;
46-
int64_t cl;
47-
int64_t seq;
48-
} cloudsync_pk_decode_bind_context;
49-
50-
typedef struct cloudsync_context cloudsync_context;
51-
typedef bool (*cloudsync_payload_apply_callback_t)(void **xdata, cloudsync_pk_decode_bind_context *decoded_change, sqlite3 *db, cloudsync_context *data, int step, int rc);
52-
void *cloudsync_get_auxdata (sqlite3_context *context);
53-
void cloudsync_set_auxdata (sqlite3_context *context, void *xdata);
54-
int cloudsync_payload_apply (sqlite3_context *context, const char *payload, int blen);
55-
void cloudsync_set_payload_apply_callback(sqlite3 *db, cloudsync_payload_apply_callback_t callback);
56-
bool cloudsync_config_exists (sqlite3 *db);
57-
sqlite3_stmt *cloudsync_colvalue_stmt (sqlite3 *db, cloudsync_context *data, const char *tbl_name, bool *persistent);
58-
const char *cloudsync_context_init (sqlite3 *db, cloudsync_context *data, sqlite3_context *context);
59-
int cloudsync_merge_insert (sqlite3_vtab *vtab, int argc, sqlite3_value **argv, sqlite3_int64 *rowid);
60-
void cloudsync_sync_key (cloudsync_context *data, const char *key, const char *value);
61-
void cloudsync_sync_table_key (cloudsync_context *data, const char *table, const char *column, const char *key, const char *value);
62-
63-
64-
6523
#endif

src/cloudsync_private.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// cloudsync_private.h
3+
// cloudsync
4+
//
5+
// Created by Marco Bambini on 30/05/25.
6+
//
7+
8+
#ifndef __CLOUDSYNC_PRIVATE__
9+
#define __CLOUDSYNC_PRIVATE__
10+
11+
#include <stdbool.h>
12+
#ifndef SQLITE_CORE
13+
#include "sqlite3ext.h"
14+
#else
15+
#include "sqlite3.h"
16+
#endif
17+
18+
19+
#define CLOUDSYNC_TOMBSTONE_VALUE "__[RIP]__"
20+
#define CLOUDSYNC_RLS_RESTRICTED_VALUE "__[RLS]__"
21+
#define CLOUDSYNC_DISABLE_ROWIDONLY_TABLES 1
22+
23+
typedef enum {
24+
CLOUDSYNC_PAYLOAD_APPLY_WILL_APPLY = 1,
25+
CLOUDSYNC_PAYLOAD_APPLY_DID_APPLY = 2,
26+
CLOUDSYNC_PAYLOAD_APPLY_CLEANUP = 3
27+
} CLOUDSYNC_PAYLOAD_APPLY_STEPS;
28+
29+
typedef struct cloudsync_context cloudsync_context;
30+
typedef struct cloudsync_pk_decode_bind_context cloudsync_pk_decode_bind_context;
31+
32+
int cloudsync_merge_insert (sqlite3_vtab *vtab, int argc, sqlite3_value **argv, sqlite3_int64 *rowid);
33+
void cloudsync_sync_key (cloudsync_context *data, const char *key, const char *value);
34+
35+
// used by network layer
36+
const char *cloudsync_context_init (sqlite3 *db, cloudsync_context *data, sqlite3_context *context);
37+
void *cloudsync_get_auxdata (sqlite3_context *context);
38+
void cloudsync_set_auxdata (sqlite3_context *context, void *xdata);
39+
int cloudsync_payload_apply (sqlite3_context *context, const char *payload, int blen);
40+
41+
// used by core
42+
typedef bool (*cloudsync_payload_apply_callback_t)(void **xdata, cloudsync_pk_decode_bind_context *decoded_change, sqlite3 *db, cloudsync_context *data, int step, int rc);
43+
void cloudsync_set_payload_apply_callback(sqlite3 *db, cloudsync_payload_apply_callback_t callback);
44+
45+
bool cloudsync_config_exists (sqlite3 *db);
46+
sqlite3_stmt *cloudsync_colvalue_stmt (sqlite3 *db, cloudsync_context *data, const char *tbl_name, bool *persistent);
47+
char *cloudsync_pk_context_tbl (cloudsync_pk_decode_bind_context *ctx, int64_t *tbl_len);
48+
void *cloudsync_pk_context_pk (cloudsync_pk_decode_bind_context *ctx, int64_t *pk_len);
49+
char *cloudsync_pk_context_colname (cloudsync_pk_decode_bind_context *ctx, int64_t *colname_len);
50+
int64_t cloudsync_pk_context_cl (cloudsync_pk_decode_bind_context *ctx);
51+
int64_t cloudsync_pk_context_dbversion (cloudsync_pk_decode_bind_context *ctx);
52+
53+
54+
#endif

src/dbutils.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,9 @@ int dbutils_table_settings_set_key_value (sqlite3 *db, sqlite3_context *context,
845845
rc = dbutils_write(db, context, sql, values, types, lens, 3);
846846
}
847847

848-
cloudsync_context *data = (context) ? (cloudsync_context *)sqlite3_user_data(context) : NULL;
849-
if (rc == SQLITE_OK && data) cloudsync_sync_table_key(data, table, column, key, value);
848+
// unused in this version
849+
// cloudsync_context *data = (context) ? (cloudsync_context *)sqlite3_user_data(context) : NULL;
850+
// if (rc == SQLITE_OK && data) cloudsync_sync_table_key(data, table, column, key, value);
850851
return rc;
851852
}
852853

src/dbutils.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@
1010

1111
#include <stdbool.h>
1212
#include "utils.h"
13-
14-
#ifndef SQLITE_CORE
15-
#include "sqlite3ext.h"
16-
#else
17-
#include "sqlite3.h"
18-
#endif
13+
#include "cloudsync_private.h"
1914

2015
#define CLOUDSYNC_SETTINGS_NAME "cloudsync_settings"
2116
#define CLOUDSYNC_SITEID_NAME "cloudsync_site_id"

src/network.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "dbutils.h"
1313
#include "utils.h"
1414
#include "curl/curl.h"
15+
#include "cloudsync_private.h"
1516

1617
#define CLOUDSYNC_ENDPOINT_PREFIX "v1/cloudsync"
1718
#define CLOUDSYNC_ENDPOINT_UPLOAD "upload"

src/vtab.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define __CLOUDSYNC_VTAB__
1010

1111
#include "cloudsync.h"
12+
#include "cloudsync_private.h"
1213

1314
int cloudsync_vtab_register_changes (sqlite3 *db, cloudsync_context *xdata);
1415
cloudsync_context *cloudsync_vtab_get_context (sqlite3_vtab *vtab);

0 commit comments

Comments
 (0)