Skip to content

Commit

Permalink
Using ValueTask
Browse files Browse the repository at this point in the history
  • Loading branch information
credfeto committed Apr 28, 2023
1 parent dc019a5 commit 2589ba1
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 42 deletions.
20 changes: 10 additions & 10 deletions src/Credfeto.Database.Source.Generation.Example/DatabaseWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ namespace Credfeto.Database.Source.Generation.Example;
internal static partial class DatabaseWrapper
{
[SqlObjectMap(name: "ethereum.account_getall", sqlObjectType: SqlObjectType.TABLE_FUNCTION)]
public static partial Task<IReadOnlyList<Accounts>> GetAllAsync(DbConnection connection,
[SqlFieldMap<AccountAddressMapper, AccountAddress>] AccountAddress address,
CancellationToken cancellationToken);
public static partial ValueTask<IReadOnlyList<Accounts>> GetAllAsync(DbConnection connection,
[SqlFieldMap<AccountAddressMapper, AccountAddress>] AccountAddress address,
CancellationToken cancellationToken);

[SqlObjectMap(name: "ethereum.account_get", sqlObjectType: SqlObjectType.TABLE_FUNCTION)]
public static partial Task<Accounts?> GetAsync(DbConnection connection, int id, CancellationToken cancellationToken);
public static partial ValueTask<Accounts?> GetAsync(DbConnection connection, int id, CancellationToken cancellationToken);

[SqlObjectMap(name: "ethereum.account_insert", sqlObjectType: SqlObjectType.STORED_PROCEDURE)]
public static partial Task InsertAsync(DbConnection connection, string name, [SqlFieldMap<AccountAddressMapper, AccountAddress>] AccountAddress address, CancellationToken cancellationToken);
public static partial ValueTask InsertAsync(DbConnection connection, string name, [SqlFieldMap<AccountAddressMapper, AccountAddress>] AccountAddress address, CancellationToken cancellationToken);

[SqlObjectMap(name: "ethereum.get_meaning_of_life_universe_and_everything", sqlObjectType: SqlObjectType.SCALAR_FUNCTION)]
public static partial Task<int> GetMeaningOfLifeAsync(DbConnection connection, CancellationToken cancellationToken);
public static partial ValueTask<int> GetMeaningOfLifeAsync(DbConnection connection, CancellationToken cancellationToken);

[SqlObjectMap(name: "ethereum.get_meaning_of_life_universe_and_everything", sqlObjectType: SqlObjectType.SCALAR_FUNCTION)]
public static partial Task<int?> GetOptionalMeaningOfLifeAsync(DbConnection connection, CancellationToken cancellationToken);
public static partial ValueTask<int?> GetOptionalMeaningOfLifeAsync(DbConnection connection, CancellationToken cancellationToken);

[SqlObjectMap(name: "ethereum.get_meaning_of_life_universe_and_everything", sqlObjectType: SqlObjectType.SCALAR_FUNCTION)]
public static partial Task<string> GetStringMeaningOfLifeAsync(DbConnection connection, CancellationToken cancellationToken);
public static partial ValueTask<string> GetStringMeaningOfLifeAsync(DbConnection connection, CancellationToken cancellationToken);

[SqlObjectMap(name: "ethereum.get_meaning_of_life_universe_and_everything", sqlObjectType: SqlObjectType.SCALAR_FUNCTION)]
[return: SqlFieldMap<AccountAddressMapper, AccountAddress>]
public static partial Task<AccountAddress> GetAddressMeaningOfLifeAsync(DbConnection connection, CancellationToken cancellationToken);
public static partial ValueTask<AccountAddress> GetAddressMeaningOfLifeAsync(DbConnection connection, CancellationToken cancellationToken);

[SqlObjectMap(name: "ethereum.get_meaning_of_life_universe_and_everything", sqlObjectType: SqlObjectType.SCALAR_FUNCTION)]
[return: SqlFieldMap<AccountAddressMapper, AccountAddress>]
public static partial Task<AccountAddress?> GetOptionalAddressMeaningOfLifeAsync(DbConnection connection, CancellationToken cancellationToken);
public static partial ValueTask<AccountAddress?> GetOptionalAddressMeaningOfLifeAsync(DbConnection connection, CancellationToken cancellationToken);
}
16 changes: 8 additions & 8 deletions src/Credfeto.Database.Source.Generation.Example/ITestDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ namespace Credfeto.Database.Source.Generation.Example;

