Skip to content

Commit 3266703

Browse files
authored
Use long for opIds instead of string (#89)
1 parent e737420 commit 3266703

11 files changed

Lines changed: 23 additions & 25 deletions

File tree

PowerSync/PowerSync.Common/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
## 1.0.1
44

55
- Full release.
6+
- `PowerSyncDatabase.OnChange` now returns the underlying raw table name instead of the view name to mirror PowerSync JS.
7+
- Use `long` for op IDs instead of `string`. This affects the types returned by some methods used in `PowerSyncBackendConnector.UploadData`, namely `PowerSyncDatabase.GetNextCrudTransaction()` and `PowerSyncDatabase.GetCrudBatch()`.
68

79
## 1.0.0 (unlisted)
810

PowerSync/PowerSync.Common/Client/PowerSyncDatabase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ public async Task<UploadQueueStats> GetUploadQueueStats(bool includeSize = false
669669
});
670670
}
671671

672-
public Task HandleCrudCheckpoint(long lastClientId, string? writeCheckpoint = null)
672+
public Task HandleCrudCheckpoint(long lastClientId, long? writeCheckpoint = null)
673673
{
674674
return BucketStorageAdapter.HandleCrudCheckpoint(lastClientId, writeCheckpoint);
675675
}

PowerSync/PowerSync.Common/Client/Sync/Bucket/BucketStorageAdapter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ public interface IBucketStorageAdapter : ICloseable
135135
Task<bool> HasCrud();
136136
Task<CrudBatch?> GetCrudBatch(int limit = 100);
137137

138-
Task<bool> UpdateLocalTarget(Func<Task<string>> callback);
138+
Task<bool> UpdateLocalTarget(Func<Task<long>> callback);
139139

140-
Task HandleCrudCheckpoint(long lastClientId, string? writeCheckpoint = null);
140+
Task HandleCrudCheckpoint(long lastClientId, long? writeCheckpoint = null);
141141

142142
/// <summary>
143143
/// Get a unique client ID.

PowerSync/PowerSync.Common/Client/Sync/Bucket/SqliteBucketStorage.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace PowerSync.Common.Client.Sync.Bucket;
1616

1717
public class SqliteBucketStorage : IBucketStorageAdapter
1818
{
19-
public static readonly string MAX_OP_ID = "9223372036854775807";
19+
public const long MAX_OP_ID = 9223372036854775807;
2020

2121
public BucketStorageEvents Events { get; } = new();
2222

@@ -72,12 +72,10 @@ public async Task<string> GetClientId()
7272
/// Reads the stored target checkpoint request id, or updates it when the update parameter is set.
7373
/// </summary>
7474
/// <returns>The previous checkpoint request.</returns>
75-
private static Task<string?> TargetCheckpointRequestId(ILockContext tx, string? update = null)
75+
private static Task<long?> TargetCheckpointRequestId(ILockContext tx, long? update = null)
7676
{
77-
// TODO Note that we are only casting in Dart/JS because this returns a 64-bit integer we can't natively represent there.
78-
// Turning MAX_OP_ID into a 64-bit integer here and comparing ints would be better.
79-
return tx.Get<string?>(
80-
"SELECT CAST(powersync_control(?, ?) AS TEXT) AS r",
77+
return tx.Get<long?>(
78+
"SELECT powersync_control(?, ?) AS r",
8179
[PowerSyncControlCommand.TARGET_CHECKPOINT_REQUEST_ID, update]);
8280
}
8381

@@ -94,7 +92,7 @@ public class ResultDetail
9492

9593
private record SequenceResult(long seq);
9694

97-
public async Task<bool> UpdateLocalTarget(Func<Task<string>> callback)
95+
public async Task<bool> UpdateLocalTarget(Func<Task<long>> callback)
9896
{
9997
var seqBeforeResult = await db.ReadTransaction(async tx =>
10098
{
@@ -118,7 +116,7 @@ public async Task<bool> UpdateLocalTarget(Func<Task<string>> callback)
118116
return false;
119117
}
120118

121-
string opId = await callback();
119+
long opId = await callback();
122120

123121
logger.LogDebug("[updateLocalTarget] Updating target to checkpoint {message}", opId);
124122

@@ -154,7 +152,7 @@ public async Task<bool> UpdateLocalTarget(Func<Task<string>> callback)
154152
return true;
155153
});
156154
}
157-
public Task HandleCrudCheckpoint(long lastClientId, string? writeCheckpoint = null)
155+
public Task HandleCrudCheckpoint(long lastClientId, long? writeCheckpoint = null)
158156
{
159157
return db.WriteTransaction(async tx =>
160158
{
@@ -165,7 +163,7 @@ public Task HandleCrudCheckpoint(long lastClientId, string? writeCheckpoint = nu
165163

166164
await TargetCheckpointRequestId(
167165
tx,
168-
!string.IsNullOrEmpty(writeCheckpoint) && !crudRemaining ? writeCheckpoint : MAX_OP_ID);
166+
writeCheckpoint is not null && !crudRemaining ? writeCheckpoint : MAX_OP_ID);
169167
});
170168
}
171169

PowerSync/PowerSync.Common/Client/Sync/Stream/StreamingSyncImplementation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,13 +804,13 @@ public void Close()
804804
}
805805

