From e677a57bf1cedc55214b0e457778311b8f1ea5ac Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 17 Jun 2013 16:35:43 -0400 Subject: switch to flat file storage --- .../Persistence/JsonItemRepository.cs | 235 +++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 MediaBrowser.Server.Implementations/Persistence/JsonItemRepository.cs (limited to 'MediaBrowser.Server.Implementations/Persistence/JsonItemRepository.cs') diff --git a/MediaBrowser.Server.Implementations/Persistence/JsonItemRepository.cs b/MediaBrowser.Server.Implementations/Persistence/JsonItemRepository.cs new file mode 100644 index 0000000000..d0333e334e --- /dev/null +++ b/MediaBrowser.Server.Implementations/Persistence/JsonItemRepository.cs @@ -0,0 +1,235 @@ +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.IO; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Persistence; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.Serialization; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Server.Implementations.Persistence +{ + public class JsonItemRepository : IItemRepository + { + private readonly ConcurrentDictionary _fileLocks = new ConcurrentDictionary(); + + private SemaphoreSlim GetLock(string filename) + { + return _fileLocks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1)); + } + + /// + /// Gets the name of the repository + /// + /// The name. + public string Name + { + get + { + return "Json"; + } + } + + /// + /// Gets the json serializer. + /// + /// The json serializer. + private readonly IJsonSerializer _jsonSerializer; + + private readonly string _criticReviewsPath; + + private readonly FileSystemRepository _itemRepo; + + /// + /// Initializes a new instance of the class. + /// + /// The app paths. + /// The json serializer. + /// The log manager. + /// appPaths + public JsonItemRepository(IApplicationPaths appPaths, IJsonSerializer jsonSerializer, ILogManager logManager) + { + if (appPaths == null) + { + throw new ArgumentNullException("appPaths"); + } + if (jsonSerializer == null) + { + throw new ArgumentNullException("jsonSerializer"); + } + + _jsonSerializer = jsonSerializer; + + _criticReviewsPath = Path.Combine(appPaths.DataPath, "critic-reviews"); + + _itemRepo = new FileSystemRepository(Path.Combine(appPaths.DataPath, "library")); + } + + /// + /// Opens the connection to the database + /// + /// Task. + public Task Initialize() + { + return Task.FromResult(true); + } + + /// + /// Save a standard item in the repo + /// + /// The item. + /// The cancellation token. + /// Task. + /// item + public async Task SaveItem(BaseItem item, CancellationToken cancellationToken) + { + if (item == null) + { + throw new ArgumentNullException("item"); + } + + if (!Directory.Exists(_criticReviewsPath)) + { + Directory.CreateDirectory(_criticReviewsPath); + } + + var path = _itemRepo.GetResourcePath(item.Id + ".json"); + + var parentPath = Path.GetDirectoryName(path); + if (!Directory.Exists(parentPath)) + { + Directory.CreateDirectory(parentPath); + } + + var semaphore = GetLock(path); + + await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); + + try + { + _jsonSerializer.SerializeToFile(item, path); + } + finally + { + semaphore.Release(); + } + } + + /// + /// Saves the items. + /// + /// The items. + /// The cancellation token. + /// Task. + /// + /// items + /// or + /// cancellationToken + /// + public Task SaveItems(IEnumerable items, CancellationToken cancellationToken) + { + if (items == null) + { + throw new ArgumentNullException("items"); + } + + if (cancellationToken == null) + { + throw new ArgumentNullException("cancellationToken"); + } + + var tasks = items.Select(i => SaveItem(i, cancellationToken)); + + return Task.WhenAll(tasks); + } + + /// + /// Retrieves the item. + /// + /// The id. + /// The type. + /// BaseItem. + /// id + public BaseItem RetrieveItem(Guid id, Type type) + { + if (id == Guid.Empty) + { + throw new ArgumentNullException("id"); + } + + var path = _itemRepo.GetResourcePath(id + ".json"); + + try + { + return (BaseItem)_jsonSerializer.DeserializeFromFile(type, path); + } + catch (IOException) + { + // File doesn't exist or is currently bring written to + return null; + } + } + + /// + /// Gets the critic reviews. + /// + /// The item id. + /// Task{IEnumerable{ItemReview}}. + public Task> GetCriticReviews(Guid itemId) + { + return Task.Run>(() => + { + var path = Path.Combine(_criticReviewsPath, itemId + ".json"); + + try + { + return _jsonSerializer.DeserializeFromFile>(path); + } + catch (IOException) + { + // File doesn't exist or is currently bring written to + return new List(); + } + }); + } + + /// + /// Saves the critic reviews. + /// + /// The item id. + /// The critic reviews. + /// Task. + public Task SaveCriticReviews(Guid itemId, IEnumerable criticReviews) + { + return Task.Run(() => + { + if (!Directory.Exists(_criticReviewsPath)) + { + Directory.CreateDirectory(_criticReviewsPath); + } + + var path = Path.Combine(_criticReviewsPath, itemId + ".json"); + + _jsonSerializer.SerializeToFile(criticReviews.ToList(), path); + }); + } + + public void Dispose() + { + // Wait up to two seconds for any existing writes to finish + var locks = _fileLocks.Values.ToList() + .Where(i => i.CurrentCount == 1) + .Select(i => i.WaitAsync(2000)); + + var task = Task.WhenAll(locks); + + Task.WaitAll(task); + } + } +} -- cgit v1.2.3