don't serialize user data in db

This commit is contained in:
Luke Pulverenti 2013-05-09 09:46:06 -04:00
parent fbd052abfc
commit 8f61e37498
3 changed files with 39 additions and 15 deletions

View File

@ -17,13 +17,13 @@ namespace MediaBrowser.Controller.Entities
/// <summary> /// <summary>
/// The _rating /// The _rating
/// </summary> /// </summary>
private float? _rating; private double? _rating;
/// <summary> /// <summary>
/// Gets or sets the users 0-10 rating /// Gets or sets the users 0-10 rating
/// </summary> /// </summary>
/// <value>The rating.</value> /// <value>The rating.</value>
/// <exception cref="System.ArgumentOutOfRangeException">Rating;A 0 to 10 rating is required for UserItemData.</exception> /// <exception cref="System.ArgumentOutOfRangeException">Rating;A 0 to 10 rating is required for UserItemData.</exception>
public float? Rating public double? Rating
{ {
get get
{ {

View File

@ -12,7 +12,7 @@ namespace MediaBrowser.Model.Dto
/// Gets or sets the rating. /// Gets or sets the rating.
/// </summary> /// </summary>
/// <value>The rating.</value> /// <value>The rating.</value>
public float? Rating { get; set; } public double? Rating { get; set; }
/// <summary> /// <summary>
/// Gets or sets the playback position ticks. /// Gets or sets the playback position ticks.

View File

@ -94,8 +94,9 @@ namespace MediaBrowser.Server.Implementations.Sqlite
string[] queries = { string[] queries = {
"create table if not exists userdata (key nvarchar, userId GUID, data BLOB)", "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 userdataindex on userdata (key, userId)", "create unique index if not exists useritemdataindex on useritemdata (key, userId)",
"create table if not exists schema_version (table_name primary key, version)", "create table if not exists schema_version (table_name primary key, version)",
//pragmas //pragmas
"pragma temp_store = memory" "pragma temp_store = memory"
@ -180,16 +181,26 @@ namespace MediaBrowser.Server.Implementations.Sqlite
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
var serialized = _jsonSerializer.SerializeToBytes(userData);
cancellationToken.ThrowIfCancellationRequested();
using (var cmd = connection.CreateCommand()) using (var cmd = connection.CreateCommand())
{ {
cmd.CommandText = "replace into userdata (key, userId, data) values (@1, @2, @3)"; cmd.CommandText = "replace into useritemdata (key, userId, Rating,PlaybackPositionTicks,PlayCount,IsFavorite,Played,LastPlayedDate) values (@1, @2, @3, @4, @5, @6, @7, @8)";
cmd.AddParam("@1", key); cmd.AddParam("@1", key);
cmd.AddParam("@2", userId); cmd.AddParam("@2", userId);
cmd.AddParam("@3", serialized);
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);
}
using (var tran = connection.BeginTransaction()) using (var tran = connection.BeginTransaction())
{ {
@ -249,7 +260,7 @@ namespace MediaBrowser.Server.Implementations.Sqlite
{ {
using (var cmd = connection.CreateCommand()) using (var cmd = connection.CreateCommand())
{ {
cmd.CommandText = "select data from userdata where key = @key and userId=@userId"; cmd.CommandText = "select Rating,PlaybackPositionTicks,PlayCount,IsFavorite,Played,LastPlayedDate from useritemdata where key = @key and userId=@userId";
var idParam = cmd.Parameters.Add("@key", DbType.String); var idParam = cmd.Parameters.Add("@key", DbType.String);
idParam.Value = key; idParam.Value = key;
@ -257,18 +268,31 @@ namespace MediaBrowser.Server.Implementations.Sqlite
var userIdParam = cmd.Parameters.Add("@userId", DbType.Guid); var userIdParam = cmd.Parameters.Add("@userId", DbType.Guid);
userIdParam.Value = userId; userIdParam.Value = userId;
var userdata = new UserItemData();
using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow).ConfigureAwait(false)) using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow).ConfigureAwait(false))
{ {
if (reader.Read()) if (reader.Read())
{ {
using (var stream = GetStream(reader, 0)) if (!reader.IsDBNull(0))
{ {
return _jsonSerializer.DeserializeFromStream<UserItemData>(stream); 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);
var ticks = (long?) reader.GetValue(5);
if (ticks.HasValue)
{
userdata.LastPlayedDate = new DateTime(ticks.Value);
} }
} }
} }
return new UserItemData(); return userdata;
} }
} }
} }