@@ -16,8 +16,8 @@ namespace Vertex.Storage.Linq2db.Storage
1616{
1717 public class EventStorageFactory : IEventStorageFactory
1818 {
19- private readonly ConcurrentDictionary < Type , EventStorageAttribute > typeAttributes = new ConcurrentDictionary < Type , EventStorageAttribute > ( ) ;
20- private readonly ConcurrentDictionary < string , Lazy < Task < object > > > eventStorageDict = new ConcurrentDictionary < string , Lazy < Task < object > > > ( ) ;
19+ private readonly ConcurrentDictionary < Type , EventStorageBaseAttribute > typeAttributes = new ( ) ;
20+ private readonly ConcurrentDictionary < string , Lazy < Task < object > > > eventStorageDict = new ( ) ;
2121 private readonly DbFactory dbFactory ;
2222 private readonly IGrainFactory grainFactory ;
2323 private readonly IServiceProvider serviceProvider ;
@@ -33,28 +33,31 @@ public async ValueTask<IEventStorage<TPrimaryKey>> Create<TPrimaryKey>(IActor<TP
3333 {
3434 var attribute = this . typeAttributes . GetOrAdd ( actor . GetType ( ) , key =>
3535 {
36- var attributes = key . GetCustomAttributes ( typeof ( EventStorageAttribute ) , false ) ;
37- if ( attributes . Length > 0 )
36+ var attributes = key . GetCustomAttributes ( false ) ;
37+ var attribute = attributes . SingleOrDefault ( att => typeof ( EventStorageBaseAttribute ) . IsAssignableFrom ( att . GetType ( ) ) ) ;
38+ if ( attribute != default )
3839 {
39- return attributes . First ( ) as EventStorageAttribute ;
40+ return attribute as EventStorageBaseAttribute ;
4041 }
4142 else
4243 {
43- throw new MissingAttributeException ( $ "{ nameof ( EventStorageAttribute ) } =>{ key . Name } ") ;
44+ throw new MissingAttributeException ( $ "{ nameof ( EventStorageBaseAttribute ) } =>{ key . Name } ") ;
4445 }
4546 } ) ;
46- var tableName = attribute . ShardingFunc ( actor . ActorId . ToString ( ) ) ;
47- var storage = await this . eventStorageDict . GetOrAdd ( $ "{ attribute . OptionName } _{ tableName } ", key =>
47+ var actorId = actor . ActorId . ToString ( ) ;
48+ var tableName = attribute . GetTableName ( actorId ) ;
49+ var optionName = attribute . GetOptionName ( actorId ) ;
50+ var storage = await this . eventStorageDict . GetOrAdd ( $ "{ optionName } _{ tableName } ", key =>
4851 new Lazy < Task < object > > ( async ( ) =>
4952 {
50- using var db = this . dbFactory . GetEventDb ( attribute . OptionName ) ;
53+ using var db = this . dbFactory . GetEventDb ( optionName ) ;
5154 await db . CreateTableIfNotExists < EventEntity < TPrimaryKey > > ( this . grainFactory , key , tableName , async ( ) =>
5255 {
5356 var indexGenerator = db . GetGenerator ( ) ;
5457 await indexGenerator . CreateUniqueIndexIfNotExists ( db , tableName , $ "{ tableName } _event_unique", nameof ( EventEntity < TPrimaryKey > . ActorId ) . ToLower ( ) , nameof ( EventEntity < TPrimaryKey > . Version ) . ToLower ( ) ) ;
5558 await indexGenerator . CreateUniqueIndexIfNotExists ( db , tableName , $ "{ tableName } _event_flow_unique", nameof ( EventEntity < TPrimaryKey > . ActorId ) . ToLower ( ) , nameof ( EventEntity < TPrimaryKey > . Name ) . ToLower ( ) , nameof ( EventEntity < TPrimaryKey > . FlowId ) . ToLower ( ) ) ;
5659 } ) ;
57- return new EventStorage < TPrimaryKey > ( this . serviceProvider , this . dbFactory , attribute . OptionName , tableName ) ;
60+ return new EventStorage < TPrimaryKey > ( this . serviceProvider , this . dbFactory , optionName , tableName ) ;
5861 } ) ) . Value ;
5962 return storage as IEventStorage < TPrimaryKey > ;
6063 }
0 commit comments