Skip to content

Commit

Permalink
ObjectSpace: added Clear method and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
HermanSchoenfeld committed Mar 17, 2024
1 parent 6bad175 commit 0a079dc
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Collections;
using Hydrogen.ObjectSpaces;

namespace Hydrogen;

public interface IStreamMappedCollection {
ObjectStream ObjectStream { get; }

void Clear();
}

public interface IStreamMappedCollection<TItem> : IStreamMappedCollection {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface IStreamMappedDictionary<TKey, TValue> : IDictionary<TKey, TValu

void RemoveAt(long index);

new void Clear();

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
namespace Hydrogen;

public interface IStreamMappedHashSet<TItem> : ISet<TItem>, IStreamMappedCollection, ILoadable, IDisposable {
new void Clear();
}
4 changes: 3 additions & 1 deletion src/Hydrogen/Collections/StreamMapped/IStreamMappedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

namespace Hydrogen;

public interface IStreamMappedList<TItem> : IExtendedList<TItem>, IStreamMappedCollection<TItem>, ILoadable, IDisposable {
public interface IStreamMappedList<TItem> : IExtendedList<TItem>, IStreamMappedCollection<TItem>, ILoadable, IDisposable {

IItemSerializer<TItem> ItemSerializer { get; }

IEqualityComparer<TItem> ItemComparer { get; }

new void Clear();

}
1 change: 1 addition & 0 deletions src/Hydrogen/Merkle/FlatMerkleTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ public override IEnumerable<byte[]> ReadRange(long index, long count) {
}

public override void UpdateRange(long index, IEnumerable<byte[]> items) {
Tools.Debugger.CounterA++;
Guard.ArgumentNotNull(items, nameof(items));
var itemsArr = items as byte[][] ?? items.ToArray();
Guard.Argument(itemsArr.All(x => x.Length == _parent._digestSize), nameof(items), "Improper digest size(s)");
Expand Down
10 changes: 9 additions & 1 deletion src/Hydrogen/ObjectSpaces/ObjectSpace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ public void Delete<TItem>(TItem item) {
}
}

public void Clear() {
using (EnterAccessScope()) {
foreach(var dimension in Dimensions) {
dimension.Clear();
}
}
}

public void Commit() {
using (EnterAccessScope()) {
// flush all cached changes
Expand Down Expand Up @@ -308,7 +316,7 @@ protected virtual IStreamMappedCollection BuildDimension(ObjectSpaceDefinition.D
// Get a comparer
var comparer = _comparerFactory.GetEqualityComparer(dimensionDefinition.ObjectType);

// construct the the collection
// construct the collection
var list = (IStreamMappedCollection)typeof(StreamMappedRecyclableList<>)
.MakeGenericType(dimensionDefinition.ObjectType)
.ActivateWithCompatibleArgs(
Expand Down
1 change: 1 addition & 0 deletions src/Hydrogen/ObjectSpaces/ObjectSpaceMerkleTreeIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private void SubscribeToDimensionTreeChanges(int i) {
// Listen to underlying collection root changes (and track the handler for unsub later)
var capturedIndex = i;
void CollectionRootListener(byte[] oldValue, byte[] newValue) {
newValue ??= Hashers.ZeroHash(Inner.MerkleTree.HashAlgorithm); // merkle-root property will return null when changed to zero's
Inner.Update(capturedIndex, newValue);
}
dimensionMerkleTree.KeyStore.RootChanged += CollectionRootListener;
Expand Down
23 changes: 22 additions & 1 deletion tests/Hydrogen.Tests/ObjectSpaces/ObjectSpacesTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ public void LoadDoesntThrow() {

#endregion

#region Clear

[Test]
public void Clear_1() {
var folder = Tools.FileSystem.GetTempEmptyDirectory(true);
var accountComparer = CreateAccountComparer();
Account savedAccount, loadedAccount;
using (var scope = CreateObjectSpaceScope(folder, true)) {
var objectSpace = scope.Item;
savedAccount = CreateAccount();
objectSpace.Save(savedAccount);
objectSpace.Clear();
objectSpace.Commit();

foreach(var dim in objectSpace.Dimensions)
Assert.That(dim.ObjectStream.Count, Is.EqualTo(0));
}

}

#endregion

#region Commit

[Test]
Expand Down Expand Up @@ -124,7 +146,6 @@ public void RollbackNotThrows() {
Assert.That(objectSpace.Rollback, Throws.Nothing);
}


[Test]
public void Rollback() {
var folder = Tools.FileSystem.GetTempEmptyDirectory(true);
Expand Down

0 comments on commit 0a079dc

Please sign in to comment.