Skip to content

Commit

Permalink
#102: Adds DeleteIndexAsync and GetIndexesAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
matteobortolazzo committed Sep 14, 2020
1 parent 65d8bc0 commit 5876419
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 6 deletions.
43 changes: 40 additions & 3 deletions src/CouchDB.Driver/CouchDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,23 @@ public async IAsyncEnumerable<ChangesFeedResponseResult<TSource>> GetContinuousC
#endregion

#region Index

/// <inheritdoc />
public async Task<List<IndexInfo>> GetIndexesAsync(CancellationToken cancellationToken = default)
{
GetIndexesResult response = await NewRequest()
.AppendPathSegment("_index")
.GetJsonAsync<GetIndexesResult>(cancellationToken)
.SendRequestAsync()
.ConfigureAwait(false);
return response.Indexes;
}

public async Task CreateIndexAsync(string name, Action<IIndexBuilder<TSource>> indexBuilderAction, IndexOptions? options = null)
/// <inheritdoc />
public async Task CreateIndexAsync(string name,
Action<IIndexBuilder<TSource>> indexBuilderAction,
IndexOptions? options = null,
CancellationToken cancellationToken = default)
{
Check.NotNull(name, nameof(name));
Check.NotNull(indexBuilderAction, nameof(indexBuilderAction));
Expand Down Expand Up @@ -445,12 +460,34 @@ public async Task CreateIndexAsync(string name, Action<IIndexBuilder<TSource>> i

var request = sb.ToString();

await NewRequest().AppendPathSegment("_index")
.PostStringAsync(request)
_ = await NewRequest()
.AppendPathSegment("_index")
.PostStringAsync(request, cancellationToken)
.SendRequestAsync()
.ConfigureAwait(false);
}

/// <inheritdoc />
public async Task DeleteIndexAsync(string designDocument, string name, CancellationToken cancellationToken = default)
{
Check.NotNull(designDocument, nameof(designDocument));
Check.NotNull(name, nameof(name));

_ = await NewRequest()
.AppendPathSegments("_index", designDocument, "json", name)
.DeleteAsync(cancellationToken)
.SendRequestAsync()
.ConfigureAwait(false);
}

/// <inheritdoc />
public Task DeleteIndexAsync(IndexInfo indexInfo, CancellationToken cancellationToken = default)
{
Check.NotNull(indexInfo, nameof(indexInfo));

return DeleteIndexAsync(indexInfo.DesignDocument, indexInfo.Name, cancellationToken);
}

#endregion

#region Utils
Expand Down
2 changes: 1 addition & 1 deletion src/CouchDB.Driver/DTOs/BulkGetResultItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ internal class BulkGetResultItem<TSource> where TSource : CouchDocument
public TSource Item { get; set; }
}
}
#nullable enable
#nullable restore
14 changes: 14 additions & 0 deletions src/CouchDB.Driver/DTOs/GetIndexesResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#nullable disable
using System.Collections.Generic;
using CouchDB.Driver.Types;
using Newtonsoft.Json;

namespace CouchDB.Driver.DTOs
{
internal class GetIndexesResult
{
[JsonProperty("indexes")]
public List<IndexInfo> Indexes { get; set; }
}
}
#nullable restore
28 changes: 27 additions & 1 deletion src/CouchDB.Driver/ICouchDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,41 @@ IAsyncEnumerable<ChangesFeedResponseResult<TSource>> GetContinuousChangesAsync(
ChangesFeedOptions options, ChangesFeedFilter filter,
CancellationToken cancellationToken);

/// <summary>
/// Gets the list of indexes in the database.
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>A Task that represents the asynchronous operation. The task result contains the list of indexes.</returns>
Task<List<IndexInfo>> GetIndexesAsync(CancellationToken cancellationToken = default);

/// <summary>
/// Creates an index for the current database with the given configuration.
/// </summary>
/// <param name="name">The name of the index.</param>
/// <param name="indexBuilderAction">The action to configure the index builder.</param>
/// <param name="options">The index options.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
Task CreateIndexAsync(string name, Action<IIndexBuilder<TSource>> indexBuilderAction,
IndexOptions? options = null);
IndexOptions? options = null,
CancellationToken cancellationToken = default);

/// <summary>
/// Deletes a specific index.
/// </summary>
/// <param name="designDocument">The design document name</param>
/// <param name="name">The index name</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
Task DeleteIndexAsync(string designDocument, string name, CancellationToken cancellationToken = default);

/// <summary>
/// Deletes a specific index.
/// </summary>
/// <param name="indexInfo">The index info.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
Task DeleteIndexAsync(IndexInfo indexInfo, CancellationToken cancellationToken = default);

/// <summary>
/// Asynchronously downloads a specific attachment.
Expand Down
1 change: 0 additions & 1 deletion src/CouchDB.Driver/Types/CouchDocumentInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#nullable disable
using System.Collections.Generic;
using System.Runtime.Serialization;
using Newtonsoft.Json;

Expand Down
24 changes: 24 additions & 0 deletions src/CouchDB.Driver/Types/IndexInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#nullable disable
using Newtonsoft.Json;

namespace CouchDB.Driver.Types
{
/// <summary>
/// Represent info about the index.
/// </summary>
public class IndexInfo
{
/// <summary>
/// ID of the design document the index belongs to.
/// </summary>
[JsonProperty("ddoc")]
public string DesignDocument { get; set; }

/// <summary>
/// The name of the index.
/// </summary>
[JsonProperty("name")]
public string Name { get; set; }
}
}
#nullable restore

0 comments on commit 5876419

Please sign in to comment.