Repository navigation
Expand file tree
/
Copy pathWitDatabaseBuilderIndexedDbExtensions.cs
More file actions
192 lines (164 loc) · 7.37 KB
/
Copy pathWitDatabaseBuilderIndexedDbExtensions.cs
File metadata and controls
192 lines (164 loc) · 7.37 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using Microsoft.JSInterop;
using OutWit.Database.Core.Builder;
using OutWit.Database.Core.IndexedDb.Indexes;
using OutWit.Database.Core.Stores;
namespace OutWit.Database.Core.IndexedDb;
/// <summary>
/// Extension methods for configuring WitDatabaseBuilder with IndexedDB storage.
/// </summary>
public static class WitDatabaseBuilderIndexedDbExtensions
{
#region Storage Configuration
/// <summary>
/// Use IndexedDB for storage (Blazor WebAssembly).
/// </summary>
/// <param name="builder">The database builder.</param>
/// <param name="databaseName">Name of the IndexedDB database.</param>
/// <param name="jsRuntime">Blazor JS runtime (inject via @inject IJSRuntime).</param>
/// <returns>The builder for chaining.</returns>
/// <exception cref="InvalidOperationException">
/// Thrown if LSM-Tree engine is selected (not compatible with IndexedDB).
/// </exception>
/// <remarks>
/// IndexedDB storage is only compatible with B+Tree engine.
/// File locking is automatically disabled as it's not applicable in browser.
/// Secondary index factory is automatically configured to use IndexedDB.
///
/// For Blazor WASM, use <see cref="WitDatabaseBuilder.BuildAsync"/> for proper async initialization:
/// <code>
/// @inject IJSRuntime JSRuntime
///
/// var db = await new WitDatabaseBuilder()
/// .WithIndexedDbStorage("MyDatabase", JSRuntime)
/// .WithBTree()
/// .WithTransactions()
/// .BuildAsync(); // Use BuildAsync for WASM!
/// </code>
/// </remarks>
public static WitDatabaseBuilder WithIndexedDbStorage(
this WitDatabaseBuilder builder,
string databaseName,
IJSRuntime jsRuntime)
{
return builder.WithIndexedDbStorage(databaseName, jsRuntime, builder.Options.PageSize);
}
/// <summary>
/// Use IndexedDB for storage with custom page size.
/// </summary>
/// <param name="builder">The database builder.</param>
/// <param name="databaseName">Name of the IndexedDB database.</param>
/// <param name="jsRuntime">Blazor JS runtime.</param>
/// <param name="pageSize">Page size in bytes.</param>
/// <returns>The builder for chaining.</returns>
public static WitDatabaseBuilder WithIndexedDbStorage(
this WitDatabaseBuilder builder,
string databaseName,
IJSRuntime jsRuntime,
int pageSize)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(jsRuntime);
if (string.IsNullOrWhiteSpace(databaseName))
throw new ArgumentException("Database name cannot be empty", nameof(databaseName));
// Validate compatibility BEFORE setting options
ValidateIndexedDbCompatibility(builder.Options);
// Register validation event to catch late configuration changes
builder.OnValidating += ValidateIndexedDbOnBuild;
// Auto-disable incompatible features
builder.Options.TransactionParameters.Set("fileLocking", false); // Not applicable in browser
// Force BTree if no engine specified
if (!builder.Options.UseBTree && !builder.Options.UseLsmTree)
{
builder.Options.StoreProviderKey = StoreBTree.PROVIDER_KEY;
}
// Set storage
builder.Options.CustomStorage = new StorageIndexedDb(databaseName, jsRuntime, pageSize);
builder.Options.StoreParameters.Remove("useMemory");
builder.Options.StoreParameters.Remove("filePath");
builder.Options.StoreParameters.Remove("directory");
// Auto-configure secondary index factory for IndexedDB
// Use database name with "_indexes" suffix to avoid conflicts
var indexDbName = databaseName + "_indexes";
builder.Options.SecondaryIndexFactory = new SecondaryIndexFactoryIndexedDb(jsRuntime, indexDbName);
return builder;
}
#endregion
#region Index Configuration
/// <summary>
/// Explicitly configures IndexedDB-based secondary indexes.
/// </summary>
/// <param name="builder">The database builder.</param>
/// <param name="jsRuntime">Blazor JS runtime.</param>
/// <param name="indexDatabaseName">Name of the IndexedDB database for indexes.</param>
/// <returns>The builder for chaining.</returns>
/// <remarks>
/// This method is typically not needed when using WithIndexedDbStorage,
/// as it automatically configures the index factory. Use this method
/// when you need to customize the index database name or when using
/// a different primary storage with IndexedDB indexes.
/// </remarks>
public static WitDatabaseBuilder WithIndexedDbIndexes(
this WitDatabaseBuilder builder,
IJSRuntime jsRuntime,
string indexDatabaseName)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(jsRuntime);
if (string.IsNullOrWhiteSpace(indexDatabaseName))
throw new ArgumentException("Index database name cannot be empty", nameof(indexDatabaseName));
builder.Options.SecondaryIndexFactory = new SecondaryIndexFactoryIndexedDb(jsRuntime, indexDatabaseName);
return builder;
}
#endregion
#region Validation
/// <summary>
/// Validation handler for OnValidating event.
/// Catches configuration changes made after WithIndexedDbStorage was called.
/// </summary>
private static void ValidateIndexedDbOnBuild(WitDatabaseBuilderOptions options)
{
// Only validate if IndexedDB storage is configured
if (options.CustomStorage is not StorageIndexedDb)
return;
ValidateIndexedDbCompatibility(options);
}
/// <summary>
/// Validates that the current builder options are compatible with IndexedDB storage.
/// </summary>
/// <param name="options">Builder options to validate.</param>
/// <exception cref="InvalidOperationException">Thrown if configuration is incompatible.</exception>
private static void ValidateIndexedDbCompatibility(WitDatabaseBuilderOptions options)
{
var errors = new List<string>();
// LSM-Tree is not compatible
if (options.UseLsmTree)
{
errors.Add(
"IndexedDB storage is not compatible with LSM-Tree engine. " +
"LSM-Tree requires file system operations (multiple files, directory scanning) " +
"which are not available in browser environment. " +
"Use .WithBTree() instead of .WithLsmTree().");
}
// Check if LSM directory was specified
if (!string.IsNullOrEmpty(options.LsmDirectory))
{
errors.Add(
"IndexedDB storage cannot use LSM directory. " +
"Remove .WithLsmTree(directory) call.");
}
// External transaction journal not supported
if (options.CustomJournal != null || !string.IsNullOrEmpty(options.JournalProviderKey))
{
errors.Add(
"External transaction journal is not supported with IndexedDB storage. " +
"The built-in transaction handling will be used instead.");
}
if (errors.Count > 0)
{
throw new InvalidOperationException(
"IndexedDB configuration error:\n" +
string.Join("\n", errors.Select((e, i) => $" {i + 1}. {e}")));
}
}
#endregion
}