Skip to content

Commit

Permalink
Port to Npgsql 8.0.0-rc.2 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
roji authored Oct 12, 2023
1 parent 3a9bad2 commit 6903d8f
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 156 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v3
- uses: ankane/setup-postgres@v1
with:
database: pgvector_dotnet_test
Expand Down
7 changes: 7 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sdk": {
"version": "8.0.100-rc.2.23502.2",
"rollForward": "latestMajor",
"allowPrerelease": "true"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageProjectUrl>https://github.com/pgvector/pgvector-dotnet</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>

<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
Expand All @@ -19,7 +19,7 @@
<None Include="..\..\LICENSE.txt" Pack="true" PackagePath="\" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
<ProjectReference Include="..\Pgvector\Pgvector.csproj" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0-rc.2" />
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions src/Pgvector.EntityFrameworkCore/VectorTypeMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Pgvector.EntityFrameworkCore;

public class VectorTypeMapping : NpgsqlTypeMapping
public class VectorTypeMapping : RelationalTypeMapping
{
public VectorTypeMapping(string storeType) : base(storeType, typeof(Vector), NpgsqlDbType.Unknown) { }
public VectorTypeMapping(string storeType) : base(storeType, typeof(Vector)) { }

protected VectorTypeMapping(RelationalTypeMappingParameters parameters) : base(parameters, NpgsqlDbType.Unknown) { }
protected VectorTypeMapping(RelationalTypeMappingParameters parameters) : base(parameters) { }

protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> new VectorTypeMapping(parameters);
Expand Down
91 changes: 91 additions & 0 deletions src/Pgvector/Npgsql/VectorConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Npgsql.Internal;

namespace Pgvector.Npgsql;

public class VectorConverter : PgStreamingConverter<Vector>
{
public override Vector Read(PgReader reader)
{
if (reader.ShouldBuffer(2 * sizeof(ushort)))
reader.Buffer(2 * sizeof(ushort));

var dim = reader.ReadUInt16();
var unused = reader.ReadUInt16();
if (unused != 0)
throw new InvalidCastException("expected unused to be 0");

var vec = new float[dim];
for (var i = 0; i < dim; i++)
{
if (reader.ShouldBuffer(sizeof(float)))
reader.Buffer(sizeof(float));
vec[i] = reader.ReadFloat();
}

return new Vector(vec);
}

public override async ValueTask<Vector> ReadAsync(PgReader reader, CancellationToken cancellationToken = default)
{
if (reader.ShouldBuffer(2 * sizeof(ushort)))
await reader.BufferAsync(2 * sizeof(ushort), cancellationToken).ConfigureAwait(false);

var dim = reader.ReadUInt16();
var unused = reader.ReadUInt16();
if (unused != 0)
throw new InvalidCastException("expected unused to be 0");

var vec = new float[dim];
for (var i = 0; i < dim; i++)
{
if (reader.ShouldBuffer(sizeof(float)))
await reader.BufferAsync(sizeof(float), cancellationToken).ConfigureAwait(false);
vec[i] = reader.ReadFloat();
}

return new Vector(vec);
}

public override Size GetSize(SizeContext context, Vector value, ref object? writeState)
=> sizeof(ushort) * 2 + sizeof(float) * value.ToArray().Length;

public override void Write(PgWriter writer, Vector value)
{
if (writer.ShouldFlush(sizeof(ushort) * 2))
writer.Flush();

var vec = value.ToArray();
var dim = vec.Length;
writer.WriteUInt16(Convert.ToUInt16(dim));
writer.WriteUInt16(0);

for (int i = 0; i < dim; i++)
{
if (writer.ShouldFlush(sizeof(float)))
writer.Flush();
writer.WriteFloat(vec[i]);
}
}

public override async ValueTask WriteAsync(
PgWriter writer, Vector value, CancellationToken cancellationToken = default)
{
if (writer.ShouldFlush(sizeof(ushort) * 2))
await writer.FlushAsync(cancellationToken);

var vec = value.ToArray();
var dim = vec.Length;
writer.WriteUInt16(Convert.ToUInt16(dim));
writer.WriteUInt16(0);

for (int i = 0; i < dim; i++)
{
if (writer.ShouldFlush(sizeof(float)))
await writer.FlushAsync(cancellationToken);
writer.WriteFloat(vec[i]);
}
}
}
2 changes: 1 addition & 1 deletion src/Pgvector/Npgsql/VectorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public static class VectorExtensions
{
public static INpgsqlTypeMapper UseVector(this INpgsqlTypeMapper mapper)
{
mapper.AddTypeResolverFactory(new VectorTypeHandlerResolverFactory());
mapper.AddTypeInfoResolver(new VectorTypeInfoResolver());
return mapper;
}
}
77 changes: 0 additions & 77 deletions src/Pgvector/Npgsql/VectorHandler.cs

This file was deleted.

52 changes: 0 additions & 52 deletions src/Pgvector/Npgsql/VectorTypeHandlerResolver.cs

This file was deleted.

17 changes: 0 additions & 17 deletions src/Pgvector/Npgsql/VectorTypeHandlerResolverFactory.cs

This file was deleted.

28 changes: 28 additions & 0 deletions src/Pgvector/Npgsql/VectorTypeInfoResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using Npgsql.Internal;
using Npgsql.Internal.Postgres;

namespace Pgvector.Npgsql;

public class VectorTypeInfoResolver : IPgTypeInfoResolver
{
TypeInfoMappingCollection Mappings { get; }

public VectorTypeInfoResolver()
{
Mappings = new TypeInfoMappingCollection();
AddInfos(Mappings);
// TODO: Opt-in only
AddArrayInfos(Mappings);
}

public PgTypeInfo? GetTypeInfo(Type? type, DataTypeName? dataTypeName, PgSerializerOptions options)
=> Mappings.Find(type, dataTypeName, options);

static void AddInfos(TypeInfoMappingCollection mappings)
=> mappings.AddType<Vector>("vector",
static (options, mapping, _) => mapping.CreateInfo(options, new VectorConverter()), isDefault: true);

static void AddArrayInfos(TypeInfoMappingCollection mappings)
=> mappings.AddArrayType<Vector>("vector");
}
2 changes: 1 addition & 1 deletion src/Pgvector/Pgvector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<ItemGroup>
<None Include="..\..\LICENSE.txt" Pack="true" PackagePath="\" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
<PackageReference Include="Npgsql" Version="7" />
<PackageReference Include="Npgsql" Version="8.0.0-rc.2" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions tests/Pgvector.Tests/EntityFrameworkCoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ItemContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connString = "Host=localhost;Database=pgvector_dotnet_test";
optionsBuilder.UseNpgsql(connString, o => o.UseVector()).UseSnakeCaseNamingConvention();
optionsBuilder.UseNpgsql(connString, o => o.UseVector());
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand All @@ -30,7 +30,7 @@ public class Item
{
public int Id { get; set; }

[Column(TypeName = "vector(3)")]
[Column("embedding", TypeName = "vector(3)")]
public Vector? Embedding { get; set; }
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Pgvector.Tests/Pgvector.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down

0 comments on commit 6903d8f

Please sign in to comment.