Skip to content

Commit 2563630

Browse files
committed
PostgreSqlEffectsStore uses position instead of guid for effects
1 parent 4adc76e commit 2563630

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

Core/Cleipnir.ResilientFunctions/Helpers/Helpers.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ public static long GenerateRandomLong()
2222
var randomLong = BitConverter.ToInt64(randomLongBytes, 0);
2323
return randomLong;
2424
}
25+
26+
public static long ToLong(this Guid guid)
27+
{
28+
var guidBytes = guid.ToByteArray();
29+
var longBytes = new byte[8];
30+
31+
for (var i = 0; i < 8; i++)
32+
longBytes[i] = (byte)(guidBytes[i] ^ guidBytes[i + 8]);
33+
34+
return BitConverter.ToInt64(longBytes, 0);
35+
}
2536

2637
public static int GenerateRandomInt()
2738
{

Stores/PostgreSQL/Cleipnir.ResilientFunctions.PostgreSQL/PostgreSqlEffectsStore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ public async Task Initialize()
2020
_initializeSql ??= @$"
2121
CREATE TABLE IF NOT EXISTS {tablePrefix}_effects (
2222
id UUID,
23-
id_hash UUID,
23+
position BIGINT,
2424
status INT NOT NULL,
2525
result BYTEA NULL,
2626
exception TEXT NULL,
2727
effect_id TEXT NOT NULL,
28-
PRIMARY KEY (id, id_hash)
28+
PRIMARY KEY (id, position)
2929
);";
3030
var command = new NpgsqlCommand(_initializeSql, conn);
3131
await command.ExecuteNonQueryAsync();

Stores/PostgreSQL/Cleipnir.ResilientFunctions.PostgreSQL/SqlGenerator.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ ELSE expires
4040
public StoreCommand GetEffects(StoredId storedId)
4141
{
4242
_getEffectResultsSql ??= @$"
43-
SELECT id_hash, status, result, exception, effect_id
43+
SELECT position, status, result, exception, effect_id
4444
FROM {tablePrefix}_effects
4545
WHERE id = $1;";
4646

@@ -52,7 +52,7 @@ public StoreCommand GetEffects(StoredId storedId)
5252
public StoreCommand GetEffects(IEnumerable<StoredId> storedIds)
5353
{
5454
var sql = @$"
55-
SELECT id, id_hash, status, result, exception, effect_id
55+
SELECT id, position, status, result, exception, effect_id
5656
FROM {tablePrefix}_effects
5757
WHERE id IN ({storedIds.Select(id => $"'{id}'").StringJoin(", ")});";
5858

@@ -64,7 +64,7 @@ public async Task<IReadOnlyList<StoredEffect>> ReadEffects(NpgsqlDataReader read
6464
var functions = new List<StoredEffect>();
6565
while (await reader.ReadAsync())
6666
{
67-
var idHash = reader.GetGuid(0);
67+
var position = reader.GetInt64(0);
6868
var status = (WorkStatus) reader.GetInt32(1);
6969
var result = reader.IsDBNull(2) ? null : (byte[]) reader.GetValue(2);
7070
var exception = reader.IsDBNull(3) ? null : reader.GetString(3);
@@ -85,7 +85,7 @@ public async Task<Dictionary<StoredId, List<StoredEffect>>> ReadEffectsForIds(Np
8585
while (await reader.ReadAsync())
8686
{
8787
var id = new StoredId(reader.GetGuid(0));
88-
var idHash = reader.GetGuid(1);
88+
var position = reader.GetInt64(1);
8989
var status = (WorkStatus) reader.GetInt32(2);
9090
var result = reader.IsDBNull(3) ? null : (byte[]) reader.GetValue(3);
9191
var exception = reader.IsDBNull(4) ? null : reader.GetString(4);
@@ -106,15 +106,15 @@ public IEnumerable<StoreCommand> UpdateEffects(IReadOnlyList<StoredEffectChange>
106106
{
107107
var sql= $@"
108108
INSERT INTO {tablePrefix}_effects
109-
(id, id_hash, status, result, exception, effect_id)
109+
(id, position, status, result, exception, effect_id)
110110
VALUES
111111
($1, $2, $3, $4, $5, $6);";
112112

113113
foreach (var (storedId, _, _, storedEffect) in changes.Where(s => s.Operation == CrudOperation.Insert))
114114
{
115115
var command = StoreCommand.Create(sql);
116116
command.AddParameter(storedId.AsGuid);
117-
command.AddParameter(storedEffect!.StoredEffectId.Value);
117+
command.AddParameter(storedEffect!.StoredEffectId.Value.ToLong());
118118
command.AddParameter((int) storedEffect.WorkStatus);
119119
command.AddParameter(storedEffect.Result ?? (object) DBNull.Value);
120120
command.AddParameter(JsonHelper.ToJson(storedEffect.StoredException) ?? (object) DBNull.Value);
@@ -129,7 +129,7 @@ INSERT INTO {tablePrefix}_effects
129129
var sql= $@"
130130
UPDATE {tablePrefix}_effects
131131
SET status = $1, result = $2, exception = $3
132-
WHERE id = $4 AND id_hash = $5;";
132+
WHERE id = $4 AND position = $5;";
133133

134134
foreach (var (storedId, _, _, storedEffect) in changes.Where(s => s.Operation == CrudOperation.Update))
135135
{
@@ -138,7 +138,7 @@ INSERT INTO {tablePrefix}_effects
138138
command.AddParameter(storedEffect.Result ?? (object) DBNull.Value);
139139
command.AddParameter(JsonHelper.ToJson(storedEffect.StoredException) ?? (object) DBNull.Value);
140140
command.AddParameter(storedId.AsGuid);
141-
command.AddParameter(storedEffect.StoredEffectId.Value);
141+
command.AddParameter(storedEffect.StoredEffectId.Value.ToLong());
142142

143143
commands.Add(command);
144144
}
@@ -148,15 +148,15 @@ INSERT INTO {tablePrefix}_effects
148148
var removedEffects = changes
149149
.Where(s => s.Operation == CrudOperation.Delete)
150150
.Select(s => new { Id = s.StoredId, s.EffectId })
151-
.GroupBy(s => s.Id, s => s.EffectId.ToStoredEffectId().Value);
151+
.GroupBy(s => s.Id, s => s.EffectId.ToStoredEffectId().Value.ToLong());
152152

153153
foreach (var removedEffectGroup in removedEffects)
154154
{
155155
var storedId = removedEffectGroup.Key;
156156
var removeSql = @$"
157157
DELETE FROM {tablePrefix}_effects
158158
WHERE id = '{storedId.AsGuid}' AND
159-
id_hash IN ({removedEffectGroup.Select(id => $"'{id}'").StringJoin(", ")});";
159+
position IN ({removedEffectGroup.Select(id => $"'{id}'").StringJoin(", ")});";
160160
var command = StoreCommand.Create(removeSql);
161161
commands.Add(command);
162162
}

0 commit comments

Comments
 (0)