From f233f9c4e519b87b7d6e43d1da08bd0fab12a6c4 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 9 May 2013 16:29:50 -0400 Subject: [PATCH] schema-less user data --- .../Sqlite/SQLiteUserDataRepository.cs | 54 ++++++------------- 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/MediaBrowser.Server.Implementations/Sqlite/SQLiteUserDataRepository.cs b/MediaBrowser.Server.Implementations/Sqlite/SQLiteUserDataRepository.cs index 6583ab2ee..443b15c10 100644 --- a/MediaBrowser.Server.Implementations/Sqlite/SQLiteUserDataRepository.cs +++ b/MediaBrowser.Server.Implementations/Sqlite/SQLiteUserDataRepository.cs @@ -18,7 +18,7 @@ namespace MediaBrowser.Server.Implementations.Sqlite public class SQLiteUserDataRepository : SqliteRepository, IUserDataRepository { private readonly ConcurrentDictionary> _userData = new ConcurrentDictionary>(); - + /// /// The repository name /// @@ -47,7 +47,7 @@ namespace MediaBrowser.Server.Implementations.Sqlite return false; } } - + private readonly IJsonSerializer _jsonSerializer; /// @@ -94,9 +94,8 @@ namespace MediaBrowser.Server.Implementations.Sqlite string[] queries = { - "create table if not exists useritemdata (key nvarchar, userId GUID, Rating float null, PlaybackPositionTicks bigint, PlayCount int, IsFavorite bit, Played bit, LastPlayedDate bigint null)", - "create unique index if not exists useritemdataindex on useritemdata (key, userId)", - + "create table if not exists userdata (key nvarchar, userId GUID, data BLOB)", + "create unique index if not exists userdataindex on userdata (key, userId)", "create table if not exists schema_version (table_name primary key, version)", //pragmas "pragma temp_store = memory" @@ -181,26 +180,16 @@ namespace MediaBrowser.Server.Implementations.Sqlite { cancellationToken.ThrowIfCancellationRequested(); + var serialized = _jsonSerializer.SerializeToBytes(userData); + + cancellationToken.ThrowIfCancellationRequested(); + using (var cmd = connection.CreateCommand()) { - cmd.CommandText = "replace into useritemdata (key, userId, Rating,PlaybackPositionTicks,PlayCount,IsFavorite,Played,LastPlayedDate) values (@1, @2, @3, @4, @5, @6, @7, @8)"; + cmd.CommandText = "replace into userdata (key, userId, data) values (@1, @2, @3)"; cmd.AddParam("@1", key); cmd.AddParam("@2", userId); - - cmd.AddParam("@3", userData.Rating); - cmd.AddParam("@4", userData.PlaybackPositionTicks); - cmd.AddParam("@5", userData.PlayCount); - cmd.AddParam("@6", userData.IsFavorite); - cmd.AddParam("@7", userData.Played); - - if (userData.LastPlayedDate.HasValue) - { - cmd.AddParam("@8", userData.LastPlayedDate.Value.Ticks); - } - else - { - cmd.AddParam("@8", null); - } + cmd.AddParam("@3", serialized); using (var tran = connection.BeginTransaction()) { @@ -246,7 +235,7 @@ namespace MediaBrowser.Server.Implementations.Sqlite { throw new ArgumentNullException("key"); } - + return _userData.GetOrAdd(GetInternalKey(userId, key), keyName => RetrieveUserData(userId, key)); } @@ -260,7 +249,7 @@ namespace MediaBrowser.Server.Implementations.Sqlite { using (var cmd = connection.CreateCommand()) { - cmd.CommandText = "select Rating,PlaybackPositionTicks,PlayCount,IsFavorite,Played,LastPlayedDate from useritemdata where key = @key and userId=@userId"; + cmd.CommandText = "select data from userdata where key = @key and userId=@userId"; var idParam = cmd.Parameters.Add("@key", DbType.String); idParam.Value = key; @@ -268,30 +257,19 @@ namespace MediaBrowser.Server.Implementations.Sqlite var userIdParam = cmd.Parameters.Add("@userId", DbType.Guid); userIdParam.Value = userId; - var userdata = new UserItemData(); - using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow).ConfigureAwait(false)) { if (reader.Read()) { - if (!reader.IsDBNull(0)) + using (var stream = GetStream(reader, 0)) { - userdata.Rating = reader.GetDouble(0); - } - userdata.PlaybackPositionTicks = reader.GetInt64(1); - userdata.PlayCount = reader.GetInt32(2); - userdata.IsFavorite = reader.GetBoolean(3); - userdata.Played = reader.GetBoolean(4); - - if (!reader.IsDBNull(5)) - { - userdata.LastPlayedDate = new DateTime(reader.GetInt64(5)); + return _jsonSerializer.DeserializeFromStream(stream); } } } - return userdata; + return new UserItemData(); } } } -} +} \ No newline at end of file