14
14
15
15
namespace DotnetSpider . Mongo
16
16
{
17
- public class MongoOptions
18
- {
19
- private readonly IConfiguration _configuration ;
20
-
21
- public MongoOptions ( IConfiguration configuration )
22
- {
23
- _configuration = configuration ;
24
- }
25
-
26
- public string ConnectionString => _configuration [ "Mongo:ConnectionString" ] ;
27
- }
28
-
29
17
/// <summary>
30
18
/// MongoDB 保存解析(实体)结果 TODO: 是否要考虑存储模式:插入,新的插入旧的更新,更新 ETC
31
19
/// </summary>
32
20
public class MongoEntityStorage : EntityStorageBase
33
21
{
34
22
private readonly IMongoClient _client ;
35
23
36
- private readonly ConcurrentDictionary < Type , TableMetadata > _tableMetadatas =
37
- new ConcurrentDictionary < Type , TableMetadata > ( ) ;
24
+ private readonly ConcurrentDictionary < Type , TableMetadata > _tableMetadataDict =
25
+ new ( ) ;
38
26
39
27
private readonly ConcurrentDictionary < string , IMongoDatabase > _cache =
40
- new ConcurrentDictionary < string , IMongoDatabase > ( ) ;
28
+ new ( ) ;
41
29
42
30
public static IDataFlow CreateFromOptions ( IConfiguration configuration )
43
31
{
44
32
var options = new MongoOptions ( configuration ) ;
45
33
return new MongoEntityStorage ( options . ConnectionString ) ;
46
34
}
47
35
36
+ public string ConnectionString { get ; }
37
+
48
38
/// <summary>
49
39
/// 构造方法
50
40
/// </summary>
51
41
/// <param name="connectionString">连接字符串</param>
52
42
public MongoEntityStorage ( string connectionString )
53
43
{
54
- ConnectionString = connectionString ;
55
44
_client = new MongoClient ( connectionString ) ;
45
+ ConnectionString = connectionString ;
56
46
}
57
47
58
48
internal MongoEntityStorage ( IMongoClient mongoClient )
59
49
{
60
50
_client = mongoClient ;
61
51
}
62
52
63
- /// <summary>
64
- /// 连接字符串
65
- /// </summary>
66
- public string ConnectionString { get ; }
67
-
68
- protected override async Task StoreAsync ( DataFlowContext context , Dictionary < Type , List < dynamic > > dict )
53
+ protected override async Task HandleAsync ( DataFlowContext context ,
54
+ IDictionary < Type , ICollection < dynamic > > entities )
69
55
{
70
- foreach ( var kv in dict )
56
+ foreach ( var kv in entities )
71
57
{
72
58
var list = ( IList ) kv . Value ;
73
- var tableMetadata = _tableMetadatas . GetOrAdd ( kv . Key ,
74
- type => ( ( IEntity ) list [ 0 ] ) . GetTableMetadata ( ) ) ;
59
+ var tableMetadata = _tableMetadataDict . GetOrAdd ( kv . Key ,
60
+ _ => ( ( IEntity ) list [ 0 ] ) . GetTableMetadata ( ) ) ;
75
61
76
62
if ( string . IsNullOrWhiteSpace ( tableMetadata . Schema . Database ) )
77
63
{
@@ -95,5 +81,10 @@ protected override async Task StoreAsync(DataFlowContext context, Dictionary<Typ
95
81
await collection . InsertManyAsync ( bsonDocs ) ;
96
82
}
97
83
}
84
+
85
+ public override string ToString ( )
86
+ {
87
+ return $ "{ ConnectionString } ";
88
+ }
98
89
}
99
90
}
0 commit comments