Skip to content

Commit

Permalink
run Upgrade_20211130_ChunksAndMinBy
Browse files Browse the repository at this point in the history
  • Loading branch information
olmobrutall committed Nov 30, 2021
1 parent d1ef304 commit 45303a1
Show file tree
Hide file tree
Showing 30 changed files with 57 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static void AssertAllowed<T>(PrimaryKey[] requested, TypeAllowedBasic typeAllowe
{
using (DisableQueryFilter())
{
var found = requested.GroupsOf(1000).SelectMany(gr => Database.Query<T>().Where(a => gr.Contains(a.Id)).Select(a => new
var found = requested.Chunks(1000).SelectMany(gr => Database.Query<T>().Where(a => gr.Contains(a.Id)).Select(a => new
{
a.Id,
Allowed = a.IsAllowedFor(typeAllowed, ExecutionMode.InUserInterface),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static void AddDynamicRename(string replacementKey, string oldName, strin
private static string AutoReplacementEnums(Replacements.AutoReplacementContext ctx)
{
StringDistance sd = new StringDistance();
return ctx.NewValues!.WithMin(nv => sd.LevenshteinDistance(nv, ctx.OldValue))!;
return ctx.NewValues!.MinBy(nv => sd.LevenshteinDistance(nv, ctx.OldValue))!;
}

public static string? DynamicAutoReplacementsSimple(Replacements.AutoReplacementContext ctx, List<DynamicRenameEntity> lastRenames, string replacementKey)
Expand Down
4 changes: 2 additions & 2 deletions Signum.Engine.Extensions/Dynamic/DynamicTypeLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public string GetFileCode()
public string GetEntityCode()
{
StringBuilder sb = new StringBuilder();
foreach (var gr in GetEntityAttributes().GroupsOf(a => a.Length, 100))
foreach (var gr in GetEntityAttributes().Chunk(a => a.Length, 100))
{
sb.AppendLine("[" + gr.ToString(", ") + "]");
}
Expand Down Expand Up @@ -494,7 +494,7 @@ private string GetValidatorAttribute(DynamicValidator v)

protected virtual void WriteAttributeTag(StringBuilder sb, IEnumerable<string> attributes)
{
foreach (var gr in attributes.GroupsOf(a => a.Length, 100))
foreach (var gr in attributes.Chunk(a => a.Length, 100))
{
sb.AppendLine("[" + gr.ToString(", ") + "]");
}
Expand Down
2 changes: 1 addition & 1 deletion Signum.Engine.Extensions/Isolation/IsolationLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public static IEnumerable<T> WhereCurrentIsolationInMemory<T>(this IEnumerable<T

public static Lite<IsolationEntity>? GetOnlyIsolation<T>(IEnumerable<Lite<Entity>> selectedEntities) where T : Entity
{
return selectedEntities.Cast<Lite<T>>().GroupsOf(100).Select(gr =>
return selectedEntities.Cast<Lite<T>>().Chunk(100).Select(gr =>
Database.Query<T>().Where(e => gr.Contains(e.ToLite())).Select(e => e.Isolation()).Only()
).NotNull().Only();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void SavePredictions(PredictorTrainingContext ctx)

var pc = PredictorPredictLogic.CreatePredictContext(ctx.Predictor);
int grIndex = 0;
foreach (var gr in dictionary.GroupsOf(PredictionBatchSize))
foreach (var gr in dictionary.Chunk(PredictionBatchSize))
{
using (HeavyProfiler.LogNoStackTrace("Group"))
{
Expand Down Expand Up @@ -110,7 +110,7 @@ public void SavePredictions(PredictorTrainingContext ctx)

if (SaveAllResults)
{
var groups = toInsert.GroupsOf(PredictionBatchSize).ToList();
var groups = toInsert.Chunk(PredictionBatchSize).ToList();
foreach (var iter in groups.Iterate())
{
ctx.ReportProgress($"Inserting {typeof(PredictSimpleResultEntity).NicePluralName()}", iter.Position / (decimal)groups.Count);
Expand Down
2 changes: 1 addition & 1 deletion Signum.Engine.Extensions/Mailing/EmailPackageLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void Execute(ExecutingProcess executingProcess)
using (AuthLogic.Disable())
{

foreach (var group in emails.GroupsOf(EmailLogic.Configuration.ChunkSizeSendingEmails))
foreach (var group in emails.Chunk(EmailLogic.Configuration.ChunkSizeSendingEmails))
{
var retrieved = group.RetrieveFromListOfLite();
foreach (var m in retrieved)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ public static Pop3ReceptionEntity ReceiveEmailsFullComparation(this Pop3Configur
{
var messageInfos = client.GetMessageInfos();

var already = messageInfos.Select(a => a.Uid).GroupsOf(50).SelectMany(l =>
var already = messageInfos.Select(a => a.Uid).Chunk(50).SelectMany(l =>
(from em in Database.Query<EmailMessageEntity>()
let ri = em.Mixin<EmailReceptionMixin>().ReceptionInfo
where ri != null && l.Contains(ri.UniqueId)
Expand Down
4 changes: 2 additions & 2 deletions Signum.Engine.Extensions/Processes/PackageLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public static PackageEntity CreateLines(this PackageEntity package, IEnumerable<

int inserts =
lites.GroupBy(a => a.EntityType).Sum(gr =>
gr.GroupsOf(100).Sum(gr2 =>
gr.Chunk(100).Sum(gr2 =>
giInsertPackageLines.GetInvoker(gr.Key)(package, gr2)));

return package;
Expand All @@ -144,7 +144,7 @@ public static PackageEntity CreateLines(this PackageEntity package, IEnumerable<

int inserts =
entities.GroupBy(a => a.GetType()).Sum(gr =>
gr.GroupsOf(100).Sum(gr2 =>
gr.Chunk(100).Sum(gr2 =>
giInsertPackageLines.GetInvoker(gr.Key)(package, gr2.Select(a => a.ToLite()))));

return package;
Expand Down
2 changes: 1 addition & 1 deletion Signum.Engine.Extensions/Translation/AzureTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public AzureTranslator(Func<string?> azureKey, Func<string?>? region = null, Fun
if(this.AzureKey() == null)
return null;

var result = list.GroupsOf(10).SelectMany(listPart => Task.Run(async () =>
var result = list.Chunk(10).SelectMany(listPart => Task.Run(async () =>
{
return await this.TranslateBatchAsync(listPart, from, to);
}).Result!).ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static TypeInstancesChanges GetTypeInstanceChangesTranslated(ITranslator[

totalInstances = instances.Count;
if (instances.Sum(a => a.TotalOriginalLength()) > MaxTotalSyncCharacters)
instances = instances.GroupsOf(a => a.TotalOriginalLength(), MaxTotalSyncCharacters).First().ToList();
instances = instances.Chunk(a => a.TotalOriginalLength(), MaxTotalSyncCharacters).First().ToList();

return TranslateInstances(translators, type, targetCulture, instances);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static LocalizedAssemblyChanges GetAssemblyChanges(ITranslator[] translat
totalTypes = types.Count;

if (types.Sum(a => a.TotalOriginalLength()) > MaxTotalSyncCharacters)
types = types.GroupsOf(a => a.TotalOriginalLength(), MaxTotalSyncCharacters).First().ToList();
types = types.Chunk(a => a.TotalOriginalLength(), MaxTotalSyncCharacters).First().ToList();

var result = Translate(translators, target, types);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public void Train(PredictorTrainingContext ctx)
}
}

var best = candidate.WithMin(a => a.ResultValidation.Loss!.Value)!;
var best = candidate.MinBy(a => a.ResultValidation.Loss!.Value)!;

p.ResultTraining = best.ResultTraining;
p.ResultValidation = best.ResultValidation;
Expand Down
2 changes: 1 addition & 1 deletion Signum.Engine/CodeGeneration/EntityCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected virtual string GetNamespace(string fileName)

protected virtual void WriteAttributeTag(StringBuilder sb, IEnumerable<string> attributes)
{
foreach (var gr in attributes.GroupsOf(a => a.Length, 100))
foreach (var gr in attributes.Chunk(a => a.Length, 100))
{
sb.AppendLine("[" + gr.ToString(", ") + "]");
}
Expand Down
2 changes: 1 addition & 1 deletion Signum.Engine/CodeGeneration/ReactCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ protected virtual string WriteClientFile(Module mod)
foreach (var gr in mod.Types.GroupBy(a => a.Namespace))
{
sb.AppendLine("import { "
+ gr.Select(t => t.Name).GroupsOf(5).ToString(a => a.ToString(", "), ",\r\n")
+ gr.Select(t => t.Name).Chunk(5).ToString(a => a.ToString(", "), ",\r\n")
+ " } from './" + gr.Key + "'");
}

Expand Down
15 changes: 8 additions & 7 deletions Signum.Engine/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Signum.Engine.Maps;
using Signum.Entities.Basics;
using Signum.Entities.Internal;
using Signum.Utilities;
using Signum.Utilities.Reflection;
using System.Collections;
using System.Collections.ObjectModel;
Expand Down Expand Up @@ -701,16 +702,16 @@ static List<T> RetrieveFromDatabaseOrCache<T>(List<PrimaryKey> ids, string? mess
}

if (message == null)
return ids.GroupsOf(Schema.Current.Settings.MaxNumberOfParameters)
return ids.Chunk(Schema.Current.Settings.MaxNumberOfParameters)
.SelectMany(gr => Database.Query<T>().Where(a => gr.Contains(a.Id)))
.ToList();
else
{
SafeConsole.WriteLineColor(ConsoleColor.Cyan, message == "auto" ? "Retriving " + typeof(T).Name : message);

var result = new List<T>();
var groups = ids.GroupsOf(Schema.Current.Settings.MaxNumberOfParameters).ToList();
groups.ProgressForeach(gr => gr.Count.ToString(), gr =>
var groups = ids.Chunk(Schema.Current.Settings.MaxNumberOfParameters).ToList();
groups.ProgressForeach(gr => gr.Length.ToString(), gr =>
{
result.AddRange(Database.Query<T>().Where(a => gr.Contains(a.Id)));
});
Expand Down Expand Up @@ -794,7 +795,7 @@ static async Task<List<T>> RetrieveFromDatabaseOrCache<T>(List<PrimaryKey> ids,
}
}

var tasks = ids.GroupsOf(Schema.Current.Settings.MaxNumberOfParameters)
var tasks = ids.Chunk(Schema.Current.Settings.MaxNumberOfParameters)
.Select(gr => Database.Query<T>().Where(a => gr.Contains(a.Id)).ToListAsync(token))
.ToList();

Expand Down Expand Up @@ -837,7 +838,7 @@ public static List<Lite<T>> RetrieveListLite<T>(List<PrimaryKey> ids)
return ids.Select(id => (Lite<T>)new LiteImp<T>(id, cc.GetToString(id))).ToList();
}

var retrieved = ids.GroupsOf(Schema.Current.Settings.MaxNumberOfParameters).SelectMany(gr => Database.Query<T>().Where(a => gr.Contains(a.Id)).Select(a => a.ToLite())).ToDictionary(a => a.Id);
var retrieved = ids.Chunk(Schema.Current.Settings.MaxNumberOfParameters).SelectMany(gr => Database.Query<T>().Where(a => gr.Contains(a.Id)).Select(a => a.ToLite())).ToDictionary(a => a.Id);

var missing = ids.Except(retrieved.Keys);

Expand Down Expand Up @@ -865,7 +866,7 @@ public static async Task<List<Lite<T>>> RetrieveListLiteAsync<T>(List<PrimaryKey
return ids.Select(id => (Lite<T>)new LiteImp<T>(id, cc.GetToString(id))).ToList();
}

var tasks = ids.GroupsOf(Schema.Current.Settings.MaxNumberOfParameters)
var tasks = ids.Chunk(Schema.Current.Settings.MaxNumberOfParameters)
.Select(gr => Database.Query<T>().Where(a => gr.Contains(a.Id)).Select(a => a.ToLite()).ToListAsync(token))
.ToList();

Expand Down Expand Up @@ -1059,7 +1060,7 @@ public static void DeleteList<T>(IList<PrimaryKey> ids)
{
using (var tr = new Transaction())
{
var groups = ids.GroupsOf(Schema.Current.Settings.MaxNumberOfParameters);
var groups = ids.Chunk(Schema.Current.Settings.MaxNumberOfParameters);
int result = 0;
foreach (var group in groups)
result += Database.Query<T>().Where(a => group.Contains(a.Id)).UnsafeDelete();
Expand Down
2 changes: 1 addition & 1 deletion Signum.Engine/Engine/Saver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private static void SaveGraph(Schema schema, DirectedGraph<Entity> identifiables
{
IGrouping<(Type type, bool isNew), Entity> group = clone.Sinks()
.GroupBy(ident => (ident.GetType(), ident.IsNew))
.WithMin(g => stats[g.Key] - g.Count())!;
.MinBy(g => stats[g.Key] - g.Count())!;

foreach (var node in group)
clone.RemoveFullNode(node, inv.RelatedTo(node));
Expand Down
2 changes: 1 addition & 1 deletion Signum.Engine/Engine/Synchronizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public virtual void AskForReplacements(

while (oldOnly.Count > 0 && newOnly.Count > 0)
{
var oldDist = distances.WithMin(kvp => kvp.Value.Values.Min());
var oldDist = distances.MinBy(kvp => kvp.Value.Values.Min());

var alternatives = oldDist.Value.OrderBy(a => a.Value).Select(a => a.Key).ToList();

Expand Down
2 changes: 1 addition & 1 deletion Signum.Engine/Operations/OperationLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ internal static Dictionary<OperationSymbol, string> GetContextualGraphCanExecute
{
var getState = Graph<E, S>.GetState;

var states = lites.GroupsOf(200).SelectMany(list =>
var states = lites.Chunk(200).SelectMany(list =>
Database.Query<T>().Where(e => list.Contains(e.ToLite())).Select(getState).Distinct()).Distinct().ToList();

return (from o in operations.Cast<Graph<E, S>.IGraphFromStatesOperation>()
Expand Down
2 changes: 1 addition & 1 deletion Signum.Engine/Patterns/DeletePart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static FluentInclude<T> WithDeletePart<T, L>(this FluentInclude<T> fi, Ex
var toDelete = filteredQuery.Select(relatedEntity).Select(a => a.ToLite()).ToList().NotNull().Distinct().ToList();
return new Disposable(() =>
{
var groups = toDelete.GroupsOf(Connector.Current.Schema.Settings.MaxNumberOfParameters).ToList();
var groups = toDelete.Chunk(Connector.Current.Schema.Settings.MaxNumberOfParameters).ToList();
groups.ForEach(l => Database.DeleteList(l));
});
};
Expand Down
6 changes: 3 additions & 3 deletions Signum.Engine/Retriever.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public async Task CompleteAllPrivate(CancellationToken? token)
{
while (requests.Count > 0)
{
var group = requests.WithMax(a => a.Value.Count);
var group = requests.MaxBy(a => a.Value.Count);

var dic = group.Value;

Expand Down Expand Up @@ -305,7 +305,7 @@ static async Task<Dictionary<PrimaryKey, string>> GetStrings<T>(List<PrimaryKey>
}
else if (token != null)
{
var tasks = ids.GroupsOf(Schema.Current.Settings.MaxNumberOfParameters)
var tasks = ids.Chunk(Schema.Current.Settings.MaxNumberOfParameters)
.Select(gr => Database.Query<T>().Where(e => gr.Contains(e.Id)).Select(a => KeyValuePair.Create(a.Id, a.ToString())).ToListAsync(token!.Value))
.ToList();

Expand All @@ -316,7 +316,7 @@ static async Task<Dictionary<PrimaryKey, string>> GetStrings<T>(List<PrimaryKey>
}
else
{
var dic = ids.GroupsOf(Schema.Current.Settings.MaxNumberOfParameters)
var dic = ids.Chunk(Schema.Current.Settings.MaxNumberOfParameters)
.SelectMany(gr => Database.Query<T>().Where(e => gr.Contains(e.Id)).Select(a => KeyValuePair.Create(a.Id, a.ToString())))
.ToDictionaryEx();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Signum.Entities.Scheduler;
using System.ComponentModel;

namespace Signum.Entities.Authorization;
Expand Down Expand Up @@ -163,3 +164,10 @@ public static class ADGroupOperation
public static readonly ExecuteSymbol<ADGroupEntity> Save;
public static readonly DeleteSymbol<ADGroupEntity> Delete;
}

[AutoInit]
public static class ActiveDirectoryTask
{
public static readonly SimpleTaskSymbol DeactivateUsers;
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public Task<List<ActiveDirectoryUser>> FindADUsers(string subString, int count,
{
var config = ((ActiveDirectoryAuthorizer)AuthLogic.Authorizer!).GetConfig();
if (config.Azure_ApplicationID.HasText())
return MicrosoftGraphLogic.FindActiveDirectoryUsers(subString, count, token);
return AzureADLogic.FindActiveDirectoryUsers(subString, count, token);

if (config.DomainName.HasText())
return ActiveDirectoryLogic.SearchUser(subString);
Expand All @@ -33,7 +33,7 @@ public Lite<UserEntity> CreateADUser([FromBody][Required] ActiveDirectoryUser us
var config = ((ActiveDirectoryAuthorizer)AuthLogic.Authorizer!).GetConfig();

if (config.Azure_ApplicationID.HasText())
return MicrosoftGraphLogic.CreateUserFromAD(user).ToLite();
return AzureADLogic.CreateUserFromAD(user).ToLite();

if (config.DomainName.HasText())
return ActiveDirectoryLogic.CreateUserFromAD(user).ToLite();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as Entities from '../../Signum.React/Scripts/Signum.Entities'
import * as Basics from '../../Signum.React/Scripts/Signum.Entities.Basics'
import * as Signum from '../Basics/Signum.Entities.Basics'
import * as Mailing from '../Mailing/Signum.Entities.Mailing'
import * as Scheduler from '../Scheduler/Signum.Entities.Scheduler'

export interface UserEntity {
newPassword: string;
Expand Down Expand Up @@ -59,6 +60,10 @@ export module ActiveDirectoryPermission {
export const InviteUsersFromAD : PermissionSymbol = registerSymbol("Permission", "ActiveDirectoryPermission.InviteUsersFromAD");
}

export module ActiveDirectoryTask {
export const DeactivateUsers : Scheduler.SimpleTaskSymbol = registerSymbol("SimpleTask", "ActiveDirectoryTask.DeactivateUsers");
}

export const ADGroupEntity = new Type<ADGroupEntity>("ADGroup");
export interface ADGroupEntity extends Entities.Entity {
Type: "ADGroup";
Expand Down
4 changes: 2 additions & 2 deletions Signum.Upgrade/Upgrades/Upgrade_20211130_ChunksAndMinBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ namespace Signum.Upgrade.Upgrades;

class Upgrade_20211130_ChunksAndMinBy : CodeUpgradeBase
{
public override string Description => "Replace some Signum.Utilities methods (GroupsOf, WithMin, WithMax) with the .Net 6 counterparts (Chunks, MinBy, MaxBy)";
public override string Description => "Replace some Signum.Utilities methods (GroupsOf, WithMin, WithMax) with the .Net 6 counterparts (Chunk, MinBy, MaxBy)";

public override void Execute(UpgradeContext uctx)
{
uctx.ForeachCodeFile(@"*.cs", file =>
{
file.Replace(".WithMin(", ".MinBy(");
file.Replace(".WithMax(", ".MaxBy(");
file.Replace(".GroupsOf(", ".Chunks(");
file.Replace(".GroupsOf(", ".Chunk(");
});
}
}
2 changes: 1 addition & 1 deletion Signum.Utilities/ConsoleSwitch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ int GetIndex(string value)
return exact;

var sd = new StringDistance();
var best = dictionary.Keys.WithMin(a => sd.LevenshteinDistance(input.ToLowerInvariant(), a.ToLowerInvariant()));
var best = dictionary.Keys.MinBy(a => sd.LevenshteinDistance(input.ToLowerInvariant(), a.ToLowerInvariant()));
if (best != null && sd.LevenshteinDistance(input.ToLowerInvariant(), best.ToLowerInvariant()) <= 2)
{
if (SafeConsole.Ask($"Did you mean '{best}'?"))
Expand Down
2 changes: 1 addition & 1 deletion Signum.Utilities/DataStructures/DirectedEdgedGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ public DirectedEdgedGraph<T, E> FeedbackEdgeSet()

Func<T, int> fanInOut = n => clone.RelatedTo(n).Count() - inv.RelatedTo(n).Count();

MinMax<T> mm = clone.WithMinMaxPair(fanInOut);
MinMax<T> mm = clone.MinMaxBy(fanInOut);

if (fanInOut(mm.Max) > -fanInOut(mm.Min))
{
Expand Down
2 changes: 1 addition & 1 deletion Signum.Utilities/DataStructures/DirectedGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ public DirectedGraph<T> FeedbackEdgeSet()

int fanInOut(T n) => clone.RelatedTo(n).Count() - inv.RelatedTo(n).Count();

MinMax<T> mm = clone.WithMinMaxPair(fanInOut);
MinMax<T> mm = clone.MinMaxBy(fanInOut);

if (fanInOut(mm.Max) > -fanInOut(mm.Min))
{
Expand Down
Loading

2 comments on commit 45303a1

@olmobrutall
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace GroupsOf -> Chunk, WithMin -> MinBy and WithMax -> MaxBy

.Net 6 has a few new LINQ methods.

Some of there are clear copies of Signum inventions, but instead of getting angry and kicking ourselves we have decided to be constructive and adopt the new methods to avoid the division of the .Net community in two.

If you are using this methods in your code, the Upgrade_20211130_ChunksAndMinBy should make all the necessary changes.

@MehdyKarimpour
Copy link
Contributor

@MehdyKarimpour MehdyKarimpour commented on 45303a1 Nov 30, 2021 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.