aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Services/DataManager.cs
blob: d6b8b6a4b6efe99c1f7310efe42049493b958154 (plain)
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
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading.Tasks;
using Timeline.Entities;

namespace Timeline.Services
{
    /// <summary>
    /// A data manager controlling data.
    /// </summary>
    /// <remarks>
    /// Identical data will be saved as one copy and return the same tag.
    /// Every data has a ref count. When data is retained, ref count increase.
    /// When data is freed, ref count decease. If ref count is decreased
    /// to 0, the data entry will be destroyed and no longer occupy space.
    /// </remarks>
    public interface IDataManager
    {
        /// <summary>
        /// Saves the data to a new entry if it does not exist, 
        /// increases its ref count and returns a tag to the entry.
        /// </summary>
        /// <param name="data">The data. Can't be null.</param>
        /// <returns>The tag of the created entry.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="data"/> is null.</exception>
        public Task<string> RetainEntry(byte[] data);

        /// <summary>
        /// Decrease the the ref count of the entry.
        /// Remove it if ref count is zero.
        /// </summary>
        /// <param name="tag">The tag of the entry.</param>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="tag"/> is null.</exception>
        /// <remarks>
        /// It's no-op if entry with tag does not exist.
        /// </remarks>
        public Task FreeEntry(string tag);

        /// <summary>
        /// Retrieve the entry with given tag.
        /// </summary>
        /// <param name="tag">The tag of the entry.</param>
        /// <returns>The data of the entry.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="tag"/> is null.</exception>
        /// <exception cref="InvalidOperationException">Thrown when entry with given tag does not exist.</exception>
        public Task<byte[]> GetEntry(string tag);
    }

    public class DataManager : IDataManager
    {
        private readonly DatabaseContext _database;
        private readonly IETagGenerator _eTagGenerator;

        public DataManager(DatabaseContext database, IETagGenerator eTagGenerator)
        {
            _database = database;
            _eTagGenerator = eTagGenerator;
        }

        public async Task<string> RetainEntry(byte[] data)
        {
            if (data == null)
                throw new ArgumentNullException(nameof(data));

            var tag = await _eTagGenerator.Generate(data);

            var entity = await _database.Data.Where(d => d.Tag == tag).SingleOrDefaultAsync();

            if (entity == null)
            {
                entity = new DataEntity
                {
                    Tag = tag,
                    Data = data,
                    Ref = 1
                };
                _database.Data.Add(entity);
                await _database.SaveChangesAsync();
            }
            else
            {
                entity.Ref += 1;
                await _database.SaveChangesAsync();
            }
            return tag;
        }

        public async Task FreeEntry(string tag)
        {
            if (tag == null)
                throw new ArgumentNullException(nameof(tag));

            var entity = await _database.Data.Where(d => d.Tag == tag).SingleOrDefaultAsync();

            if (entity != null)
            {
                if (entity.Ref == 1)
                {
                    _database.Data.Remove(entity);
                }
                else
                {
                    entity.Ref -= 1;
                }
                await _database.SaveChangesAsync();
            }
        }

        public async Task<byte[]> GetEntry(string tag)
        {
            if (tag == null)
                throw new ArgumentNullException(nameof(tag));

            var entity = await _database.Data.Where(d => d.Tag == tag).Select(d => new { d.Data }).SingleOrDefaultAsync();

            if (entity == null)
                throw new InvalidOperationException(Resources.Services.DataManager.ExceptionEntryNotExist);

            return entity.Data;
        }
    }
}