Skip to content

Commit

Permalink
RavenDB-22729 : Prevent adding prefixes with different casing
Browse files Browse the repository at this point in the history
  • Loading branch information
aviv committed Aug 4, 2024
1 parent 7bf4782 commit 0694000
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/Raven.Server/ServerWide/ShardingStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,24 @@ private static void FillPrefixedSharding(ShardingConfiguration shardingConfigura
if (shardingConfiguration.Prefixed is not { Count: > 0 })
return;

shardingConfiguration.Prefixed = shardingConfiguration.Prefixed
.OrderByDescending(x => x.Prefix)
.ToList();
var allPrefixesIgnoreCase = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
List<PrefixedShardingSetting> ordered = new();

var start = ShardHelper.NumberOfBuckets;
foreach (var setting in shardingConfiguration.Prefixed)
var start = ShardHelper.NumberOfBuckets;
foreach (var setting in shardingConfiguration.Prefixed.OrderByDescending(x => x.Prefix))
{
if (allPrefixesIgnoreCase.Add(setting.Prefix) == false)
throw new InvalidOperationException(
$"Cannot add prefix '{setting.Prefix}' to {nameof(ShardingConfiguration)}.{nameof(ShardingConfiguration.Prefixed)}. " +
$"Prefix '{setting.Prefix}' already exists in different casing.");

ordered.Add(setting);

AddPrefixedBucketRange(setting, start, shardingConfiguration);
start += ShardHelper.NumberOfBuckets;
}

shardingConfiguration.Prefixed = ordered;
}

private static void AddPrefixedBucketRange(PrefixedShardingSetting setting, int rangeStart, ShardingConfiguration shardingConfiguration)
Expand Down
60 changes: 60 additions & 0 deletions test/SlowTests/Sharding/PrefixedSharding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2700,6 +2700,66 @@ public async Task PrefixesShouldGetPrecedenceOverAnchoring()
}
}

[RavenFact(RavenTestCategory.Sharding)]
public async Task PreventAddingSamePrefixWithDifferentCasing()
{
using var store = Sharding.GetDocumentStore(new Options
{
ModifyDatabaseRecord = record =>
{
record.Sharding ??= new ShardingConfiguration();
record.Sharding.Prefixed =
[
new PrefixedShardingSetting
{
Prefix = "users/us/",
Shards = [0]
}
];
}
});
{

var ex = await Assert.ThrowsAsync<RavenException>((async () => await store.Maintenance.SendAsync(new AddPrefixedShardingSettingOperation(new PrefixedShardingSetting
{
Prefix = "users/US/",
Shards = [1]
}))));


Assert.Contains($"Prefix 'users/US/' already exists", ex.Message);
}


}

[RavenFact(RavenTestCategory.Sharding)]
public void PreventCreatingDatabaseWithSamePrefixInDifferentCasing()
{
var ex = Assert.Throws<RavenException>(() => Sharding.GetDocumentStore(new Options
{
ModifyDatabaseRecord = record =>
{
record.Sharding ??= new ShardingConfiguration();
record.Sharding.Prefixed =
[
new PrefixedShardingSetting
{
Prefix = "users/us/",
Shards = [0]
},
new PrefixedShardingSetting
{
Prefix = "users/US/",
Shards = [0]
}
];
}
}));

Assert.Contains("Prefix 'users/us/' already exists in different casing", ex.Message);
}

private class Item
{
#pragma warning disable CS0649
Expand Down

0 comments on commit 0694000

Please sign in to comment.