-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
137 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters