Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Upgrade [email protected]

* Query LiteDb transform query to work in v5

* Upgrade [email protected]
  • Loading branch information
psavva authored Apr 7, 2024
1 parent 59e5b31 commit 25d97a1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/Blockcore/Blockcore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<ItemGroup>
<PackageReference Include="ConcurrentHashSet" Version="1.3.0" />
<PackageReference Include="DBreeze" Version="1.115.2023.1103" />
<PackageReference Include="LiteDB" Version="4.1.4" />
<PackageReference Include="LiteDB" Version="5.0.15" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.2.8" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AddressIndexRepository : MemorySizeCache<string, AddressIndexerData
{
private const string DbAddressDataKey = "AddrData";

private readonly LiteCollection<AddressIndexerData> addressIndexerDataCollection;
private readonly ILiteCollection<AddressIndexerData> addressIndexerDataCollection;

private readonly ILogger logger;

Expand Down Expand Up @@ -58,7 +58,8 @@ public List<string> GetAddressesHigherThanHeight(int height)
this.SaveAllItems();

// Need to specify index name explicitly so that it gets used for the query.
IEnumerable<AddressIndexerData> affectedAddresses = this.addressIndexerDataCollection.Find(Query.GT("BalanceChangedHeightIndex", height));
// Query to find documents where any BalanceChangedHeight in the BalanceChanges array is greater than 'height'
IEnumerable<AddressIndexerData> affectedAddresses = this.addressIndexerDataCollection.Find(Query.GT("$.BalanceChanges[*].BalanceChangedHeight ANY", height)).ToList();

// Per LiteDb documentation:
// "Returning an IEnumerable your code still connected to datafile.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using Blockcore.Networks;
using Blockcore.Utilities;
using LiteDB;
using FileMode = LiteDB.FileMode;
using Microsoft.Extensions.Logging;
using Script = Blockcore.Consensus.ScriptInfo.Script;

Expand Down Expand Up @@ -86,7 +85,7 @@ public class AddressIndexer : IAddressIndexer

private LiteDatabase db;

private LiteCollection<AddressIndexerTipData> tipDataStore;
private ILiteCollection<AddressIndexerTipData> tipDataStore;

/// <summary>A mapping between addresses and their balance changes.</summary>
/// <remarks>All access should be protected by <see cref="lockObject"/>.</remarks>
Expand Down Expand Up @@ -179,8 +178,8 @@ public void Initialize()

string dbPath = Path.Combine(this.dataFolder.RootPath, AddressIndexerDatabaseFilename);

FileMode fileMode = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? FileMode.Exclusive : FileMode.Shared;
this.db = new LiteDatabase(new ConnectionString() { Filename = dbPath, Mode = fileMode });
ConnectionType connectionType = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ConnectionType.Direct : ConnectionType.Shared;
this.db = new LiteDatabase(new ConnectionString() { Filename = dbPath, Connection = connectionType });

this.addressIndexRepository = new AddressIndexRepository(this.db, this.loggerFactory);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public sealed class AddressIndexerOutpointsRepository : MemoryCache<string, OutP

/// <summary>Represents the output collection.</summary>
/// <remarks>Should be protected by <see cref="LockObject"/></remarks>
private readonly LiteCollection<OutPointData> addressIndexerOutPointData;
private readonly ILiteCollection<OutPointData> addressIndexerOutPointData;

/// <summary>Represents the rewind data collection.</summary>
/// <remarks>Should be protected by <see cref="LockObject"/></remarks>
private readonly LiteCollection<AddressIndexerRewindData> addressIndexerRewindData;
private readonly ILiteCollection<AddressIndexerRewindData> addressIndexerRewindData;

private readonly ILogger logger;

Expand Down Expand Up @@ -119,7 +119,9 @@ public void RecordRewindData(AddressIndexerRewindData rewindData)
public void PurgeOldRewindData(int height)
{
// Delete all in one go based on query. This is more optimal than query, iterate and delete individual records.
int purgedCount = this.addressIndexerRewindData.Delete(x => x.BlockHeight < height);
//BsonExpression predicate = Query.LT("BlockHeight", height);
// int purgedCount = this.addressIndexerRewindData.DeleteMany(predicate);
int purgedCount = this.addressIndexerRewindData.DeleteMany(x => x.BlockHeight < height);

this.logger.LogInformation("Purged {0} rewind data items.", purgedCount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class AddressIndexerOutpointsRepositoryTests

public AddressIndexerOutpointsRepositoryTests()
{
LiteDB.FileMode fileMode = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? LiteDB.FileMode.Exclusive : LiteDB.FileMode.Shared;
var db = new LiteDatabase(new ConnectionString() { Filename = this.RandomString(20) + ".db", Upgrade = true, Mode = fileMode });
ConnectionType connectionType = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ConnectionType.Direct : ConnectionType.Shared;
var db = new LiteDatabase(new ConnectionString() { Filename = this.RandomString(20) + ".db", Upgrade = true, Connection = connectionType });

this.repository = new AddressIndexerOutpointsRepository(db, new ExtendedLoggerFactory(), this.maxItems);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ public void OutPointCacheCanRetrieveExisting()
var dataFolder = new DataFolder(TestBase.CreateTestDir(this));
string dbPath = Path.Combine(dataFolder.RootPath, CollectionName);

LiteDB.FileMode fileMode = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? LiteDB.FileMode.Exclusive : LiteDB.FileMode.Shared;
var database = new LiteDatabase(new ConnectionString() { Filename = dbPath, Upgrade = true, Mode = fileMode });
ConnectionType connectionType = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ConnectionType.Direct : ConnectionType.Shared;
var database = new LiteDatabase(new ConnectionString() { Filename = dbPath, Upgrade = true, Connection = connectionType });
var cache = new AddressIndexerOutpointsRepository(database, new ExtendedLoggerFactory());

var outPoint = new OutPoint(uint256.Parse("0000af9ab2c8660481328d0444cf167dfd31f24ca2dbba8e5e963a2434cffa93"), 0);
Expand All @@ -204,8 +204,8 @@ public void OutPointCacheCannotRetrieveNonexistent()
var dataFolder = new DataFolder(TestBase.CreateTestDir(this));
string dbPath = Path.Combine(dataFolder.RootPath, CollectionName);

LiteDB.FileMode fileMode = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? LiteDB.FileMode.Exclusive : LiteDB.FileMode.Shared;
var database = new LiteDatabase(new ConnectionString() { Filename = dbPath, Upgrade = true, Mode = fileMode });
ConnectionType connectionType = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ConnectionType.Direct : ConnectionType.Shared;
var database = new LiteDatabase(new ConnectionString() { Filename = dbPath, Upgrade = true, Connection = connectionType });
var cache = new AddressIndexerOutpointsRepository(database, new ExtendedLoggerFactory());

Assert.False(cache.TryGetOutPointData(new OutPoint(uint256.Parse("0000af9ab2c8660481328d0444cf167dfd31f24ca2dbba8e5e963a2434cffa93"), 1), out OutPointData retrieved));
Expand All @@ -219,8 +219,8 @@ public void OutPointCacheEvicts()
var dataFolder = new DataFolder(TestBase.CreateTestDir(this));
string dbPath = Path.Combine(dataFolder.RootPath, CollectionName);

LiteDB.FileMode fileMode = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? LiteDB.FileMode.Exclusive : LiteDB.FileMode.Shared;
var database = new LiteDatabase(new ConnectionString() { Filename = dbPath, Upgrade = true, Mode = fileMode });
ConnectionType connectionType = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ConnectionType.Direct : ConnectionType.Shared;
var database = new LiteDatabase(new ConnectionString() { Filename = dbPath, Upgrade = true, Connection = connectionType });
var cache = new AddressIndexerOutpointsRepository(database, new ExtendedLoggerFactory(), 2);

Assert.Equal(0, cache.Count);
Expand Down Expand Up @@ -270,8 +270,8 @@ public void AddressCacheCanRetrieveExisting()
var dataFolder = new DataFolder(TestBase.CreateTestDir(this));
string dbPath = Path.Combine(dataFolder.RootPath, CollectionName);

LiteDB.FileMode fileMode = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? LiteDB.FileMode.Exclusive : LiteDB.FileMode.Shared;
var database = new LiteDatabase(new ConnectionString() { Filename = dbPath, Upgrade = true, Mode = fileMode });
ConnectionType connectionType = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ConnectionType.Direct : ConnectionType.Shared;
var database = new LiteDatabase(new ConnectionString() { Filename = dbPath, Upgrade = true, Connection = connectionType });
var cache = new AddressIndexRepository(database, new ExtendedLoggerFactory());

string address = "xyz";
Expand Down Expand Up @@ -299,8 +299,8 @@ public void AddressCacheRetrievesBlankRecordForNonexistent()
var dataFolder = new DataFolder(TestBase.CreateTestDir(this));
string dbPath = Path.Combine(dataFolder.RootPath, CollectionName);

LiteDB.FileMode fileMode = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? LiteDB.FileMode.Exclusive : LiteDB.FileMode.Shared;
var database = new LiteDatabase(new ConnectionString() { Filename = dbPath, Upgrade = true, Mode = fileMode });
ConnectionType connectionType = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ConnectionType.Direct : ConnectionType.Shared;
var database = new LiteDatabase(new ConnectionString() { Filename = dbPath, Upgrade = true, Connection = connectionType });
var cache = new AddressIndexRepository(database, new ExtendedLoggerFactory());

AddressIndexerData retrieved = cache.GetOrCreateAddress("xyz");
Expand All @@ -318,8 +318,8 @@ public void AddressCacheEvicts()
var dataFolder = new DataFolder(TestBase.CreateTestDir(this));
string dbPath = Path.Combine(dataFolder.RootPath, CollectionName);

LiteDB.FileMode fileMode = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? LiteDB.FileMode.Exclusive : LiteDB.FileMode.Shared;
var database = new LiteDatabase(new ConnectionString() { Filename = dbPath, Upgrade = true, Mode = fileMode });
ConnectionType connectionType = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ConnectionType.Direct : ConnectionType.Shared;
var database = new LiteDatabase(new ConnectionString() { Filename = dbPath, Upgrade = true, Connection = connectionType });
var cache = new AddressIndexRepository(database, new ExtendedLoggerFactory(), 4);

// Recall, each index entry counts as 1 and each balance change associated with it is an additional 1.
Expand Down

0 comments on commit 25d97a1

Please sign in to comment.