diff options
author | vnugent <public@vaughnnugent.com> | 2023-11-02 01:50:05 -0400 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2023-11-02 01:50:05 -0400 |
commit | d2d812213b99ee17f9433f81871b694c4053ff23 (patch) | |
tree | 11a1106602112c134e65bf197ef701d1b8d63b67 /lib/VNLib.Data.Caching/src/ClientExtensions.cs | |
parent | 483c014b938e2d55ea7c89b67f6d19ba2c2d5b5e (diff) |
also carried away
Diffstat (limited to 'lib/VNLib.Data.Caching/src/ClientExtensions.cs')
-rw-r--r-- | lib/VNLib.Data.Caching/src/ClientExtensions.cs | 297 |
1 files changed, 168 insertions, 129 deletions
diff --git a/lib/VNLib.Data.Caching/src/ClientExtensions.cs b/lib/VNLib.Data.Caching/src/ClientExtensions.cs index 946c9b5..a2ec27d 100644 --- a/lib/VNLib.Data.Caching/src/ClientExtensions.cs +++ b/lib/VNLib.Data.Caching/src/ClientExtensions.cs @@ -24,7 +24,6 @@ using System; using System.Linq; -using System.Buffers; using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -52,26 +51,7 @@ namespace VNLib.Data.Caching private static void LogDebug(this FBMClient client, string message, params object?[] args) { client.Config.DebugLog?.Debug($"[CACHE] : {message}", args); - } - - /// <summary> - /// Gets an object from the server if it exists, and uses the default serialzer to - /// recover the object - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="client"></param> - /// <param name="objectId">The id of the object to get</param> - /// <param name="cancellationToken">A token to cancel the operation</param> - /// <returns>A task that completes to return the results of the response payload</returns> - /// <exception cref="JsonException"></exception> - /// <exception cref="OutOfMemoryException"></exception> - /// <exception cref="InvalidStatusException"></exception> - /// <exception cref="ObjectDisposedException"></exception> - /// <exception cref="InvalidResponseException"></exception> - public static Task<T?> GetObjectAsync<T>(this FBMClient client, string objectId, CancellationToken cancellationToken = default) - { - return GetObjectAsync<T>(client, objectId, DefaultSerializer, cancellationToken); - } + } /// <summary> /// Updates the state of the object, and optionally updates the ID of the object. The data @@ -95,37 +75,58 @@ namespace VNLib.Data.Caching { //Use the default/json serialzer if not specified return AddOrUpdateObjectAsync(client, objectId, newId, data, DefaultSerializer, cancellationToken); - } + } /// <summary> - /// Gets an object from the server if it exists + /// Updates the state of the object, and optionally updates the ID of the object. The data + /// parameter is serialized, buffered, and streamed to the remote server /// </summary> /// <typeparam name="T"></typeparam> /// <param name="client"></param> - /// <param name="objectId">The id of the object to get</param> + /// <param name="objectId">The id of the object to update or replace</param> + /// <param name="newId">An optional parameter to specify a new ID for the old object</param> + /// <param name="data">The payload data to serialize and set as the data state of the session</param> + /// <param name="serializer">The custom serializer to used to serialze the object to binary</param> /// <param name="cancellationToken">A token to cancel the operation</param> - /// <param name="deserialzer">The custom data deserialzer used to deserialze the binary cache result</param> - /// <returns>A task that completes to return the results of the response payload</returns> + /// <returns>A task that resolves when the server responds</returns> + /// <exception cref="OutOfMemoryException"></exception> /// <exception cref="InvalidStatusException"></exception> /// <exception cref="ObjectDisposedException"></exception> /// <exception cref="InvalidResponseException"></exception> - public static async Task<T?> GetObjectAsync<T>(this FBMClient client, string objectId, ICacheObjectDeserialzer deserialzer, CancellationToken cancellationToken = default) + /// <exception cref="MessageTooLargeException"></exception> + /// <exception cref="ObjectNotFoundException"></exception> + public static async Task AddOrUpdateObjectAsync<T>( + this FBMClient client, + string objectId, + string? newId, + T data, + ICacheObjectSerializer serializer, + CancellationToken cancellationToken = default) { _ = client ?? throw new ArgumentNullException(nameof(client)); - _ = deserialzer ?? throw new ArgumentNullException(nameof(deserialzer)); + _ = serializer ?? throw new ArgumentNullException(nameof(serializer)); - client.LogDebug("Getting object {id}", objectId); + client.LogDebug("Updating object {id}, newid {nid}", objectId, newId); //Rent a new request FBMRequest request = client.RentRequest(); try { //Set action as get/create - request.WriteHeader(HeaderCommand.Action, Actions.Get); + request.WriteHeader(HeaderCommand.Action, Actions.AddOrUpdate); - //Set object id header + //Set object-id header request.WriteHeader(Constants.ObjectId, objectId); + //if new-id set, set the new-id header + if (!string.IsNullOrWhiteSpace(newId)) + { + request.WriteHeader(Constants.NewObjectId, newId); + } + + //Serialize the message using the request buffer + serializer.Serialize(data, request.GetBodyWriter()); + //Make request using FBMResponse response = await client.SendAsync(request, cancellationToken); response.ThrowIfNotSet(); @@ -133,22 +134,22 @@ namespace VNLib.Data.Caching //Get the status code FBMMessageHeader status = response.Headers.FirstOrDefault(static a => a.Header == HeaderCommand.Status); - //Check ok status code, then its safe to deserialize - if (status.Value.Equals(ResponseCodes.Okay, StringComparison.Ordinal)) + //Check status code + if (status.Value.Equals(ResponseCodes.Okay, StringComparison.OrdinalIgnoreCase)) { - return deserialzer.Deserialze<T>(response.ResponseBody); + return; } - - //Object may not exist on the server yet - if (status.Value.Equals(ResponseCodes.NotFound, StringComparison.Ordinal)) + else if (status.Value.Equals(ResponseCodes.NotFound, StringComparison.OrdinalIgnoreCase)) { - return default; + throw new ObjectNotFoundException($"object {objectId} not found on remote server"); } - throw new InvalidStatusException("Invalid status code recived for object get request", status.ToString()); + //Invalid status + throw new InvalidStatusException("Invalid status code recived for object upsert request", status.ToString()); } finally { + //Return the request(clears data and reset) client.ReturnRequest(request); } } @@ -157,12 +158,10 @@ namespace VNLib.Data.Caching /// Updates the state of the object, and optionally updates the ID of the object. The data /// parameter is serialized, buffered, and streamed to the remote server /// </summary> - /// <typeparam name="T"></typeparam> /// <param name="client"></param> /// <param name="objectId">The id of the object to update or replace</param> /// <param name="newId">An optional parameter to specify a new ID for the old object</param> - /// <param name="data">The payload data to serialize and set as the data state of the session</param> - /// <param name="serializer">The custom serializer to used to serialze the object to binary</param> + /// <param name="data">An <see cref="IObjectData"/> that represents the data to set</param> /// <param name="cancellationToken">A token to cancel the operation</param> /// <returns>A task that resolves when the server responds</returns> /// <exception cref="OutOfMemoryException"></exception> @@ -171,16 +170,32 @@ namespace VNLib.Data.Caching /// <exception cref="InvalidResponseException"></exception> /// <exception cref="MessageTooLargeException"></exception> /// <exception cref="ObjectNotFoundException"></exception> - public static async Task AddOrUpdateObjectAsync<T>( - this FBMClient client, - string objectId, - string? newId, - T data, - ICacheObjectSerialzer serializer, - CancellationToken cancellationToken = default) + public static Task AddOrUpdateObjectAsync(this FBMClient client, string objectId, string? newId, IObjectData data, CancellationToken cancellationToken = default) + { + return AddOrUpdateObjectAsync(client, objectId, newId, static d => d.GetData(), data, cancellationToken); + } + + /// <summary> + /// Updates the state of the object, and optionally updates the ID of the object. The data + /// parameter is serialized, buffered, and streamed to the remote server + /// </summary> + /// <param name="client"></param> + /// <param name="objectId">The id of the object to update or replace</param> + /// <param name="newId">An optional parameter to specify a new ID for the old object</param> + /// <param name="callback">A callback method that will return the desired object data</param> + /// <param name="cancellationToken">A token to cancel the operation</param> + /// <param name="state">The state to be passed to the callback</param> + /// <returns>A task that resolves when the server responds</returns> + /// <exception cref="OutOfMemoryException"></exception> + /// <exception cref="InvalidStatusException"></exception> + /// <exception cref="ObjectDisposedException"></exception> + /// <exception cref="InvalidResponseException"></exception> + /// <exception cref="MessageTooLargeException"></exception> + /// <exception cref="ObjectNotFoundException"></exception> + public async static Task AddOrUpdateObjectAsync<T>(this FBMClient client, string objectId, string? newId, ObjectDataReader<T> callback, T state, CancellationToken cancellationToken = default) { _ = client ?? throw new ArgumentNullException(nameof(client)); - _ = serializer ?? throw new ArgumentNullException(nameof(serializer)); + _ = callback ?? throw new ArgumentNullException(nameof(callback)); client.LogDebug("Updating object {id}, newid {nid}", objectId, newId); @@ -200,11 +215,8 @@ namespace VNLib.Data.Caching request.WriteHeader(Constants.NewObjectId, newId); } - //Get the body writer for the message - IBufferWriter<byte> bodyWriter = request.GetBodyWriter(); - - //Serialize the message - serializer.Serialize(data, bodyWriter); + //Write the message body as the objet data + request.WriteBody(callback(state)); //Make request using FBMResponse response = await client.SendAsync(request, cancellationToken); @@ -234,45 +246,70 @@ namespace VNLib.Data.Caching } /// <summary> - /// Asynchronously deletes an object in the remote store + /// Gets an object from the server if it exists, and uses the default serialzer to + /// recover the object /// </summary> + /// <typeparam name="T"></typeparam> /// <param name="client"></param> - /// <param name="objectId">The id of the object to update or replace</param> + /// <param name="objectId">The id of the object to get</param> /// <param name="cancellationToken">A token to cancel the operation</param> - /// <returns>A task that resolves when the operation has completed</returns> + /// <returns>A task that completes to return the results of the response payload</returns> + /// <exception cref="JsonException"></exception> + /// <exception cref="OutOfMemoryException"></exception> /// <exception cref="InvalidStatusException"></exception> /// <exception cref="ObjectDisposedException"></exception> /// <exception cref="InvalidResponseException"></exception> - /// <exception cref="ObjectNotFoundException"></exception> - public static async Task DeleteObjectAsync(this FBMClient client, string objectId, CancellationToken cancellationToken = default) + public static Task<T?> GetObjectAsync<T>(this FBMClient client, string objectId, CancellationToken cancellationToken = default) + { + return GetObjectAsync<T>(client, objectId, DefaultSerializer, cancellationToken); + } + + /// <summary> + /// Gets an object from the server if it exists + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="client"></param> + /// <param name="objectId">The id of the object to get</param> + /// <param name="cancellationToken">A token to cancel the operation</param> + /// <param name="deserialzer">The custom data deserialzer used to deserialze the binary cache result</param> + /// <returns>A task that completes to return the results of the response payload</returns> + /// <exception cref="InvalidStatusException"></exception> + /// <exception cref="ObjectDisposedException"></exception> + /// <exception cref="InvalidResponseException"></exception> + public static async Task<T?> GetObjectAsync<T>(this FBMClient client, string objectId, ICacheObjectDeserializer deserialzer, CancellationToken cancellationToken = default) { _ = client ?? throw new ArgumentNullException(nameof(client)); + _ = deserialzer ?? throw new ArgumentNullException(nameof(deserialzer)); + + client.LogDebug("Getting object {id}", objectId); - client.LogDebug("Deleting object {id}", objectId); - //Rent a new request FBMRequest request = client.RentRequest(); try { - //Set action as delete - request.WriteHeader(HeaderCommand.Action, Actions.Delete); - //Set session-id header + //Set action as get/create + request.WriteHeader(HeaderCommand.Action, Actions.Get); + + //Set object id header request.WriteHeader(Constants.ObjectId, objectId); //Make request using FBMResponse response = await client.SendAsync(request, cancellationToken); response.ThrowIfNotSet(); - + //Get the status code FBMMessageHeader status = response.Headers.FirstOrDefault(static a => a.Header == HeaderCommand.Status); - + + //Check ok status code, then its safe to deserialize if (status.Value.Equals(ResponseCodes.Okay, StringComparison.Ordinal)) { - return; + return deserialzer.Deserialize<T>(response.ResponseBody); } - else if(status.Value.Equals(ResponseCodes.NotFound, StringComparison.OrdinalIgnoreCase)) + + //Object may not exist on the server yet + if (status.Value.Equals(ResponseCodes.NotFound, StringComparison.Ordinal)) { - throw new ObjectNotFoundException($"object {objectId} not found on remote server"); + return default; } throw new InvalidStatusException("Invalid status code recived for object get request", status.ToString()); @@ -284,47 +321,54 @@ namespace VNLib.Data.Caching } /// <summary> - /// Updates the state of the object, and optionally updates the ID of the object. The data - /// parameter is serialized, buffered, and streamed to the remote server + /// Gets an object from the server if it exists. If data is retreived, it sets + /// the <see cref="IObjectData.SetData(ReadOnlySpan{byte})"/>, if no data is + /// found, this method returns and never calls SetData. /// </summary> /// <param name="client"></param> - /// <param name="objectId">The id of the object to update or replace</param> - /// <param name="newId">An optional parameter to specify a new ID for the old object</param> - /// <param name="data">An <see cref="IObjectData"/> that represents the data to set</param> + /// <param name="objectId">The id of the object to get</param> + /// <param name="data">An object data instance used to store the found object data</param> /// <param name="cancellationToken">A token to cancel the operation</param> - /// <returns>A task that resolves when the server responds</returns> - /// <exception cref="OutOfMemoryException"></exception> + /// <returns>A task that completes to return the results of the response payload</returns> /// <exception cref="InvalidStatusException"></exception> /// <exception cref="ObjectDisposedException"></exception> /// <exception cref="InvalidResponseException"></exception> - /// <exception cref="MessageTooLargeException"></exception> - /// <exception cref="ObjectNotFoundException"></exception> - public async static Task AddOrUpdateObjectAsync(this FBMClient client, string objectId, string? newId, IObjectData data, CancellationToken cancellationToken = default) + public static Task<bool> GetObjectAsync(this FBMClient client, string objectId, IObjectData data, CancellationToken cancellationToken = default) + { + return GetObjectAsync(client, objectId, static (p, d) => p.SetData(d), data, cancellationToken); + } + + /// <summary> + /// Gets an object from the server if it exists. If data is retreived, it sets + /// the <see cref="IObjectData.SetData(ReadOnlySpan{byte})"/>, if no data is + /// found, this method returns and never calls SetData. + /// </summary> + /// <param name="client"></param> + /// <param name="objectId">The id of the object to get</param> + /// <param name="setter">A callback method used to store the recovered object data</param> + /// <param name="state">The state parameter to pass to the callback method</param> + /// <param name="cancellationToken">A token to cancel the operation</param> + /// <returns>When complete, true if the object was found, false if not found, and an exception otherwise</returns> + /// <exception cref="InvalidStatusException"></exception> + /// <exception cref="ObjectDisposedException"></exception> + /// <exception cref="InvalidResponseException"></exception> + public static async Task<bool> GetObjectAsync<T>(this FBMClient client, string objectId, ObjectDataSet<T> setter, T state, CancellationToken cancellationToken = default) { _ = client ?? throw new ArgumentNullException(nameof(client)); - _ = data ?? throw new ArgumentNullException(nameof(data)); + _ = setter ?? throw new ArgumentNullException(nameof(setter)); - client.LogDebug("Updating object {id}, newid {nid}", objectId, newId); + client.LogDebug("Getting object {id}", objectId); //Rent a new request FBMRequest request = client.RentRequest(); try { //Set action as get/create - request.WriteHeader(HeaderCommand.Action, Actions.AddOrUpdate); + request.WriteHeader(HeaderCommand.Action, Actions.Get); - //Set session-id header + //Set object id header request.WriteHeader(Constants.ObjectId, objectId); - //if new-id set, set the new-id header - if (!string.IsNullOrWhiteSpace(newId)) - { - request.WriteHeader(Constants.NewObjectId, newId); - } - - //Write the message body as the objet data - request.WriteBody(data.GetData()); - //Make request using FBMResponse response = await client.SendAsync(request, cancellationToken); response.ThrowIfNotSet(); @@ -332,54 +376,52 @@ namespace VNLib.Data.Caching //Get the status code FBMMessageHeader status = response.Headers.FirstOrDefault(static a => a.Header == HeaderCommand.Status); - //Check status code - if (status.Value.Equals(ResponseCodes.Okay, StringComparison.OrdinalIgnoreCase)) + //Check ok status code, then its safe to deserialize + if (status.Value.Equals(ResponseCodes.Okay, StringComparison.Ordinal)) { - return; + //Write the object data + setter(state, response.ResponseBody); + return true; } - else if (status.Value.Equals(ResponseCodes.NotFound, StringComparison.OrdinalIgnoreCase)) + + //Object may not exist on the server yet + if (status.Value.Equals(ResponseCodes.NotFound, StringComparison.Ordinal)) { - throw new ObjectNotFoundException($"object {objectId} not found on remote server"); + return false; } - //Invalid status - throw new InvalidStatusException("Invalid status code recived for object upsert request", status.ToString()); + throw new InvalidStatusException("Invalid status code recived for object get request", status.ToString()); } finally { - //Return the request(clears data and reset) client.ReturnRequest(request); } } /// <summary> - /// Gets an object from the server if it exists. If data is retreived, it sets - /// the <see cref="IObjectData.SetData(ReadOnlySpan{byte})"/>, if no data is - /// found, this method returns and never calls SetData. + /// Asynchronously deletes an object in the remote store /// </summary> /// <param name="client"></param> - /// <param name="objectId">The id of the object to get</param> + /// <param name="objectId">The id of the object to update or replace</param> /// <param name="cancellationToken">A token to cancel the operation</param> - /// <param name="data">An <see cref="IObjectData"/> that represents the object data to set</param> - /// <returns>A task that completes to return the results of the response payload</returns> + /// <returns>A task that resolves when the operation has completed</returns> /// <exception cref="InvalidStatusException"></exception> /// <exception cref="ObjectDisposedException"></exception> /// <exception cref="InvalidResponseException"></exception> - public static async Task GetObjectAsync(this FBMClient client, string objectId, IObjectData data, CancellationToken cancellationToken = default) + /// <exception cref="ObjectNotFoundException"></exception> + public static async Task<bool> DeleteObjectAsync(this FBMClient client, string objectId, CancellationToken cancellationToken = default) { _ = client ?? throw new ArgumentNullException(nameof(client)); - _ = data ?? throw new ArgumentNullException(nameof(data)); - client.LogDebug("Getting object {id}", objectId); + client.LogDebug("Deleting object {id}", objectId); //Rent a new request FBMRequest request = client.RentRequest(); try { - //Set action as get/create - request.WriteHeader(HeaderCommand.Action, Actions.Get); - - //Set object id header + //Set action as delete + request.WriteHeader(HeaderCommand.Action, Actions.Delete); + //Set session-id header request.WriteHeader(Constants.ObjectId, objectId); //Make request @@ -389,18 +431,13 @@ namespace VNLib.Data.Caching //Get the status code FBMMessageHeader status = response.Headers.FirstOrDefault(static a => a.Header == HeaderCommand.Status); - //Check ok status code, then its safe to deserialize if (status.Value.Equals(ResponseCodes.Okay, StringComparison.Ordinal)) { - //Write the object data - data.SetData(response.ResponseBody); - return; + return true; } - - //Object may not exist on the server yet - if (status.Value.Equals(ResponseCodes.NotFound, StringComparison.Ordinal)) + else if (status.Value.Equals(ResponseCodes.NotFound, StringComparison.OrdinalIgnoreCase)) { - return; + return false; } throw new InvalidStatusException("Invalid status code recived for object get request", status.ToString()); @@ -415,10 +452,15 @@ namespace VNLib.Data.Caching /// Dequeues a change event from the server event queue for the current connection, or waits until a change happens /// </summary> /// <param name="client"></param> + /// <param name="change">The instance to store change event data to</param> /// <param name="cancellationToken">A token to cancel the deuque operation</param> /// <returns>A <see cref="WaitForChangeResult"/> that contains information about the modified element</returns> - public static async Task<WaitForChangeResult> WaitForChangeAsync(this FBMClient client, CancellationToken cancellationToken = default) + /// <exception cref="InvalidResponseException"></exception> + /// <exception cref="InvalidOperationException"></exception> + public static async Task WaitForChangeAsync(this FBMClient client, WaitForChangeResult change, CancellationToken cancellationToken = default) { + _ = change ?? throw new ArgumentNullException(nameof(change)); + //Rent a new request FBMRequest request = client.RentRequest(); try @@ -431,12 +473,9 @@ namespace VNLib.Data.Caching response.ThrowIfNotSet(); - return new() - { - Status = response.Headers.FirstOrDefault(static a => a.Header == HeaderCommand.Status).Value.ToString(), - CurrentId = response.Headers.SingleOrDefault(static v => v.Header == Constants.ObjectId).Value.ToString(), - NewId = response.Headers.SingleOrDefault(static v => v.Header == Constants.NewObjectId).Value.ToString() - }; + change.Status = response.Headers.FirstOrDefault(static a => a.Header == HeaderCommand.Status).Value.ToString(); + change.CurrentId = response.Headers.SingleOrDefault(static v => v.Header == Constants.ObjectId).Value.ToString(); + change.NewId = response.Headers.SingleOrDefault(static v => v.Header == Constants.NewObjectId).Value.ToString(); } finally { |