Skip to content

Commit

Permalink
Included LinkedLanguages in Query Request so that related data is ret…
Browse files Browse the repository at this point in the history
…rieved together, if it's not currently cached.
  • Loading branch information
Benjamin Stern committed Jun 24, 2020
1 parent 9d3e29e commit faa4b89
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
6 changes: 5 additions & 1 deletion Converter/Model/SQLite/Link.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ public class LinkItem
public string SecondaryLocation { get; set; }
}

public class LinkGroup{
public class LinkGroup {
public LinkGroup() {
LinkedLanguages = new List<LinkLanguage>();
}

public int Id { get; set; }
[ForeignKey("Topic")]
public int PrimaryTopicId { get; set; }
Expand Down
29 changes: 11 additions & 18 deletions Converter/SefariaMongoDBService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,12 @@ public LinkItem GetLinkAt(int index, SefariaSQLiteConversionContext targetContex
bool isLinkGroupNew = false;
if (primaryTopicId != 0 && secondaryTopicId != 0)
{
link.LinkGroup = targetContext.FindFirstOrDefaultWhere(targetContext.LinkGroups, (lg => lg.PrimaryTopicId == primaryTopicId && lg.SecondaryTopicId == secondaryTopicId));
link.LinkGroup = targetContext.FindTrackedFirstOrDefaultWhere(targetContext.LinkGroups, (lg => lg.PrimaryTopicId == primaryTopicId && lg.SecondaryTopicId == secondaryTopicId));
if (link.LinkGroup == null) {
link.LinkGroup = targetContext.LinkGroups
.Where(lg => lg.PrimaryTopicId == primaryTopicId && lg.SecondaryTopicId == secondaryTopicId)
.Include(lg => lg.LinkedLanguages).FirstOrDefault();
}
if (link.LinkGroup == null)
{
isLinkGroupNew = true;
Expand All @@ -259,21 +264,9 @@ public LinkItem GetLinkAt(int index, SefariaSQLiteConversionContext targetContex
};
targetContext.Add(targetContext.LinkGroups, link.LinkGroup);
}

if (availableLangs.IsBsonArray)
{
//May not be necessary should be retrieved from the LinkGroup Automatically
//if (!isLinkGroupNew)
//{
// //link.LinkGroup.LinkedLanguages = targetContext
// // .LinkLanguages
// // .Where(l => l.LinkGroup == link.LinkGroup)
// // .Concat(targetContext.LinkLanguages
// // .Local
// // .Where(l => l.LinkGroup == link.LinkGroup))
// // .ToList();

//}
if (link.LinkGroup.LinkedLanguages == null)
{
link.LinkGroup.LinkedLanguages = new List<LinkLanguage>();
Expand All @@ -282,8 +275,8 @@ public LinkItem GetLinkAt(int index, SefariaSQLiteConversionContext targetContex
{
LinkLanguage linkLanguage = null;
int languageId = GetLanguageIdByString(item.AsString);
linkLanguage = targetContext.FindFirstOrDefaultWhere(targetContext.LinkLanguages, (l => l.LinkGroup == link.LinkGroup && l.TopicId == primaryTopicId && l.LanguageId == languageId));
//linkLanguage = link.LinkGroup.LinkedLanguages.Where(l => l.TopicId == primaryTopicId && l.LanguageId == languageId).FirstOrDefault();
//linkLanguage = targetContext.FindFirstOrDefaultWhere(targetContext.LinkLanguages, (l => l.LinkGroup == link.LinkGroup && l.TopicId == primaryTopicId && l.LanguageId == languageId));
linkLanguage = link.LinkGroup.LinkedLanguages.Where(l => l.TopicId == primaryTopicId && l.LanguageId == languageId).FirstOrDefault();
if (linkLanguage == null)
{
linkLanguage = new LinkLanguage
Expand All @@ -301,8 +294,8 @@ public LinkItem GetLinkAt(int index, SefariaSQLiteConversionContext targetContex
{
LinkLanguage linkLanguage = null;
int languageId = GetLanguageIdByString(item.AsString);
linkLanguage = targetContext.FindFirstOrDefaultWhere(targetContext.LinkLanguages, (l => l.LinkGroup == link.LinkGroup && l.TopicId == secondaryTopicId && l.LanguageId == languageId));
//linkLanguage = link.LinkGroup.LinkedLanguages.Where(l => l.TopicId == secondaryTopicId && l.LanguageId == languageId).FirstOrDefault();
//linkLanguage = targetContext.FindFirstOrDefaultWhere(targetContext.LinkLanguages, (l => l.LinkGroup == link.LinkGroup && l.TopicId == secondaryTopicId && l.LanguageId == languageId));
linkLanguage = link.LinkGroup.LinkedLanguages.Where(l => l.TopicId == secondaryTopicId && l.LanguageId == languageId).FirstOrDefault();
if (linkLanguage == null)
{
linkLanguage = new LinkLanguage
Expand Down
28 changes: 19 additions & 9 deletions Converter/SefariaSQLiteConversionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

public T FindFirstOrDefaultWhere<T>(DbSet<T> target, Func<T, bool> predicate) where T : class
{
var localList = (_trackedList.GetValueOrDefault(typeof(T)) as List<T>) ?? new List<T>();
//if (localList == null)
//{
// localList = target.Local;
// _trackedList.Add(typeof(T), localList);
//}
var resultLocal = localList.Where(predicate).FirstOrDefault();
//var localList = (_trackedList.GetValueOrDefault(typeof(T)) as List<T>) ?? new List<T>();
////if (localList == null)
////{
//// localList = target.Local;
//// _trackedList.Add(typeof(T), localList);
////}
//var resultLocal = localList.Where(predicate).FirstOrDefault();
//if(resultLocal != null) return resultLocal;

var resultLocal = FindTrackedFirstOrDefaultWhere(target, predicate);
if(resultLocal != null) return resultLocal;

var resultDb = target.Where(predicate).FirstOrDefault();
Expand All @@ -55,13 +58,20 @@ public List<T> FindListWhere<T>(DbSet<T> target, Func<T, bool> predicate) where
List<T> result = new List<T>();
var localList = (_trackedList.GetValueOrDefault(typeof(T)) as List<T>) ?? new List<T>();
var resultLocal = localList.Where(predicate).ToList();
result = result.Concat(resultLocal);
result = result.Concat(resultLocal).ToList();

var resultDb = target.Where(predicate).ToList();
result = result.Concat(resultDb);
result = result.Concat(resultDb).ToList();
return result;
}

public T FindTrackedFirstOrDefaultWhere<T>(DbSet<T> target, Func<T, bool> predicate) where T : class
{
var localList = (_trackedList.GetValueOrDefault(typeof(T)) as List<T>) ?? new List<T>();
var resultLocal = localList.Where(predicate).FirstOrDefault();
return resultLocal;
}

private List<Type> _typesChanged = new List<Type>();
public EntityEntry<T> Add<T>(DbSet<T> target, T item) where T : class
{
Expand Down

0 comments on commit faa4b89

Please sign in to comment.