Skip to content

Commit 6011a0f

Browse files
committed
Minor fixes
1 parent 09585a2 commit 6011a0f

File tree

4 files changed

+98
-81
lines changed

4 files changed

+98
-81
lines changed

src/cloudsync.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ void *cloudsync_siteid (cloudsync_context *data) {
423423
}
424424

425425
void cloudsync_reset_siteid (cloudsync_context *data) {
426-
data->site_id[0] = 0;
426+
memset(data->site_id, 0, sizeof(uint8_t) * UUID_LEN);
427427
}
428428

429429
int cloudsync_load_siteid (cloudsync_context *data) {
@@ -2367,7 +2367,7 @@ int cloudsync_table_sanity_check (cloudsync_context *data, const char *name, boo
23672367
return cloudsync_set_error(data, "cloudsync_init requires a non-null table parameter", DBRES_ERROR);
23682368
}
23692369

2370-
// avoid allocating heap memory for SQL statements by setting a maximum length of 1900 characters
2370+
// avoid allocating heap memory for SQL statements by setting a maximum length of 512 characters
23712371
// for table names. This limit is reasonable and helps prevent memory management issues.
23722372
const size_t maxlen = CLOUDSYNC_MAX_TABLENAME_LEN;
23732373
if (strlen(name) > maxlen) {
@@ -2402,7 +2402,7 @@ int cloudsync_table_sanity_check (cloudsync_context *data, const char *name, boo
24022402
int npri_keys_int = database_count_int_pk(data, name);
24032403
if (npri_keys_int < 0) return cloudsync_set_dberror(data);
24042404
if (npri_keys == npri_keys_int) {
2405-
snprintf(buffer, sizeof(buffer), "Table %s uses an single-column INTEGER primary key. For CRDT replication, primary keys must be globally unique. Consider using a TEXT primary key with UUIDs or ULID to avoid conflicts across nodes. If you understand the risk and still want to use this INTEGER primary key, set the third argument of the cloudsync_init function to 1 to skip this check.", name);
2405+
snprintf(buffer, sizeof(buffer), "Table %s uses a single-column INTEGER primary key. For CRDT replication, primary keys must be globally unique. Consider using a TEXT primary key with UUIDs or ULID to avoid conflicts across nodes. If you understand the risk and still want to use this INTEGER primary key, set the third argument of the cloudsync_init function to 1 to skip this check.", name);
24062406
return cloudsync_set_error(data, buffer, DBRES_ERROR);
24072407
}
24082408

@@ -2462,8 +2462,7 @@ int cloudsync_cleanup (cloudsync_context *data, const char *table_name) {
24622462
cloudsync_table_context *table = table_lookup(data, table_name);
24632463
if (!table) return DBRES_OK;
24642464

2465-
// TODO: check what happen if cloudsync_cleanup_internal failes (not eveything dropped)
2466-
// and the table is still in memory?
2465+
// TODO: check what happen if cloudsync_cleanup_internal failes (not eveything dropped) and the table is still in memory?
24672466

24682467
int rc = cloudsync_cleanup_internal(data, table);
24692468
if (rc != DBRES_OK) return rc;

src/postgresql/cloudsync_postgresql.c

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@
3232

3333
PG_MODULE_MAGIC;
3434

35-
// ============================================================================
36-
// Function Declarations
37-
// ============================================================================
38-
39-
// Extension entry points
40-
void _PG_init(void);
41-
void _PG_fini(void);
42-
4335
// Note: PG_FUNCTION_INFO_V1 macros are declared before each function implementation below
4436
// They should NOT be duplicated here to avoid redefinition errors
4537

@@ -67,7 +59,7 @@ static cloudsync_context *get_cloudsync_context(void) {
6759

6860
// MARK: - Extension Entry Points -
6961

70-
void _PG_init(void) {
62+
void _PG_init (void) {
7163
// Extension initialization
7264
// SPI will be connected per-function call
7365
elog(DEBUG1, "CloudSync extension loading");
@@ -103,7 +95,7 @@ void _PG_init(void) {
10395
PG_END_TRY();
10496
}
10597

106-
void _PG_fini(void) {
98+
void _PG_fini (void) {
10799
// Extension cleanup
108100
elog(DEBUG1, "CloudSync extension unloading");
109101

@@ -194,7 +186,7 @@ Datum cloudsync_db_version (PG_FUNCTION_ARGS) {
194186

195187
// cloudsync_db_version_next([merging_version]) - Get next database version
196188
PG_FUNCTION_INFO_V1(cloudsync_db_version_next);
197-
Datum cloudsync_db_version_next(PG_FUNCTION_ARGS) {
189+
Datum cloudsync_db_version_next (PG_FUNCTION_ARGS) {
198190
cloudsync_context *data = get_cloudsync_context();
199191

200192
int64_t merging_version = CLOUDSYNC_VALUE_NOTSET;
@@ -227,7 +219,7 @@ Datum cloudsync_db_version_next(PG_FUNCTION_ARGS) {
227219

228220
// Internal helper for cloudsync_init - replicates dbsync_init logic from SQLite
229221
// Returns site_id as text on success, raises error on failure
230-
static text *cloudsync_init_internal(cloudsync_context *data, const char *table, const char *algo, bool skip_int_pk_check) {
222+
static text *cloudsync_init_internal (cloudsync_context *data, const char *table, const char *algo, bool skip_int_pk_check) {
231223
text *result = NULL;
232224

233225
// Connect SPI for database operations
@@ -336,7 +328,7 @@ Datum cloudsync_enable (PG_FUNCTION_ARGS) {
336328

337329
// cloudsync_disable - Disable sync for a table
338330
PG_FUNCTION_INFO_V1(cloudsync_disable);
339-
Datum cloudsync_disable(PG_FUNCTION_ARGS) {
331+
Datum cloudsync_disable (PG_FUNCTION_ARGS) {
340332
if (PG_ARGISNULL(0)) {
341333
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("table_name cannot be NULL")));
342334
}
@@ -348,7 +340,7 @@ Datum cloudsync_disable(PG_FUNCTION_ARGS) {
348340

349341
// cloudsync_is_enabled - Check if table is sync-enabled
350342
PG_FUNCTION_INFO_V1(cloudsync_is_enabled);
351-
Datum cloudsync_is_enabled(PG_FUNCTION_ARGS) {
343+
Datum cloudsync_is_enabled (PG_FUNCTION_ARGS) {
352344
if (PG_ARGISNULL(0)) {
353345
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("table_name cannot be NULL")));
354346
}
@@ -365,7 +357,7 @@ Datum cloudsync_is_enabled(PG_FUNCTION_ARGS) {
365357

366358
// cloudsync_cleanup - Cleanup orphaned metadata for a table
367359
PG_FUNCTION_INFO_V1(pg_cloudsync_cleanup);
368-
Datum pg_cloudsync_cleanup(PG_FUNCTION_ARGS) {
360+
Datum pg_cloudsync_cleanup (PG_FUNCTION_ARGS) {
369361
if (PG_ARGISNULL(0)) {
370362
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("table_name cannot be NULL")));
371363
}
@@ -399,7 +391,7 @@ Datum pg_cloudsync_cleanup(PG_FUNCTION_ARGS) {
399391

400392
// cloudsync_terminate - Terminate CloudSync
401393
PG_FUNCTION_INFO_V1(pg_cloudsync_terminate);
402-
Datum pg_cloudsync_terminate(PG_FUNCTION_ARGS) {
394+
Datum pg_cloudsync_terminate (PG_FUNCTION_ARGS) {
403395
UNUSED_PARAMETER(fcinfo);
404396

405397
cloudsync_context *data = get_cloudsync_context();
@@ -427,7 +419,7 @@ Datum pg_cloudsync_terminate(PG_FUNCTION_ARGS) {
427419

428420
// cloudsync_set - Set global configuration
429421
PG_FUNCTION_INFO_V1(cloudsync_set);
430-
Datum cloudsync_set(PG_FUNCTION_ARGS) {
422+
Datum cloudsync_set (PG_FUNCTION_ARGS) {
431423
const char *key = NULL;
432424
const char *value = NULL;
433425

@@ -468,7 +460,7 @@ Datum cloudsync_set(PG_FUNCTION_ARGS) {
468460

469461
// cloudsync_set_table - Set table-level configuration
470462
PG_FUNCTION_INFO_V1(cloudsync_set_table);
471-
Datum cloudsync_set_table(PG_FUNCTION_ARGS) {
463+
Datum cloudsync_set_table (PG_FUNCTION_ARGS) {
472464
const char *tbl = NULL;
473465
const char *key = NULL;
474466
const char *value = NULL;
@@ -506,7 +498,7 @@ Datum cloudsync_set_table(PG_FUNCTION_ARGS) {
506498

507499
// cloudsync_set_column - Set column-level configuration
508500
PG_FUNCTION_INFO_V1(cloudsync_set_column);
509-
Datum cloudsync_set_column(PG_FUNCTION_ARGS) {
501+
Datum cloudsync_set_column (PG_FUNCTION_ARGS) {
510502
const char *tbl = NULL;
511503
const char *col = NULL;
512504
const char *key = NULL;
@@ -552,7 +544,7 @@ Datum cloudsync_set_column(PG_FUNCTION_ARGS) {
552544

553545
// cloudsync_begin_alter - Begin schema alteration
554546
PG_FUNCTION_INFO_V1(pg_cloudsync_begin_alter);
555-
Datum pg_cloudsync_begin_alter(PG_FUNCTION_ARGS) {
547+
Datum pg_cloudsync_begin_alter (PG_FUNCTION_ARGS) {
556548
if (PG_ARGISNULL(0)) {
557549
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("table_name cannot be NULL")));
558550
}
@@ -588,7 +580,7 @@ Datum pg_cloudsync_begin_alter(PG_FUNCTION_ARGS) {
588580

589581
// cloudsync_commit_alter - Commit schema alteration
590582
PG_FUNCTION_INFO_V1(pg_cloudsync_commit_alter);
591-
Datum pg_cloudsync_commit_alter(PG_FUNCTION_ARGS) {
583+
Datum pg_cloudsync_commit_alter (PG_FUNCTION_ARGS) {
592584
if (PG_ARGISNULL(0)) {
593585
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("table_name cannot be NULL")));
594586
}
@@ -624,7 +616,7 @@ Datum pg_cloudsync_commit_alter(PG_FUNCTION_ARGS) {
624616

625617
// Aggregate function: cloudsync_payload_encode transition function
626618
PG_FUNCTION_INFO_V1(cloudsync_payload_encode_transfn);
627-
Datum cloudsync_payload_encode_transfn(PG_FUNCTION_ARGS) {
619+
Datum cloudsync_payload_encode_transfn (PG_FUNCTION_ARGS) {
628620
MemoryContext aggContext;
629621
cloudsync_payload_context *payload = NULL;
630622

@@ -665,7 +657,7 @@ Datum cloudsync_payload_encode_transfn(PG_FUNCTION_ARGS) {
665657

666658
// Aggregate function: cloudsync_payload_encode finalize function
667659
PG_FUNCTION_INFO_V1(cloudsync_payload_encode_finalfn);
668-
Datum cloudsync_payload_encode_finalfn(PG_FUNCTION_ARGS) {
660+
Datum cloudsync_payload_encode_finalfn (PG_FUNCTION_ARGS) {
669661
if (PG_ARGISNULL(0)) {
670662
PG_RETURN_NULL();
671663
}
@@ -696,7 +688,7 @@ Datum cloudsync_payload_encode_finalfn(PG_FUNCTION_ARGS) {
696688

697689
// Payload decode - Apply changes from payload
698690
PG_FUNCTION_INFO_V1(cloudsync_payload_decode);
699-
Datum cloudsync_payload_decode(PG_FUNCTION_ARGS) {
691+
Datum cloudsync_payload_decode (PG_FUNCTION_ARGS) {
700692
if (PG_ARGISNULL(0)) {
701693
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("payload cannot be NULL")));
702694
}
@@ -741,15 +733,15 @@ Datum cloudsync_payload_decode(PG_FUNCTION_ARGS) {
741733

742734
// Alias for payload_decode
743735
PG_FUNCTION_INFO_V1(pg_cloudsync_payload_apply);
744-
Datum pg_cloudsync_payload_apply(PG_FUNCTION_ARGS) {
736+
Datum pg_cloudsync_payload_apply (PG_FUNCTION_ARGS) {
745737
return cloudsync_payload_decode(fcinfo);
746738
}
747739

748740
// MARK: - Private/Internal Functions -
749741

750742
// cloudsync_is_sync - Check if table has sync metadata
751743
PG_FUNCTION_INFO_V1(cloudsync_is_sync);
752-
Datum cloudsync_is_sync(PG_FUNCTION_ARGS) {
744+
Datum cloudsync_is_sync (PG_FUNCTION_ARGS) {
753745
cloudsync_context *data = get_cloudsync_context();
754746

755747
if (cloudsync_insync(data)) {
@@ -769,7 +761,7 @@ Datum cloudsync_is_sync(PG_FUNCTION_ARGS) {
769761

770762
// cloudsync_seq - Get sequence number
771763
PG_FUNCTION_INFO_V1(cloudsync_seq);
772-
Datum cloudsync_seq(PG_FUNCTION_ARGS) {
764+
Datum cloudsync_seq (PG_FUNCTION_ARGS) {
773765
UNUSED_PARAMETER(fcinfo);
774766

775767
cloudsync_context *data = get_cloudsync_context();
@@ -780,7 +772,7 @@ Datum cloudsync_seq(PG_FUNCTION_ARGS) {
780772

781773
// cloudsync_pk_encode - Encode primary key from variadic arguments
782774
PG_FUNCTION_INFO_V1(cloudsync_pk_encode);
783-
Datum cloudsync_pk_encode(PG_FUNCTION_ARGS) {
775+
Datum cloudsync_pk_encode (PG_FUNCTION_ARGS) {
784776
int argc = 0;
785777
pgvalue_t **argv = NULL;
786778

@@ -809,7 +801,7 @@ Datum cloudsync_pk_encode(PG_FUNCTION_ARGS) {
809801

810802
// cloudsync_pk_decode - Decode primary key component at given index
811803
PG_FUNCTION_INFO_V1(cloudsync_pk_decode);
812-
Datum cloudsync_pk_decode(PG_FUNCTION_ARGS) {
804+
Datum cloudsync_pk_decode (PG_FUNCTION_ARGS) {
813805
// TODO: Implement pk_decode with callback pattern
814806
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("cloudsync_pk_decode not yet implemented - requires callback implementation")));
815807
PG_RETURN_NULL();
@@ -818,7 +810,7 @@ Datum cloudsync_pk_decode(PG_FUNCTION_ARGS) {
818810
// cloudsync_insert - Internal insert handler
819811
// Signature: cloudsync_insert(table_name text, VARIADIC pk_values anyarray)
820812
PG_FUNCTION_INFO_V1(cloudsync_insert);
821-
Datum cloudsync_insert(PG_FUNCTION_ARGS) {
813+
Datum cloudsync_insert (PG_FUNCTION_ARGS) {
822814
if (PG_ARGISNULL(0)) {
823815
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("table_name cannot be NULL")));
824816
}
@@ -934,29 +926,28 @@ Datum cloudsync_insert(PG_FUNCTION_ARGS) {
934926

935927
// Aggregate function: cloudsync_update (not implemented - complex)
936928
PG_FUNCTION_INFO_V1(cloudsync_update);
937-
Datum cloudsync_update(PG_FUNCTION_ARGS) {
929+
Datum cloudsync_update (PG_FUNCTION_ARGS) {
938930
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("cloudsync_update not yet implemented - aggregate function")));
939931
PG_RETURN_NULL();
940932
}
941933

942934
PG_FUNCTION_INFO_V1(cloudsync_update_transfn);
943-
Datum cloudsync_update_transfn(PG_FUNCTION_ARGS) {
935+
Datum cloudsync_update_transfn (PG_FUNCTION_ARGS) {
944936
// TODO: Implement update aggregate transition function
945937
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("cloudsync_update_transfn not yet implemented")));
946938
PG_RETURN_NULL();
947939
}
948940

949941
PG_FUNCTION_INFO_V1(cloudsync_update_finalfn);
950-
Datum cloudsync_update_finalfn(PG_FUNCTION_ARGS) {
942+
Datum cloudsync_update_finalfn (PG_FUNCTION_ARGS) {
951943
// TODO: Implement update aggregate finalize function
952944
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("cloudsync_update_finalfn not yet implemented")));
953945
PG_RETURN_NULL();
954946
}
955947

956948
// Placeholder - not implemented yet
957949
PG_FUNCTION_INFO_V1(cloudsync_payload_encode);
958-
Datum
959-
cloudsync_payload_encode(PG_FUNCTION_ARGS) {
950+
Datum cloudsync_payload_encode (PG_FUNCTION_ARGS) {
960951
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("cloudsync_payload_encode should not be called directly - use aggregate version")));
961952
PG_RETURN_NULL();
962953
}

src/utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
#define DEBUG_FUNCTION(...)
6161
#endif
6262

63-
#if CLOUDSYNC_DEBUG_DBFUNCTION
63+
#if CLOUDSYNC_DEBUG_DBFUNCTIONS
6464
#define DEBUG_DBFUNCTION(...) DEBUG_PRINTLN(__VA_ARGS__)
6565
#else
6666
#define DEBUG_DBFUNCTION(...)

0 commit comments

Comments
 (0)