@@ -13,37 +13,29 @@ namespace Files.App.Server.Database
13
13
{
14
14
public sealed class FileTagsDatabase
15
15
{
16
- private static LiteDatabase _database = default ! ;
17
- private static readonly object _lockObject = new ( ) ;
18
-
19
16
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" ) ;
20
19
21
- public static string FileTagsDbPath
22
- => Path . Combine ( ApplicationData . Current . LocalFolder . Path , "filetags.db" ) ;
23
-
24
- public FileTagsDatabase ( )
20
+ static FileTagsDatabase ( )
25
21
{
26
- lock ( _lockObject )
27
- {
28
- if ( _database is null )
29
- {
30
- SafetyExtensions . IgnoreExceptions ( ( ) => CheckDbVersion ( FileTagsDbPath ) ) ;
22
+ SafetyExtensions . IgnoreExceptions ( ( ) => CheckDbVersion ( FileTagsDbPath ) ) ;
31
23
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
+ } ) ;
37
29
38
- UpdateDb ( ) ;
39
- }
40
- }
30
+ UpdateDb ( ) ;
41
31
}
42
32
33
+ public static string GetFileTagsDbPath ( ) => FileTagsDbPath ;
34
+
43
35
public void SetTags ( string filePath , ulong ? frn , [ ReadOnlyArray ] string [ ] tags )
44
36
{
45
37
// Get a collection (or create, if doesn't exist)
46
- var col = _database . GetCollection < TaggedFile > ( TaggedFiles ) ;
38
+ var col = Database . GetCollection < TaggedFile > ( TaggedFiles ) ;
47
39
48
40
var tmp = FindTag ( filePath , frn ) ;
49
41
if ( tmp is null )
@@ -81,7 +73,7 @@ public void SetTags(string filePath, ulong? frn, [ReadOnlyArray] string[] tags)
81
73
private TaggedFile ? FindTag ( string ? filePath , ulong ? frn )
82
74
{
83
75
// Get a collection (or create, if doesn't exist)
84
- var col = _database . GetCollection < TaggedFile > ( TaggedFiles ) ;
76
+ var col = Database . GetCollection < TaggedFile > ( TaggedFiles ) ;
85
77
86
78
if ( filePath is not null )
87
79
{
@@ -122,7 +114,7 @@ public void SetTags(string filePath, ulong? frn, [ReadOnlyArray] string[] tags)
122
114
public void UpdateTag ( string oldFilePath , ulong ? frn , string ? newFilePath )
123
115
{
124
116
// Get a collection (or create, if doesn't exist)
125
- var col = _database . GetCollection < TaggedFile > ( TaggedFiles ) ;
117
+ var col = Database . GetCollection < TaggedFile > ( TaggedFiles ) ;
126
118
var tmp = col . FindOne ( x => x . FilePath == oldFilePath ) ;
127
119
if ( tmp is not null )
128
120
{
@@ -144,7 +136,7 @@ public void UpdateTag(string oldFilePath, ulong? frn, string? newFilePath)
144
136
public void UpdateTag ( ulong oldFrn , ulong ? frn , string ? newFilePath )
145
137
{
146
138
// Get a collection (or create, if doesn't exist)
147
- var col = _database . GetCollection < TaggedFile > ( TaggedFiles ) ;
139
+ var col = Database . GetCollection < TaggedFile > ( TaggedFiles ) ;
148
140
var tmp = col . FindOne ( x => x . Frn == oldFrn ) ;
149
141
if ( tmp is not null )
150
142
{
@@ -169,13 +161,13 @@ public string[] GetTags(string? filePath, ulong? frn)
169
161
170
162
public IEnumerable < TaggedFile > GetAll ( )
171
163
{
172
- var col = _database . GetCollection < TaggedFile > ( TaggedFiles ) ;
164
+ var col = Database . GetCollection < TaggedFile > ( TaggedFiles ) ;
173
165
return col . FindAll ( ) ;
174
166
}
175
167
176
168
public IEnumerable < TaggedFile > GetAllUnderPath ( string folderPath )
177
169
{
178
- var col = _database . GetCollection < TaggedFile > ( TaggedFiles ) ;
170
+ var col = Database . GetCollection < TaggedFile > ( TaggedFiles ) ;
179
171
if ( string . IsNullOrEmpty ( folderPath ) )
180
172
return col . FindAll ( ) ;
181
173
return col . Find ( x => x . FilePath . StartsWith ( folderPath , StringComparison . OrdinalIgnoreCase ) ) ;
@@ -184,33 +176,33 @@ public IEnumerable<TaggedFile> GetAllUnderPath(string folderPath)
184
176
public void Import ( string json )
185
177
{
186
178
var dataValues = JsonSerializer . DeserializeArray ( json ) ;
187
- var col = _database . GetCollection ( TaggedFiles ) ;
179
+ var col = Database . GetCollection ( TaggedFiles ) ;
188
180
col . DeleteAll ( ) ;
189
181
col . InsertBulk ( dataValues . Select ( x => x . AsDocument ) ) ;
190
182
}
191
183
192
184
public string Export ( )
193
185
{
194
- return JsonSerializer . Serialize ( new BsonArray ( _database . GetCollection ( TaggedFiles ) . FindAll ( ) ) ) ;
186
+ return JsonSerializer . Serialize ( new BsonArray ( Database . GetCollection ( TaggedFiles ) . FindAll ( ) ) ) ;
195
187
}
196
188
197
- private void UpdateDb ( )
189
+ private static void UpdateDb ( )
198
190
{
199
- if ( _database . UserVersion == 0 )
191
+ if ( Database . UserVersion == 0 )
200
192
{
201
- var col = _database . GetCollection ( TaggedFiles ) ;
193
+ var col = Database . GetCollection ( TaggedFiles ) ;
202
194
foreach ( var doc in col . FindAll ( ) )
203
195
{
204
196
doc [ "Tags" ] = new BsonValue ( new [ ] { doc [ "Tag" ] . AsString } ) ;
205
197
doc . Remove ( "Tags" ) ;
206
198
col . Update ( doc ) ;
207
199
}
208
- _database . UserVersion = 1 ;
200
+ Database . UserVersion = 1 ;
209
201
}
210
202
}
211
203
212
204
// 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 )
214
206
{
215
207
var buffer = new byte [ 8192 * 2 ] ;
216
208
using ( var stream = new FileStream ( filename , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) )
0 commit comments