Skip to content

Commit ff691b2

Browse files
#122 - Partitioned option
1 parent 797dfaf commit ff691b2

File tree

6 files changed

+69
-18
lines changed

6 files changed

+69
-18
lines changed

src/CouchDB.Driver/CouchClient.cs

+18-13
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ public ICouchDatabase<TSource> GetDatabase<TSource>(string database, string? dis
117117

118118
/// <inheritdoc />
119119
public async Task<ICouchDatabase<TSource>> CreateDatabaseAsync<TSource>(string database,
120-
int? shards = null, int? replicas = null, string? discriminator = null, CancellationToken cancellationToken = default)
120+
int? shards = null, int? replicas = null, bool? partitioned = null, string? discriminator = null, CancellationToken cancellationToken = default)
121121
where TSource : CouchDocument
122122
{
123123
QueryContext queryContext = NewQueryContext(database);
124-
IFlurlResponse response = await CreateDatabaseAsync(queryContext, shards, replicas, cancellationToken)
124+
IFlurlResponse response = await CreateDatabaseAsync(queryContext, shards, replicas, partitioned, cancellationToken)
125125
.ConfigureAwait(false);
126126

127127
if (response.IsSuccessful())
@@ -139,11 +139,11 @@ public async Task<ICouchDatabase<TSource>> CreateDatabaseAsync<TSource>(string d
139139

140140
/// <inheritdoc />
141141
public async Task<ICouchDatabase<TSource>> GetOrCreateDatabaseAsync<TSource>(string database,
142-
int? shards = null, int? replicas = null, string? discriminator = null, CancellationToken cancellationToken = default)
142+
int? shards = null, int? replicas = null, bool? partitioned = null, string? discriminator = null, CancellationToken cancellationToken = default)
143143
where TSource : CouchDocument
144144
{
145145
QueryContext queryContext = NewQueryContext(database);
146-
IFlurlResponse response = await CreateDatabaseAsync(queryContext, shards, replicas, cancellationToken)
146+
IFlurlResponse response = await CreateDatabaseAsync(queryContext, shards, replicas, partitioned, cancellationToken)
147147
.ConfigureAwait(false);
148148

149149
if (response.IsSuccessful() || response.StatusCode == (int)HttpStatusCode.PreconditionFailed)
@@ -173,21 +173,26 @@ public async Task DeleteDatabaseAsync(string database, CancellationToken cancell
173173
}
174174

175175
private Task<IFlurlResponse> CreateDatabaseAsync(QueryContext queryContext,
176-
int? shards = null, int? replicas = null, CancellationToken cancellationToken = default)
176+
int? shards = null, int? replicas = null, bool? partitioned = null, CancellationToken cancellationToken = default)
177177
{
178178
IFlurlRequest request = NewRequest()
179179
.AppendPathSegment(queryContext.EscapedDatabaseName);
180180

181-
if (shards.HasValue)
181+
if (shards.HasValue && shards.Value != 8)
182182
{
183183
request = request.SetQueryParam("q", shards.Value);
184184
}
185185

186-
if (replicas.HasValue)
186+
if (replicas.HasValue && replicas.Value != 3)
187187
{
188188
request = request.SetQueryParam("n", replicas.Value);
189189
}
190190

191+
if (partitioned.HasValue && partitioned.Value)
192+
{
193+
request = request.SetQueryParam("partitioned", "true");
194+
}
195+
191196
return request
192197
.AllowHttpStatus(HttpStatusCode.PreconditionFailed)
193198
.PutAsync(null, cancellationToken)
@@ -205,17 +210,17 @@ public ICouchDatabase<TSource> GetDatabase<TSource>() where TSource : CouchDocum
205210
}
206211

207212
/// <inheritdoc />
208-
public Task<ICouchDatabase<TSource>> CreateDatabaseAsync<TSource>(int? shards = null, int? replicas = null, string? discriminator = null,
213+
public Task<ICouchDatabase<TSource>> CreateDatabaseAsync<TSource>(int? shards = null, int? replicas = null, bool? partitioned = null, string? discriminator = null,
209214
CancellationToken cancellationToken = default) where TSource : CouchDocument
210215
{
211-
return CreateDatabaseAsync<TSource>(GetClassName<TSource>(), shards, replicas, discriminator, cancellationToken);
216+
return CreateDatabaseAsync<TSource>(GetClassName<TSource>(), shards, replicas, partitioned, discriminator, cancellationToken);
212217
}
213218

214219
/// <inheritdoc />
215-
public Task<ICouchDatabase<TSource>> GetOrCreateDatabaseAsync<TSource>(int? shards = null, int? replicas = null, string? discriminator = null,
220+
public Task<ICouchDatabase<TSource>> GetOrCreateDatabaseAsync<TSource>(int? shards = null, int? replicas = null, bool? partitioned = null, string? discriminator = null,
216221
CancellationToken cancellationToken = default) where TSource : CouchDocument
217222
{
218-
return GetOrCreateDatabaseAsync<TSource>(GetClassName<TSource>(), shards, replicas, discriminator, cancellationToken);
223+
return GetOrCreateDatabaseAsync<TSource>(GetClassName<TSource>(), shards, replicas, partitioned, discriminator, cancellationToken);
219224
}
220225

221226
/// <inheritdoc />
@@ -243,13 +248,13 @@ public ICouchDatabase<TUser> GetUsersDatabase<TUser>() where TUser : CouchUser
243248
/// <inheritdoc />
244249
public Task<ICouchDatabase<CouchUser>> GetOrCreateUsersDatabaseAsync(CancellationToken cancellationToken = default)
245250
{
246-
return GetOrCreateDatabaseAsync<CouchUser>(null, null, null, cancellationToken);
251+
return GetOrCreateDatabaseAsync<CouchUser>(null, null, null, null, cancellationToken);
247252
}
248253

249254
/// <inheritdoc />
250255
public Task<ICouchDatabase<TUser>> GetOrCreateUsersDatabaseAsync<TUser>(CancellationToken cancellationToken = default) where TUser : CouchUser
251256
{
252-
return GetOrCreateDatabaseAsync<TUser>(null, null, null, cancellationToken);
257+
return GetOrCreateDatabaseAsync<TUser>(null, null, null, null, cancellationToken);
253258
}
254259

255260
#endregion

src/CouchDB.Driver/CouchContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private async Task InitDatabaseAsync<TSource>(PropertyInfo propertyInfo, CouchOp
9999
var documentBuilder = (CouchDocumentBuilder<TSource>)databaseBuilder.DocumentBuilders[documentType];
100100
var databaseName = documentBuilder.Database ?? Client.GetClassName(documentType);
101101
database = options.CheckDatabaseExists
102-
? await Client.GetOrCreateDatabaseAsync<TSource>(databaseName, documentBuilder.Shards, documentBuilder.Replicas, documentBuilder.Discriminator).ConfigureAwait(false)
102+
? await Client.GetOrCreateDatabaseAsync<TSource>(databaseName, documentBuilder.Shards, documentBuilder.Replicas, documentBuilder.Partitioned, documentBuilder.Discriminator).ConfigureAwait(false)
103103
: Client.GetDatabase<TSource>(databaseName, documentBuilder.Discriminator);
104104
}
105105
else

src/CouchDB.Driver/ICouchClient.cs

+8-4
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ public interface ICouchClient: IAsyncDisposable
2626
/// <param name="database">The database name.</param>
2727
/// <param name="shards">The number of range partitions. Default is 8, unless overridden in the cluster config.</param>
2828
/// <param name="replicas">The number of copies of the database in the cluster. The default is 3, unless overridden in the cluster config.</param>
29+
/// <param name="partitioned">Whether to create a partitioned database. Default is <c>False</c>.</param>
2930
/// <param name="discriminator">Filters documents by the given discriminator.</param>
3031
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
3132
/// <returns>A task that represents the asynchronous operation. The task result contains the newly created CouchDB database.</returns>
3233
Task<ICouchDatabase<TSource>> CreateDatabaseAsync<TSource>(string database,
33-
int? shards = null, int? replicas = null, string? discriminator = null, CancellationToken cancellationToken = default)
34+
int? shards = null, int? replicas = null, bool? partitioned = null, string? discriminator = null, CancellationToken cancellationToken = default)
3435
where TSource : CouchDocument;
3536

3637
/// <summary>
@@ -42,11 +43,12 @@ Task<ICouchDatabase<TSource>> CreateDatabaseAsync<TSource>(string database,
4243
/// <param name="database">The database name.</param>
4344
/// <param name="shards">Used when creating. The number of range partitions. Default is 8, unless overridden in the cluster config.</param>
4445
/// <param name="replicas">Used when creating. The number of copies of the database in the cluster. The default is 3, unless overridden in the cluster config.</param>
46+
/// <param name="partitioned">Whether to create a partitioned database. Default is <c>False</c>.</param>
4547
/// <param name="discriminator">Filters documents by the given discriminator.</param>
4648
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
4749
/// <returns>A task that represents the asynchronous operation. The task result contains the newly created CouchDB database.</returns>
4850
Task<ICouchDatabase<TSource>> GetOrCreateDatabaseAsync<TSource>(string database,
49-
int? shards = null, int? replicas = null, string? discriminator = null, CancellationToken cancellationToken = default)
51+
int? shards = null, int? replicas = null, bool? partitioned = null, string? discriminator = null, CancellationToken cancellationToken = default)
5052
where TSource : CouchDocument;
5153

5254
/// <summary>
@@ -72,10 +74,11 @@ Task<ICouchDatabase<TSource>> GetOrCreateDatabaseAsync<TSource>(string database,
7274
/// <typeparam name="TSource">The type of database documents.</typeparam>
7375
/// <param name="shards">The number of range partitions. Default is 8, unless overridden in the cluster config.</param>
7476
/// <param name="replicas">The number of copies of the database in the cluster. The default is 3, unless overridden in the cluster config.</param>
77+
/// <param name="partitioned">Whether to create a partitioned database. Default is <c>False</c>.</param>
7578
/// <param name="discriminator">Filters documents by the given discriminator.</param>
7679
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
7780
/// <returns>A task that represents the asynchronous operation. The task result contains the newly created CouchDB database.</returns>
78-
Task<ICouchDatabase<TSource>> CreateDatabaseAsync<TSource>(int? shards = null, int? replicas = null, string? discriminator = null, CancellationToken cancellationToken = default) where TSource : CouchDocument;
81+
Task<ICouchDatabase<TSource>> CreateDatabaseAsync<TSource>(int? shards = null, int? replicas = null, bool? partitioned = null, string? discriminator = null, CancellationToken cancellationToken = default) where TSource : CouchDocument;
7982

8083
/// <summary>
8184
/// Returns an instance of the CouchDB database with the name type <see cref="TSource"/>.
@@ -84,10 +87,11 @@ Task<ICouchDatabase<TSource>> GetOrCreateDatabaseAsync<TSource>(string database,
8487
/// <typeparam name="TSource">The type of database documents.</typeparam>
8588
/// <param name="shards">Used when creating. The number of range partitions. Default is 8, unless overridden in the cluster config.</param>
8689
/// <param name="replicas">Used when creating. The number of copies of the database in the cluster. The default is 3, unless overridden in the cluster config.</param>
90+
/// <param name="partitioned">Whether to create a partitioned database. Default is <c>False</c>.</param>
8791
/// <param name="discriminator">Filters documents by the given discriminator.</param>
8892
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
8993
/// <returns>A task that represents the asynchronous operation. The task result contains the newly created CouchDB database.</returns>
90-
Task<ICouchDatabase<TSource>> GetOrCreateDatabaseAsync<TSource>(int? shards = null, int? replicas = null, string? discriminator = null, CancellationToken cancellationToken = default)
94+
Task<ICouchDatabase<TSource>> GetOrCreateDatabaseAsync<TSource>(int? shards = null, int? replicas = null, bool? partitioned = null, string? discriminator = null, CancellationToken cancellationToken = default)
9195
where TSource : CouchDocument;
9296

9397
/// <summary>

src/CouchDB.Driver/Options/CouchDocumentBuilder.cs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public abstract class CouchDocumentBuilder
55
internal string? Database { get; set; }
66
internal int? Shards { get; set; }
77
internal int? Replicas { get; set; }
8+
internal bool Partitioned { get; set; }
89
internal string? Discriminator { get; set; }
910
}
1011
}

src/CouchDB.Driver/Options/CouchDocumentBuilder`.cs

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public CouchDocumentBuilder<TSource> WithReplicas(int replicas)
3333
return this;
3434
}
3535

36+
public CouchDocumentBuilder<TSource> IsPartitioned()
37+
{
38+
Partitioned = true;
39+
return this;
40+
}
41+
3642
public CouchDocumentBuilder<TSource> HasIndex(string name, Action<IIndexBuilder<TSource>> indexBuilderAction,
3743
IndexOptions? options = null)
3844
{

tests/CouchDB.Driver.UnitTests/Client_Tests.cs

+35
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,41 @@ public async Task CreateDatabase_402_ThrowsException()
166166
await Assert.ThrowsAsync<CouchException>(action);
167167
}
168168

169+
[Fact]
170+
public async Task CreateDatabaseAsync_Params()
171+
{
172+
using var httpTest = new HttpTest();
173+
// Logout
174+
httpTest.RespondWithJson(new { ok = true });
175+
176+
await using var client = new CouchClient("http://localhost");
177+
httpTest.RespondWithJson(new { ok = true });
178+
var rebels = await client.CreateDatabaseAsync<Rebel>(9, 2, true);
179+
httpTest
180+
.ShouldHaveCalled("http://localhost/rebels*")
181+
.WithQueryParam("q", 9)
182+
.WithQueryParam("n", 2)
183+
.WithQueryParam("partitioned", "true")
184+
.WithVerb(HttpMethod.Put);
185+
Assert.Equal("rebels", rebels.Database);
186+
}
187+
188+
[Fact]
189+
public async Task CreateDatabaseAsync_Params_Default()
190+
{
191+
using var httpTest = new HttpTest();
192+
// Logout
193+
httpTest.RespondWithJson(new { ok = true });
194+
195+
await using var client = new CouchClient("http://localhost");
196+
httpTest.RespondWithJson(new { ok = true });
197+
var rebels = await client.CreateDatabaseAsync<Rebel>(8, 3, false);
198+
httpTest
199+
.ShouldHaveCalled("http://localhost/rebels")
200+
.WithVerb(HttpMethod.Put);
201+
Assert.Equal("rebels", rebels.Database);
202+
}
203+
169204
#endregion
170205

171206
#region Delete

0 commit comments

Comments
 (0)