Skip to content

Commit

Permalink
refactor: split serializer contexts to avoid rooting RestTransport (a…
Browse files Browse the repository at this point in the history
…nd its deps) and negatively impacting AOT size when it's not referenced
  • Loading branch information
neon-sunset committed Jun 5, 2024
1 parent 0fd32b6 commit e460772
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
14 changes: 7 additions & 7 deletions src/PineconeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public PineconeClient(string apiKey, HttpClient client)
public async Task<IndexDetails[]> ListIndexes(CancellationToken ct = default)
{
var listIndexesResult = await Http
.GetFromJsonAsync("/indexes", SerializerContext.Default.ListIndexesResult, ct)
.GetFromJsonAsync("/indexes", ClientContext.Default.ListIndexesResult, ct)
.ConfigureAwait(false);

return listIndexesResult?.Indexes ?? [];
Expand Down Expand Up @@ -118,7 +118,7 @@ public Task CreateServerlessIndex(string name, uint dimension, Metric metric, st
private async Task CreateIndexAsync(CreateIndexRequest request, CancellationToken ct = default)
{
var response = await Http
.PostAsJsonAsync("/indexes", request, SerializerContext.Default.CreateIndexRequest, ct)
.PostAsJsonAsync("/indexes", request, ClientContext.Default.CreateIndexRequest, ct)
.ConfigureAwait(false);

await response.CheckStatusCode(ct).ConfigureAwait(false);
Expand Down Expand Up @@ -150,7 +150,7 @@ public async Task<Index<TTransport>> GetIndex<
where TTransport : ITransport<TTransport>
{
var response = await Http
.GetFromJsonAsync($"/indexes/{UrlEncoder.Default.Encode(name)}", SerializerContext.Default.IndexDetails, ct)
.GetFromJsonAsync($"/indexes/{UrlEncoder.Default.Encode(name)}", ClientContext.Default.IndexDetails, ct)
.ConfigureAwait(false) ?? throw new HttpRequestException("GetIndex request has failed.");

// TODO: Host is optional according to the API spec: https://docs.pinecone.io/reference/api/control-plane/describe_index
Expand Down Expand Up @@ -196,7 +196,7 @@ public async Task ConfigureIndex(string name, int? replicas = null, string? podT
.PatchAsJsonAsync(
$"/indexes/{UrlEncoder.Default.Encode(name)}",
request,
SerializerContext.Default.ConfigureIndexRequest,
ClientContext.Default.ConfigureIndexRequest,
ct)
.ConfigureAwait(false);

Expand All @@ -221,7 +221,7 @@ public async Task DeleteIndex(string name, CancellationToken ct = default) =>
public async Task<CollectionDetails[]> ListCollections(CancellationToken ct = default)
{
var listCollectionsResult = await Http
.GetFromJsonAsync("/collections", SerializerContext.Default.ListCollectionsResult, ct)
.GetFromJsonAsync("/collections", ClientContext.Default.ListCollectionsResult, ct)
.ConfigureAwait(false);

return listCollectionsResult?.Collections ?? [];
Expand All @@ -237,7 +237,7 @@ public async Task CreateCollection(string name, string source, CancellationToken
{
var request = new CreateCollectionRequest { Name = name, Source = source };
var response = await Http
.PostAsJsonAsync("/collections", request, SerializerContext.Default.CreateCollectionRequest, ct)
.PostAsJsonAsync("/collections", request, ClientContext.Default.CreateCollectionRequest, ct)
.ConfigureAwait(false);

await response.CheckStatusCode(ct).ConfigureAwait(false);
Expand All @@ -254,7 +254,7 @@ public async Task<CollectionDetails> DescribeCollection(string name, Cancellatio
return await Http
.GetFromJsonAsync(
$"/collections/{UrlEncoder.Default.Encode(name)}",
SerializerContext.Default.CollectionDetails,
ClientContext.Default.CollectionDetails,
ct)
.ConfigureAwait(false) ?? ThrowHelpers.JsonException<CollectionDetails>();
}
Expand Down
24 changes: 12 additions & 12 deletions src/Rest/SerializerContext.cs → src/Rest/Contexts.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
using System.Text.Json.Serialization;
using Pinecone.Grpc;

namespace Pinecone.Rest;

[JsonSerializable(typeof(string[]))]
[JsonSerializable(typeof(Metric))]
[JsonSerializable(typeof(IndexStats))]
[JsonSerializable(typeof(IndexState))]
[JsonSerializable(typeof(IndexStatus))]
[JsonSerializable(typeof(IndexDetails))]
[JsonSerializable(typeof(Index<GrpcTransport>))]
[JsonSerializable(typeof(Index<RestTransport>))]
[JsonSerializable(typeof(MetadataValue[]))]
[JsonSerializable(typeof(CreateIndexRequest))]
[JsonSerializable(typeof(ConfigureIndexRequest))]
[JsonSerializable(typeof(DescribeStatsRequest))]
[JsonSerializable(typeof(CreateCollectionRequest))]
[JsonSerializable(typeof(CollectionDetails))]
[JsonSerializable(typeof(ListIndexesResult))]
[JsonSerializable(typeof(ListCollectionsResult))]
[JsonSourceGenerationOptions(
GenerationMode = JsonSourceGenerationMode.Default,
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
internal sealed partial class ClientContext : JsonSerializerContext;

[JsonSerializable(typeof(DescribeStatsRequest))]
[JsonSerializable(typeof(IndexStats))]
[JsonSerializable(typeof(QueryRequest))]
[JsonSerializable(typeof(QueryResponse))]
[JsonSerializable(typeof(UpsertRequest))]
[JsonSerializable(typeof(UpsertResponse))]
[JsonSerializable(typeof(UpdateRequest))]
[JsonSerializable(typeof(FetchResponse))]
[JsonSerializable(typeof(DeleteRequest))]
[JsonSerializable(typeof(MetadataMap))]
[JsonSerializable(typeof(MetadataValue[]))]
[JsonSerializable(typeof(ListIndexesResult))]
[JsonSerializable(typeof(ListCollectionsResult))]
[JsonSourceGenerationOptions(
GenerationMode = JsonSourceGenerationMode.Default,
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
internal sealed partial class SerializerContext : JsonSerializerContext { }
internal sealed partial class RestTransportContext : JsonSerializerContext;
4 changes: 2 additions & 2 deletions src/Rest/Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ private static MetadataValue ReadValue(ref Utf8JsonReader reader)

private static MetadataValue[] ReadArray(ref Utf8JsonReader reader)
{
return JsonSerializer.Deserialize(ref reader, SerializerContext.Default.MetadataValueArray)
return JsonSerializer.Deserialize(ref reader, ClientContext.Default.MetadataValueArray)
?? ThrowHelpers.JsonException<MetadataValue[]>();
}

private static MetadataMap ReadMap(ref Utf8JsonReader reader)
{
return JsonSerializer.Deserialize(ref reader, SerializerContext.Default.MetadataMap)
return JsonSerializer.Deserialize(ref reader, ClientContext.Default.MetadataMap)
?? ThrowHelpers.JsonException<MetadataMap>();
}

Expand Down
20 changes: 10 additions & 10 deletions src/Rest/RestTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public async Task<IndexStats> DescribeStats(MetadataMap? filter = null, Cancella
{
var request = new DescribeStatsRequest { Filter = filter };
var response = await Http
.PostAsJsonAsync("/describe_index_stats", request, SerializerContext.Default.DescribeStatsRequest, ct)
.PostAsJsonAsync("/describe_index_stats", request, RestTransportContext.Default.DescribeStatsRequest, ct)
.ConfigureAwait(false);

await response.CheckStatusCode(ct).ConfigureAwait(false);
return await response.Content
.ReadFromJsonAsync(SerializerContext.Default.IndexStats, ct)
.ReadFromJsonAsync(RestTransportContext.Default.IndexStats, ct)
.ConfigureAwait(false) ?? ThrowHelpers.JsonException<IndexStats>();
}

Expand Down Expand Up @@ -64,12 +64,12 @@ public async Task<ScoredVector[]> Query(
};

var response = await Http
.PostAsJsonAsync("/query", request, SerializerContext.Default.QueryRequest, ct)
.PostAsJsonAsync("/query", request, RestTransportContext.Default.QueryRequest, ct)
.ConfigureAwait(false);

await response.CheckStatusCode(ct).ConfigureAwait(false);
return (await response.Content
.ReadFromJsonAsync(SerializerContext.Default.QueryResponse, ct)
.ReadFromJsonAsync(RestTransportContext.Default.QueryResponse, ct)
.ConfigureAwait(false))
.Matches ?? ThrowHelpers.JsonException<ScoredVector[]>();
}
Expand All @@ -83,12 +83,12 @@ public async Task<uint> Upsert(IEnumerable<Vector> vectors, string? indexNamespa
};

var response = await Http
.PostAsJsonAsync("/vectors/upsert", request, SerializerContext.Default.UpsertRequest, ct)
.PostAsJsonAsync("/vectors/upsert", request, RestTransportContext.Default.UpsertRequest, ct)
.ConfigureAwait(false);

await response.CheckStatusCode(ct).ConfigureAwait(false);
return (await response.Content
.ReadFromJsonAsync(SerializerContext.Default.UpsertResponse, ct)
.ReadFromJsonAsync(RestTransportContext.Default.UpsertResponse, ct)
.ConfigureAwait(false)).UpsertedCount;
}

Expand All @@ -98,7 +98,7 @@ public async Task Update(Vector vector, string? indexNamespace = null, Cancellat
Debug.Assert(request.Metadata is null);

var response = await Http
.PostAsJsonAsync("/vectors/update", request, SerializerContext.Default.UpdateRequest, ct)
.PostAsJsonAsync("/vectors/update", request, RestTransportContext.Default.UpdateRequest, ct)
.ConfigureAwait(false);
await response.CheckStatusCode(ct).ConfigureAwait(false);
}
Expand Down Expand Up @@ -127,7 +127,7 @@ public async Task Update(
};

var response = await Http
.PostAsJsonAsync("/vectors/update", request, SerializerContext.Default.UpdateRequest, ct)
.PostAsJsonAsync("/vectors/update", request, RestTransportContext.Default.UpdateRequest, ct)
.ConfigureAwait(false);
await response.CheckStatusCode(ct).ConfigureAwait(false);
}
Expand All @@ -151,7 +151,7 @@ public async Task<Dictionary<string, Vector>> Fetch(
}

return (await Http
.GetFromJsonAsync(addressBuilder.ToString(), SerializerContext.Default.FetchResponse, ct)
.GetFromJsonAsync(addressBuilder.ToString(), RestTransportContext.Default.FetchResponse, ct)
.ConfigureAwait(false)).Vectors;
}

Expand All @@ -177,7 +177,7 @@ public Task DeleteAll(string? indexNamespace = null, CancellationToken ct = defa
private async Task Delete(DeleteRequest request, CancellationToken ct)
{
var response = await Http
.PostAsJsonAsync("/vectors/delete", request, SerializerContext.Default.DeleteRequest, ct)
.PostAsJsonAsync("/vectors/delete", request, RestTransportContext.Default.DeleteRequest, ct)
.ConfigureAwait(false);
await response.CheckStatusCode(ct).ConfigureAwait(false);
}
Expand Down

0 comments on commit e460772

Please sign in to comment.