-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgresFileStorageConfiguration.cs
More file actions
112 lines (97 loc) · 3.85 KB
/
Copy pathPostgresFileStorageConfiguration.cs
File metadata and controls
112 lines (97 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.IO;
using Sa.Data.PostgreSql;
using Sa.HybridFileStorage.Domain;
using Sa.Partitional.PostgreSql;
namespace Sa.HybridFileStorage.Postgres;
internal sealed class PostgresFileStorageConfiguration : IPostgresFileStorageConfiguration
{
private readonly PostgresFileStorageOptions _options = new();
private readonly IPartConfiguration _partConfiguration;
private Action<IServiceProvider, PostgresFileStorageOptions>? _configure;
public PostgresFileStorageConfiguration(IServiceCollection services)
{
services.TryAddSingleton<RecyclableMemoryStreamManager>();
_partConfiguration = services.AddSaPartitional((sp, builder) =>
{
var dataSource = sp.GetRequiredService<IPgDataSource>();
_options.StorageOptions.SchemaName = dataSource.GetSearchPath();
_configure?.Invoke(sp, _options);
_options.StorageOptions.TableName = _options.StorageOptions.TableName.Trim('"');
builder.AddSchema(_options.StorageOptions.SchemaName, schema =>
{
schema.AddTable(_options.StorageOptions.TableName,
"id TEXT NOT NULL",
"name TEXT NOT NULL",
"size INT NOT NULL",
"file_ext TEXT NOT NULL",
"tenant_id INT NOT NULL",
"basket TEXT NOT NULL",
"data BYTEA NOT NULL"
)
.PartByList("tenant_id", "basket")
.PartByRange(_options.PartOptions.PgPartBy, "created_at");
});
})
// Schedule for creating new partitions
.AddPartMigrationSchedule((sp, opts) =>
{
opts.AsBackgroundJob = true;
opts.ForwardDays = _options.PartOptions.MigrationScheduleForwardDays;
})
// Schedule for removing old partitions
.AddPartCleanupSchedule((sp, opts) =>
{
opts.AsBackgroundJob = true;
opts.DropPartsAfterRetention = TimeSpan.FromDays(_options.CleanupOptions.ExpireDays);
});
services.AddSingleton<IFileStorage>(sp =>
{
var dataSource = sp.GetRequiredService<IPgDataSource>();
var pm = sp.GetRequiredService<IPartitionManager>();
var time = sp.GetService<TimeProvider>();
var sm = sp.GetRequiredService<RecyclableMemoryStreamManager>();
var storage = new PostgresFileStorage(
dataSource: dataSource,
partManager: pm,
streamManager: sm,
options: _options.StorageOptions,
basket: _options.PartOptions.Basket,
timeProvider: time);
return storage;
});
}
public IPostgresFileStorageConfiguration WithTableName(string tableName)
{
_options.StorageOptions.TableName = tableName;
return this;
}
public IPostgresFileStorageConfiguration WithSchemaName(string schemaName)
{
_options.StorageOptions.SchemaName = schemaName;
return this;
}
public IPostgresFileStorageConfiguration WithStorageType(string storageType)
{
_options.StorageOptions.StorageType = storageType;
return this;
}
public IPostgresFileStorageConfiguration AsReadOnly()
{
_options.StorageOptions.IsReadOnly = true;
return this;
}
public IPostgresFileStorageConfiguration ConfigureOptions(
Action<IServiceProvider, PostgresFileStorageOptions> configure)
{
_configure = configure;
return this;
}
public IPostgresFileStorageConfiguration AddDataSource(
Action<IPgDataSourceSettingsBuilder>? configure = null)
{
_partConfiguration.AddDataSource(configure);
return this;
}
}