2020-02-06 15:20:23 +01:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
#pragma warning disable SA1600
|
|
|
|
|
2019-01-13 20:54:44 +01:00
|
|
|
using System;
|
2015-07-20 20:32:55 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2019-01-13 20:22:00 +01:00
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2015-07-20 20:32:55 +02:00
|
|
|
|
2016-11-04 00:35:19 +01:00
|
|
|
namespace Emby.Server.Implementations.LiveTv.EmbyTV
|
2015-07-20 20:32:55 +02:00
|
|
|
{
|
|
|
|
public class ItemDataProvider<T>
|
|
|
|
where T : class
|
|
|
|
{
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
|
|
private readonly string _dataPath;
|
2019-09-12 21:30:57 +02:00
|
|
|
private readonly object _fileDataLock = new object();
|
|
|
|
private T[] _items;
|
2015-07-20 20:32:55 +02:00
|
|
|
|
2019-09-12 21:30:57 +02:00
|
|
|
public ItemDataProvider(
|
|
|
|
IJsonSerializer jsonSerializer,
|
|
|
|
ILogger logger,
|
|
|
|
string dataPath,
|
|
|
|
Func<T, T, bool> equalityComparer)
|
2015-07-20 20:32:55 +02:00
|
|
|
{
|
2019-09-12 21:30:57 +02:00
|
|
|
_jsonSerializer = jsonSerializer;
|
2015-07-20 20:32:55 +02:00
|
|
|
Logger = logger;
|
|
|
|
_dataPath = dataPath;
|
|
|
|
EqualityComparer = equalityComparer;
|
|
|
|
}
|
|
|
|
|
2019-09-12 21:30:57 +02:00
|
|
|
protected ILogger Logger { get; }
|
2019-02-24 15:47:59 +01:00
|
|
|
|
2019-09-12 21:30:57 +02:00
|
|
|
protected Func<T, T, bool> EqualityComparer { get; }
|
2015-07-20 20:32:55 +02:00
|
|
|
|
2019-09-12 21:30:57 +02:00
|
|
|
private void EnsureLoaded()
|
2015-07-20 20:32:55 +02:00
|
|
|
{
|
2019-09-12 21:30:57 +02:00
|
|
|
if (_items != null)
|
2015-07-20 20:32:55 +02:00
|
|
|
{
|
2019-09-12 21:30:57 +02:00
|
|
|
return;
|
2015-07-20 20:32:55 +02:00
|
|
|
}
|
2019-09-12 21:30:57 +02:00
|
|
|
|
|
|
|
if (File.Exists(_dataPath))
|
2015-07-20 20:32:55 +02:00
|
|
|
{
|
2019-09-12 21:30:57 +02:00
|
|
|
Logger.LogInformation("Loading live tv data from {Path}", _dataPath);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_items = _jsonSerializer.DeserializeFromFile<T[]>(_dataPath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Logger.LogError(ex, "Error deserializing {Path}", _dataPath);
|
|
|
|
}
|
2015-07-20 20:32:55 +02:00
|
|
|
}
|
2019-02-20 14:29:34 +01:00
|
|
|
|
2019-09-12 21:30:57 +02:00
|
|
|
_items = Array.Empty<T>();
|
2015-07-20 20:32:55 +02:00
|
|
|
}
|
|
|
|
|
2019-09-12 21:30:57 +02:00
|
|
|
private void SaveList()
|
2015-07-20 20:32:55 +02:00
|
|
|
{
|
2019-02-24 15:47:59 +01:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(_dataPath));
|
2019-09-12 21:30:57 +02:00
|
|
|
_jsonSerializer.SerializeToFile(_items, _dataPath);
|
|
|
|
}
|
2015-07-24 01:40:54 +02:00
|
|
|
|
2019-09-12 21:30:57 +02:00
|
|
|
public IReadOnlyList<T> GetAll()
|
|
|
|
{
|
2015-07-20 20:32:55 +02:00
|
|
|
lock (_fileDataLock)
|
|
|
|
{
|
2019-09-12 21:30:57 +02:00
|
|
|
EnsureLoaded();
|
|
|
|
return (T[])_items.Clone();
|
2015-07-20 20:32:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Update(T item)
|
|
|
|
{
|
2016-03-13 04:30:23 +01:00
|
|
|
if (item == null)
|
|
|
|
{
|
2019-01-06 21:50:43 +01:00
|
|
|
throw new ArgumentNullException(nameof(item));
|
2016-03-13 04:30:23 +01:00
|
|
|
}
|
|
|
|
|
2019-09-12 21:30:57 +02:00
|
|
|
lock (_fileDataLock)
|
2015-07-20 20:32:55 +02:00
|
|
|
{
|
2019-09-12 21:30:57 +02:00
|
|
|
EnsureLoaded();
|
2015-07-20 20:32:55 +02:00
|
|
|
|
2019-09-12 21:30:57 +02:00
|
|
|
var index = Array.FindIndex(_items, i => EqualityComparer(i, item));
|
|
|
|
if (index == -1)
|
|
|
|
{
|
|
|
|
throw new ArgumentException("item not found");
|
|
|
|
}
|
2015-07-20 20:32:55 +02:00
|
|
|
|
2019-09-12 21:30:57 +02:00
|
|
|
_items[index] = item;
|
|
|
|
|
|
|
|
SaveList();
|
|
|
|
}
|
2015-07-20 20:32:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Add(T item)
|
|
|
|
{
|
2016-03-13 04:30:23 +01:00
|
|
|
if (item == null)
|
|
|
|
{
|
2019-01-06 21:50:43 +01:00
|
|
|
throw new ArgumentNullException(nameof(item));
|
2016-03-13 04:30:23 +01:00
|
|
|
}
|
|
|
|
|
2019-09-12 21:30:57 +02:00
|
|
|
lock (_fileDataLock)
|
2015-07-20 20:32:55 +02:00
|
|
|
{
|
2019-09-12 21:30:57 +02:00
|
|
|
EnsureLoaded();
|
2015-07-20 20:32:55 +02:00
|
|
|
|
2019-09-12 21:30:57 +02:00
|
|
|
if (_items.Any(i => EqualityComparer(i, item)))
|
|
|
|
{
|
|
|
|
throw new ArgumentException("item already exists", nameof(item));
|
|
|
|
}
|
2015-07-20 20:32:55 +02:00
|
|
|
|
2019-09-12 21:30:57 +02:00
|
|
|
int oldLen = _items.Length;
|
|
|
|
var newList = new T[oldLen + 1];
|
|
|
|
_items.CopyTo(newList, 0);
|
|
|
|
newList[oldLen] = item;
|
|
|
|
_items = newList;
|
|
|
|
|
|
|
|
SaveList();
|
|
|
|
}
|
2015-07-20 20:32:55 +02:00
|
|
|
}
|
|
|
|
|
2019-09-12 21:30:57 +02:00
|
|
|
public virtual void AddOrUpdate(T item)
|
2015-11-18 05:18:51 +01:00
|
|
|
{
|
2019-09-12 21:30:57 +02:00
|
|
|
lock (_fileDataLock)
|
2015-11-18 05:18:51 +01:00
|
|
|
{
|
2019-09-12 21:30:57 +02:00
|
|
|
EnsureLoaded();
|
|
|
|
|
|
|
|
int index = Array.FindIndex(_items, i => EqualityComparer(i, item));
|
|
|
|
if (index == -1)
|
|
|
|
{
|
|
|
|
int oldLen = _items.Length;
|
|
|
|
var newList = new T[oldLen + 1];
|
|
|
|
_items.CopyTo(newList, 0);
|
|
|
|
newList[oldLen] = item;
|
|
|
|
_items = newList;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_items[index] = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
SaveList();
|
2015-11-18 05:18:51 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-21 05:55:26 +01:00
|
|
|
|
2015-07-20 20:32:55 +02:00
|
|
|
public virtual void Delete(T item)
|
|
|
|
{
|
2019-09-12 21:30:57 +02:00
|
|
|
lock (_fileDataLock)
|
|
|
|
{
|
|
|
|
EnsureLoaded();
|
|
|
|
_items = _items.Where(i => !EqualityComparer(i, item)).ToArray();
|
2015-07-20 20:32:55 +02:00
|
|
|
|
2019-09-12 21:30:57 +02:00
|
|
|
SaveList();
|
|
|
|
}
|
2015-07-20 20:32:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|