blob: 19cb8ecea623de4c123b70651fc332c9a1ceb076 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/*
* Copyright (c) 2022 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Plugins.Essentials
* File: TimestampedCounter.cs
*
* TimestampedCounter.cs is part of VNLib.Plugins.Essentials which is part of the larger
* VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Essentials 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.Plugins.Essentials 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;
#nullable enable
namespace VNLib.Plugins.Essentials
{
/// <summary>
/// Stucture that allows for convient storage of a counter value
/// and a second precision timestamp into a 64-bit unsigned integer
/// </summary>
public readonly struct TimestampedCounter : IEquatable<TimestampedCounter>
{
/// <summary>
/// The time the count was last modifed
/// </summary>
public readonly DateTimeOffset LastModified;
/// <summary>
/// The last failed login attempt count value
/// </summary>
public readonly uint Count;
/// <summary>
/// Initalizes a new flc structure with the current UTC date
/// and the specified count value
/// </summary>
/// <param name="count">FLC current count</param>
public TimestampedCounter(uint count) : this(DateTimeOffset.UtcNow, count)
{ }
private TimestampedCounter(DateTimeOffset dto, uint count)
{
Count = count;
LastModified = dto;
}
/// <summary>
/// Compacts and converts the counter value and timestamp into
/// a 64bit unsigned integer
/// </summary>
/// <param name="count">The counter to convert</param>
public static explicit operator ulong(TimestampedCounter count) => count.ToUInt64();
/// <summary>
/// Compacts and converts the counter value and timestamp into
/// a 64bit unsigned integer
/// </summary>
/// <returns>The uint64 compacted value</returns>
public ulong ToUInt64()
{
//Upper 32 bits time, lower 32 bits count
ulong value = (ulong)LastModified.ToUnixTimeSeconds() << 32;
value |= Count;
return value;
}
/// <summary>
/// The previously compacted <see cref="TimestampedCounter"/>
/// value to cast back to a counter
/// </summary>
/// <param name="value"></param>
public static explicit operator TimestampedCounter(ulong value) => FromUInt64(value);
///<inheritdoc/>
public override bool Equals(object? obj) => obj is TimestampedCounter counter && Equals(counter);
///<inheritdoc/>
public override int GetHashCode() => this.ToUInt64().GetHashCode();
///<inheritdoc/>
public static bool operator ==(TimestampedCounter left, TimestampedCounter right) => left.Equals(right);
///<inheritdoc/>
public static bool operator !=(TimestampedCounter left, TimestampedCounter right) => !(left == right);
///<inheritdoc/>
public bool Equals(TimestampedCounter other) => ToUInt64() == other.ToUInt64();
/// <summary>
/// The previously compacted <see cref="TimestampedCounter"/>
/// value to cast back to a counter
/// </summary>
/// <param name="value">The uint64 encoded <see cref="TimestampedCounter"/></param>
/// <returns>
/// The decoded <see cref="TimestampedCounter"/> from its
/// compatcted representation
/// </returns>
public static TimestampedCounter FromUInt64(ulong value)
{
//Upper 32 bits time, lower 32 bits count
long time = (long)(value >> 32);
uint count = (uint)(value & uint.MaxValue);
//Init dto struct
return new(DateTimeOffset.FromUnixTimeSeconds(time), count);
}
}
}
|