Skip to content

Commit

Permalink
Don't double-query recently added datoms
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari committed Jul 17, 2024
1 parent 8b397ec commit 5150379
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/NexusMods.MnemonicDB.Abstractions/IConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface IConnection
/// <summary>
/// A sequential stream of database revisions.
/// </summary>
public IObservable<Revision> Revisions { get; }
public IObservable<IDb> Revisions { get; }

/// <summary>
/// A service provider that entities can use to resolve their values
Expand Down
5 changes: 5 additions & 0 deletions src/NexusMods.MnemonicDB.Abstractions/IDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public interface IDb : IEquatable<IDb>
/// The connection that this database is using for its state.
/// </summary>
IConnection Connection { get; }

/// <summary>
/// The datoms that were added in the most recent transaction (indicated by the basis TxId).
/// </summary>
IndexSegment RecentlyAdded { get; }

/// <summary>
/// The snapshot that this database is based on.
Expand Down
10 changes: 5 additions & 5 deletions src/NexusMods.MnemonicDB.Abstractions/Query/ObservableDatoms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ public static IObservable<IChangeSet<Datom>> ObserveDatoms(this IConnection conn
var lastTxId = TxId.From(0);

return conn.Revisions
.Where(rev => rev.AddedDatoms.Valid)
.Where(rev => rev.RecentlyAdded.Count > 0)
.Select((rev, idx) =>
{
lock (set)
{
if (rev.Database.BasisTxId <= lastTxId)
if (rev.BasisTxId <= lastTxId)
return ChangeSet<Datom>.Empty;

lastTxId = rev.Database.BasisTxId;
lastTxId = rev.BasisTxId;

if (idx == 0)
return Setup(set, rev.Database, descriptor);
return Diff(conn.Registry, set, rev.AddedDatoms, descriptor, equality);
return Setup(set, rev, descriptor);
return Diff(conn.Registry, set, rev.RecentlyAdded, descriptor, equality);
}
});
}
Expand Down
17 changes: 6 additions & 11 deletions src/NexusMods.MnemonicDB/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Connection : IConnection
private readonly Dictionary<Symbol, IAttribute> _declaredAttributes;
private readonly ILogger<Connection> _logger;

private BehaviorSubject<Revision> _dbStream;
private BehaviorSubject<IDb> _dbStream;
private IDisposable? _dbStreamDisposable;

/// <summary>
Expand All @@ -37,7 +37,7 @@ public Connection(ILogger<Connection> logger, IDatomStore store, IServiceProvide
_logger = logger;
_declaredAttributes = declaredAttributes.ToDictionary(a => a.Id);
_store = store;
_dbStream = new BehaviorSubject<Revision>(default!);
_dbStream = new BehaviorSubject<IDb>(default!);
Bootstrap();
}

Expand Down Expand Up @@ -73,7 +73,7 @@ public IDb Db
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (val == null)
ThrowNullDb();
return val!.Value.Database;
return val!.Value;
}
}

Expand Down Expand Up @@ -106,7 +106,7 @@ public ITransaction BeginTransaction()
}

/// <inheritdoc />
public IObservable<Revision> Revisions
public IObservable<IDb> Revisions
{
get
{
Expand Down Expand Up @@ -178,13 +178,8 @@ private void Bootstrap()
_dbStreamDisposable = ForwardOnly(_store.TxLog)
.Select(db =>
{
db.Connection = this;
var addedItems = db.Datoms(SliceDescriptor.Create(db.BasisTxId, _store.Registry));
return new Revision
{
Database = db,
AddedDatoms = addedItems
};
db.Connection = this;
return db;
})
.Subscribe(_dbStream);
}
Expand Down
2 changes: 1 addition & 1 deletion src/NexusMods.MnemonicDB/Db.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class Db : IDb
public ISnapshot Snapshot { get; }
public IAttributeRegistry Registry => _registry;

public IndexSegment RecentlyAdded;
public IndexSegment RecentlyAdded { get; }

public Db(ISnapshot snapshot, TxId txId, AttributeRegistry registry)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/NexusMods.MnemonicDB.Storage.Tests/NullConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class NullConnection : IConnection
public IDb Db => throw new NotSupportedException();
public IAttributeRegistry Registry => throw new NotSupportedException();
public TxId TxId => throw new NotSupportedException();
public IObservable<Revision> Revisions => throw new NotSupportedException();
public IObservable<IDb> Revisions => throw new NotSupportedException();
public IServiceProvider ServiceProvider => throw new NotSupportedException();
public IDb AsOf(TxId txId)
{
Expand Down
4 changes: 2 additions & 2 deletions tests/NexusMods.MnemonicDB.Tests/DbTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ public async Task CanGetCommitUpdates()
Connection.Revisions.Subscribe(update =>
{
// Only Txes we care about
if (update.AddedDatoms.Any(d => d.E == realId))
updates.Add(update.AddedDatoms.Select(d => d.Resolved).ToArray());
if (update.RecentlyAdded.Any(d => d.E == realId))
updates.Add(update.RecentlyAdded.Select(d => d.Resolved).ToArray());
});

for (var idx = 0; idx < 4; idx++)
Expand Down

0 comments on commit 5150379

Please sign in to comment.