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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Server.Implementations.Reflection;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Sqlite
{
/// <summary>
/// Class SQLiteItemRepository
/// </summary>
public class SQLiteItemRepository : SqliteRepository, IItemRepository
{
/// <summary>
/// The _type mapper
/// </summary>
private readonly TypeMapper _typeMapper = new TypeMapper();
/// <summary>
/// The repository name
/// </summary>
public const string RepositoryName = "SQLite";
/// <summary>
/// Gets the name of the repository
/// </summary>
/// <value>The name.</value>
public string Name
{
get
{
return RepositoryName;
}
}
/// <summary>
/// Gets the json serializer.
/// </summary>
/// <value>The json serializer.</value>
private readonly IJsonSerializer _jsonSerializer;
/// <summary>
/// The _app paths
/// </summary>
private readonly IApplicationPaths _appPaths;
/// <summary>
/// The _save item command
/// </summary>
private SQLiteCommand _saveItemCommand;
/// <summary>
/// The _delete children command
/// </summary>
private SQLiteCommand _deleteChildrenCommand;
/// <summary>
/// The _save children command
/// </summary>
private SQLiteCommand _saveChildrenCommand;
/// <summary>
/// Initializes a new instance of the <see cref="SQLiteUserDataRepository" /> class.
/// </summary>
/// <param name="appPaths">The app paths.</param>
/// <param name="jsonSerializer">The json serializer.</param>
/// <param name="logManager">The log manager.</param>
/// <exception cref="System.ArgumentNullException">appPaths</exception>
public SQLiteItemRepository(IApplicationPaths appPaths, IJsonSerializer jsonSerializer, ILogManager logManager)
: base(logManager)
{
if (appPaths == null)
{
throw new ArgumentNullException("appPaths");
}
if (jsonSerializer == null)
{
throw new ArgumentNullException("jsonSerializer");
}
_appPaths = appPaths;
_jsonSerializer = jsonSerializer;
}
/// <summary>
/// Opens the connection to the database
/// </summary>
/// <returns>Task.</returns>
public async Task Initialize()
{
var dbFile = Path.Combine(_appPaths.DataPath, "library.db");
await ConnectToDb(dbFile).ConfigureAwait(false);
string[] queries = {
"create table if not exists items (guid GUID primary key, obj_type, data BLOB)",
"create index if not exists idx_items on items(guid)",
"create table if not exists children (guid GUID, child GUID)",
"create unique index if not exists idx_children on children(guid, child)",
"create table if not exists schema_version (table_name primary key, version)",
//triggers
TriggerSql,
//pragmas
"pragma temp_store = memory"
};
RunQueries(queries);
PrepareStatements();
}
//cascade delete triggers
/// <summary>
/// The trigger SQL
/// </summary>
protected string TriggerSql =
@"CREATE TRIGGER if not exists delete_item
AFTER DELETE
ON items
FOR EACH ROW
BEGIN
DELETE FROM children WHERE children.guid = old.child;
DELETE FROM children WHERE children.child = old.child;
END";
/// <summary>
/// The _write lock
/// </summary>
private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1);
/// <summary>
/// Prepares the statements.
/// </summary>
private void PrepareStatements()
{
_saveItemCommand = new SQLiteCommand
{
CommandText = "replace into items (guid, obj_type, data) values (@1, @2, @3)"
};
_saveItemCommand.Parameters.Add(new SQLiteParameter("@1"));
_saveItemCommand.Parameters.Add(new SQLiteParameter("@2"));
_saveItemCommand.Parameters.Add(new SQLiteParameter("@3"));
_deleteChildrenCommand = new SQLiteCommand
{
CommandText = "delete from children where guid = @guid"
};
_deleteChildrenCommand.Parameters.Add(new SQLiteParameter("@guid"));
_saveChildrenCommand = new SQLiteCommand
{
CommandText = "replace into children (guid, child) values (@guid, @child)"
};
_saveChildrenCommand.Parameters.Add(new SQLiteParameter("@guid"));
_saveChildrenCommand.Parameters.Add(new SQLiteParameter("@child"));
}
/// <summary>
/// Save a standard item in the repo
/// </summary>
/// <param name="item">The item.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
/// <exception cref="System.ArgumentNullException">item</exception>
public Task SaveItem(BaseItem item, CancellationToken cancellationToken)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
return SaveItems(new[] { item }, cancellationToken);
}
/// <summary>
/// Saves the items.
/// </summary>
/// <param name="items">The items.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
/// <exception cref="System.ArgumentNullException">
/// items
/// or
/// cancellationToken
/// </exception>
public async Task SaveItems(IEnumerable<BaseItem> items, CancellationToken cancellationToken)
{
if (items == null)
{
throw new ArgumentNullException("items");
}
if (cancellationToken == null)
{
throw new ArgumentNullException("cancellationToken");
}
cancellationToken.ThrowIfCancellationRequested();
await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
SQLiteTransaction transaction = null;
try
{
transaction = Connection.BeginTransaction();
foreach (var item in items)
{
cancellationToken.ThrowIfCancellationRequested();
_saveItemCommand.Parameters[0].Value = item.Id;
_saveItemCommand.Parameters[1].Value = item.GetType().FullName;
_saveItemCommand.Parameters[2].Value = _jsonSerializer.SerializeToBytes(item);
_saveItemCommand.Transaction = transaction;
await _saveItemCommand.ExecuteNonQueryAsync(cancellationToken);
}
transaction.Commit();
}
catch (OperationCanceledException)
{
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
catch (Exception e)
{
Logger.ErrorException("Failed to save items:", e);
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
finally
{
if (transaction != null)
{
transaction.Dispose();
}
_writeLock.Release();
}
}
/// <summary>
/// Retrieve a standard item from the repo
/// </summary>
/// <param name="id">The id.</param>
/// <returns>BaseItem.</returns>
/// <exception cref="System.ArgumentNullException">id</exception>
/// <exception cref="System.ArgumentException"></exception>
public BaseItem GetItem(Guid id)
{
if (id == Guid.Empty)
{
throw new ArgumentNullException("id");
}
return RetrieveItemInternal(id);
}
/// <summary>
/// Retrieves the items.
/// </summary>
/// <param name="ids">The ids.</param>
/// <returns>IEnumerable{BaseItem}.</returns>
/// <exception cref="System.ArgumentNullException">ids</exception>
public IEnumerable<BaseItem> GetItems(IEnumerable<Guid> ids)
{
if (ids == null)
{
throw new ArgumentNullException("ids");
}
return ids.Select(RetrieveItemInternal);
}
/// <summary>
/// Internal retrieve from items or users table
/// </summary>
/// <param name="id">The id.</param>
/// <returns>BaseItem.</returns>
/// <exception cref="System.ArgumentNullException">id</exception>
/// <exception cref="System.ArgumentException"></exception>
protected BaseItem RetrieveItemInternal(Guid id)
{
if (id == Guid.Empty)
{
throw new ArgumentNullException("id");
}
using (var cmd = Connection.CreateCommand())
{
cmd.CommandText = "select obj_type,data from items where guid = @guid";
var guidParam = cmd.Parameters.Add("@guid", DbType.Guid);
guidParam.Value = id;
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow))
{
if (reader.Read())
{
var type = reader.GetString(0);
using (var stream = GetStream(reader, 1))
{
var itemType = _typeMapper.GetType(type);
if (itemType == null)
{
Logger.Error("Cannot find type {0}. Probably belongs to plug-in that is no longer loaded.", type);
return null;
}
var item = _jsonSerializer.DeserializeFromStream(stream, itemType);
return item as BaseItem;
}
}
}
return null;
}
}
/// <summary>
/// Retrieve all the children of the given folder
/// </summary>
/// <param name="parent">The parent.</param>
/// <returns>IEnumerable{BaseItem}.</returns>
/// <exception cref="System.ArgumentNullException"></exception>
public IEnumerable<BaseItem> RetrieveChildren(Folder parent)
{
if (parent == null)
{
throw new ArgumentNullException();
}
using (var cmd = Connection.CreateCommand())
{
cmd.CommandText = "select obj_type,data from items where guid in (select child from children where guid = @guid)";
var guidParam = cmd.Parameters.Add("@guid", DbType.Guid);
guidParam.Value = parent.Id;
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
{
while (reader.Read())
{
var type = reader.GetString(0);
using (var stream = GetStream(reader, 1))
{
var itemType = _typeMapper.GetType(type);
if (itemType == null)
{
Logger.Error("Cannot find type {0}. Probably belongs to plug-in that is no longer loaded.", type);
continue;
}
var item = _jsonSerializer.DeserializeFromStream(stream, itemType) as BaseItem;
if (item != null)
{
item.Parent = parent;
yield return item;
}
}
}
}
}
}
/// <summary>
/// Save references to all the children for the given folder
/// (Doesn't actually save the child entities)
/// </summary>
/// <param name="id">The id.</param>
/// <param name="children">The children.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
/// <exception cref="System.ArgumentNullException">id</exception>
public async Task SaveChildren(Guid id, IEnumerable<BaseItem> children, CancellationToken cancellationToken)
{
if (id == Guid.Empty)
{
throw new ArgumentNullException("id");
}
if (children == null)
{
throw new ArgumentNullException("children");
}
if (cancellationToken == null)
{
throw new ArgumentNullException("cancellationToken");
}
cancellationToken.ThrowIfCancellationRequested();
await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
SQLiteTransaction transaction = null;
try
{
transaction = Connection.BeginTransaction();
// Delete exising children
_deleteChildrenCommand.Parameters[0].Value = id;
_deleteChildrenCommand.Transaction = transaction;
await _deleteChildrenCommand.ExecuteNonQueryAsync(cancellationToken);
// Save new children
foreach (var child in children)
{
_saveChildrenCommand.Transaction = transaction;
_saveChildrenCommand.Parameters[0].Value = id;
_saveChildrenCommand.Parameters[1].Value = child.Id;
await _saveChildrenCommand.ExecuteNonQueryAsync(cancellationToken);
}
transaction.Commit();
}
catch (OperationCanceledException)
{
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
catch (Exception e)
{
Logger.ErrorException("Failed to save children:", e);
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
finally
{
if (transaction != null)
{
transaction.Dispose();
}
_writeLock.Release();
}
}
/// <summary>
/// Gets the critic reviews path.
/// </summary>
/// <value>The critic reviews path.</value>
private string CriticReviewsPath
{
get
{
var path = Path.Combine(_appPaths.DataPath, "critic-reviews");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
}
/// <summary>
/// Gets the critic reviews.
/// </summary>
/// <param name="itemId">The item id.</param>
/// <returns>Task{IEnumerable{ItemReview}}.</returns>
public Task<IEnumerable<ItemReview>> GetCriticReviews(Guid itemId)
{
return Task.Run<IEnumerable<ItemReview>>(() =>
{
try
{
var path = Path.Combine(CriticReviewsPath, itemId + ".json");
return _jsonSerializer.DeserializeFromFile<List<ItemReview>>(path);
}
catch (FileNotFoundException)
{
return new List<ItemReview>();
}
});
}
/// <summary>
/// Saves the critic reviews.
/// </summary>
/// <param name="itemId">The item id.</param>
/// <param name="criticReviews">The critic reviews.</param>
/// <returns>Task.</returns>
public Task SaveCriticReviews(Guid itemId, IEnumerable<ItemReview> criticReviews)
{
return Task.Run(() =>
{
var path = Path.Combine(CriticReviewsPath, itemId + ".json");
_jsonSerializer.SerializeToFile(criticReviews.ToList(), path);
});
}
}
}
|