public interface ITestDatabase
{
Task<IReadOnlyList<Accounts>> GetAllAsync(AccountAddress accountAddress, CancellationToken cancellationToken);
ValueTask<IReadOnlyList<Accounts>> GetAllAsync(AccountAddress accountAddress, CancellationToken cancellationToken);

Task<Accounts?> GetAsync(int id, CancellationToken cancellationToken);
ValueTask<Accounts?> GetAsync(int id, CancellationToken cancellationToken);

Task InsertAsync(string name, AccountAddress address, CancellationToken cancellationToken);
ValueTask InsertAsync(string name, AccountAddress address, CancellationToken cancellationToken);

Task<int> GetMeaningOfLifeAsync(CancellationToken cancellationToken);
ValueTask<int> GetMeaningOfLifeAsync(CancellationToken cancellationToken);

Task<int?> GetOptionalMeaningOfLifeAsync(CancellationToken cancellationToken);
ValueTask<int?> GetOptionalMeaningOfLifeAsync(CancellationToken cancellationToken);

Task<string> GetStringMeaningOfLifeAsync(CancellationToken cancellationToken);
ValueTask<string> GetStringMeaningOfLifeAsync(CancellationToken cancellationToken);

Task<AccountAddress> GetAddressMeaningOfLifeAsync(CancellationToken cancellationToken);
ValueTask<AccountAddress> GetAddressMeaningOfLifeAsync(CancellationToken cancellationToken);

Task<AccountAddress?> GetOptionalAddressMeaningOfLifeAsync(CancellationToken cancellationToken);
ValueTask<AccountAddress?> GetOptionalAddressMeaningOfLifeAsync(CancellationToken cancellationToken);
}
16 changes: 8 additions & 8 deletions src/Credfeto.Database.Source.Generation.Example/TestDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,42 @@ public TestDatabase(IDatabase database)
this._database = database;
}

public Task<IReadOnlyList<Accounts>> GetAllAsync(AccountAddress accountAddress, CancellationToken cancellationToken)
public ValueTask<IReadOnlyList<Accounts>> GetAllAsync(AccountAddress accountAddress, CancellationToken cancellationToken)
{
return this._database.ExecuteAsync(action: (c, ct) => DatabaseWrapper.GetAllAsync(connection: c, address: accountAddress, cancellationToken: ct), cancellationToken: cancellationToken);
}

public Task<Accounts?> GetAsync(int id, CancellationToken cancellationToken)
public ValueTask<Accounts?> GetAsync(int id, CancellationToken cancellationToken)
{
return this._database.ExecuteAsync(action: (c, ct) => DatabaseWrapper.GetAsync(connection: c, id: id, cancellationToken: ct), cancellationToken: cancellationToken);
}

public Task InsertAsync(string name, AccountAddress address, CancellationToken cancellationToken)
public ValueTask InsertAsync(string name, AccountAddress address, CancellationToken cancellationToken)
{
return this._database.ExecuteAsync(action: (c, ct) => DatabaseWrapper.InsertAsync(connection: c, name: name, address: address, cancellationToken: ct), cancellationToken: cancellationToken);
}

public Task<int> GetMeaningOfLifeAsync(CancellationToken cancellationToken)
public ValueTask<int> GetMeaningOfLifeAsync(CancellationToken cancellationToken)
{
return this._database.ExecuteAsync(action: (c, ct) => DatabaseWrapper.GetMeaningOfLifeAsync(connection: c, cancellationToken: ct), cancellationToken: cancellationToken);
}

public Task<int?> GetOptionalMeaningOfLifeAsync(CancellationToken cancellationToken)
public ValueTask<int?> GetOptionalMeaningOfLifeAsync(CancellationToken cancellationToken)
{
return this._database.ExecuteAsync(action: (c, ct) => DatabaseWrapper.GetOptionalMeaningOfLifeAsync(connection: c, cancellationToken: ct), cancellationToken: cancellationToken);
}

public Task<string> GetStringMeaningOfLifeAsync(CancellationToken cancellationToken)
public ValueTask<string> GetStringMeaningOfLifeAsync(CancellationToken cancellationToken)
{
return this._database.ExecuteAsync(action: (c, ct) => DatabaseWrapper.GetStringMeaningOfLifeAsync(connection: c, cancellationToken: ct), cancellationToken: cancellationToken);
}

public Task<AccountAddress> GetAddressMeaningOfLifeAsync(CancellationToken cancellationToken)
public ValueTask<AccountAddress> GetAddressMeaningOfLifeAsync(CancellationToken cancellationToken)
{
return this._database.ExecuteAsync(action: (c, ct) => DatabaseWrapper.GetAddressMeaningOfLifeAsync(connection: c, cancellationToken: ct), cancellationToken: cancellationToken);
}

