Skip to content

Commit

Permalink
NCBC-3235: Create Index Key Encoding
Browse files Browse the repository at this point in the history
Motivation
----------
Update query index fields to match RFC (name is now Index Keys, and ensure they're encoded/escaped.)

Changes
-------
*Escaped Index Keys and updated Unit Tests to match changes
*Re-named fields to Index Keys

Change-Id: If49768263161f46bad729c4c0d10854db01d9d01
Reviewed-on: https://review.couchbase.org/c/couchbase-net-client/+/191426
Tested-by: Build Bot <build@couchbase.com>
Reviewed-by: Jeffry Morris <jeffrymorris@gmail.com>
  • Loading branch information
emilienbev committed May 31, 2023
1 parent 17204cc commit 57b3400
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Couchbase/Management/Query/IQueryIndexManger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface IQueryIndexManager
{
Task<IEnumerable<QueryIndex>> GetAllIndexesAsync(string bucketName, GetAllQueryIndexOptions? options = null);

Task CreateIndexAsync(string bucketName, string indexName, IEnumerable<string> fields, CreateQueryIndexOptions? options = null);
Task CreateIndexAsync(string bucketName, string indexName, IEnumerable<string> indexKeys, CreateQueryIndexOptions? options = null);

Task CreatePrimaryIndexAsync(string bucketName, CreatePrimaryQueryIndexOptions? options = null);

Expand Down
9 changes: 6 additions & 3 deletions src/Couchbase/Management/Query/QueryGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
using Couchbase.Core.Utils;
using System.Collections.Generic;
using System.Linq;

namespace Couchbase.Management.Query
{
internal static class QueryGenerator
{
private const string Default = "_default";
public static string CreateIndexStatement(string bucketName, string indexName, IEnumerable<string> fields, CreateQueryIndexOptions options)
public static string CreateIndexStatement(string bucketName, string indexName, IEnumerable<string> indexKeys, CreateQueryIndexOptions options)
{
indexKeys = indexKeys.Select(x => x.EscapeIfRequired());

if(options.ScopeNameValue == null)
{
return $"CREATE INDEX {indexName.EscapeIfRequired()} ON {bucketName.EscapeIfRequired()}({string.Join(",", fields)}) USING GSI WITH {{\"defer_build\":{options.DeferredValue}}};";
return $"CREATE INDEX {indexName.EscapeIfRequired()} ON {bucketName.EscapeIfRequired()}({string.Join(",", indexKeys)}) USING GSI WITH {{\"defer_build\":{options.DeferredValue}}};";
}
return $"CREATE INDEX {indexName.EscapeIfRequired()} ON {bucketName.EscapeIfRequired()}.{options.ScopeNameValue.EscapeIfRequired()}.{options.CollectionNameValue.EscapeIfRequired()}({string.Join(",", fields)}) USING GSI WITH {{\"defer_build\":{options.DeferredValue}}};";
return $"CREATE INDEX {indexName.EscapeIfRequired()} ON {bucketName.EscapeIfRequired()}.{options.ScopeNameValue.EscapeIfRequired()}.{options.CollectionNameValue.EscapeIfRequired()}({string.Join(",", indexKeys)}) USING GSI WITH {{\"defer_build\":{options.DeferredValue}}};";
}

public static string CreateDeferredIndexStatement(string bucketName, string indexName, BuildDeferredQueryIndexOptions options)
Expand Down
10 changes: 5 additions & 5 deletions src/Couchbase/Management/Query/QueryIndexManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public async Task BuildDeferredIndexesAsync(string bucketName, BuildDeferredQuer
}
}

public async Task CreateIndexAsync(string bucketName, string indexName, IEnumerable<string> fields, CreateQueryIndexOptions? options = null)
public async Task CreateIndexAsync(string bucketName, string indexName, IEnumerable<string> indexKeys, CreateQueryIndexOptions? options = null)
{
options ??= CreateQueryIndexOptions.Default;
_logger.LogInformation("Attempting to create query index {indexName} on bucket {bucketName}",
Expand All @@ -79,15 +79,15 @@ public async Task CreateIndexAsync(string bucketName, string indexName, IEnumera
{
throw new ArgumentNullException(nameof(indexName));
}
if(fields == null)
if(indexKeys == null)
{
throw new ArgumentNullException(nameof(indexName));
throw new ArgumentNullException(nameof(indexKeys));
}

var enumerable = fields as string[] ?? fields.ToArray();
var enumerable = indexKeys as string[] ?? indexKeys.ToArray();
if(!enumerable.Any())
{
throw new ArgumentOutOfRangeException(nameof(fields));
throw new ArgumentOutOfRangeException(nameof(indexKeys));
}

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public QueryGeneratorTests(ITestOutputHelper outputHelper)
}

[Theory]
[InlineData("default", "index1", new[] {"field1", "field2"}, "_default", "_default", "CREATE INDEX `index1` ON `default`.`_default`.`_default`(field1,field2) USING GSI WITH {\"defer_build\":False};")]
[InlineData("default", "index1", new[] { "field1", "field2" }, null, null, "CREATE INDEX `index1` ON `default`(field1,field2) USING GSI WITH {\"defer_build\":False};")]
[InlineData("default", "index1", new[] {"field1", "field2"}, "_default", "_default", "CREATE INDEX `index1` ON `default`.`_default`.`_default`(`field1`,`field2`) USING GSI WITH {\"defer_build\":False};")]
[InlineData("default", "index1", new[] { "field1", "field2" }, null, null, "CREATE INDEX `index1` ON `default`(`field1`,`field2`) USING GSI WITH {\"defer_build\":False};")]
public void Test_CreateIndexStatement(string bucketName, string indexName, IEnumerable<string> fields, string scopeName, string collectionName, string expected)
{
//arrange
Expand Down

0 comments on commit 57b3400

Please sign in to comment.