diff options
author | vnugent <public@vaughnnugent.com> | 2023-09-08 23:20:19 -0400 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2023-09-08 23:20:19 -0400 |
commit | dcf6f9dc0143eb50b702eb7dcd9e5dadd140537c (patch) | |
tree | 41efba0de085e6d49a0228b3f942824b2395383d /lib/Utils/src/Resources | |
parent | c38d794d808b06240b778b5b320f506382215e29 (diff) |
Resource, argon2, accounts. sessions core updates
Diffstat (limited to 'lib/Utils/src/Resources')
-rw-r--r-- | lib/Utils/src/Resources/BackedResourceBase.cs | 38 | ||||
-rw-r--r-- | lib/Utils/src/Resources/IResourceStateHandler.cs | 47 | ||||
-rw-r--r-- | lib/Utils/src/Resources/UpdatableResource.cs | 46 |
3 files changed, 81 insertions, 50 deletions
diff --git a/lib/Utils/src/Resources/BackedResourceBase.cs b/lib/Utils/src/Resources/BackedResourceBase.cs index 0a2e1e3..22a965c 100644 --- a/lib/Utils/src/Resources/BackedResourceBase.cs +++ b/lib/Utils/src/Resources/BackedResourceBase.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2022 Vaughn Nugent +* Copyright (c) 2023 Vaughn Nugent * * Library: VNLib * Package: VNLib.Utils @@ -23,7 +23,6 @@ */ using System; -using System.Text.Json; using System.Runtime.CompilerServices; namespace VNLib.Utils.Resources @@ -34,23 +33,36 @@ namespace VNLib.Utils.Resources /// </summary> public abstract class BackedResourceBase : IResource { - ///<inheritdoc/> - public bool IsReleased { get; protected set; } + const int IsReleasedFlag = 1 << 0; + const int IsDeletedFlag = 1 << 2; + const int IsModifiedFlag = 1 << 3; - /// <summary> - /// Optional <see cref="JsonSerializerOptions"/> to be used when serializing - /// the resource - /// </summary> - internal protected virtual JsonSerializerOptions? JSO { get; } + private uint _flags; + + ///<inheritdoc/> + public bool IsReleased + { + get => (_flags & IsReleasedFlag) == IsReleasedFlag; + protected set => _flags |= IsReleasedFlag; + } /// <summary> /// A value indicating whether the instance should be deleted when released /// </summary> - protected bool Deleted { get; set; } + protected bool Deleted + { + get => (_flags & IsDeletedFlag) == IsDeletedFlag; + set => _flags |= IsDeletedFlag; + } + /// <summary> /// A value indicating whether the instance should be updated when released /// </summary> - protected bool Modified { get; set; } + protected bool Modified + { + get => (_flags & IsModifiedFlag) == IsModifiedFlag; + set => _flags |= IsModifiedFlag; + } /// <summary> /// Checks if the resouce has been disposed and raises an exception if it is @@ -61,7 +73,7 @@ namespace VNLib.Utils.Resources { if (IsReleased) { - throw new ObjectDisposedException("The resource has been disposed"); + throw new ObjectDisposedException(null, "The resource has been disposed"); } } @@ -74,6 +86,6 @@ namespace VNLib.Utils.Resources /// <summary> /// Marks the resource for deletion from backing store during closing events /// </summary> - public virtual void Delete() => Deleted = true; + public virtual void Delete() => _flags |= IsDeletedFlag; } }
\ No newline at end of file diff --git a/lib/Utils/src/Resources/IResourceStateHandler.cs b/lib/Utils/src/Resources/IResourceStateHandler.cs new file mode 100644 index 0000000..c3d0d56 --- /dev/null +++ b/lib/Utils/src/Resources/IResourceStateHandler.cs @@ -0,0 +1,47 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Utils +* File: IResourceStateHandler.cs +* +* IResourceStateHandler.cs is part of VNLib.Utils which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.Utils is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.Utils is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.Utils. If not, see http://www.gnu.org/licenses/. +*/ + +namespace VNLib.Utils.Resources +{ + /// <summary> + /// Implemented by a resource that is backed by an external data store, that when modified or deleted will + /// be reflected to the backing store. + /// </summary> + public interface IResourceStateHandler + { + /// <summary> + /// Called when a resource update has been requested + /// </summary> + /// <param name="resource">The <see cref="UpdatableResource"/> to be updated</param> + /// <param name="data">The wrapped state data to update</param> + void Update(UpdatableResource resource, object data); + + /// <summary> + /// Called when a resource delete has been requested + /// </summary> + /// <param name="resource">The <see cref="UpdatableResource"/> to be deleted</param> + /// <exception cref="ResourceDeleteFailedException"></exception> + void Delete(UpdatableResource resource); + } +}
\ No newline at end of file diff --git a/lib/Utils/src/Resources/UpdatableResource.cs b/lib/Utils/src/Resources/UpdatableResource.cs index 16f26f2..68072f9 100644 --- a/lib/Utils/src/Resources/UpdatableResource.cs +++ b/lib/Utils/src/Resources/UpdatableResource.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2022 Vaughn Nugent +* Copyright (c) 2023 Vaughn Nugent * * Library: VNLib * Package: VNLib.Utils @@ -23,25 +23,9 @@ */ using System; -using System.IO; - -using VNLib.Utils.IO; namespace VNLib.Utils.Resources { - /// <summary> - /// A callback delegate used for updating a <see cref="UpdatableResource"/> - /// </summary> - /// <param name="source">The <see cref="UpdatableResource"/> to be updated</param> - /// <param name="data">The serialized data to be stored/updated</param> - /// <exception cref="ResourceUpdateFailedException"></exception> - public delegate void UpdateCallback(object source, Stream data); - /// <summary> - /// A callback delegate invoked when a <see cref="UpdatableResource"/> delete is requested - /// </summary> - /// <param name="source">The <see cref="UpdatableResource"/> to be deleted</param> - /// <exception cref="ResourceDeleteFailedException"></exception> - public delegate void DeleteCallback(object source); /// <summary> /// Implemented by a resource that is backed by an external data store, that when modified or deleted will @@ -50,19 +34,11 @@ namespace VNLib.Utils.Resources public abstract class UpdatableResource : BackedResourceBase, IExclusiveResource { /// <summary> - /// The update callback method to invoke during a release operation - /// when the resource is updated. + /// Gets the <see cref="IResourceStateHandler"/> that will be invoked when the resource is released /// </summary> - protected abstract UpdateCallback UpdateCb { get; } - /// <summary> - /// The callback method to invoke during a realease operation - /// when the resource should be deleted - /// </summary> - protected abstract DeleteCallback DeleteCb { get; } + protected abstract IResourceStateHandler Handler { get; } - /// <summary> /// <inheritdoc/> - /// </summary> /// <exception cref="InvalidOperationException"></exception> /// <exception cref="ResourceDeleteFailedException"></exception> /// <exception cref="ResourceUpdateFailedException"></exception> @@ -76,7 +52,7 @@ namespace VNLib.Utils.Resources //If deleted flag is set, invoke the delete callback if (Deleted) { - DeleteCb(this); + Handler.Delete(this); } //If the state has been modifed, flush changes to the store else if (Modified) @@ -88,24 +64,20 @@ namespace VNLib.Utils.Resources } /// <summary> + /// <para> /// Writes the current state of the the resource to the backing store /// immediatly by invoking the specified callback. - /// <br></br> - /// <br></br> + /// </para> + /// <para> /// Only call this method if your store supports multiple state updates + /// </para> /// </summary> protected virtual void FlushPendingChanges() { //Get the resource object resource = GetResource(); - //Open a memory stream to store data in - using VnMemoryStream data = new(); - //Serialize and write to stream - VnEncoding.JSONSerializeToBinary(resource, data, resource.GetType(), JSO); - //Reset stream to begining - _ = data.Seek(0, SeekOrigin.Begin); //Invoke update callback - UpdateCb(this, data); + Handler.Update(this, resource); //Clear modified flag Modified = false; } |