1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading;
using Jellyfin.Extensions.Json;
using Microsoft.Extensions.Logging;
namespace Jellyfin.LiveTv.Timers
{
public class ItemDataProvider<T>
where T : class
{
private readonly string _dataPath;
private readonly Lock _fileDataLock = new();
private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
private T[]? _items;
public ItemDataProvider(
ILogger logger,
string dataPath,
Func<T, T, bool> equalityComparer)
{
Logger = logger;
_dataPath = dataPath;
EqualityComparer = equalityComparer;
}
protected ILogger Logger { get; }
protected Func<T, T, bool> EqualityComparer { get; }
[MemberNotNull(nameof(_items))]
private void EnsureLoaded()
{
if (_items is not null)
{
return;
}
if (File.Exists(_dataPath))
{
Logger.LogInformation("Loading live tv data from {Path}", _dataPath);
try
{
var bytes = File.ReadAllBytes(_dataPath);
_items = JsonSerializer.Deserialize<T[]>(bytes, _jsonOptions);
if (_items is null)
{
Logger.LogError("Error deserializing {Path}, data was null", _dataPath);
_items = Array.Empty<T>();
}
return;
}
catch (JsonException ex)
{
Logger.LogError(ex, "Error deserializing {Path}", _dataPath);
}
}
_items = Array.Empty<T>();
}
private void SaveList()
{
Directory.CreateDirectory(Path.GetDirectoryName(_dataPath) ?? throw new ArgumentException("Path can't be a root directory.", nameof(_dataPath)));
var jsonString = JsonSerializer.Serialize(_items, _jsonOptions);
File.WriteAllText(_dataPath, jsonString);
}
public IReadOnlyList<T> GetAll()
{
lock (_fileDataLock)
{
EnsureLoaded();
return (T[])_items.Clone();
}
}
public virtual void Update(T item)
{
ArgumentNullException.ThrowIfNull(item);
lock (_fileDataLock)
{
EnsureLoaded();
var index = Array.FindIndex(_items, i => EqualityComparer(i, item));
if (index == -1)
{
throw new ArgumentException("item not found");
}
_items[index] = item;
SaveList();
}
}
public virtual void Add(T item)
{
ArgumentNullException.ThrowIfNull(item);
lock (_fileDataLock)
{
EnsureLoaded();
if (_items.Any(i => EqualityComparer(i, item)))
{
throw new ArgumentException("item already exists", nameof(item));
}
_items = [.._items, item];
SaveList();
}
}
public virtual void AddOrUpdate(T item)
{
lock (_fileDataLock)
{
EnsureLoaded();
int index = Array.FindIndex(_items, i => EqualityComparer(i, item));
if (index == -1)
{
_items = [.._items, item];
}
else
{
_items[index] = item;
}
SaveList();
}
}
public virtual void Delete(T item)
{
lock (_fileDataLock)
{
EnsureLoaded();
_items = _items.Where(i => !EqualityComparer(i, item)).ToArray();
SaveList();
}
}
}
}
|