Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NF] Code cleanup #2512

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Remove the line below if you want to inherit .editorconfig settings from higher directories
root = true

[*]
charset = utf-8
end_of_line = crlf
indent_size = 4
indent_style = space
trim_trailing_whitespace = true

[*.cs]
csharp_style_namespace_declarations = file_scoped:warning
27 changes: 13 additions & 14 deletions ConsoleApp1/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using LiteDB;
using LiteDB.Engine;

using System.Reflection.Emit;
using System.Reflection.PortableExecutable;

var password = "46jLz5QWd5fI3m4LiL2r";
var path = $"C:\\LiteDB\\Examples\\CrashDB_{DateTime.Now.Ticks}.db";

Expand All @@ -14,27 +11,30 @@
Password = password
};

var data = Enumerable.Range(1, 10_000).Select(i => new BsonDocument
{
["_id"] = i,
["name"] = Faker.Fullname(),
["age"] = Faker.Age(),
["created"] = Faker.Birthday(),
["lorem"] = Faker.Lorem(5, 25)
}).ToArray();
var data = Enumerable.Range(1, 10_000)
.Select(
i => new BsonDocument
{
["_id"] = i,
["name"] = Faker.Fullname(),
["age"] = Faker.Age(),
["created"] = Faker.Birthday(),
["lorem"] = Faker.Lorem(5, 25)
})
.ToArray();