public Task<AccountAddress?> GetOptionalAddressMeaningOfLifeAsync(CancellationToken cancellationToken)
public ValueTask<AccountAddress?> GetOptionalAddressMeaningOfLifeAsync(CancellationToken cancellationToken)
{
return this._database.ExecuteAsync(action: (c, ct) => DatabaseWrapper.GetOptionalAddressMeaningOfLifeAsync(connection: c, cancellationToken: ct), cancellationToken: cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ private static MethodReturnType GetNonGenericMethodReturnType(SemanticModel sema
IdentifierNameSyntax identifierNameSyntax,
CancellationToken cancellationToken)
{
if (identifierNameSyntax.Identifier.Text != "Task")
if (identifierNameSyntax.Identifier.Text != "Task" && identifierNameSyntax.Identifier.Text != "ValueTask")
{
throw new InvalidModelException(message: $"Method {name} does not return a Task");
throw new InvalidModelException(message: $"Method {name} does not return a Task or ValueTask");
}

ISymbol returnSymbol = ValidateSymbol(semanticModel.GetSymbol(node: identifierNameSyntax, cancellationToken: cancellationToken), $"Method {name} could not determine task type");
Expand All @@ -163,9 +163,9 @@ private static ISymbol ValidateSymbol(ISymbol? symbol, string error)

private static MethodReturnType GetGenericTaskReturnType(SemanticModel semanticModel, MapperInfo? mapperInfo, string name, GenericNameSyntax genericNameSyntax, CancellationToken cancellationToken)
{
if (genericNameSyntax.Identifier.Text != "Task")
if (genericNameSyntax.Identifier.Text != "Task" && genericNameSyntax.Identifier.Text != "ValueTask")
{
throw new InvalidModelException(message: $"Method {name} does not return a Task");
throw new InvalidModelException(message: $"Method {name} does not return a Task or ValueTask");
}

ISymbol returnSymbol = ValidateSymbol(semanticModel.GetSymbol(node: genericNameSyntax, cancellationToken: cancellationToken), $"Method {name} could not determine task type");
Expand Down
22 changes: 12 additions & 10 deletions src/Credfeto.Database/BaseDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ protected BaseDatabase()
this._retryPolicyAsync = this.DefineAsyncPolicy();
}

public Task<T> ExecuteAsync<T>(Func<DbConnection, CancellationToken, Task<T>> action, CancellationToken cancellationToken)
public ValueTask<T> ExecuteAsync<T>(Func<DbConnection, CancellationToken, ValueTask<T>> action, CancellationToken cancellationToken)
{
async Task<T> Exec()
async ValueTask<T> Exec()
{
await using (DbConnection connection = await this.GetConnectionAsync(cancellationToken))
{
Expand All @@ -31,9 +31,9 @@ async Task<T> Exec()
return this.ExecuteWithRetriesAsync(func: Exec, context: "ExecuteAsync");
}

public Task ExecuteAsync(Func<DbConnection, CancellationToken, Task> action, CancellationToken cancellationToken)
public ValueTask ExecuteAsync(Func<DbConnection, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
async Task Exec()
async ValueTask Exec()
{
await using (DbConnection connection = await this.GetConnectionAsync(cancellationToken))
{
Expand Down Expand Up @@ -61,27 +61,29 @@ private AsyncRetryPolicy DefineAsyncPolicy()

protected abstract ValueTask<DbConnection> GetConnectionAsync(CancellationToken cancellationToken);

private Task ExecuteWithRetriesAsync(Func<Task> func, string context)
private async ValueTask ExecuteWithRetriesAsync(Func<ValueTask> func, string context)
{
Context loggingContext = new(context);

Task Wrapped(Context c)
{
return func();
return func()
.AsTask();
}

return this._retryPolicyAsync.ExecuteAsync(action: Wrapped, context: loggingContext);
await this._retryPolicyAsync.ExecuteAsync(action: Wrapped, context: loggingContext);
}

private Task<TReturn> ExecuteWithRetriesAsync<TReturn>(Func<Task<TReturn>> func, string context)
private async ValueTask<TReturn> ExecuteWithRetriesAsync<TReturn>(Func<ValueTask<TReturn>> func, string context)
{
Context loggingContext = new(context);

Task<TReturn> Wrapped(Context c)
{
return func();
return func()
.AsTask();
}

return this._retryPolicyAsync.ExecuteAsync(action: Wrapped, context: loggingContext);
return await this._retryPolicyAsync.ExecuteAsync(action: Wrapped, context: loggingContext);
}
}
4 changes: 2 additions & 2 deletions src/Credfeto.Database/IDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Credfeto.Database;

public interface IDatabase
{
Task ExecuteAsync(Func<DbConnection, CancellationToken, Task> action, CancellationToken cancellationToken);
ValueTask ExecuteAsync(Func<DbConnection, CancellationToken, ValueTask> action, CancellationToken cancellationToken);

Task<T> ExecuteAsync<T>(Func<DbConnection, CancellationToken, Task<T>> action, CancellationToken cancellationToken);
ValueTask<T> ExecuteAsync<T>(Func<DbConnection, CancellationToken, ValueTask<T>> action, CancellationToken cancellationToken);
}

0 comments on commit 2589ba1

Please sign in to comment.