/* * Copyright (c) 2023 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; namespace VNLib.Plugins.Essentials { /// /// Stucture that allows for convient storage of a counter value /// and a second precision timestamp into a 64-bit unsigned integer /// public readonly record struct TimestampedCounter(uint Count, uint UnixMs) { /// /// The time the count was last modifed /// public readonly DateTimeOffset LastModified => DateTimeOffset.FromUnixTimeSeconds(UnixMs); /// /// Compacts and converts the counter value and timestamp into /// a 64bit unsigned integer /// /// The uint64 compacted value public readonly ulong ToUInt64() { //Upper 32 bits time, lower 32 bits count ulong value = UnixMs; //Lshift to upper32 value <<= 32; //Set count as lower32 value |= Count; return value; } /// /// The previously compacted /// value to cast back to a counter /// /// The uint64 encoded /// /// The decoded from its /// compatcted representation /// public static TimestampedCounter FromUInt64(ulong value) { //Upper 32 bits time, lower 32 bits count uint time = (uint)(value >> 32); uint count = (uint)(value & uint.MaxValue); return new TimestampedCounter(count, time); } /// /// Creates a new from the given counter /// value and the unix ms value /// /// /// The internal time to store in the counter /// An initialized /// public static TimestampedCounter FromValues(uint count, DateTimeOffset time) { //The time in seconds truncated to a uint32 uint sec = Convert.ToUInt32(time.ToUnixTimeSeconds()); return new TimestampedCounter(count, sec); } /// /// The previously compacted /// value to cast back to a counter /// /// public static explicit operator TimestampedCounter(ulong value) => FromUInt64(value); /// /// Compacts and converts the counter value and timestamp into /// a 64bit unsigned integer /// /// The counter to convert public static explicit operator ulong(TimestampedCounter count) => count.ToUInt64(); } }