|
| 1 | +using System; |
1 | 2 | using System.Collections.Generic; |
2 | 3 | using System.Linq; |
3 | 4 | using LiteDB; |
|
6 | 7 |
|
7 | 8 | namespace LibVideo.Data |
8 | 9 | { |
9 | | - public class DatabaseManager |
| 10 | + public class DatabaseManager : IDisposable |
10 | 11 | { |
11 | 12 | private readonly string dbPath; |
| 13 | + private readonly LiteDatabase _db; |
12 | 14 |
|
13 | 15 | public DatabaseManager() |
14 | 16 | { |
15 | 17 | dbPath = AppPaths.DatabaseFile; |
| 18 | + // LiteDatabase instance is thread-safe. |
| 19 | + _db = new LiteDatabase($"Filename={dbPath};Connection=Shared"); |
16 | 20 | } |
17 | 21 |
|
18 | 22 | public void SyncDiskItems(IEnumerable<VideoItem> currentFilesList, HashSet<string> scannedRoots) |
19 | 23 | { |
20 | | - using (var db = new LiteDatabase($"Filename={dbPath};Connection=Shared")) |
21 | | - { |
22 | | - var col = db.GetCollection<VideoItem>("videos"); |
23 | | - col.EnsureIndex(x => x.FullName, true); |
24 | | - |
25 | | - var allDb = col.FindAll().ToList(); |
26 | | - var currentPaths = new HashSet<string>(currentFilesList.Select(x => x.FullName), System.StringComparer.OrdinalIgnoreCase); |
| 24 | + var col = _db.GetCollection<VideoItem>("videos"); |
| 25 | + col.EnsureIndex(x => x.FullName, true); |
| 26 | + |
| 27 | + var allDb = col.FindAll().ToList(); |
| 28 | + var currentPaths = new HashSet<string>(currentFilesList.Select(x => x.FullName), StringComparer.OrdinalIgnoreCase); |
27 | 29 |
|
28 | | - foreach(var dbItem in allDb) |
| 30 | + foreach(var dbItem in allDb) |
| 31 | + { |
| 32 | + bool isUnderScannedRoot = scannedRoots.Any(root => dbItem.FullName.StartsWith(root, StringComparison.OrdinalIgnoreCase)); |
| 33 | + if (isUnderScannedRoot) |
29 | 34 | { |
30 | | - bool isUnderScannedRoot = scannedRoots.Any(root => dbItem.FullName.StartsWith(root, System.StringComparison.OrdinalIgnoreCase)); |
31 | | - if (isUnderScannedRoot) |
| 35 | + if (!currentPaths.Contains(dbItem.FullName)) |
32 | 36 | { |
33 | | - if (!currentPaths.Contains(dbItem.FullName)) |
34 | | - { |
35 | | - col.Delete(dbItem.Id); |
36 | | - } |
| 37 | + col.Delete(dbItem.Id); |
37 | 38 | } |
38 | 39 | } |
39 | | - |
40 | | - var existingFiles = new HashSet<string>(col.FindAll().Select(v => v.FullName), System.StringComparer.OrdinalIgnoreCase); |
41 | | - var newItems = currentFilesList.Where(i => !existingFiles.Contains(i.FullName)).ToList(); |
| 40 | + } |
| 41 | + |
| 42 | + var existingFiles = new HashSet<string>(col.FindAll().Select(v => v.FullName), StringComparer.OrdinalIgnoreCase); |
| 43 | + var newItems = currentFilesList.Where(i => !existingFiles.Contains(i.FullName)).ToList(); |
42 | 44 |
|
43 | | - if (newItems.Count > 0) |
44 | | - { |
45 | | - col.InsertBulk(newItems); |
46 | | - } |
| 45 | + if (newItems.Count > 0) |
| 46 | + { |
| 47 | + col.InsertBulk(newItems); |
47 | 48 | } |
48 | 49 | } |
49 | 50 |
|
50 | 51 | public List<VideoItem> GetAllItems() |
51 | 52 | { |
52 | | - using (var db = new LiteDatabase(dbPath)) |
53 | | - { |
54 | | - var col = db.GetCollection<VideoItem>("videos"); |
55 | | - return col.FindAll().ToList(); |
56 | | - } |
| 53 | + var col = _db.GetCollection<VideoItem>("videos"); |
| 54 | + return col.FindAll().ToList(); |
57 | 55 | } |
58 | 56 |
|
59 | 57 | public void ClearItems() |
60 | 58 | { |
61 | | - using (var db = new LiteDatabase(dbPath)) |
62 | | - { |
63 | | - db.DropCollection("videos"); |
64 | | - } |
| 59 | + _db.DropCollection("videos"); |
65 | 60 | } |
66 | 61 |
|
67 | 62 | public void RemoveDirectoryItems(string directoryPath) |
68 | 63 | { |
69 | | - using (var db = new LiteDatabase(dbPath)) |
| 64 | + var col = _db.GetCollection<VideoItem>("videos"); |
| 65 | + var itemsToDelete = col.Find(x => x.FullName.StartsWith(directoryPath)).Select(x => x.Id).ToList(); |
| 66 | + foreach (var id in itemsToDelete) |
70 | 67 | { |
71 | | - var col = db.GetCollection<VideoItem>("videos"); |
72 | | - var itemsToDelete = col.Find(x => x.FullName.StartsWith(directoryPath)).Select(x => x.Id).ToList(); |
73 | | - foreach (var id in itemsToDelete) |
74 | | - { |
75 | | - col.Delete(id); |
76 | | - } |
| 68 | + col.Delete(id); |
77 | 69 | } |
78 | 70 | } |
79 | 71 |
|
80 | 72 | public void UpdateItemMetadata(VideoItem memItem) |
81 | 73 | { |
82 | | - using (var db = new LiteDatabase(dbPath)) |
| 74 | + var col = _db.GetCollection<VideoItem>("videos"); |
| 75 | + var dbItem = col.FindOne(x => x.FullName == memItem.FullName); |
| 76 | + if (dbItem != null) |
83 | 77 | { |
84 | | - var col = db.GetCollection<VideoItem>("videos"); |
85 | | - var dbItem = col.FindOne(x => x.FullName == memItem.FullName); |
86 | | - if (dbItem != null) |
87 | | - { |
88 | | - dbItem.MetaTitle = memItem.MetaTitle; |
89 | | - dbItem.MetaPlot = memItem.MetaPlot; |
90 | | - dbItem.MetaGenre = memItem.MetaGenre; |
91 | | - dbItem.MetaPosterPath = memItem.MetaPosterPath; |
92 | | - dbItem.MetaRating = memItem.MetaRating; |
93 | | - dbItem.HasScraped = memItem.HasScraped; |
94 | | - col.Update(dbItem); |
95 | | - } |
| 78 | + dbItem.MetaTitle = memItem.MetaTitle; |
| 79 | + dbItem.MetaPlot = memItem.MetaPlot; |
| 80 | + dbItem.MetaGenre = memItem.MetaGenre; |
| 81 | + dbItem.MetaPosterPath = memItem.MetaPosterPath; |
| 82 | + dbItem.MetaRating = memItem.MetaRating; |
| 83 | + dbItem.HasScraped = memItem.HasScraped; |
| 84 | + col.Update(dbItem); |
96 | 85 | } |
97 | 86 | } |
| 87 | + |
| 88 | + public void Dispose() |
| 89 | + { |
| 90 | + _db?.Dispose(); |
| 91 | + } |
98 | 92 | } |
99 | 93 | } |
0 commit comments