aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Services/Data/IDataManager.cs
blob: 6a87c1b2d8e9b63b9573c9227cbd6c31fb9e8bac (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
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Timeline.Services.Data
{
    /// <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>
        /// <param name="cancellationToken">Cancellation token.</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, CancellationToken cancellationToken = default);

        /// <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>
        /// <param name="cancellationToken">Cancellation token.</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, CancellationToken cancellationToken = default);

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