Skip to content

Commit 0947237

Browse files
authored
Code Quality: Downgrade LiteDB to fix transaction issue (#15217)
1 parent 20e6c60 commit 0947237

File tree

4 files changed

+44
-58
lines changed

4 files changed

+44
-58
lines changed

src/Files.App.Server/Database/FileTagsDatabase.cs

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,29 @@ namespace Files.App.Server.Database
1313
{
1414
public sealed class FileTagsDatabase
1515
{
16-
private static LiteDatabase _database = default!;
17-
private static readonly object _lockObject = new();
18-
1916
private const string TaggedFiles = "taggedfiles";
17+
private readonly static LiteDatabase Database;
18+
private readonly static string FileTagsDbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "filetags.db");
2019

21-
public static string FileTagsDbPath
22-
=> Path.Combine(ApplicationData.Current.LocalFolder.Path, "filetags.db");
23-
24-
public FileTagsDatabase()
20+
static FileTagsDatabase()
2521
{
26-
lock (_lockObject)
27-
{
28-
if (_database is null)
29-
{
30-
SafetyExtensions.IgnoreExceptions(() => CheckDbVersion(FileTagsDbPath));
22+
SafetyExtensions.IgnoreExceptions(() => CheckDbVersion(FileTagsDbPath));
3123

32-
_database = new LiteDatabase(new ConnectionString(FileTagsDbPath)
33-
{
34-
Connection = ConnectionType.Direct,
35-
Upgrade = true
36-
});
24+
Database = new LiteDatabase(new ConnectionString(FileTagsDbPath)
25+
{
26+
Connection = ConnectionType.Direct,
27+
Upgrade = true
28+
});
3729

38-
UpdateDb();
39-
}
40-
}
30+
UpdateDb();
4131
}
4232

33+
public static string GetFileTagsDbPath() => FileTagsDbPath;
34+
4335
public void SetTags(string filePath, ulong? frn, [ReadOnlyArray] string[] tags)
4436
{
4537
// Get a collection (or create, if doesn't exist)
46-
var col = _database.GetCollection<TaggedFile>(TaggedFiles);
38+
var col = Database.GetCollection<TaggedFile>(TaggedFiles);
4739

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

8678
if (filePath is not null)
8779
{
@@ -122,7 +114,7 @@ public void SetTags(string filePath, ulong? frn, [ReadOnlyArray] string[] tags)
122114
public void UpdateTag(string oldFilePath, ulong? frn, string? newFilePath)
123115
{
124116
// Get a collection (or create, if doesn't exist)
125-
var col = _database.GetCollection<TaggedFile>(TaggedFiles);
117+
var col = Database.GetCollection<TaggedFile>(TaggedFiles);
126118
var tmp = col.FindOne(x => x.FilePath == oldFilePath);
127119
if (tmp is not null)
128120
{
@@ -144,7 +136,7 @@ public void UpdateTag(string oldFilePath, ulong? frn, string? newFilePath)
144136
public void UpdateTag(ulong oldFrn, ulong? frn, string? newFilePath)
145137
{
146138
// Get a collection (or create, if doesn't exist)
147-
var col = _database.GetCollection<TaggedFile>(TaggedFiles);
139+
var col = Database.GetCollection<TaggedFile>(TaggedFiles);
148140
var tmp = col.FindOne(x => x.Frn == oldFrn);
149141
if (tmp is not null)
150142
{
@@ -169,13 +161,13 @@ public string[] GetTags(string? filePath, ulong? frn)
169161

170162
public IEnumerable<TaggedFile> GetAll()
171163
{
172-
var col = _database.GetCollection<TaggedFile>(TaggedFiles);
164+
var col = Database.GetCollection<TaggedFile>(TaggedFiles);
173165
return col.FindAll();
174166
}
175167

176168
public IEnumerable<TaggedFile> GetAllUnderPath(string folderPath)
177169
{
178-
var col = _database.GetCollection<TaggedFile>(TaggedFiles);
170+
var col = Database.GetCollection<TaggedFile>(TaggedFiles);
179171
if (string.IsNullOrEmpty(folderPath))
180172
return col.FindAll();
181173
return col.Find(x => x.FilePath.StartsWith(folderPath, StringComparison.OrdinalIgnoreCase));
@@ -184,33 +176,33 @@ public IEnumerable<TaggedFile> GetAllUnderPath(string folderPath)
184176
public void Import(string json)
185177
{
186178
var dataValues = JsonSerializer.DeserializeArray(json);
187-
var col = _database.GetCollection(TaggedFiles);
179+
var col = Database.GetCollection(TaggedFiles);
188180
col.DeleteAll();
189181
col.InsertBulk(dataValues.Select(x => x.AsDocument));
190182
}
191183

192184
public string Export()
193185
{
194-
return JsonSerializer.Serialize(new BsonArray(_database.GetCollection(TaggedFiles).FindAll()));
186+
return JsonSerializer.Serialize(new BsonArray(Database.GetCollection(TaggedFiles).FindAll()));
195187
}
196188

197-
private void UpdateDb()
189+
private static void UpdateDb()
198190
{
199-
if (_database.UserVersion == 0)
191+
if (Database.UserVersion == 0)
200192
{
201-
var col = _database.GetCollection(TaggedFiles);
193+
var col = Database.GetCollection(TaggedFiles);
202194
foreach (var doc in col.FindAll())
203195
{
204196
doc["Tags"] = new BsonValue(new[] { doc["Tag"].AsString });
205197
doc.Remove("Tags");
206198
col.Update(doc);
207199
}
208-
_database.UserVersion = 1;
200+
Database.UserVersion = 1;
209201
}
210202
}
211203

212204
// https://github.com/mbdavid/LiteDB/blob/master/LiteDB/Engine/Engine/Upgrade.cs
213-
private void CheckDbVersion(string filename)
205+
private static void CheckDbVersion(string filename)
214206
{
215207
var buffer = new byte[8192 * 2];
216208
using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))

src/Files.App.Server/Database/LayoutPreferencesDatabase.cs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,18 @@ namespace Files.App.Server.Database
99
{
1010
public sealed class LayoutPreferencesDatabase
1111
{
12-
private static LiteDatabase _database = default!;
13-
private static readonly object _lockObject = new();
14-
1512
private const string LayoutPreferences = "layoutprefs";
13+
private readonly static LiteDatabase Database;
14+
private readonly static string LayoutSettingsDbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "user_settings.db");
1615

17-
public static string LayoutSettingsDbPath
18-
=> Path.Combine(ApplicationData.Current.LocalFolder.Path, "user_settings.db");
19-
20-
public LayoutPreferencesDatabase()
16+
static LayoutPreferencesDatabase()
2117
{
22-
lock (_lockObject)
23-
{
24-
_database ??= new(
25-
new ConnectionString(LayoutSettingsDbPath)
26-
{
27-
Connection = ConnectionType.Direct,
28-
Upgrade = true,
29-
});
30-
}
18+
Database = new(
19+
new ConnectionString(LayoutSettingsDbPath)
20+
{
21+
Connection = ConnectionType.Direct,
22+
Upgrade = true,
23+
});
3124
}
3225

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

4336
var tmp = FindPreferences(filePath, frn);
4437

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

7871
public void ResetAll()
7972
{
80-
var col = _database.GetCollection<LayoutPreferences>(LayoutPreferences);
73+
var col = Database.GetCollection<LayoutPreferences>(LayoutPreferences);
8174

8275
col.DeleteAll();
8376
}
8477

8578
public void ApplyToAll(LayoutPreferencesUpdateAction updateAction)
8679
{
87-
var col = _database.GetCollection<LayoutPreferences>(LayoutPreferences);
80+
var col = Database.GetCollection<LayoutPreferences>(LayoutPreferences);
8881

8982
var allDocs = col.FindAll();
9083

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

103-
var col = _database.GetCollection(LayoutPreferences);
96+
var col = Database.GetCollection(LayoutPreferences);
10497

10598
col.DeleteAll();
10699
col.InsertBulk(dataValues.Select(x => x.AsDocument));
107100
}
108101

109102
public string Export()
110103
{
111-
return JsonSerializer.Serialize(new BsonArray(_database.GetCollection(LayoutPreferences).FindAll()));
104+
return JsonSerializer.Serialize(new BsonArray(Database.GetCollection(LayoutPreferences).FindAll()));
112105
}
113106

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

119112
if (filePath is not null)
120113
{

src/Files.App.Server/Files.App.Server.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
<ItemGroup>
4040
<Manifest Include="app.manifest" />
4141
<TrimmerRootAssembly Include="Files.App.Server" />
42-
<PackageReference Include="LiteDB" Version="5.0.19" />
42+
<!-- Don't upgrade LiteDB until https://github.com/mbdavid/LiteDB/issues/2435 gets fixed -->
43+
<PackageReference Include="LiteDB" Version="5.0.17" />
4344
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.0.7" />
4445
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.1.647-beta" PrivateAssets="all" />
4546
</ItemGroup>

src/Files.App/ViewModels/Settings/AdvancedViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private async Task ImportSettingsAsync()
176176
var fileTagsList = await zipFolder.GetFileAsync(Constants.LocalSettings.FileTagSettingsFileName);
177177
string importTags = await fileTagsList.ReadTextAsync();
178178
fileTagsSettingsService.ImportSettings(importTags);
179-
var fileTagsDB = await zipFolder.GetFileAsync(Path.GetFileName(Server.Database.FileTagsDatabase.FileTagsDbPath));
179+
var fileTagsDB = await zipFolder.GetFileAsync(Path.GetFileName(Server.Database.FileTagsDatabase.GetFileTagsDbPath()));
180180
string importTagsDB = await fileTagsDB.ReadTextAsync();
181181
var tagDbInstance = FileTagsHelper.GetDbInstance();
182182
tagDbInstance.Import(importTagsDB);
@@ -224,7 +224,7 @@ private async Task ExportSettingsAsync()
224224
await zipFolder.CreateFileAsync(new MemoryStream(exportTags), Constants.LocalSettings.FileTagSettingsFileName, CreationCollisionOption.ReplaceExisting);
225225
var tagDbInstance = FileTagsHelper.GetDbInstance();
226226
byte[] exportTagsDB = UTF8Encoding.UTF8.GetBytes(tagDbInstance.Export());
227-
await zipFolder.CreateFileAsync(new MemoryStream(exportTagsDB), Path.GetFileName(Server.Database.FileTagsDatabase.FileTagsDbPath), CreationCollisionOption.ReplaceExisting);
227+
await zipFolder.CreateFileAsync(new MemoryStream(exportTagsDB), Path.GetFileName(Server.Database.FileTagsDatabase.GetFileTagsDbPath()), CreationCollisionOption.ReplaceExisting);
228228

229229
// Export layout preferences DB
230230
var layoutDbInstance = LayoutPreferencesManager.GetDatabaseManagerInstance();

0 commit comments

Comments
 (0)