aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Services/DataManager.cs
blob: 005ad23c37c18a837830469bb443e5acd5b7b9a4 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Timeline.Services
{
    public class DataEntry
    {
#pragma warning disable CA1819 // Properties should not return arrays
        public byte[] Data { get; set; } = default!;
#pragma warning restore CA1819 // Properties should not return arrays
        public DataInfo Info { get; set; } = default!;
    }

    public class DataInfo
    {
        public DateTime Time { get; set; }
        public string Type { get; set; } = default!;
    }

    /// <summary>
    /// A data manager controling data.
    /// </summary>
    /// <remarks>
    /// All data to be saved will be checked identity.
    /// Identical data will be saved as one copy and return the same tag.
    /// Every data has a ref count. When data is saved, ref count increase.
    /// When data is removed, ref count decease. If ref count is decreased
    /// to 0, the data entry will be destroyed and no longer occupy space.
    /// 
    /// Type is just an attached attribute for convenience and not participate
    /// in identity verification. This should be only used to save blobs but not
    /// strings. It will be rare for identity blob with different type, I think.
    /// </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>
        /// <param name="type">The type of the data. Can't be null.</param>
        /// <returns>The tag of the created entry.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="data"/> or <paramref name="type"/> is null.</exception>
        /// <exception cref="InvalidOperationException">Thrown when a saved copy of data already exists but type is different.</exception>
        public Task<string> RetainEntry(byte[] data, string type);

        /// <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 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<DataEntry> GetEntry(string tag);

        /// <summary>
        /// Retrieve info of the entry with given tag.
        /// </summary>
        /// <param name="tag">The tag of the entry.</param>
        /// <returns>The entry info.</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<DataInfo> GetEntryInfo(string tag);
    }

}