try
{
using (var db = new LiteEngine(settings))
{
#if DEBUG
db.SimulateDiskWriteFail = (page) =>
db.SimulateDiskWriteFail = page =>
{
var p = new BasePage(page);

if (p.PageID == 248)
{
page.Write((uint)123123123, 8192-4);
page.Write((uint) 123123123, 8192 - 4);
}
};
#endif
Expand Down Expand Up @@ -71,7 +71,6 @@
var errors = new BsonArray(db.Query("_rebuild_errors", Query.All()).ToList()).ToString();

Console.WriteLine("Errors: " + errors);

}

/*
Expand Down
376 changes: 364 additions & 12 deletions ConsoleApp1/Tools/Faker.Names.cs

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions ConsoleApp1/Tools/Faker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static DateTime Birthday()
{
var oldest = DateTime.Today.AddYears(-110).Ticks;
var now = DateTime.Now.Ticks;
var range = now - oldest;
var range = now - oldest;

var date = new DateTime(oldest + _random.NextLong(0, range));

Expand All @@ -29,8 +29,10 @@ public static DateTime Birthday()

public static string Lorem(int size, int end = -1)
{
return string.Join(" ", Enumerable.Range(1, end == -1 ? size : _random.Next(size, end))
.Select(x => _lorem[_random.Next(_lorem.Length - 1)]));
return string.Join(
" ",
Enumerable.Range(1, end == -1 ? size : _random.Next(size, end))
.Select(x => _lorem[_random.Next(_lorem.Length - 1)]));
}

public static int Next(int start, int end)
Expand All @@ -50,7 +52,7 @@ public static long NextLong(this Random random, long min, long max)
throw new ArgumentOutOfRangeException("max", "max must be > min!");

//Working with ulong so that modulo works correctly with values > long.MaxValue
ulong uRange = (ulong)(max - min);
ulong uRange = (ulong) (max - min);

//Prevent a modolo bias; see https://stackoverflow.com/a/10984975/238419
//for more information.
Expand All @@ -61,10 +63,10 @@ public static long NextLong(this Random random, long min, long max)
{
byte[] buf = new byte[8];
random.NextBytes(buf);
ulongRand = (ulong)BitConverter.ToInt64(buf, 0);
ulongRand = (ulong) BitConverter.ToInt64(buf, 0);
} while (ulongRand > ulong.MaxValue - ((ulong.MaxValue % uRange) + 1) % uRange);

return (long)(ulongRand % uRange) + min;
return (long) (ulongRand % uRange) + min;
}

public static bool NextBool(this Random random)
Expand Down Expand Up @@ -95,5 +97,4 @@ internal static BsonValue Created()

internal static string SkuNumber() =>
string.Join("", Enumerable.Range(1, 8).Select(x => _skuDigits[_random.Next(0, _skuDigits.Length - 1)]));

}
}
12 changes: 12 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>

<Project ToolsVersion="17.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
</PropertyGroup>

<PropertyGroup>
<LangVersion>latest</LangVersion>
</PropertyGroup>

</Project>
43 changes: 21 additions & 22 deletions LiteDB.Benchmarks/Benchmarks/BenchmarkBase.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
namespace LiteDB.Benchmarks.Benchmarks;

using BenchmarkDotNet.Attributes;

namespace LiteDB.Benchmarks.Benchmarks
public abstract class BenchmarkBase
{
public abstract class BenchmarkBase
{
// Insertion data size
[Params(10, 50, 100, 500, 1000, 5000, 10000)]
public int DatasetSize;
// Insertion data size
[Params(10, 50, 100, 500, 1000, 5000, 10000)]
public int DatasetSize;

public virtual string DatabasePath
{
get => Constants.DATABASE_NAME;
set => throw new System.NotImplementedException();
}
public virtual string DatabasePath
{
get => Constants.DATABASE_NAME;
set => throw new System.NotImplementedException();
}

[Params(ConnectionType.Direct)]
public ConnectionType ConnectionType;
[Params(ConnectionType.Direct)]
public ConnectionType ConnectionType;

[Params(null, "SecurePassword")]
public string Password;
[Params(null, "SecurePassword")]
public string Password;

public ConnectionString ConnectionString() => new ConnectionString(DatabasePath)
{
Connection = ConnectionType,
Password = Password
};
public ConnectionString ConnectionString() => new ConnectionString(DatabasePath)
{
Connection = ConnectionType,
Password = Password
};

protected ILiteDatabase DatabaseInstance { get; set; }
}
protected ILiteDatabase DatabaseInstance { get; set; }
}
21 changes: 10 additions & 11 deletions LiteDB.Benchmarks/Benchmarks/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
namespace LiteDB.Benchmarks.Benchmarks
namespace LiteDB.Benchmarks.Benchmarks;

internal static class Constants
{
internal static class Constants
{
internal const string DATABASE_NAME = "Lite.db";
internal const string DATABASE_NAME = "Lite.db";

internal static class Categories
{
internal const string DATA_GEN = nameof(DATA_GEN);
internal const string QUERIES = nameof(QUERIES);
internal const string INSERTION = nameof(INSERTION);
internal const string DELETION = nameof(DELETION);
}
internal static class Categories
{
internal const string DATA_GEN = nameof(DATA_GEN);
internal const string QUERIES = nameof(QUERIES);
internal const string INSERTION = nameof(INSERTION);
internal const string DELETION = nameof(DELETION);
}
}
122 changes: 62 additions & 60 deletions LiteDB.Benchmarks/Benchmarks/Deletion/DeletionBenchmark.cs
Original file line number Diff line number Diff line change
@@ -1,82 +1,84 @@
namespace LiteDB.Benchmarks.Benchmarks.Deletion;

using System.Collections.Generic;
using System.IO;
using System.Linq;
using BenchmarkDotNet.Attributes;
using LiteDB.Benchmarks.Models;
using LiteDB.Benchmarks.Models.Generators;

namespace LiteDB.Benchmarks.Benchmarks.Deletion
[BenchmarkCategory(Constants.Categories.DELETION)]
public class DeletionBenchmark : BenchmarkBase
{
[BenchmarkCategory(Constants.Categories.DELETION)]
public class DeletionBenchmark : BenchmarkBase
{
private List<FileMetaBase> _data;
private ILiteCollection<FileMetaBase> _fileMetaCollection;
private List<FileMetaBase> _data;
private ILiteCollection<FileMetaBase> _fileMetaCollection;

[GlobalSetup]
public void GlobalSetup()
{
File.Delete(DatabasePath);
[GlobalSetup]
public void GlobalSetup()
{
File.Delete(DatabasePath);

DatabaseInstance = new LiteDatabase(ConnectionString());
_fileMetaCollection = DatabaseInstance.GetCollection<FileMetaBase>();
_fileMetaCollection.EnsureIndex(file => file.IsFavorite);
_fileMetaCollection.EnsureIndex(file => file.ShouldBeShown);
DatabaseInstance = new LiteDatabase(ConnectionString());
_fileMetaCollection = DatabaseInstance.GetCollection<FileMetaBase>();
_fileMetaCollection.EnsureIndex(file => file.IsFavorite);
_fileMetaCollection.EnsureIndex(file => file.ShouldBeShown);

_data = FileMetaGenerator<FileMetaBase>.GenerateList(DatasetSize);
}
_data = FileMetaGenerator<FileMetaBase>.GenerateList(DatasetSize);
}

[IterationSetup]
public void IterationSetup()
{
_fileMetaCollection.Insert(_data);
DatabaseInstance.Checkpoint();
}
[IterationSetup]
public void IterationSetup()
{
_fileMetaCollection.Insert(_data);
DatabaseInstance.Checkpoint();
}

[Benchmark(Baseline = true)]
public int DeleteAllExpression()
{
var count = _fileMetaCollection.DeleteMany(_ => true);
DatabaseInstance.Checkpoint();
return count;
}
[Benchmark(Baseline = true)]
public int DeleteAllExpression()
{
var count = _fileMetaCollection.DeleteMany(_ => true);
DatabaseInstance.Checkpoint();
return count;
}

[Benchmark]
public int DeleteAllBsonExpression()
{
var count = _fileMetaCollection.DeleteMany("1 = 1");
DatabaseInstance.Checkpoint();
return count;
}
[Benchmark]
public int DeleteAllBsonExpression()
{
var count = _fileMetaCollection.DeleteMany("1 = 1");
DatabaseInstance.Checkpoint();
return count;
}

[Benchmark]
public void DropCollectionAndRecreate()
{
const string collectionName = nameof(FileMetaBase);
[Benchmark]
public void DropCollectionAndRecreate()
{
const string collectionName = nameof(FileMetaBase);

var indexesCollection = DatabaseInstance.GetCollection("$indexes");
var droppedCollectionIndexes = indexesCollection.Query().Where(x => x["collection"] == collectionName && x["name"] != "_id").ToDocuments().ToList();
var indexesCollection = DatabaseInstance.GetCollection("$indexes");
var droppedCollectionIndexes = indexesCollection.Query()
.Where(x => x["collection"] == collectionName && x["name"] != "_id")
.ToDocuments()
.ToList();

DatabaseInstance.DropCollection(collectionName);
DatabaseInstance.DropCollection(collectionName);

foreach (var indexInfo in droppedCollectionIndexes)
{
DatabaseInstance.GetCollection(collectionName)
.EnsureIndex(indexInfo["name"], BsonExpression.Create(indexInfo["expression"]), indexInfo["unique"]);
}
foreach (var indexInfo in droppedCollectionIndexes)
{
DatabaseInstance.GetCollection(collectionName)
.EnsureIndex(indexInfo["name"], BsonExpression.Create(indexInfo["expression"]), indexInfo["unique"]);
}

DatabaseInstance.Checkpoint();
}
DatabaseInstance.Checkpoint();
}

[GlobalCleanup]
public void GlobalCleanup()
{
// Disposing logic
DatabaseInstance?.Checkpoint();
DatabaseInstance?.Dispose();
DatabaseInstance = null;
[GlobalCleanup]
public void GlobalCleanup()
{
// Disposing logic
DatabaseInstance?.Checkpoint();
DatabaseInstance?.Dispose();
DatabaseInstance = null;

File.Delete(DatabasePath);
}
}
File.Delete(DatabasePath);
}
}
Loading