Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for netstandard2.0 #117

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Compatibility/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace System.Linq
{
#if NETSTANDARD2_0
public static class ExtensionMethods
{
public static void Deconstruct<T1, T2>(this KeyValuePair<T1, T2> tuple, out T1 key, out T2 value)
{
key = tuple.Key;
value = tuple.Value;
}
}
#endif
}
35 changes: 35 additions & 0 deletions src/Compatibility/RuntimeHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace System.Runtime.CompilerServices
{
#if NETSTANDARD2_0
internal static class RuntimeHelpers
{
public static T[] GetSubArray<T>(T[] array, Range range)
{
if (array == null)
{
throw new ArgumentNullException();
}

(int offset, int length) = range.GetOffsetAndLength(array.Length);

if (default(T)! != null || typeof(T[]) == array.GetType())
{
if (length == 0)
{
return Array.Empty<T>();
}

var dest = new T[length];
Array.Copy(array, offset, dest, 0, length);
return dest;
}
else
{
T[] dest = (T[])Array.CreateInstance(array.GetType().GetElementType()!, length);
Array.Copy(array, offset, dest, 0, length);
return dest;
}
}
}
#endif
}
9 changes: 9 additions & 0 deletions src/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@ internal static class Extensions
internal static ValueTask CheckStatusCode(
this HttpResponseMessage response, CancellationToken ct, [CallerMemberName] string requestName = "")
{
#if NETSTANDARD2_0
return response.IsSuccessStatusCode ? default : ThrowOnFailedResponse(response, requestName, ct);
#else
return response.IsSuccessStatusCode ? ValueTask.CompletedTask : ThrowOnFailedResponse(response, requestName, ct);
#endif

[DoesNotReturn, StackTraceHidden]
static async ValueTask ThrowOnFailedResponse(
HttpResponseMessage response, string requestName, CancellationToken ct)
{

throw new HttpRequestException($"{requestName} request has failed. " +
#if NETSTANDARD2_0
$"Code: {response.StatusCode}. Message: {await response.Content.ReadAsStringAsync().ConfigureAwait(false)}");
#else
$"Code: {response.StatusCode}. Message: {await response.Content.ReadAsStringAsync(ct).ConfigureAwait(false)}");
#endif
}
}
}
2 changes: 1 addition & 1 deletion src/ITransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface ITransport<
{
#if NET7_0_OR_GREATER
static abstract T Create(string host, string apiKey);
#else
#elif !NETSTANDARD2_0
static T Create(string host, string apiKey)
{
if (typeof(T) == typeof(GrpcTransport))
Expand Down
8 changes: 8 additions & 0 deletions src/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public Task<uint> Upsert(
string? indexNamespace = null,
CancellationToken ct = default)
{
#if !NETSTANDARD2_0
maumar marked this conversation as resolved.
Show resolved Hide resolved
const int batchSize = 100;
const int parallelism = 20;
const int threshold = 400;
Expand All @@ -156,10 +157,12 @@ public Task<uint> Upsert(
{
return Upsert(vectors, batchSize, parallelism, indexNamespace, ct);
}
#endif

return Transport.Upsert(vectors, indexNamespace, ct);
}

#if !NETSTANDARD2_0
/// <summary>
/// Writes vectors into the index as batches in parallel. If a new value is provided for an existing vector ID, it will overwrite the previous value.
/// </summary>
Expand Down Expand Up @@ -200,6 +203,7 @@ await Parallel.ForEachAsync(batches, options, async (batch, ct) =>

return upserted;
}
#endif

/// <summary>
/// Updates a vector using the <see cref="Vector"/> object.
Expand Down Expand Up @@ -242,6 +246,7 @@ public Task Update(
/// <returns>A dictionary containing vector IDs and the corresponding <see cref="Vector"/> objects containing the vector information.</returns>
public Task<Dictionary<string, Vector>> Fetch(IEnumerable<string> ids, string? indexNamespace = null, CancellationToken ct = default)
{
#if !NETSTANDARD2_0
const int batchSize = 200;
const int parallelism = 20;
const int threshold = 600;
Expand All @@ -250,10 +255,12 @@ public Task<Dictionary<string, Vector>> Fetch(IEnumerable<string> ids, string? i
{
return Fetch(ids, batchSize, parallelism, indexNamespace, ct);
}
#endif

return Transport.Fetch(ids, indexNamespace, ct);
}

#if !NETSTANDARD2_0
/// <summary>
/// Looks up and returns vectors by ID as batches in parallel.
/// </summary>
Expand Down Expand Up @@ -292,6 +299,7 @@ await Parallel.ForEachAsync(batches, options, async (batch, ct) =>

return new(fetched.SelectMany(batch => batch));
}
#endif

/// <summary>
/// Deletes vectors with specified ids.
Expand Down
8 changes: 4 additions & 4 deletions src/Pinecone.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageId>Pinecone.NET</PackageId>
Expand All @@ -13,7 +13,7 @@ In the absence of an official SDK, it provides first-class support for Pinecone
</PropertyGroup>

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0;netstandard2.0</TargetFrameworks>
<WarningsAsErrors>nullable</WarningsAsErrors>
<IsAotCompatible>true</IsAotCompatible>
<ImplicitUsings>enable</ImplicitUsings>
Expand All @@ -23,11 +23,11 @@ In the absence of an official SDK, it provides first-class support for Pinecone
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0' Or '$(TargetFramework)' == 'netstandard2.0'">
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0' Or '$(TargetFramework)' == 'net6.0'">
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0' Or '$(TargetFramework)' == 'net6.0' Or '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.3" />
<PackageReference Include="PolySharp" Version="1.14.1">
Expand Down
24 changes: 23 additions & 1 deletion src/PineconeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,36 @@ public async Task<Index<TTransport>> GetIndex<
Status = response.Status,
#if NET7_0_OR_GREATER
Transport = TTransport.Create(host, apiKey)
#else
#elif !NETSTANDARD2_0
Transport = ITransport<TTransport>.Create(host, apiKey)
#else
Transport = Create<TTransport>(host, apiKey)
#endif
};

return index;
}

#if !NET7_0_OR_GREATER
private static T Create<T>(string host, string apiKey)
{
if (typeof(T) == typeof(GrpcTransport))
{
return (T)(object)new GrpcTransport(host, apiKey);
}
else if (typeof(T) == typeof(RestTransport))
{
return (T)(object)new RestTransport(host, apiKey);
}
else
{
var instance = (T?)Activator.CreateInstance(typeof(T), host, apiKey);

return instance ?? throw new InvalidOperationException($"Unable to create instance of {typeof(T)}");
}
}
#endif

/// <summary>
/// Specifies the pod type and number of replicas for an index. It applies to pod-based indexes only. Serverless indexes scale automatically based on usage.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions src/Rest/Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ public override IndexNamespace[] Read(ref Utf8JsonReader reader, Type typeToConv
ThrowHelper.ThrowFormatException("Expected number value");
}

#if !NETSTANDARD2_0
buffer.Add(new() { Name = Encoding.UTF8.GetString(nameSpan), VectorCount = reader.GetUInt32() });
#else
buffer.Add(new() { Name = Encoding.UTF8.GetString(nameSpan.ToArray()), VectorCount = reader.GetUInt32() });
#endif
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/Types/VectorTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ public MetadataMap() : base() { }
/// Creates a new instance of the <see cref="MetadataMap" /> class from an existing collection.
/// </summary>
/// <param name="collection"></param>
public MetadataMap(IEnumerable<KeyValuePair<string, MetadataValue>> collection) : base(collection) { }
public MetadataMap(IEnumerable<KeyValuePair<string, MetadataValue>> collection)
#if NETSTANDARD2_0
: base(collection.ToDictionary(e => e.Key, e => e.Value))
#else
: base(collection)
#endif
{ }
}

/// <summary>
Expand Down
Loading