@@ -20,53 +20,89 @@ protected RemoteBase(DbConnection conn)
2020
2121 public interface IRemoteTableHandle
2222 {
23- void SetCache ( ClientCache . ITableCache cache ) ;
24-
23+ // These methods need to be overridden by autogen.
2524 object ? GetPrimaryKey ( IDatabaseRow row ) ;
26-
2725 void InternalInvokeValueInserted ( IDatabaseRow row ) ;
2826 void InternalInvokeValueDeleted ( IDatabaseRow row ) ;
29- void InvokeInsert ( IEventContext context , IDatabaseRow row ) ;
30- void InvokeDelete ( IEventContext context , IDatabaseRow row ) ;
31- void InvokeBeforeDelete ( IEventContext context , IDatabaseRow row ) ;
32- void InvokeUpdate ( IEventContext context , IDatabaseRow oldRow , IDatabaseRow newRow ) ;
27+
28+ // These are provided by RemoteTableHandle.
29+ internal Type ClientTableType { get ; }
30+ internal IEnumerable < KeyValuePair < byte [ ] , IDatabaseRow > > IterEntries ( ) ;
31+ internal bool InsertEntry ( byte [ ] rowBytes , IDatabaseRow value ) ;
32+ internal bool DeleteEntry ( byte [ ] rowBytes ) ;
33+ internal IDatabaseRow DecodeValue ( byte [ ] bytes ) ;
34+
35+ internal void InvokeInsert ( IEventContext context , IDatabaseRow row ) ;
36+ internal void InvokeDelete ( IEventContext context , IDatabaseRow row ) ;
37+ internal void InvokeBeforeDelete ( IEventContext context , IDatabaseRow row ) ;
38+ internal void InvokeUpdate ( IEventContext context , IDatabaseRow oldRow , IDatabaseRow newRow ) ;
3339 }
3440
3541 public abstract class RemoteTableHandle < EventContext , Row > : IRemoteTableHandle
3642 where EventContext : class , IEventContext
3743 where Row : IDatabaseRow , new ( )
3844 {
39- public void SetCache ( ClientCache . ITableCache cache ) => Cache = ( ClientCache . TableCache < Row > ) cache ;
45+ // These methods need to be overridden by autogen.
46+ public virtual object ? GetPrimaryKey ( IDatabaseRow row ) => null ;
47+ public virtual void InternalInvokeValueInserted ( IDatabaseRow row ) { }
48+ public virtual void InternalInvokeValueDeleted ( IDatabaseRow row ) { }
49+
50+ // These are provided by RemoteTableHandle.
51+ Type IRemoteTableHandle . ClientTableType => typeof ( Row ) ;
52+
53+ private readonly Dictionary < byte [ ] , Row > Entries = new ( Internal . ByteArrayComparer . Instance ) ;
54+
55+ IEnumerable < KeyValuePair < byte [ ] , IDatabaseRow > > IRemoteTableHandle . IterEntries ( ) =>
56+ Entries . Select ( kv => new KeyValuePair < byte [ ] , IDatabaseRow > ( kv . Key , kv . Value ) ) ;
57+
58+ /// <summary>
59+ /// Inserts the value into the table. There can be no existing value with the provided BSATN bytes.
60+ /// </summary>
61+ /// <param name="rowBytes">The BSATN encoded bytes of the row to retrieve.</param>
62+ /// <param name="value">The parsed row encoded by the <paramref>rowBytes</paramref>.</param>
63+ /// <returns>True if the row was inserted, false if the row wasn't inserted because it was a duplicate.</returns>
64+ bool IRemoteTableHandle . InsertEntry ( byte [ ] rowBytes , IDatabaseRow value ) => Entries . TryAdd ( rowBytes , ( Row ) value ) ;
4065
41- internal ClientCache . TableCache < Row > ? Cache ;
66+ /// <summary>
67+ /// Deletes a value from the table.
68+ /// </summary>
69+ /// <param name="rowBytes">The BSATN encoded bytes of the row to remove.</param>
70+ /// <returns>True if and only if the value was previously resident and has been deleted.</returns>
71+ bool IRemoteTableHandle . DeleteEntry ( byte [ ] rowBytes )
72+ {
73+ if ( Entries . Remove ( rowBytes ) )
74+ {
75+ return true ;
76+ }
77+
78+ Log . Warn ( "Deleting value that we don't have (no cached value available)" ) ;
79+ return false ;
80+ }
81+
82+ // The function to use for decoding a type value.
83+ IDatabaseRow IRemoteTableHandle . DecodeValue ( byte [ ] bytes ) => BSATNHelpers . Decode < Row > ( bytes ) ;
4284
4385 public event Action < EventContext , Row > ? OnInsert ;
4486 public event Action < EventContext , Row > ? OnDelete ;
4587 public event Action < EventContext , Row > ? OnBeforeDelete ;
4688 public event Action < EventContext , Row , Row > ? OnUpdate ;
4789
48- public virtual object ? GetPrimaryKey ( IDatabaseRow row ) => null ;
49-
50- public virtual void InternalInvokeValueInserted ( IDatabaseRow row ) { }
51-
52- public virtual void InternalInvokeValueDeleted ( IDatabaseRow row ) { }
53-
54- public int Count => Cache ! . Entries . Count ;
90+ public int Count => Entries . Count ;
5591
56- public IEnumerable < Row > Iter ( ) => Cache ! . Entries . Values ;
92+ public IEnumerable < Row > Iter ( ) => Entries . Values ;
5793
58- public IEnumerable < Row > Query ( Func < Row , bool > filter ) => Iter ( ) . Where ( filter ) ;
94+ protected IEnumerable < Row > Query ( Func < Row , bool > filter ) => Iter ( ) . Where ( filter ) ;
5995
60- public void InvokeInsert ( IEventContext context , IDatabaseRow row ) =>
96+ void IRemoteTableHandle . InvokeInsert ( IEventContext context , IDatabaseRow row ) =>
6197 OnInsert ? . Invoke ( ( EventContext ) context , ( Row ) row ) ;
6298
63- public void InvokeDelete ( IEventContext context , IDatabaseRow row ) =>
99+ void IRemoteTableHandle . InvokeDelete ( IEventContext context , IDatabaseRow row ) =>
64100 OnDelete ? . Invoke ( ( EventContext ) context , ( Row ) row ) ;
65101
66- public void InvokeBeforeDelete ( IEventContext context , IDatabaseRow row ) =>
102+ void IRemoteTableHandle . InvokeBeforeDelete ( IEventContext context , IDatabaseRow row ) =>
67103 OnBeforeDelete ? . Invoke ( ( EventContext ) context , ( Row ) row ) ;
68104
69- public void InvokeUpdate ( IEventContext context , IDatabaseRow oldRow , IDatabaseRow newRow ) =>
105+ void IRemoteTableHandle . InvokeUpdate ( IEventContext context , IDatabaseRow oldRow , IDatabaseRow newRow ) =>
70106 OnUpdate ? . Invoke ( ( EventContext ) context , ( Row ) oldRow , ( Row ) newRow ) ;
71107 }
72- }
108+ }
0 commit comments