Skip to content

Commit

Permalink
Some final 0.6.0 updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrinkmann-citadel committed Aug 1, 2023
1 parent 083ba3d commit c70676d
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 156 deletions.
16 changes: 16 additions & 0 deletions Scripts/ClientCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public Type ClientTableType
// TODO: Consider renaming this one, this kind of implies that its a callback for the Update operation
public Action<SpacetimeDBClient.TableOp, object, object, ClientApi.Event> RowUpdatedCallback;
public Func<AlgebraicType, AlgebraicValue, AlgebraicValue, bool> ComparePrimaryKeyFunc;
public Func<AlgebraicValue, AlgebraicValue> GetPrimaryKeyValueFunc;
public Func<AlgebraicType, AlgebraicType> GetPrimaryKeyTypeFunc;

public string Name
{
Expand Down Expand Up @@ -90,6 +92,10 @@ public TableCache(Type clientTableType, AlgebraicType rowSchema, Func<AlgebraicV
?.CreateDelegate(typeof(Action<SpacetimeDBClient.TableOp, object, object, ClientApi.Event>));
ComparePrimaryKeyFunc = (Func<AlgebraicType, AlgebraicValue, AlgebraicValue, bool>)clientTableType.GetMethod("ComparePrimaryKey", BindingFlags.Static | BindingFlags.Public)
?.CreateDelegate(typeof(Func<AlgebraicType, AlgebraicValue, AlgebraicValue, bool>));
GetPrimaryKeyValueFunc = (Func<AlgebraicValue, AlgebraicValue>)clientTableType.GetMethod("GetPrimaryKeyValue", BindingFlags.Static | BindingFlags.Public)
?.CreateDelegate(typeof(Func<AlgebraicValue, AlgebraicValue>));
GetPrimaryKeyTypeFunc = (Func<AlgebraicType, AlgebraicType>)clientTableType.GetMethod("GetPrimaryKeyType", BindingFlags.Static | BindingFlags.Public)
?.CreateDelegate(typeof(Func<AlgebraicType, AlgebraicType>));
entries = new Dictionary<byte[], (AlgebraicValue, object)>(new ByteArrayComparer());
decodedValues = new ConcurrentDictionary<byte[], (AlgebraicValue, object)>(new ByteArrayComparer());
}
Expand Down Expand Up @@ -212,6 +218,16 @@ public bool ComparePrimaryKey(AlgebraicValue v1, AlgebraicValue v2)
return (bool)ComparePrimaryKeyFunc.Invoke(rowSchema, v1, v2);
}

public AlgebraicValue GetPrimaryKeyValue(AlgebraicValue row)
{
return GetPrimaryKeyValueFunc != null ? GetPrimaryKeyValueFunc.Invoke(row) : null;
}

public AlgebraicType GetPrimaryKeyType(AlgebraicType row)
{
return GetPrimaryKeyTypeFunc != null ? GetPrimaryKeyTypeFunc.Invoke(row) : null;
}

public bool ComparePrimaryKey(byte[] rowPk1, byte[] rowPk2)
{
if (!decodedValues.TryGetValue(rowPk1, out var v1))
Expand Down
29 changes: 29 additions & 0 deletions Scripts/SATS/AlgebraicValue.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;

Expand Down Expand Up @@ -475,5 +476,33 @@ public void Serialize(AlgebraicType type, BinaryWriter writer)
throw new NotImplementedException();
}
}

public class AlgebraicValueComparer : IEqualityComparer<AlgebraicValue>
{
private AlgebraicType type;
public AlgebraicValueComparer(AlgebraicType type)
{
this.type = type;
}

public bool Equals(AlgebraicValue l, AlgebraicValue r)
{
return AlgebraicValue.Compare(type, l, r);
}

public int GetHashCode(AlgebraicValue value)
{
var stream = new MemoryStream();
var writer = new BinaryWriter(stream);
value.Serialize(type, writer);
var s = stream.ToArray();
if (s.Length >= 4)
{
return BitConverter.ToInt32(s, 0);
}
return s.Sum(b => b);
}
}

}
}
Loading

0 comments on commit c70676d

Please sign in to comment.