806806
public record ResponseData(
807-
[property: JsonProperty("write_checkpoint")] string WriteCheckpoint
807+
[property: JsonProperty("write_checkpoint")] long WriteCheckpoint
808808
);
809809

810810
public record ApiResponse(
811811
[property: JsonProperty("data")] ResponseData Data
812812
);
813-
public async Task<string> GetWriteCheckpoint()
813+
public async Task<long> GetWriteCheckpoint()
814814
{
815815
var clientId = await Options.Adapter.GetClientId();
816816
var path = $"/write-checkpoint2.json?client_id={clientId}";

PowerSync/PowerSync.Common/Client/WatchManager.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,9 @@ public IAsyncEnumerable<WatchOnChangeEvent> OnChange(SQLWatchOptions? options)
7373
refreshOnSchemaChange: false
7474
);
7575

76-
// TODO: powersync-js onChange returns table names in `ps_data__{table}` format.
77-
// We should make a decision on whether or not to mirror that before v1.
7876
return Stream(subscription, changed => Task.FromResult(new WatchOnChangeEvent
7977
{
80-
ChangedTables = [.. changed.Select(InternalToFriendlyTableName)]
78+
ChangedTables = [.. changed]
8179
}));
8280
}
8381

PowerSync/PowerSync.Common/DB/Crud/CrudBatch.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ namespace PowerSync.Common.DB.Crud;
33
using System;
44
using System.Threading.Tasks;
55

6-
public class CrudBatch(CrudEntry[] Crud, bool HaveMore, Func<string?, Task> CompleteCallback)
6+
public class CrudBatch(CrudEntry[] Crud, bool HaveMore, Func<long?, Task> CompleteCallback)
77
{
88
public CrudEntry[] Crud { get; private set; } = Crud;
99

1010
public bool HaveMore { get; private set; } = HaveMore;
1111

12-
public async Task Complete(string? checkpoint = null)
12+
public async Task Complete(long? checkpoint = null)
1313
{
1414
await CompleteCallback(checkpoint);
1515
}

PowerSync/PowerSync.Common/DB/Crud/CrudTransaction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace PowerSync.Common.DB.Crud;
33
using System;
44
using System.Threading.Tasks;
55

6-
public class CrudTransaction(CrudEntry[] crud, Func<string?, Task> complete, long? transactionId = null) : CrudBatch(crud, false, complete)
6+
public class CrudTransaction(CrudEntry[] crud, Func<long?, Task> complete, long? transactionId = null) : CrudBatch(crud, false, complete)
77
{
88
public long? TransactionId { get; private set; } = transactionId;
99
}

Tests/PowerSync/PowerSync.Common.Tests/Client/Sync/StreamingSyncRetryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ SemaphoreSlim signal
9999
public override Task<T> Get<T>(string path, Dictionary<string, string>? headers = null)
100100
{
101101
var response = new StreamingSyncImplementation.ApiResponse(
102-
new StreamingSyncImplementation.ResponseData("1")
102+
new StreamingSyncImplementation.ResponseData(1)
103103
);
104104
return Task.FromResult((T)(object)response);
105105
}

Tests/PowerSync/PowerSync.Common.Tests/Client/Sync/SyncIterationControlFlowTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ public Task<string> Control(string op, object? payload)
255255
public Task<CrudEntry?> NextCrudItem() => Task.FromResult<CrudEntry?>(null);
256256
public Task<bool> HasCrud() => Task.FromResult(false);
257257
public Task<CrudBatch?> GetCrudBatch(int limit = 100) => Task.FromResult<CrudBatch?>(null);
258-
public Task<bool> UpdateLocalTarget(Func<Task<string>> callback) => Task.FromResult(false);
259-
public Task HandleCrudCheckpoint(long lastClientId, string? writeCheckpoint = null) => Task.CompletedTask;
258+
public Task<bool> UpdateLocalTarget(Func<Task<long>> callback) => Task.FromResult(false);
259+
public Task HandleCrudCheckpoint(long lastClientId, long? writeCheckpoint = null) => Task.CompletedTask;
260260
public Task<string> GetClientId() => Task.FromResult("test-client");
261261
public void Close() { }
262262
}

0 commit comments

Comments
 (0)