Skip to content

Commit

Permalink
Merge pull request #47 from Nexus-Mods/find-indexed-datoms
Browse files Browse the repository at this point in the history
Added: FindIndexedDatoms API for providing a lower level search utility.
  • Loading branch information
Sewer56 authored Apr 30, 2024
2 parents c7dbea5 + 637f99d commit e024fc6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/NexusMods.MnemonicDB.Abstractions/IDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,16 @@ public Entities<EntityIds, TModel> GetReverse<TModel>(EntityId id, Attribute<Ent
/// </summary>
IEnumerable<TValueType> GetAll<TValueType, TLowLevel>(EntityId modelId, Attribute<TValueType, TLowLevel> attribute);


/// <summary>
/// Finds all the entity ids that have the given attribute with the given value.
/// </summary>
IEnumerable<EntityId> FindIndexed<TValue, TLowLevel>(TValue value, Attribute<TValue, TLowLevel> attribute);

/// <summary>
/// Finds all the datoms have the given attribute with the given value.
/// </summary>
IEnumerable<Datom> FindIndexedDatoms<TValue, TLowLevel>(TValue value, Attribute<TValue, TLowLevel> attribute);

/// <summary>
/// Finds all the entity ids that have the given attribute.
/// </summary>
Expand Down
14 changes: 8 additions & 6 deletions src/NexusMods.MnemonicDB/Db.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ public IEnumerable<TValue> GetAll<TValue, TLowLevel>(EntityId id, Attribute<TVal

public IEnumerable<EntityId> FindIndexed<TValue, TLowLevel>(TValue value, Attribute<TValue, TLowLevel> attribute)
{
var attrId = attribute.GetDbId(_registry.Id);
return FindIndexedDatoms(value, attribute)
.Select(d => d.E);
}

public IEnumerable<Datom> FindIndexedDatoms<TValue, TLowLevel>(TValue value, Attribute<TValue, TLowLevel> attribute)
{
if (!attribute.IsIndexed)
throw new InvalidOperationException($"Attribute {attribute.Id} is not indexed");

Expand All @@ -120,11 +125,8 @@ public IEnumerable<EntityId> FindIndexed<TValue, TLowLevel>(TValue value, Attrib
using var end = new PooledMemoryBufferWriter(64);
attribute.Write(EntityId.MaxValueNoPartition, _registry.Id, value, TxId.MinValue, false, end);

var results = Snapshot
.Datoms(IndexType.AVETCurrent, start.GetWrittenSpan(), end.GetWrittenSpan())
.Select(d => d.E);

return results;
return Snapshot
.Datoms(IndexType.AVETCurrent, start.GetWrittenSpan(), end.GetWrittenSpan());;
}

public TModel Get<TModel>(EntityId id)
Expand Down

0 comments on commit e024fc6

Please sign in to comment.