aboutsummaryrefslogtreecommitdiff
path: root/lib/VNLib.Plugins.Extensions.Data/src/DbStore.cs
blob: 761d78f1b5e7946605bfe697d3742f84a7567c8a (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
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
/*
* Copyright (c) 2023 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Extensions.Data
* File: DbStore.cs 
*
* DbStore.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 Affero General Public License as 
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see https://www.gnu.org/licenses/.
*/

using System;
using System.Linq;
using System.Threading;
using System.Transactions;
using System.Threading.Tasks;
using System.Collections.Generic;

using Microsoft.EntityFrameworkCore;

using VNLib.Utils;
using VNLib.Utils.Memory.Caching;
using VNLib.Plugins.Extensions.Data.Abstractions;

namespace VNLib.Plugins.Extensions.Data
{

    /// <summary>
    /// Implements basic data-store functionality with abstract query builders
    /// </summary>
    /// <typeparam name="T">A <see cref="DbModelBase"/> implemented type</typeparam>
    public abstract partial class DbStore<T> : IDataStore<T>, IPaginatedDataStore<T> where T: class, IDbModel
    {
        /// <summary>
        /// Gets a unique ID for a new record being added to the store
        /// </summary>
        public abstract string RecordIdBuilder { get; }

        /// <summary>
        /// Gets a new <see cref="TransactionalDbContext"/> ready for use
        /// </summary>
        /// <returns></returns>
        public abstract TransactionalDbContext NewContext();
    
        /// <summary>
        /// An object rental for entity collections
        /// </summary>
        public ObjectRental<List<T>> ListRental { get; } = ObjectRental.Create<List<T>>(null, static ret => ret.Clear());

        #region Add Or Update
        ///<inheritdoc/>
        public virtual async Task<ERRNO> AddOrUpdateAsync(T record, CancellationToken cancellation = default)
        {
            //Open new db context
            await using TransactionalDbContext ctx = await this.OpenAsync(IsolationLevel.ReadCommitted, cancellation);

            IQueryable<T> query;

            if (string.IsNullOrWhiteSpace(record.Id))
            {
                //Get the application
                query = AddOrUpdateQueryBuilder(ctx, record);
            }
            else
            {
                //Get the application
                query = (from et in ctx.Set<T>()
                         where et.Id == record.Id
                         select et);
            }

            //Using single
            T? entry = await query.SingleOrDefaultAsync(cancellation);

            //Check if creted
            if (entry == null)
            {
                //Create a new template id
                record.Id = RecordIdBuilder;
                //Set the created/lm times
                record.Created = record.LastModified = DateTime.UtcNow;
                //Add the new template to the ctx
                ctx.Add(record);
            }
            else
            {
                OnRecordUpdate(record, entry);
            }           

            return await ctx.SaveAndCloseAsync(cancellation);
        }

        ///<inheritdoc/>
        public virtual async Task<ERRNO> UpdateAsync(T record, CancellationToken cancellation = default)
        {
            //Open new db context
            await using TransactionalDbContext ctx = await this.OpenAsync(IsolationLevel.Serializable, cancellation);

            //Get the application
            IQueryable<T> query = UpdateQueryBuilder(ctx, record);

            //Using single to make sure only one app is in the db (should never be an issue)
            T? oldEntry = await query.SingleOrDefaultAsync(cancellation);

            if (oldEntry == null)
            {
                return false;
            }

            //Update the template meta-data
            OnRecordUpdate(record, oldEntry);

            //Only publish update if changes happened
            if (!ctx.ChangeTracker.HasChanges())
            {
                //commit transaction if no changes need to be made
                await ctx.CommitTransactionAsync(cancellation);
                return true;
            }

            return await ctx.SaveAndCloseAsync(cancellation);
        }

        ///<inheritdoc/>
        public virtual async Task<ERRNO> CreateAsync(T record, CancellationToken cancellation = default)
        {
            //Open new db context
            await using TransactionalDbContext ctx = await this.OpenAsync(IsolationLevel.ReadUncommitted, cancellation);
            
            //Create a new template id
            record.Id = RecordIdBuilder;

            //Update the created/last modified time of the record
            record.Created = record.LastModified = DateTime.UtcNow;

            //Add the new template
            ctx.Add(record);
         
            return await ctx.SaveAndCloseAsync(cancellation);
        }  
      
        #endregion

        #region Delete

        ///<inheritdoc/>
        public virtual async Task<ERRNO> DeleteAsync(string key, CancellationToken cancellation = default)
        {
            //Open new db context
            await using TransactionalDbContext ctx = await this.OpenAsync(IsolationLevel.RepeatableRead, cancellation);

            //Get the template by its id
            IQueryable<T> query = (from temp in ctx.Set<T>()
                                    where temp.Id == key
                                    select temp);

            T? record = await query.SingleOrDefaultAsync(cancellation);

            if (record == null)
            {
                return false;
            }

            //Add the new application
            ctx.Remove(record);

            return await ctx.SaveAndCloseAsync(cancellation);
        }

        ///<inheritdoc/>
        public virtual async Task<ERRNO> DeleteAsync(T record, CancellationToken cancellation = default)
        {
            //Open new db context
            await using TransactionalDbContext ctx = await this.OpenAsync(IsolationLevel.RepeatableRead, cancellation);

            //Get a query for a a single item
            IQueryable<T> query = GetSingleQueryBuilder(ctx, record);

            //Get the entry
            T? entry = await query.SingleOrDefaultAsync(cancellation);

            if (entry == null)
            {
                return false;
            }

            //Add the new application
            ctx.Remove(entry);

            return await ctx.SaveAndCloseAsync(cancellation);
        }

        ///<inheritdoc/>
        public virtual async Task<ERRNO> DeleteAsync(params string[] specifiers)
        {
            //Open new db context
            await using TransactionalDbContext ctx = await this.OpenAsync(IsolationLevel.RepeatableRead);
            
            //Get the template by its id
            IQueryable<T> query = DeleteQueryBuilder(ctx, specifiers);

            T? entry = await query.SingleOrDefaultAsync();

            if (entry == null)
            {
                return false;
            }

            //Add the new application
            ctx.Remove(entry);
            
            return await ctx.SaveAndCloseAsync();
        }
      
        #endregion

        #region Get Collection

        ///<inheritdoc/>
        public virtual async Task<ERRNO> GetCollectionAsync(ICollection<T> collection, string specifier, int limit, CancellationToken cancellation = default)
        {
            int previous = collection.Count;

            //Open new db context
            await using TransactionalDbContext ctx = await this.OpenAsync(IsolationLevel.ReadUncommitted, cancellation);

            //Get the single template by its id
            await GetCollectionQueryBuilder(ctx, specifier)
                .Take(limit)
                .Select(static e => e)
                .ForEachAsync(collection.Add, cancellation);

            //close db and transaction
            await ctx.CommitTransactionAsync(cancellation);

            //Return the number of elements add to the collection
            return collection.Count - previous;
        }

        ///<inheritdoc/>
        public virtual async Task<ERRNO> GetCollectionAsync(ICollection<T> collection, int limit, params string[] args)
        {
            int previous = collection.Count;

            //Open new db context
            await using TransactionalDbContext ctx = await this.OpenAsync(IsolationLevel.ReadUncommitted);

            //Get the single template by the supplied user arguments
            await GetCollectionQueryBuilder(ctx, args)
                .Take(limit)
                .Select(static e => e)
                .ForEachAsync(collection.Add);

            //close db and transaction
            await ctx.CommitTransactionAsync();

            //Return the number of elements add to the collection
            return collection.Count - previous;
        }

        #endregion

        #region Get Count

        ///<inheritdoc/>
        public virtual async Task<long> GetCountAsync(CancellationToken cancellation = default)
        {
            //Open db connection
            await using TransactionalDbContext ctx = await this.OpenAsync(IsolationLevel.ReadUncommitted, cancellation);
            
            //Async get the number of records of the given entity type
            long count = await ctx.Set<T>().LongCountAsync(cancellation);
            
            //close db and transaction
            await ctx.CommitTransactionAsync(cancellation);

            return count;
        }

        ///<inheritdoc/>
        public virtual async Task<long> GetCountAsync(string specifier, CancellationToken cancellation)
        {
            await using TransactionalDbContext ctx = await this.OpenAsync(IsolationLevel.ReadUncommitted, cancellation);

            //Async get the number of records of the given entity type
            long count = await GetCountQueryBuilder(ctx, specifier).LongCountAsync(cancellation);

            //close db and transaction
            await ctx.CommitTransactionAsync(cancellation);

            return count;
        }

       
        #endregion

        #region Get Single

        ///<inheritdoc/>
        public virtual async Task<T?> GetSingleAsync(string key, CancellationToken cancellation = default)
        {
            //Open db connection
            await using TransactionalDbContext ctx = await this.OpenAsync(IsolationLevel.ReadUncommitted, cancellation);
            
            //Get the single template by its id
            T? record = await (from entry in ctx.Set<T>()
                              where entry.Id == key
                              select entry)
                              .SingleOrDefaultAsync(cancellation);

            //close db and transaction
            await ctx.CommitTransactionAsync(cancellation);
            return record;
        }

        ///<inheritdoc/>
        public virtual async Task<T?> GetSingleAsync(T record, CancellationToken cancellation = default)
        {
            //Open db connection
            await using TransactionalDbContext ctx = await this.OpenAsync(IsolationLevel.ReadUncommitted, cancellation);

            //Get the single template by its id
            T? entry = await GetSingleQueryBuilder(ctx, record).SingleOrDefaultAsync(cancellation);

            //close db and transaction
            await ctx.CommitTransactionAsync(cancellation);

            return record;
        }

        ///<inheritdoc/>
        public virtual async Task<T?> GetSingleAsync(params string[] specifiers)
        {
            //Open db connection
            await using TransactionalDbContext ctx = await this.OpenAsync(IsolationLevel.ReadUncommitted);
            
            //Get the single template by its id
            T? record = await GetSingleQueryBuilder(ctx, specifiers).SingleOrDefaultAsync();

            //close db and transaction
            await ctx.CommitTransactionAsync();

            return record;
        }

        #endregion

        #region Get Page

        ///<inheritdoc/>
        public virtual async Task<int> GetPageAsync(ICollection<T> collection, int page, int limit, CancellationToken cancellation = default)
        {
            //Store preivous count
            int previous = collection.Count;

            //Open db connection
            await using TransactionalDbContext ctx = await this.OpenAsync(IsolationLevel.ReadUncommitted, cancellation);
         
            //Get a page offset and a limit for the 
            await ctx.Set<T>()
                .Skip(page * limit)
                .Take(limit)
                .Select(static p => p)
                .ForEachAsync(collection.Add, cancellation);
           
            //close db and transaction
            await ctx.CommitTransactionAsync(cancellation);

            //Return the number of records added
            return collection.Count - previous;
        }

        ///<inheritdoc/>
        public virtual async Task<int> GetPageAsync(ICollection<T> collection, int page, int limit, params string[] constraints)
        {
            //Store preivous count
            int previous = collection.Count;

            //Open new db context
            await using TransactionalDbContext ctx = await this.OpenAsync(IsolationLevel.ReadUncommitted);
           
            //Get a page of records constrained by the given arguments
            await GetPageQueryBuilder(ctx, constraints)
                .Skip(page * limit)
                .Take(limit)
                .Select(static e => e)
                .ForEachAsync(collection.Add);
            
            //close db and transaction
            await ctx.CommitTransactionAsync();

            //Return the number of records added
            return collection.Count - previous;
        }

        #endregion
    }
}