Skip to content

Commit

Permalink
Use DateTimeOffset and TimeProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
erri120 committed Oct 16, 2024
1 parent 1c982a4 commit ec0fa38
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace NexusMods.MnemonicDB.Abstractions.Attributes;
/// <summary>
/// An attribute that holds a timestamp
/// </summary>
public class TimestampAttribute(string ns, string name) : ScalarAttribute<DateTime, long>(ValueTag.Int64, ns, name)
public class TimestampAttribute(string ns, string name) : ScalarAttribute<DateTimeOffset, long>(ValueTag.Int64, ns, name)
{
/// <inheritdoc />
protected override long ToLowLevel(DateTime value) => value.ToFileTimeUtc();
protected override long ToLowLevel(DateTimeOffset value) => value.ToUnixTimeMilliseconds();

/// <inheritdoc />
protected override DateTime FromLowLevel(long value, AttributeResolver resolver) => DateTime.FromFileTimeUtc(value);
protected override DateTimeOffset FromLowLevel(long value, AttributeResolver resolver) => DateTimeOffset.FromUnixTimeMilliseconds(value);
}
15 changes: 11 additions & 4 deletions src/NexusMods.MnemonicDB/Caching/IndexSegmentCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,25 @@ public struct CacheValue : IEquatable<CacheValue>
public long LastAccessed;
public readonly IndexSegment Segment;

public CacheValue(long lastAccessed, IndexSegment segment)
public CacheValue(IndexSegment segment)
{
LastAccessed = lastAccessed;
LastAccessed = CreateLastAccessed();
Segment = segment;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static long CreateLastAccessed(TimeProvider? timeProvider = null)
{
timeProvider ??= TimeProvider.System;
return timeProvider.GetTimestamp();
}

/// <summary>
/// Update the last accessed time to now.
/// </summary>
public void Hit()
{
LastAccessed = DateTime.UtcNow.Ticks;
LastAccessed = CreateLastAccessed();
}

/// <inheritdoc />
Expand Down Expand Up @@ -133,7 +140,7 @@ public bool TryGetValue(CacheKey key, out IndexSegment segment)
/// </summary>
public CacheRoot With(CacheKey key, IndexSegment segment, IndexSegmentCache cache)
{
var newEntries = _entries.SetItem(key, new CacheValue(DateTime.UtcNow.Ticks, segment));
var newEntries = _entries.SetItem(key, new CacheValue(segment));
if (newEntries.Count > cache._entryCapacity)
{
newEntries = PurgeEntries(newEntries, newEntries.Count / 10);
Expand Down
21 changes: 14 additions & 7 deletions src/NexusMods.MnemonicDB/Storage/DatomStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
Expand Down Expand Up @@ -72,20 +73,28 @@ public sealed partial class DatomStore : IDatomStore
/// </summary>
private Thread? _loggerThread;

private CancellationTokenSource _shutdownToken = new();
private readonly CancellationTokenSource _shutdownToken = new();
private TxId _thisTx;

/// <summary>
/// Scratch spaced to create new datoms while processing transactions
/// </summary>
private readonly Memory<byte> _txScratchSpace;

private readonly TimeProvider _timeProvider;

/// <summary>
/// DI constructor
/// </summary>
public DatomStore(ILogger<DatomStore> logger, DatomStoreSettings settings, IStoreBackend backend, bool bootstrap = true)
public DatomStore(
ILogger<DatomStore> logger,
DatomStoreSettings settings,
IStoreBackend backend,
TimeProvider? timeProvider = null,
bool bootstrap = true)
{
CurrentSnapshot = default!;
_timeProvider = timeProvider ?? TimeProvider.System;
_txScratchSpace = new Memory<byte>(new byte[1024]);
_remapFunc = Remap;
_dbStream = new DbStream();
Expand All @@ -97,10 +106,9 @@ public DatomStore(ILogger<DatomStore> logger, DatomStoreSettings settings, IStor
_retractWriter = new PooledMemoryBufferWriter();
_prevWriter = new PooledMemoryBufferWriter();


Logger = logger;
_settings = settings;

Backend.DeclareEAVT(IndexType.EAVTCurrent);
Backend.DeclareEAVT(IndexType.EAVTHistory);
Backend.DeclareAEVT(IndexType.AEVTCurrent);
Expand All @@ -123,8 +131,7 @@ public DatomStore(ILogger<DatomStore> logger, DatomStoreSettings settings, IStor
AVETCurrent = Backend.GetIndex(IndexType.AVETCurrent);
AVETHistory = Backend.GetIndex(IndexType.AVETHistory);

if (bootstrap)
Bootstrap();
if (bootstrap) Bootstrap();
}

/// <inheritdoc />
Expand Down Expand Up @@ -411,7 +418,7 @@ internal void LogDatoms<TSource>(IWriteBatch batch, TSource datoms, bool advanc
/// <exception cref="NotImplementedException"></exception>
private void LogTx(IWriteBatch batch)
{
MemoryMarshal.Write(_txScratchSpace.Span, DateTime.UtcNow.ToFileTimeUtc());
MemoryMarshal.Write(_txScratchSpace.Span, _timeProvider.GetTimestamp());
var id = EntityId.From(_thisTx.Value);
var keyPrefix = new KeyPrefix(id, AttributeCache.GetAttributeId(MnemonicDB.Abstractions.BuiltInEntities.Transaction.Timestamp.Id), _thisTx, false, ValueTag.Int64);
var datom = new Datom(keyPrefix, _txScratchSpace[..sizeof(long)]);
Expand Down
2 changes: 1 addition & 1 deletion tests/NexusMods.MnemonicDB.Tests/AMnemonicDBTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ string TruncateOrPad(string val, int length)
var hash = code.ToString("X16");
sb.Append($"Blob 0x{hash} {byteArray.Length} bytes".PadRight(48));
break;
case DateTime dateTime:
case DateTimeOffset:
sb.Append($"DateTime : {dateTimeCount++}".PadRight(48));
break;
default:
Expand Down

0 comments on commit ec0fa38

Please sign in to comment.