Skip to content

Commit a6b04a9

Browse files
Replace arbitrary group size with constant
1 parent c5ba23a commit a6b04a9

13 files changed

+25
-27
lines changed

src/Umbraco.Core/Persistence/NPocoDatabaseExtensions-Bulk.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ internal static IDbCommand[] GenerateBulkInsertCommands<T>(this IUmbracoDatabase
137137
// Math.Floor(2100 / 8) = 262 record per command
138138
// 4168 / 262 = 15.908... = there will be 16 command in total
139139
// (if we have disabled db parameters, then all records will be included, in only one command)
140-
var recordsPerCommand = paramsPerRecord == 0 ? int.MaxValue : Convert.ToInt32(Math.Floor(2000.00 / paramsPerRecord));
140+
var recordsPerCommand = paramsPerRecord == 0 ? int.MaxValue : Convert.ToInt32(Math.Floor((double)Constants.Sql.MaxParameterCount / paramsPerRecord));
141141
var commandsCount = Convert.ToInt32(Math.Ceiling((double)records.Length / recordsPerCommand));
142142

143143
var commands = new IDbCommand[commandsCount];

src/Umbraco.Core/Persistence/Repositories/Implement/AuditEntryRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected override IEnumerable<IAuditEntry> PerformGetAll(params int[] ids)
5454

5555
var entries = new List<IAuditEntry>();
5656

57-
foreach (var group in ids.InGroupsOf(2000))
57+
foreach (var group in ids.InGroupsOf(Constants.Sql.MaxParameterCount))
5858
{
5959
var sql = Sql()
6060
.Select<AuditEntryDto>()

src/Umbraco.Core/Persistence/Repositories/Implement/ContentRepositoryBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ protected IDictionary<int, PropertyCollection> GetPropertyCollections<T>(List<Te
638638
// in the table?
639639

640640
// get all PropertyDataDto for all definitions / versions
641-
var allPropertyDataDtos = Database.FetchByGroups<PropertyDataDto, int>(versions, 2000, batch =>
641+
var allPropertyDataDtos = Database.FetchByGroups<PropertyDataDto, int>(versions, Constants.Sql.MaxParameterCount, batch =>
642642
SqlContext.Sql()
643643
.Select<PropertyDataDto>()
644644
.From<PropertyDataDto>()
@@ -647,7 +647,7 @@ protected IDictionary<int, PropertyCollection> GetPropertyCollections<T>(List<Te
647647

648648
// get PropertyDataDto distinct PropertyTypeDto
649649
var allPropertyTypeIds = allPropertyDataDtos.Select(x => x.PropertyTypeId).Distinct().ToList();
650-
var allPropertyTypeDtos = Database.FetchByGroups<PropertyTypeDto, int>(allPropertyTypeIds, 2000, batch =>
650+
var allPropertyTypeDtos = Database.FetchByGroups<PropertyTypeDto, int>(allPropertyTypeIds, Constants.Sql.MaxParameterCount, batch =>
651651
SqlContext.Sql()
652652
.Select<PropertyTypeDto>(r => r.Select(x => x.DataTypeDto))
653653
.From<PropertyTypeDto>()

src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepositoryBase.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ private void CopyTagData(int? sourceLanguageId, int? targetLanguageId, IReadOnly
768768
// note: important to use SqlNullableEquals for nullable types, cannot directly compare language identifiers
769769

770770
var whereInArgsCount = propertyTypeIds.Count + (contentTypeIds?.Count ?? 0);
771-
if (whereInArgsCount > 2000)
771+
if (whereInArgsCount > Constants.Sql.MaxParameterCount)
772772
throw new NotSupportedException("Too many property/content types.");
773773

774774
// delete existing relations (for target language)
@@ -906,7 +906,7 @@ private void CopyPropertyData(int? sourceLanguageId, int? targetLanguageId, IRea
906906
// note: important to use SqlNullableEquals for nullable types, cannot directly compare language identifiers
907907
//
908908
var whereInArgsCount = propertyTypeIds.Count + (contentTypeIds?.Count ?? 0);
909-
if (whereInArgsCount > 2000)
909+
if (whereInArgsCount > Constants.Sql.MaxParameterCount)
910910
throw new NotSupportedException("Too many property/content types.");
911911

912912
//first clear out any existing property data that might already exists under the target language
@@ -1005,7 +1005,7 @@ private void RenormalizeDocumentEditedFlags(IReadOnlyCollection<int> propertyTyp
10051005
//based on the current variance of each item to see if it's 'edited' value should be true/false.
10061006

10071007
var whereInArgsCount = propertyTypeIds.Count + (contentTypeIds?.Count ?? 0);
1008-
if (whereInArgsCount > 2000)
1008+
if (whereInArgsCount > Constants.Sql.MaxParameterCount)
10091009
throw new NotSupportedException("Too many property/content types.");
10101010

10111011
var propertySql = Sql()
@@ -1095,7 +1095,7 @@ private void RenormalizeDocumentEditedFlags(IReadOnlyCollection<int> propertyTyp
10951095
}
10961096

10971097
//lookup all matching rows in umbracoDocumentCultureVariation
1098-
var docCultureVariationsToUpdate = editedLanguageVersions.InGroupsOf(2000)
1098+
var docCultureVariationsToUpdate = editedLanguageVersions.InGroupsOf(Constants.Sql.MaxParameterCount)
10991099
.SelectMany(_ => Database.Fetch<DocumentCultureVariationDto>(
11001100
Sql().Select<DocumentCultureVariationDto>().From<DocumentCultureVariationDto>()
11011101
.WhereIn<DocumentCultureVariationDto>(x => x.LanguageId, editedLanguageVersions.Keys.Select(x => x.langId).ToList())

src/Umbraco.Core/Persistence/Repositories/Implement/DictionaryRepository.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,7 @@ public IEnumerable<IDictionaryItem> GetDictionaryItemDescendants(Guid? parentId)
260260

261261
Func<Guid[], IEnumerable<IEnumerable<IDictionaryItem>>> getItemsFromParents = guids =>
262262
{
263-
//needs to be in groups of 2000 because we are doing an IN clause and there's a max parameter count that can be used.
264-
return guids.InGroupsOf(2000)
263+
return guids.InGroupsOf(Constants.Sql.MaxParameterCount)
265264
.Select(@group =>
266265
{
267266
var sqlClause = GetBaseQuery(false)

src/Umbraco.Core/Persistence/Repositories/Implement/DocumentRepository.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,7 @@ private IDictionary<int, ContentScheduleCollection> GetContentSchedule(params in
13421342
{
13431343
var result = new Dictionary<int, ContentScheduleCollection>();
13441344

1345-
var scheduleDtos = Database.FetchByGroups<ContentScheduleDto, int>(contentIds, 2000, batch => Sql()
1345+
var scheduleDtos = Database.FetchByGroups<ContentScheduleDto, int>(contentIds, Constants.Sql.MaxParameterCount, batch => Sql()
13461346
.Select<ContentScheduleDto>()
13471347
.From<ContentScheduleDto>()
13481348
.WhereIn<ContentScheduleDto>(x => x.NodeId, batch));
@@ -1391,7 +1391,7 @@ private IDictionary<int, List<ContentVariation>> GetContentVariations<T>(List<Te
13911391
}
13921392
if (versions.Count == 0) return new Dictionary<int, List<ContentVariation>>();
13931393

1394-
var dtos = Database.FetchByGroups<ContentVersionCultureVariationDto, int>(versions, 2000, batch
1394+
var dtos = Database.FetchByGroups<ContentVersionCultureVariationDto, int>(versions, Constants.Sql.MaxParameterCount, batch
13951395
=> Sql()
13961396
.Select<ContentVersionCultureVariationDto>()
13971397
.From<ContentVersionCultureVariationDto>()
@@ -1420,7 +1420,7 @@ private IDictionary<int, List<DocumentVariation>> GetDocumentVariations<T>(List<
14201420
{
14211421
var ids = temps.Select(x => x.Id);
14221422

1423-
var dtos = Database.FetchByGroups<DocumentCultureVariationDto, int>(ids, 2000, batch =>
1423+
var dtos = Database.FetchByGroups<DocumentCultureVariationDto, int>(ids, Constants.Sql.MaxParameterCount, batch =>
14241424
Sql()
14251425
.Select<DocumentCultureVariationDto>()
14261426
.From<DocumentCultureVariationDto>()

src/Umbraco.Core/Persistence/Repositories/Implement/EntityContainerRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected override IEnumerable<EntityContainer> PerformGetAll(params int[] ids)
6060
{
6161
if (ids.Any())
6262
{
63-
return Database.FetchByGroups<NodeDto, int>(ids, 2000, batch =>
63+
return Database.FetchByGroups<NodeDto, int>(ids, Constants.Sql.MaxParameterCount, batch =>
6464
GetBaseQuery(false)
6565
.Where<NodeDto>(x => x.NodeObjectType == NodeObjectTypeId)
6666
.WhereIn<NodeDto>(x => x.NodeId, batch))

src/Umbraco.Core/Persistence/Repositories/Implement/EntityRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ private IEnumerable<DocumentEntitySlim> BuildVariants(IEnumerable<DocumentEntity
281281
if (v == null) return entitiesList;
282282

283283
// fetch all variant info dtos
284-
var dtos = Database.FetchByGroups<VariantInfoDto, int>(v.Select(x => x.Id), 2000, GetVariantInfos);
284+
var dtos = Database.FetchByGroups<VariantInfoDto, int>(v.Select(x => x.Id), Constants.Sql.MaxParameterCount, GetVariantInfos);
285285

286286
// group by node id (each group contains all languages)
287287
var xdtos = dtos.GroupBy(x => x.NodeId).ToDictionary(x => x.Key, x => x);

src/Umbraco.Core/Persistence/Repositories/Implement/PermissionRepository.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public EntityPermissionCollection GetPermissionsForEntities(int[] groupIds, para
4444
{
4545
var result = new EntityPermissionCollection();
4646

47-
foreach (var groupOfGroupIds in groupIds.InGroupsOf(2000))
47+
foreach (var groupOfGroupIds in groupIds.InGroupsOf(Constants.Sql.MaxParameterCount))
4848
{
4949
//copy local
5050
var localIds = groupOfGroupIds.ToArray();
@@ -64,7 +64,7 @@ public EntityPermissionCollection GetPermissionsForEntities(int[] groupIds, para
6464
else
6565
{
6666
//iterate in groups of 2000 since we don't want to exceed the max SQL param count
67-
foreach (var groupOfEntityIds in entityIds.InGroupsOf(2000))
67+
foreach (var groupOfEntityIds in entityIds.InGroupsOf(Constants.Sql.MaxParameterCount))
6868
{
6969
var ids = groupOfEntityIds;
7070
var sql = Sql()
@@ -133,11 +133,10 @@ public void ReplacePermissions(int groupId, IEnumerable<char> permissions, param
133133

134134
var db = AmbientScope.Database;
135135

136-
//we need to batch these in groups of 2000 so we don't exceed the max 2100 limit
137136
var sql = "DELETE FROM umbracoUserGroup2NodePermission WHERE userGroupId = @groupId AND nodeId in (@nodeIds)";
138-
foreach (var idGroup in entityIds.InGroupsOf(2000))
137+
foreach (var group in entityIds.InGroupsOf(Constants.Sql.MaxParameterCount))
139138
{
140-
db.Execute(sql, new { groupId, nodeIds = idGroup });
139+
db.Execute(sql, new { groupId, nodeIds = group });
141140
}
142141

143142
var toInsert = new List<UserGroup2NodePermissionDto>();

src/Umbraco.Core/Persistence/Repositories/Implement/RedirectUrlRepository.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ protected override IRedirectUrl PerformGet(Guid id)
3737

3838
protected override IEnumerable<IRedirectUrl> PerformGetAll(params Guid[] ids)
3939
{
40-
if (ids.Length > 2000)
41-
throw new NotSupportedException("This repository does not support more than 2000 ids.");
40+
if (ids.Length > Constants.Sql.MaxParameterCount)
41+
throw new NotSupportedException($"This repository does not support more than {Constants.Sql.MaxParameterCount} ids.");
42+
4243
var sql = GetBaseQuery(false).WhereIn<RedirectUrlDto>(x => x.Id, ids);
4344
var dtos = Database.Fetch<RedirectUrlDto>(sql);
4445
return dtos.WhereNotNull().Select(Map);

0 commit comments

Comments
 (0)