/*
* Copyright (c) 2023 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Data.Caching.ObjectCache
* File: CacheBucketHandle.cs
*
* CacheBucketHandle.cs is part of VNLib.Data.Caching.ObjectCache which is part of the larger
* VNLib collection of libraries and utilities.
*
* VNLib.Data.Caching.ObjectCache is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* VNLib.Data.Caching.ObjectCache 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
using System;
using System.Diagnostics.CodeAnalysis;
namespace VNLib.Data.Caching.ObjectCache
{
///
/// Holds an exclusive lock on a , and exposes
/// acess to its internal
///
public readonly struct CacheBucketHandle : IDisposable, IEquatable
{
private readonly IBlobCacheBucket? _bucket;
///
/// The held by the current handle
///
public readonly IBlobCache Cache { get; }
///
/// Initializes an empty blobcache handle
///
public CacheBucketHandle()
{
_bucket = null;
Cache = null!;
}
///
/// Creates a new bucket lock handle to be released on dispose
///
/// The bucket to release access to on dispose
/// The underlying provide exclusive access to
public CacheBucketHandle(IBlobCacheBucket bucket, IBlobCache cache)
{
_bucket = bucket;
Cache = cache;
}
///
/// Releases the exlusive lock held on the bucket
///
public void Dispose()
{
//Release the bucket when disposed
_bucket?.Release();
}
///
/// Determines if the other handle instance is equal to the current. Handles are
/// equal iff the underlying bucket referrence is equal.
///
/// The other handle to compare
/// True if the handles hold a referrence to the same bucket
public bool Equals(CacheBucketHandle other) => ReferenceEquals(_bucket, other._bucket);
///
/// Determines if the other handle instance is equal to the current. Handles are
/// equal iff the underlying bucket referrence is equal.
///
/// The other handle to compare
/// True if the handles hold a referrence to the same bucket
public override bool Equals([NotNullWhen(true)] object? obj) => obj is CacheBucketHandle other && Equals(other);
///
/// Gets the hashcode of the underlying bucket
///
///
public override int GetHashCode() => _bucket?.GetHashCode() ?? -1;
///
/// Determines if the handles are equal by the
/// method.
///
///
///
/// True if the internal bucket references are equal
public static bool operator ==(CacheBucketHandle left, CacheBucketHandle right) => left.Equals(right);
///
/// Determines if the handles are equal by the
/// method.
///
///
///
/// True if the internal bucket references are NOT equal
public static bool operator !=(CacheBucketHandle left, CacheBucketHandle right) => !(left == right);
}
}