Skip to content

Commit

Permalink
Code Quality: Downgrade LiteDB to fix transaction issue (files-commun…
Browse files Browse the repository at this point in the history
  • Loading branch information
hez2010 authored Apr 17, 2024
1 parent 20e6c60 commit 0947237
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 58 deletions.
58 changes: 25 additions & 33 deletions src/Files.App.Server/Database/FileTagsDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,29 @@ namespace Files.App.Server.Database
{
public sealed class FileTagsDatabase
{
private static LiteDatabase _database = default!;
private static readonly object _lockObject = new();

private const string TaggedFiles = "taggedfiles";
private readonly static LiteDatabase Database;
private readonly static string FileTagsDbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "filetags.db");

public static string FileTagsDbPath
=> Path.Combine(ApplicationData.Current.LocalFolder.Path, "filetags.db");

public FileTagsDatabase()
static FileTagsDatabase()
{
lock (_lockObject)
{
if (_database is null)
{
SafetyExtensions.IgnoreExceptions(() => CheckDbVersion(FileTagsDbPath));
SafetyExtensions.IgnoreExceptions(() => CheckDbVersion(FileTagsDbPath));

_database = new LiteDatabase(new ConnectionString(FileTagsDbPath)
{
Connection = ConnectionType.Direct,
Upgrade = true
});
Database = new LiteDatabase(new ConnectionString(FileTagsDbPath)
{
Connection = ConnectionType.Direct,
Upgrade = true
});

UpdateDb();
}
}
UpdateDb();
}

public static string GetFileTagsDbPath() => FileTagsDbPath;

public void SetTags(string filePath, ulong? frn, [ReadOnlyArray] string[] tags)
{
// Get a collection (or create, if doesn't exist)
var col = _database.GetCollection<TaggedFile>(TaggedFiles);
var col = Database.GetCollection<TaggedFile>(TaggedFiles);

var tmp = FindTag(filePath, frn);
if (tmp is null)
Expand Down Expand Up @@ -81,7 +73,7 @@ public void SetTags(string filePath, ulong? frn, [ReadOnlyArray] string[] tags)
private TaggedFile? FindTag(string? filePath, ulong? frn)
{
// Get a collection (or create, if doesn't exist)
var col = _database.GetCollection<TaggedFile>(TaggedFiles);
var col = Database.GetCollection<TaggedFile>(TaggedFiles);

if (filePath is not null)
{
Expand Down Expand Up @@ -122,7 +114,7 @@ public void SetTags(string filePath, ulong? frn, [ReadOnlyArray] string[] tags)
public void UpdateTag(string oldFilePath, ulong? frn, string? newFilePath)
{
// Get a collection (or create, if doesn't exist)
var col = _database.GetCollection<TaggedFile>(TaggedFiles);
var col = Database.GetCollection<TaggedFile>(TaggedFiles);
var tmp = col.FindOne(x => x.FilePath == oldFilePath);
if (tmp is not null)
{
Expand All @@ -144,7 +136,7 @@ public void UpdateTag(string oldFilePath, ulong? frn, string? newFilePath)
public void UpdateTag(ulong oldFrn, ulong? frn, string? newFilePath)
{
// Get a collection (or create, if doesn't exist)
var col = _database.GetCollection<TaggedFile>(TaggedFiles);
var col = Database.GetCollection<TaggedFile>(TaggedFiles);
var tmp = col.FindOne(x => x.Frn == oldFrn);
if (tmp is not null)
{
Expand All @@ -169,13 +161,13 @@ public string[] GetTags(string? filePath, ulong? frn)

public IEnumerable<TaggedFile> GetAll()
{
var col = _database.GetCollection<TaggedFile>(TaggedFiles);
var col = Database.GetCollection<TaggedFile>(TaggedFiles);
return col.FindAll();
}

public IEnumerable<TaggedFile> GetAllUnderPath(string folderPath)
{
var col = _database.GetCollection<TaggedFile>(TaggedFiles);
var col = Database.GetCollection<TaggedFile>(TaggedFiles);
if (string.IsNullOrEmpty(folderPath))
return col.FindAll();
return col.Find(x => x.FilePath.StartsWith(folderPath, StringComparison.OrdinalIgnoreCase));
Expand All @@ -184,33 +176,33 @@ public IEnumerable<TaggedFile> GetAllUnderPath(string folderPath)
public void Import(string json)
{
var dataValues = JsonSerializer.DeserializeArray(json);
var col = _database.GetCollection(TaggedFiles);
var col = Database.GetCollection(TaggedFiles);
col.DeleteAll();
col.InsertBulk(dataValues.Select(x => x.AsDocument));
}

public string Export()
{
return JsonSerializer.Serialize(new BsonArray(_database.GetCollection(TaggedFiles).FindAll()));
return JsonSerializer.Serialize(new BsonArray(Database.GetCollection(TaggedFiles).FindAll()));
}

private void UpdateDb()
private static void UpdateDb()
{
if (_database.UserVersion == 0)
if (Database.UserVersion == 0)
{
var col = _database.GetCollection(TaggedFiles);
var col = Database.GetCollection(TaggedFiles);
foreach (var doc in col.FindAll())
{
doc["Tags"] = new BsonValue(new[] { doc["Tag"].AsString });
doc.Remove("Tags");
col.Update(doc);
}
_database.UserVersion = 1;
Database.UserVersion = 1;
}
}

// https://github.com/mbdavid/LiteDB/blob/master/LiteDB/Engine/Engine/Upgrade.cs
private void CheckDbVersion(string filename)
private static void CheckDbVersion(string filename)
{
var buffer = new byte[8192 * 2];
using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
Expand Down
37 changes: 15 additions & 22 deletions src/Files.App.Server/Database/LayoutPreferencesDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,18 @@ namespace Files.App.Server.Database
{
public sealed class LayoutPreferencesDatabase
{
private static LiteDatabase _database = default!;
private static readonly object _lockObject = new();

private const string LayoutPreferences = "layoutprefs";
private readonly static LiteDatabase Database;
private readonly static string LayoutSettingsDbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "user_settings.db");

public static string LayoutSettingsDbPath
=> Path.Combine(ApplicationData.Current.LocalFolder.Path, "user_settings.db");

public LayoutPreferencesDatabase()
static LayoutPreferencesDatabase()
{
lock (_lockObject)
{
_database ??= new(
new ConnectionString(LayoutSettingsDbPath)
{
Connection = ConnectionType.Direct,
Upgrade = true,
});
}
Database = new(
new ConnectionString(LayoutSettingsDbPath)
{
Connection = ConnectionType.Direct,
Upgrade = true,
});
}

public LayoutPreferencesItem? GetPreferences(string? filePath, ulong? frn)
Expand All @@ -38,7 +31,7 @@ public LayoutPreferencesDatabase()
public void SetPreferences(string filePath, ulong? frn, LayoutPreferencesItem? preferencesItem)
{
// Get a collection (or create, if doesn't exist)
var col = _database.GetCollection<LayoutPreferences>(LayoutPreferences);
var col = Database.GetCollection<LayoutPreferences>(LayoutPreferences);

var tmp = FindPreferences(filePath, frn);

Expand Down Expand Up @@ -77,14 +70,14 @@ public void SetPreferences(string filePath, ulong? frn, LayoutPreferencesItem? p

public void ResetAll()
{
var col = _database.GetCollection<LayoutPreferences>(LayoutPreferences);
var col = Database.GetCollection<LayoutPreferences>(LayoutPreferences);

col.DeleteAll();
}

public void ApplyToAll(LayoutPreferencesUpdateAction updateAction)
{
var col = _database.GetCollection<LayoutPreferences>(LayoutPreferences);
var col = Database.GetCollection<LayoutPreferences>(LayoutPreferences);

var allDocs = col.FindAll();

Expand All @@ -100,21 +93,21 @@ public void Import(string json)
{
var dataValues = JsonSerializer.DeserializeArray(json);

var col = _database.GetCollection(LayoutPreferences);
var col = Database.GetCollection(LayoutPreferences);

col.DeleteAll();
col.InsertBulk(dataValues.Select(x => x.AsDocument));
}

public string Export()
{
return JsonSerializer.Serialize(new BsonArray(_database.GetCollection(LayoutPreferences).FindAll()));
return JsonSerializer.Serialize(new BsonArray(Database.GetCollection(LayoutPreferences).FindAll()));
}

private LayoutPreferences? FindPreferences(string? filePath, ulong? frn)
{
// Get a collection (or create, if doesn't exist)
var col = _database.GetCollection<LayoutPreferences>(LayoutPreferences);
var col = Database.GetCollection<LayoutPreferences>(LayoutPreferences);

if (filePath is not null)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Files.App.Server/Files.App.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
<ItemGroup>
<Manifest Include="app.manifest" />
<TrimmerRootAssembly Include="Files.App.Server" />
<PackageReference Include="LiteDB" Version="5.0.19" />
<!-- Don't upgrade LiteDB until https://github.com/mbdavid/LiteDB/issues/2435 gets fixed -->
<PackageReference Include="LiteDB" Version="5.0.17" />
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.0.7" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.1.647-beta" PrivateAssets="all" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Files.App/ViewModels/Settings/AdvancedViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private async Task ImportSettingsAsync()
var fileTagsList = await zipFolder.GetFileAsync(Constants.LocalSettings.FileTagSettingsFileName);
string importTags = await fileTagsList.ReadTextAsync();
fileTagsSettingsService.ImportSettings(importTags);
var fileTagsDB = await zipFolder.GetFileAsync(Path.GetFileName(Server.Database.FileTagsDatabase.FileTagsDbPath));
var fileTagsDB = await zipFolder.GetFileAsync(Path.GetFileName(Server.Database.FileTagsDatabase.GetFileTagsDbPath()));
string importTagsDB = await fileTagsDB.ReadTextAsync();
var tagDbInstance = FileTagsHelper.GetDbInstance();
tagDbInstance.Import(importTagsDB);
Expand Down Expand Up @@ -224,7 +224,7 @@ private async Task ExportSettingsAsync()
await zipFolder.CreateFileAsync(new MemoryStream(exportTags), Constants.LocalSettings.FileTagSettingsFileName, CreationCollisionOption.ReplaceExisting);
var tagDbInstance = FileTagsHelper.GetDbInstance();
byte[] exportTagsDB = UTF8Encoding.UTF8.GetBytes(tagDbInstance.Export());
await zipFolder.CreateFileAsync(new MemoryStream(exportTagsDB), Path.GetFileName(Server.Database.FileTagsDatabase.FileTagsDbPath), CreationCollisionOption.ReplaceExisting);
await zipFolder.CreateFileAsync(new MemoryStream(exportTagsDB), Path.GetFileName(Server.Database.FileTagsDatabase.GetFileTagsDbPath()), CreationCollisionOption.ReplaceExisting);

// Export layout preferences DB
var layoutDbInstance = LayoutPreferencesManager.GetDatabaseManagerInstance();
Expand Down

0 comments on commit 0947237

Please sign in to comment.