Skip to content

Commit

Permalink
Add Async methods to prevent possible thread starvation (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Nov 15, 2023
1 parent a8e5e56 commit 5dfa15e
Show file tree
Hide file tree
Showing 23 changed files with 2,218 additions and 2,261 deletions.
28 changes: 12 additions & 16 deletions samples/YesSql.Bench/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,26 @@ static async Task MainAsync(string[] args)
.UseSqlServer(@"Data Source =.; Initial Catalog = yessql; Integrated Security = True")
.SetTablePrefix("Bench");

using (var connection = configuration.ConnectionFactory.CreateConnection())
await using (var connection = configuration.ConnectionFactory.CreateConnection())
{
await connection.OpenAsync();

using (var transaction = connection.BeginTransaction(configuration.IsolationLevel))
{
var builder = new SchemaBuilder(configuration, transaction);
await using var transaction = await connection.BeginTransactionAsync(configuration.IsolationLevel);
var builder = new SchemaBuilder(configuration, transaction);

builder.CreateMapIndexTable<UserByName>(c => c
.Column<string>("Name")
.Column<bool>("Adult")
.Column<int>("Age")
);
await builder.CreateMapIndexTableAsync<UserByName>(c => c
.Column<string>("Name")
.Column<bool>("Adult")
.Column<int>("Age")
);

transaction.Commit();
}
await transaction.CommitAsync();
}

var store = await StoreFactory.CreateAndInitializeAsync(configuration);
store.RegisterIndexes<UserIndexProvider>();

using (var session = store.CreateSession())
await using (var session = store.CreateSession())
{
var user = await session.Query<User>().FirstOrDefaultAsync();

Expand All @@ -51,13 +49,11 @@ static async Task MainAsync(string[] args)
Age = 1
};


session.Save(bill);

await session.SaveAsync(bill);
await session.SaveChangesAsync();
}

using (var session = store.CreateSession())
await using (var session = store.CreateSession())
{
var user = await session.Query<User, UserByName>().Where(x => x.Adult == true).FirstOrDefaultAsync();

Expand Down
33 changes: 16 additions & 17 deletions samples/YesSql.Samples.FullText/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,36 @@ public static async Task Main(string[] args)

var store = await StoreFactory.CreateAndInitializeAsync(configuration);

using (var connection = store.Configuration.ConnectionFactory.CreateConnection())
await using (var connection = store.Configuration.ConnectionFactory.CreateConnection())
{
connection.Open();
await connection.OpenAsync();

using (var transaction = connection.BeginTransaction(store.Configuration.IsolationLevel))
{
new SchemaBuilder(store.Configuration, transaction)
.CreateReduceIndexTable<ArticleByWord>(table => table
.Column<int>("Count")
.Column<string>("Word")
);
await using var transaction = await connection.BeginTransactionAsync(store.Configuration.IsolationLevel);
var builder = new SchemaBuilder(store.Configuration, transaction);

transaction.Commit();
}
await builder.CreateReduceIndexTableAsync<ArticleByWord>(table => table
.Column<int>("Count")
.Column<string>("Word")
);

await transaction.CommitAsync();
}

// register available indexes
store.RegisterIndexes<ArticleIndexProvider>();

// creating articles
using (var session = store.CreateSession())
await using (var session = store.CreateSession())
{
session.Save(new Article { Content = "This is a green fox" });
session.Save(new Article { Content = "This is a yellow cat" });
session.Save(new Article { Content = "This is a pink elephant" });
session.Save(new Article { Content = "This is a green tiger" });
await session.SaveAsync(new Article { Content = "This is a green fox" });
await session.SaveAsync(new Article { Content = "This is a yellow cat" });
await session.SaveAsync(new Article { Content = "This is a pink elephant" });
await session.SaveAsync(new Article { Content = "This is a green tiger" });

await session.SaveChangesAsync();
}

using (var session = store.CreateSession())
await using (var session = store.CreateSession())
{
Console.WriteLine("Simple term: 'green'");
var simple = await session
Expand Down
39 changes: 19 additions & 20 deletions samples/YesSql.Samples.Hi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ static async Task MainAsync(string[] args)
.SetTablePrefix("Hi")
);

using (var connection = store.Configuration.ConnectionFactory.CreateConnection())
await using (var connection = store.Configuration.ConnectionFactory.CreateConnection())
{
connection.Open();
await connection.OpenAsync();

using (var transaction = connection.BeginTransaction(store.Configuration.IsolationLevel))
{
new SchemaBuilder(store.Configuration, transaction)
.CreateMapIndexTable<BlogPostByAuthor>(table => table
.Column<string>("Author")
)
.CreateReduceIndexTable<BlogPostByDay>(table => table
.Column<int>("Count")
.Column<int>("Day")
await using var transaction = await connection.BeginTransactionAsync(store.Configuration.IsolationLevel);
var builder = new SchemaBuilder(store.Configuration, transaction);

await builder.CreateMapIndexTableAsync<BlogPostByAuthor>(table => table
.Column<string>("Author")
);

transaction.Commit();
}
await builder.CreateReduceIndexTableAsync<BlogPostByDay>(table => table
.Column<int>("Count")
.Column<int>("Day")
);

await transaction.CommitAsync();
};

// register available indexes
Expand All @@ -55,22 +55,21 @@ static async Task MainAsync(string[] args)
};

// saving the post to the database
using (var session = store.CreateSession())
await using (var session = store.CreateSession())
{
session.Save(post);

await session.SaveAsync(post);
await session.SaveChangesAsync();
}

// loading a single blog post
using (var session = store.CreateSession())
await using (var session = store.CreateSession())
{
var p = await session.Query().For<BlogPost>().FirstOrDefaultAsync();
Console.WriteLine(p.Title); // > Hello YesSql
}

// loading blog posts by author
using (var session = store.CreateSession())
await using (var session = store.CreateSession())
{
var ps = await session.Query<BlogPost, BlogPostByAuthor>().Where(x => x.Author.StartsWith("B")).ListAsync();

Expand All @@ -81,7 +80,7 @@ static async Task MainAsync(string[] args)
}

// loading blog posts by day of publication
using (var session = store.CreateSession())
await using (var session = store.CreateSession())
{
var ps = await session.Query<BlogPost, BlogPostByDay>(x => x.Day == DateTime.UtcNow.ToString("yyyyMMdd")).ListAsync();

Expand All @@ -92,7 +91,7 @@ static async Task MainAsync(string[] args)
}

// counting blog posts by day
using (var session = store.CreateSession())
await using (var session = store.CreateSession())
{
var days = await session.QueryIndex<BlogPostByDay>().ListAsync();

Expand Down
Loading

0 comments on commit 5dfa15e

Please sign in to comment.