-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathWordRepository.cs
More file actions
322 lines (272 loc) · 15.1 KB
/
Copy pathWordRepository.cs
File metadata and controls
322 lines (272 loc) · 15.1 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using BackendFramework.Helper;
using BackendFramework.Interfaces;
using BackendFramework.Models;
using BackendFramework.Otel;
using MongoDB.Driver;
namespace BackendFramework.Repositories
{
/// <summary> Atomic database functions for <see cref="Word"/>s. </summary>
[ExcludeFromCodeCoverage]
public class WordRepository(IMongoDbContext dbContext) : IWordRepository
{
private readonly IMongoCollection<Word> _frontier = dbContext.Db.GetCollection<Word>("FrontierCollection");
private readonly IMongoCollection<Word> _words = dbContext.Db.GetCollection<Word>("WordsCollection");
private const string otelTagName = "otel.WordRepository";
/// <summary>
/// Creates a mongo filter for all words in a specified project (and optionally with specified vernacular).
/// Since a variant in FieldWorks can export as an entry without any senses, filters out 0-sense words.
/// </summary>
private static FilterDefinition<Word> GetAllProjectWordsFilter(string projectId, string? vernacular = null)
{
var filterDef = new FilterDefinitionBuilder<Word>();
return (vernacular is null)
? filterDef.And(filterDef.Eq(w => w.ProjectId, projectId), filterDef.SizeGt(w => w.Senses, 0))
: filterDef.And(filterDef.Eq(w => w.ProjectId, projectId), filterDef.SizeGt(w => w.Senses, 0),
filterDef.Eq(w => w.Vernacular, vernacular));
}
/// <summary> Creates a mongo filter for words in a specified project with specified wordId. </summary>
private static FilterDefinition<Word> GetProjectWordFilter(string projectId, string wordId)
{
var filterDef = new FilterDefinitionBuilder<Word>();
return filterDef.And(filterDef.Eq(w => w.ProjectId, projectId), filterDef.Eq(w => w.Id, wordId));
}
/// <summary> Creates a mongo filter for project words with specified wordId and audio. </summary>
private static FilterDefinition<Word> GetProjectWordWithAudioFilter(
string projectId, string wordId, string fileName)
{
var filterDef = new FilterDefinitionBuilder<Word>();
return filterDef.And(filterDef.Eq(w => w.ProjectId, projectId), filterDef.Eq(w => w.Id, wordId),
filterDef.ElemMatch(w => w.Audio, a => a.FileName == fileName));
}
/// <summary> Creates a mongo filter for words in a specified project with specified wordIds. </summary>
private static FilterDefinition<Word> GetProjectWordsFilter(string projectId, List<string> wordIds)
{
var filterDef = new FilterDefinitionBuilder<Word>();
return filterDef.And(filterDef.Eq(w => w.ProjectId, projectId), filterDef.In(w => w.Id, wordIds));
}
/// <summary> Finds all <see cref="Word"/>s with specified projectId </summary>
public async Task<List<Word>> GetAllWords(string projectId)
{
using var activity = OtelService.StartActivityWithTag(otelTagName, "getting all words");
return await _words.Find(GetAllProjectWordsFilter(projectId)).ToListAsync();
}
/// <summary> Finds <see cref="Word"/> with specified wordId and projectId </summary>
public async Task<Word?> GetWord(string projectId, string wordId)
{
using var activity = OtelService.StartActivityWithTag(otelTagName, "getting a word");
var wordList = await _words.FindAsync(GetProjectWordFilter(projectId, wordId));
try
{
return await wordList.FirstAsync();
}
catch (InvalidOperationException)
{
return null;
}
}
/// <summary> Removes all <see cref="Word"/>s from the WordsCollection and Frontier for specified
/// <see cref="Project"/> </summary>
/// <returns> A bool: success of operation </returns>
public async Task<bool> DeleteAllWords(string projectId)
{
using var activity =
OtelService.StartActivityWithTag(otelTagName, "deleting all words from WordsCollection and Frontier");
var filterDef = new FilterDefinitionBuilder<Word>();
var filter = filterDef.Eq(x => x.ProjectId, projectId);
var deleted = await _words.DeleteManyAsync(filter);
await _frontier.DeleteManyAsync(filter);
return deleted.DeletedCount != 0;
}
/// <summary> Removes all <see cref="Word"/>s from the Frontier for specified <see cref="Project"/> </summary>
/// <returns> A bool: success of operation </returns>
public async Task<bool> DeleteAllFrontierWords(string projectId)
{
using var activity = OtelService.StartActivityWithTag(otelTagName, "deleting all words from Frontier");
var filterDef = new FilterDefinitionBuilder<Word>();
var filter = filterDef.Eq(x => x.ProjectId, projectId);
var deleted = await _frontier.DeleteManyAsync(filter);
return deleted.DeletedCount != 0;
}
/// <summary>
/// If the <see cref="Word"/> Created or Modified times are blank, fill them in the current time.
/// </summary>
private static void PopulateBlankWordTimes(Word word)
{
if (string.IsNullOrEmpty(word.Created))
{
// Use Roundtrip-suitable ISO 8601 format.
word.Created = Time.UtcNowIso8601();
}
if (string.IsNullOrEmpty(word.Modified))
{
word.Modified = Time.UtcNowIso8601();
}
}
/// <summary> Adds a <see cref="Word"/> to the WordsCollection and Frontier </summary>
/// <remarks>
/// If the Created or Modified time fields are blank, they will automatically calculated using the current
/// time. This allows services to set or clear the values before creation to control these fields.
/// </remarks>
/// <param name="word"></param>
/// <returns> The word created </returns>
public async Task<Word> Create(Word word)
{
using var activity =
OtelService.StartActivityWithTag(otelTagName, "creating a word in WordsCollection and Frontier");
PopulateBlankWordTimes(word);
await _words.InsertOneAsync(word);
await AddFrontier(word);
return word;
}
/// <summary> Adds a list of <see cref="Word"/>s to the WordsCollection and Frontier </summary>
/// <remarks>
/// If the Created or Modified time fields are blank, they will automatically calculated using the current
/// time. This allows services to set or clear the values before creation to control these fields.
/// </remarks>
/// <param name="words"></param>
/// <returns> The words created </returns>
public async Task<List<Word>> Create(List<Word> words)
{
using var activity =
OtelService.StartActivityWithTag(otelTagName, "creating words in WordsCollection and Frontier");
if (words.Count == 0)
{
return words;
}
foreach (var w in words)
{
PopulateBlankWordTimes(w);
}
await _words.InsertManyAsync(words);
await AddFrontier(words);
return words;
}
/// <summary> Adds a <see cref="Word"/> only to the WordsCollection </summary>
/// <remarks>
/// If the Created or Modified time fields are blank, they will automatically calculated using the current
/// time. This allows services to set or clear the values before creation to control these fields.
/// </remarks>
/// <returns> The word created </returns>
public async Task<Word> Add(Word word)
{
using var activity = OtelService.StartActivityWithTag(otelTagName, "adding a word to WordsCollection");
PopulateBlankWordTimes(word);
await _words.InsertOneAsync(word);
return word;
}
/// <summary> Checks if Words collection for specified <see cref="Project"/> has any words. </summary>
public async Task<bool> HasWords(string projectId)
{
using var activity =
OtelService.StartActivityWithTag(otelTagName, "checking if WordsCollection has words");
return await _words.Find(GetAllProjectWordsFilter(projectId)).Limit(1).AnyAsync();
}
/// <summary> Checks if Frontier for specified <see cref="Project"/> has any words. </summary>
public async Task<bool> HasFrontierWords(string projectId)
{
using var activity = OtelService.StartActivityWithTag(otelTagName, "checking if Frontier has words");
return await _frontier.Find(GetAllProjectWordsFilter(projectId)).Limit(1).AnyAsync();
}
/// <summary> Checks if specified word is in Frontier for specified <see cref="Project"/> </summary>
public async Task<bool> IsInFrontier(string projectId, string wordId)
{
using var activity = OtelService.StartActivityWithTag(otelTagName, "checking if Frontier contains a word");
return (await _frontier.CountDocumentsAsync(GetProjectWordFilter(projectId, wordId))) > 0;
}
/// <summary> Checks if given words are in the project Frontier. </summary>
/// <param name="projectId"> Id of project to check in. </param>
/// <param name="wordIds"> Ids of words to check for. </param>
/// <param name="count"> Minimum number of words required. </param>
public async Task<bool> AreInFrontier(string projectId, List<string> wordIds, int count)
{
using var activity = OtelService.StartActivityWithTag(otelTagName, "checking if Frontier contains words");
return await _frontier
.CountDocumentsAsync(GetProjectWordsFilter(projectId, wordIds), new() { Limit = count }) == count;
}
/// <summary> Gets number of <see cref="Word"/>s in the Frontier for specified <see cref="Project"/> </summary>
public async Task<int> GetFrontierCount(string projectId)
{
using var activity = OtelService.StartActivityWithTag(otelTagName, "getting count of Frontier");
return (int)await _frontier.CountDocumentsAsync(GetAllProjectWordsFilter(projectId));
}
/// <summary> Finds all <see cref="Word"/>s in the Frontier for specified <see cref="Project"/> </summary>
public async Task<List<Word>> GetAllFrontier(string projectId)
{
using var activity = OtelService.StartActivityWithTag(otelTagName, "getting all Frontier words");
return await _frontier.Find(GetAllProjectWordsFilter(projectId)).ToListAsync();
}
/// <summary> Gets a specified <see cref="Word"/> from the Frontier </summary>
/// <returns> The word, or null if not found. </returns>
public async Task<Word?> GetFrontier(string projectId, string wordId, string? audioFileName = null)
{
using var activity = OtelService.StartActivityWithTag(otelTagName, "getting a word from Frontier");
return string.IsNullOrEmpty(audioFileName)
? await _frontier.Find(GetProjectWordFilter(projectId, wordId)).FirstOrDefaultAsync()
: await _frontier.Find(GetProjectWordWithAudioFilter(projectId, wordId, audioFileName))
.FirstOrDefaultAsync();
}
/// <summary> Finds all <see cref="Word"/>s in Frontier of specified project with specified vern </summary>
public async Task<List<Word>> GetFrontierWithVernacular(string projectId, string vernacular)
{
using var activity =
OtelService.StartActivityWithTag(otelTagName, "getting all words from Frontier with vern");
return await _frontier.Find(GetAllProjectWordsFilter(projectId, vernacular)).ToListAsync();
}
/// <summary> Adds a <see cref="Word"/> only to the Frontier </summary>
/// <param name="word"></param>
/// <returns> The word created </returns>
public async Task<Word> AddFrontier(Word word)
{
using var activity = OtelService.StartActivityWithTag(otelTagName, "adding a word to Frontier");
await _frontier.InsertOneAsync(word);
return word;
}
/// <summary> Adds a list of <see cref="Word"/>s only to the Frontier </summary>
/// <param name="words"></param>
/// <returns> The words created </returns>
public async Task<List<Word>> AddFrontier(List<Word> words)
{
using var activity = OtelService.StartActivityWithTag(otelTagName, "adding words to Frontier");
await _frontier.InsertManyAsync(words);
return words;
}
/// <summary> Removes <see cref="Word"/> from the Frontier with specified wordId and projectId </summary>
/// <returns> The deleted word, or null if not found. </returns>
public async Task<Word?> DeleteFrontier(string projectId, string wordId, string? audioFileName = null)
{
using var activity = OtelService.StartActivityWithTag(otelTagName, "deleting a word from Frontier");
return string.IsNullOrEmpty(audioFileName)
? await _frontier.FindOneAndDeleteAsync(GetProjectWordFilter(projectId, wordId))
: await _frontier.FindOneAndDeleteAsync(
GetProjectWordWithAudioFilter(projectId, wordId, audioFileName));
}
/// <summary> Removes <see cref="Word"/>s from the Frontier with specified wordIds and projectId </summary>
/// <returns> Number of words deleted </returns>
public async Task<long> DeleteFrontierWords(string projectId, List<string> wordIds)
{
using var activity = OtelService.StartActivityWithTag(otelTagName, "deleting words from Frontier");
var deleted = await _frontier.DeleteManyAsync(GetProjectWordsFilter(projectId, wordIds));
return deleted.DeletedCount;
}
/// <summary>
/// Counts the number of Frontier words that have the specified semantic domain.
/// </summary>
/// <param name="projectId"> The project id </param>
/// <param name="domainId"> The semantic domain id </param>
/// <returns> The count of words containing at least one sense with the specified domain. </returns>
public async Task<int> CountFrontierWordsWithDomain(string projectId, string domainId)
{
using var activity = OtelService.StartActivityWithTag(otelTagName, "counting frontier words with domain");
var filterDef = new FilterDefinitionBuilder<Word>();
var filter = filterDef.And(
filterDef.Eq(w => w.ProjectId, projectId),
filterDef.ElemMatch(w => w.Senses, s => s.SemanticDomains.Any(sd => sd.Id == domainId)));
return (int)await _frontier.CountDocumentsAsync(filter);
}
}
}