aboutsummaryrefslogtreecommitdiff
path: root/VNLib.Plugins.Extensions.Data/Storage/LWStorageDescriptor.cs
blob: 72665f3c9c569681a975e3e18a01dc627be6c10a (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
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
/*
* Copyright (c) 2022 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Extensions.Data
* File: LWStorageDescriptor.cs 
*
* LWStorageDescriptor.cs is part of VNLib.Plugins.Extensions.Data which is part of the larger 
* VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Extensions.Data is free software: you can redistribute it and/or modify 
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 2 of the License,
* or (at your option) any later version.
*
* VNLib.Plugins.Extensions.Data is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License 
* along with VNLib.Plugins.Extensions.Data. If not, see http://www.gnu.org/licenses/.
*/

using System;
using System.IO;
using System.Text.Json;
using System.Collections;
using System.IO.Compression;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Text.Json.Serialization;

using VNLib.Utils;
using VNLib.Utils.Async;
using VNLib.Utils.Extensions;
using VNLib.Utils.Memory;

namespace VNLib.Plugins.Extensions.Data.Storage
{
    /// <summary>
    /// Represents an open storage object, that when released or disposed, will flush its changes to the underlying table 
    /// for which this descriptor represents
    /// </summary>
    public sealed class LWStorageDescriptor : AsyncUpdatableResource, IObjectStorage, IEnumerable<KeyValuePair<string, string>>, IIndexable<string, string>
    {

        private static readonly JsonSerializerOptions SerializerOptions = new()
        {
            DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
            NumberHandling = JsonNumberHandling.Strict,
            ReadCommentHandling = JsonCommentHandling.Disallow,
            WriteIndented = false,
            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
            IgnoreReadOnlyFields = true,
            DefaultBufferSize = Environment.SystemPageSize,
        };

        
        internal LWStorageEntry Entry { get; }
        
        private readonly Lazy<Dictionary<string, string>> StringStorage;

        /// <summary>
        /// The currnt descriptor's identifier string within its backing table. Usually the primary key.
        /// </summary>
        public string DescriptorID => Entry.Id;
        /// <summary>
        /// The identifier of the user for which this descriptor belongs to
        /// </summary>
        public string UserID => Entry.UserId!;
        /// <summary>
        /// The <see cref="DateTime"/> when the descriptor was created
        /// </summary>
        public DateTimeOffset Created => Entry.Created;
        /// <summary>
        /// The last time this descriptor was updated
        /// </summary>
        public DateTimeOffset LastModified => Entry.LastModified;

        ///<inheritdoc/>
        protected override AsyncUpdateCallback UpdateCb { get; }
        ///<inheritdoc/>
        protected override AsyncDeleteCallback DeleteCb { get; }
        ///<inheritdoc/>
        protected override JsonSerializerOptions JSO => SerializerOptions;

        internal LWStorageDescriptor(LWStorageManager manager, LWStorageEntry entry)
        {
            Entry = entry;
            UpdateCb = manager.UpdateDescriptorAsync;
            DeleteCb = manager.RemoveDescriptorAsync;
            StringStorage = new(OnStringStoreLoad);
        }

        internal Dictionary<string, string> OnStringStoreLoad()
        {
            if(Entry.Data == null || Entry.Data.Length == 0)
            {
                return new(StringComparer.OrdinalIgnoreCase);
            }
            else
            {
                //Calc and alloc decode buffer
                int bufferSize = (int)(Entry.Data.Length * 1.75);
                using UnsafeMemoryHandle<byte> decodeBuffer = Memory.UnsafeAlloc<byte>(bufferSize);

                //Decode and deserialize the data
                return BrotliDecoder.TryDecompress(Entry.Data, decodeBuffer, out int written)
                    ? JsonSerializer.Deserialize<Dictionary<string, string>>(Entry.Data, SerializerOptions) ?? new(StringComparer.OrdinalIgnoreCase)
                    : throw new InvalidDataException("Failed to decompress data");
            }
        }

        /// <inheritdoc/>
        /// <exception cref="JsonException"></exception>
        /// <exception cref="NotSupportedException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ObjectDisposedException"></exception>
        public T? GetObject<T>(string key)
        {
            Check();
            //De-serialize and return object
            return StringStorage.Value.TryGetValue(key, out string? val) ? val.AsJsonObject<T>(SerializerOptions) : default;
        }
        
        /// <inheritdoc/>
        /// <exception cref="NotSupportedException"></exception>
        /// <exception cref="ObjectDisposedException"></exception>
        public void SetObject<T>(string key, T obj)
        {
            //Remove the object from storage if its null
            if (obj == null)
            {
                SetStringValue(key, null);
            }
            else
            {
                //Serialize the object to a string
                string value = obj.ToJsonString(SerializerOptions)!;
                //Attempt to store string in storage
                SetStringValue(key, value);
            }
        }

        
        /// <summary>
        /// Gets a string value from string storage matching a given key
        /// </summary>
        /// <param name="key">Key for storage</param>
        /// <returns>Value associaetd with key if exists, <see cref="string.Empty"/> otherwise</returns>
        /// <exception cref="ArgumentNullException">If key is null</exception>
        /// <exception cref="ObjectDisposedException"></exception>
        public string GetStringValue(string key)
        {
            Check();
            return StringStorage.Value.TryGetValue(key, out string? val) ? val : string.Empty;
        }

        /// <summary>
        /// Creates, overwrites, or removes a string value identified by key.
        /// </summary>
        /// <param name="key">Entry key</param>
        /// <param name="value">String to store or overwrite, set to null or string.Empty to remove a property</param>
        /// <exception cref="ObjectDisposedException"></exception>
        /// <exception cref="ArgumentNullException">If key is null</exception>
        public void SetStringValue(string key, string? value)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException(nameof(key));
            }
            Check();
            //If the value is null, see if the the properties are null
            if (string.IsNullOrWhiteSpace(value))
            {
                //If the value is null and properies exist, remove the entry
                StringStorage.Value.Remove(key);
                Modified |= true;
            }
            else
            {
                //Set the value
                StringStorage.Value[key] = value;
                //Set modified flag
                Modified |= true;
            }
        }
        
        /// <summary>
        /// Gets or sets a string value from string storage matching a given key
        /// </summary>
        /// <param name="key">Key for storage</param>
        /// <returns>Value associaetd with key if exists, <seealso cref="string.Empty "/> otherwise</returns>
        /// <exception cref="ObjectDisposedException"></exception>
        /// <exception cref="ArgumentNullException">If key is null</exception>
        public string this[string key]
        {
            get => GetStringValue(key);
            set => SetStringValue(key, value);
        }

        /// <summary>
        /// Flushes all pending changes to the backing store asynchronously
        /// </summary>
        /// <exception cref="ObjectDisposedException"></exception>
        public ValueTask WritePendingChangesAsync()
        {
            Check();
            return Modified ? (new(FlushPendingChangesAsync())) : ValueTask.CompletedTask;
        }

        ///<inheritdoc/>
        public override async ValueTask ReleaseAsync()
        {
            await base.ReleaseAsync();
            //Cleanup dict on exit
            if (StringStorage.IsValueCreated)
            {
                StringStorage.Value.Clear();
            }
        }

        ///<inheritdoc/>
        public IEnumerator<KeyValuePair<string, string>> GetEnumerator() => StringStorage.Value.GetEnumerator();
        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
        ///<inheritdoc/>
        protected override object GetResource() => StringStorage.Value;
    }
}