Skip to content

Commit 77bb941

Browse files
authored
Merge pull request #7 from PandaTechAM/development
Removed bulk copy totally. Added Natural Sort functionality
2 parents 9e1c378 + 66aeaef commit 77bb941

File tree

9 files changed

+86
-615
lines changed

9 files changed

+86
-615
lines changed

Readme.md

Lines changed: 33 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ features.
1010
1. **Row-Level Locking**: Implements the PostgreSQL `FOR UPDATE` feature, providing three lock
1111
behaviors - `Wait`, `Skip`, and
1212
`NoWait`, to facilitate advanced transaction control and concurrency management.
13-
2. **Npgsql COPY Integration (Obsolete)**: Offers a high-performance, typed interface for the PostgreSQL COPY command,
14-
allowing for
15-
bulk data operations within the EF Core framework. This feature significantly enhances data insertion speeds and
16-
efficiency.
17-
3. **Random Incrementing Sequence Generation:** Provides a secure way to generate sequential IDs with random increments
13+
2. **Random Incrementing Sequence Generation:** Provides a secure way to generate sequential IDs with random increments
1814
to prevent predictability and potential data exposure. This ensures IDs are non-sequential and non-predictable,
1915
enhancing security and balancing database load.
16+
3. **Natural Sorting**: Provides way to calculate natural sort compliant order for string, which can be used
17+
in `ORDER BY` clause. This is useful for sorting strings that contain numbers in a human-friendly way.
2018

2119
## Installation
2220

@@ -123,59 +121,40 @@ public partial class PgFunction : Migration
123121
enhancing security.
124122
- The feature supports only `long` data type (`bigint` in PostgreSQL).
125123

126-
### Npgsql COPY Integration (Obsolete: Use EFCore.BulkExtensions.PostgreSql instead)
124+
### Natural Sort Key
127125

128-
For bulk data operations, use the `BulkInsert` or `BulkInsertAsync` extension methods:
126+
This package can generate a natural sort key for your text columns—especially useful when sorting addresses or other
127+
fields that contain embedded numbers. It avoids plain lexicographic ordering (e.g. `"10"` < `"2"`) by treating numeric
128+
substrings numerically.
129129

130-
```csharp
131-
public async Task BulkInsertExampleAsync()
132-
{
133-
var users = new List<UserEntity>();
134-
for (int i = 0; i < 10000; i++)
130+
#### How to Use
131+
132+
1. Create the function in your migration (once per database). Call the helper method in `Up()`:
133+
```csharp
134+
public partial class AddNaturalSortKeyToBuildings : Migration
135135
{
136-
users.Add(new UserEntity { /* Initialization */ });
136+
protected override void Up(MigrationBuilder migrationBuilder)
137+
{
138+
// Create the natural sort key function in PostgreSQL
139+
migrationBuilder.CreateNaturalSortKeyFunction();
140+
141+
}
137142
}
138-
139-
await dbContext.Users.BulkInsertAsync(users); // Or use BulkInsert for synchronous operation
140-
// It also saves changes to the database
141-
}
142-
```
143-
144-
#### Benchmarks
145-
146-
The integration of the Npgsql COPY command showcases significant performance improvements compared to traditional EF
147-
Core and Dapper methods:
148-
149-
##### General Benchmark Results
150-
151-
| Caption | Big O Notation | 1M Rows | Batch Size |
152-
|------------|----------------|-------------|------------|
153-
| BulkInsert | O(log n) | 350.000 r/s | No batch |
154-
| Dapper | O(n) | 20.000 r/s | 1500 |
155-
| EFCore | O(n) | 10.600 r/s | 1500 |
156-
157-
##### Detailed Benchmark Results
158-
159-
| Operation | BulkInsert | Dapper | EF Core |
160-
|-------------|------------|--------|---------|
161-
| Insert 10K | 76ms | 535ms | 884ms |
162-
| Insert 100K | 405ms | 5.47s | 8.58s |
163-
| Insert 1M | 2.87s | 55.85s | 94.57s |
164-
165-
##### Efficiency Comparison
166-
167-
| RowsCount | BulkInsert Efficiency | Dapper Efficiency |
168-
|-----------|----------------------------|---------------------------|
169-
| 10K | 11.63x faster than EF Core | 1.65x faster than EF Core |
170-
| 100K | 21.17x faster than EF Core | 1.57x faster than EF Core |
171-
| 1M | 32.95x faster than EF Core | 1.69x faster than EF Core |
172-
173-
##### Additional Notes
174-
175-
- The `BulkInsert` feature currently does not support entity properties intended for `JSON` storage.
176-
177-
- The performance metrics provided above are based on benchmarks conducted under controlled conditions. Real-world
178-
performance may vary based on specific use cases and configurations.
143+
```
144+
2. Configure your entity to use the natural sort key. In your `IEntityTypeConfiguration` for the table:
145+
```csharp
146+
public class BuildingConfiguration : IEntityTypeConfiguration<Building>
147+
{
148+
public void Configure(EntityTypeBuilder<Building> builder)
149+
{
150+
// Create a computed column in EF (like "address_natural_sort_key")
151+
builder
152+
.Property(x => x.AddressNaturalSortKey)
153+
.HasNaturalSortKey("address"); // Points to the column storing your original address
154+
}
155+
}
156+
```
157+
When you query the entity, simply `ORDER BY AddressNaturalSortKey` to get truenaturalordering in PostgreSQL.
179158

180159
## License
181160

src/EFCore.PostgresExtensions/EFCore.PostgresExtensions.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
<PackageReadmeFile>Readme.md</PackageReadmeFile>
99
<Authors>Pandatech</Authors>
1010
<Copyright>MIT</Copyright>
11-
<Version>4.0.2</Version>
11+
<Version>5.0.0</Version>
1212
<PackageId>Pandatech.EFCore.PostgresExtensions</PackageId>
1313
<Title>Pandatech.EFCore.PostgresExtensions</Title>
1414
<PackageTags>Pandatech, library, EntityFrameworkCore, PostgreSQL, For Update, Lock, LockingSyntax, Bulk insert, BinaryCopy</PackageTags>
1515
<Description>The Pandatech.EFCore.PostgresExtensions library enriches Entity Framework Core applications with advanced PostgreSQL functionalities, starting with the ForUpdate locking syntax and BulkInsert function. Designed for seamless integration, this NuGet package aims to enhance the efficiency and capabilities of EF Core models when working with PostgreSQL, with the potential for further PostgreSQL-specific extensions.</Description>
1616
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-efcore-postgres-extensions</RepositoryUrl>
17-
<PackageReleaseNotes>Nuget updates</PackageReleaseNotes>
17+
<PackageReleaseNotes>Removed bulk copy totally. Added Natural Sort functionality</PackageReleaseNotes>
1818
</PropertyGroup>
1919

2020
<ItemGroup>

src/EFCore.PostgresExtensions/Extensions/BulkInsertExtension/BulkInsertExtensionSync.cs

Lines changed: 0 additions & 214 deletions
This file was deleted.

src/EFCore.PostgresExtensions/EntityTypeConfigurationExtensions.cs renamed to src/EFCore.PostgresExtensions/Extensions/EntityTypeConfigurationExtensions.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,27 @@
22
using Microsoft.EntityFrameworkCore;
33
using Microsoft.EntityFrameworkCore.Metadata.Builders;
44

5-
namespace EFCore.PostgresExtensions;
5+
namespace EFCore.PostgresExtensions.Extensions;
66

77
public static class EntityTypeConfigurationExtensions
88
{
99
public static PropertyBuilder<TProperty> HasRandomIdSequence<TProperty>(
1010
this PropertyBuilder<TProperty> propertyBuilder)
1111
{
1212
var tableName = propertyBuilder.Metadata.DeclaringType.GetTableName();
13-
var pgFunctionName = PgFunctionHelpers.GetPgFunctionName(tableName!);
13+
var pgFunctionName = PgFunctionHelpers.GetRandomIdFunctionName(tableName!);
1414
propertyBuilder.HasDefaultValueSql(pgFunctionName);
1515

1616

17+
return propertyBuilder;
18+
}
19+
20+
public static PropertyBuilder<TProperty> HasNaturalSortKey<TProperty>(this PropertyBuilder<TProperty> propertyBuilder,
21+
string originalPropName)
22+
{
23+
propertyBuilder.HasComputedColumnSql($"get_natural_sort_key({originalPropName})::text", true);
24+
25+
1726
return propertyBuilder;
1827
}
1928
}

src/EFCore.PostgresExtensions/Extensions/MigrationBuilderExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ public static void CreateRandomIdSequence(this MigrationBuilder migrationBuilder
1212
int minRandIncrementValue,
1313
int maxRandIncrementValue)
1414
{
15-
migrationBuilder.Sql(PgFunctionHelpers.GetPgFunction(tableName,
15+
migrationBuilder.Sql(PgFunctionHelpers.GetRandomIdFunctionSql(tableName,
1616
pkName,
1717
startValue,
1818
minRandIncrementValue,
1919
maxRandIncrementValue));
2020
}
21+
22+
public static void CreateNaturalSortKeyFunction(this MigrationBuilder migrationBuilder)
23+
{
24+
migrationBuilder.Sql(PgFunctionHelpers.GetNaturalSortKeyFunction());
25+
}
2126
}

0 commit comments

Comments
 (0)