Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Jan 17, 2024
1 parent 24d334b commit 17272e7
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 142 deletions.
16 changes: 12 additions & 4 deletions starsky/starsky.foundation.database/Query/Query.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -278,7 +279,10 @@ async Task LocalQuery(ApplicationDbContext context, FileIndexItem fileIndexItem)
/// <returns>same item</returns>
public async Task<List<FileIndexItem>> UpdateItemAsync(List<FileIndexItem> updateStatusContentList)
{
if ( !updateStatusContentList.Any() ) return new List<FileIndexItem>();
if ( updateStatusContentList.Count == 0 )
{
return new List<FileIndexItem>();
}

async Task<List<FileIndexItem>> LocalQuery(DbContext context, List<FileIndexItem> fileIndexItems)
{
Expand Down Expand Up @@ -430,7 +434,8 @@ internal void AddCacheItem(FileIndexItem updateStatusContent)

if (!_cache.TryGetValue(queryCacheName, out var objectFileFolders)) return;

var displayFileFolders = (List<FileIndexItem>?) objectFileFolders;
objectFileFolders ??= new List<FileIndexItem>();
var displayFileFolders = (List<FileIndexItem>) objectFileFolders;

if ( updateStatusContent.FilePath == "/" )
{
Expand Down Expand Up @@ -471,7 +476,8 @@ public void CacheUpdateItem(List<FileIndexItem> updateStatusContent)
skippedCacheItems.Add(item.ParentDirectory!);
continue;
}


objectFileFolders ??= new List<FileIndexItem>();
var displayFileFolders = (List<FileIndexItem>) objectFileFolders;

// make it a list to avoid enum errors
Expand Down Expand Up @@ -499,7 +505,7 @@ public void CacheUpdateItem(List<FileIndexItem> updateStatusContent)
_cache.Set(queryCacheName, displayFileFolders, new TimeSpan(1,0,0));
}

if ( skippedCacheItems.Any() && _appSettings.Verbose == true )
if ( skippedCacheItems.Count >= 1 && _appSettings.Verbose == true )
{
_logger.LogInformation($"[CacheUpdateItem] skipped: {string.Join(", ", skippedCacheItems)}");
}
Expand Down Expand Up @@ -536,7 +542,9 @@ public void RemoveCacheItem(FileIndexItem updateStatusContent)

if (!_cache.TryGetValue(queryCacheName, out var objectFileFolders)) return;

objectFileFolders ??= new List<FileIndexItem>();
var displayFileFolders = (List<FileIndexItem>) objectFileFolders;

// Order by filename
displayFileFolders = displayFileFolders
.Where(p => p.FilePath != updateStatusContent.FilePath)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
Expand All @@ -11,6 +12,7 @@ namespace starsky.foundation.platform.Extensions
/// <summary>
/// @see: https://stackoverflow.com/a/64291008
/// </summary>
[SuppressMessage("Usage", "S3011:Make sure that this accessibility bypass is safe here", Justification = "Safe")]
public static class MemoryCacheExtensions
{
private static readonly Lazy<Func<MemoryCache, object>> GetCoherentState =
Expand Down
33 changes: 13 additions & 20 deletions starsky/starskytest/Extensions/EntityFrameworkExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,29 +92,22 @@ public AppDbMySqlException(DbContextOptions options) : base(options)

private static MySqlException CreateMySqlException(string message)
{
var info = new SerializationInfo(typeof(Exception),
new FormatterConverter());
info.AddValue("Number", 1);
info.AddValue("SqlState", "SqlState");
info.AddValue("Message", message);
info.AddValue("InnerException", new Exception());
info.AddValue("HelpURL", "");
info.AddValue("StackTraceString", "");
info.AddValue("RemoteStackTraceString", "");
info.AddValue("RemoteStackIndex", 1);
info.AddValue("HResult", 1);
info.AddValue("Source", "");
info.AddValue("WatsonBuckets", Array.Empty<byte>() );

// private MySqlException(SerializationInfo info, StreamingContext context)
var ctor =
typeof(MySqlException).GetConstructors(BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.InvokeMethod).FirstOrDefault();
// MySqlErrorCode errorCode, string? sqlState, string message, Exception? innerException

var ctorLIst =
typeof(MySqlException).GetConstructors(
BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.InvokeMethod);
var ctor = ctorLIst.FirstOrDefault(p =>
p.ToString() == "Void .ctor(MySqlConnector.MySqlErrorCode, System.String, System.String, System.Exception)" );

var instance =
( MySqlException ) ctor?.Invoke(new object[]
{
info,
new StreamingContext(StreamingContextStates.All)
MySqlErrorCode.AccessDenied,
"test",
message,
new Exception()
});
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,22 @@ public void SqlXmlRepositoryTest_ExpectedException_NullReferenceException()

private static MySqlException CreateMySqlException(string message)
{
var info = new SerializationInfo(typeof(Exception),
new FormatterConverter());
info.AddValue("Number", 1);
info.AddValue("SqlState", "SqlState");
info.AddValue("Message", message);
info.AddValue("InnerException", new Exception());
info.AddValue("HelpURL", "");
info.AddValue("StackTraceString", "");
info.AddValue("RemoteStackTraceString", "");
info.AddValue("RemoteStackIndex", 1);
info.AddValue("HResult", 1);
info.AddValue("Source", "");
info.AddValue("WatsonBuckets", Array.Empty<byte>() );

// private MySqlException(SerializationInfo info, StreamingContext context)
var ctor =
typeof(MySqlException).GetConstructors(BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.InvokeMethod).FirstOrDefault();
// MySqlErrorCode errorCode, string? sqlState, string message, Exception? innerException

var ctorLIst =
typeof(MySqlException).GetConstructors(
BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.InvokeMethod);
var ctor = ctorLIst.FirstOrDefault(p =>
p.ToString() == "Void .ctor(MySqlConnector.MySqlErrorCode, System.String, System.String, System.Exception)" );

var instance =
( MySqlException ) ctor?.Invoke(new object[]
{
info,
new StreamingContext(StreamingContextStates.All)
MySqlErrorCode.AccessDenied,
"test",
message,
new Exception()
});
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,22 @@ public async Task Test()

private static MySqlException CreateMySqlException(string message)
{
var info = new SerializationInfo(typeof(Exception),
new FormatterConverter());
info.AddValue("Number", 1);
info.AddValue("SqlState", "SqlState");
info.AddValue("Message", message);
info.AddValue("InnerException", new Exception());
info.AddValue("HelpURL", "");
info.AddValue("StackTraceString", "");
info.AddValue("RemoteStackTraceString", "");
info.AddValue("RemoteStackIndex", 1);
info.AddValue("HResult", 1);
info.AddValue("Source", "");
info.AddValue("WatsonBuckets", Array.Empty<byte>() );

// private MySqlException(SerializationInfo info, StreamingContext context)
var ctor =
typeof(MySqlException).GetConstructors(BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.InvokeMethod).FirstOrDefault();
// MySqlErrorCode errorCode, string? sqlState, string message, Exception? innerException

var ctorLIst =
typeof(MySqlException).GetConstructors(
BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.InvokeMethod);
var ctor = ctorLIst.FirstOrDefault(p =>
p.ToString() == "Void .ctor(MySqlConnector.MySqlErrorCode, System.String, System.String, System.Exception)" );

var instance =
( MySqlException ) ctor.Invoke(new object[]
( MySqlException ) ctor?.Invoke(new object[]
{
info,
new StreamingContext(StreamingContextStates.All)
MySqlErrorCode.AccessDenied,
"test",
message,
new Exception()
});
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ public bool IsConceptualNull(IProperty property)
throw new NotImplementedException();
}

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public DbContext Context { get; }

#pragma warning disable 8618
// ReSharper disable once UnassignedGetOnlyAutoProperty
public IEntityType EntityType { get; }
#pragma warning restore 8618

public EntityState EntityState { get; set; }
#pragma warning disable 8618

// ReSharper disable once UnassignedGetOnlyAutoProperty
public IUpdateEntry SharedIdentityEntry { get; }
#pragma warning restore 8618
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,31 +77,24 @@ await _query.AddItemAsync(

private static MySqlException CreateMySqlException(string message)
{
var info = new SerializationInfo(typeof(Exception),
new FormatterConverter());
info.AddValue("Number", 1);
info.AddValue("SqlState", "SqlState");
info.AddValue("Message", message);
info.AddValue("InnerException", new Exception());
info.AddValue("HelpURL", "");
info.AddValue("StackTraceString", "");
info.AddValue("RemoteStackTraceString", "");
info.AddValue("RemoteStackIndex", 1);
info.AddValue("HResult", 1);
info.AddValue("Source", "");
info.AddValue("WatsonBuckets", Array.Empty<byte>() );

// private MySqlException(SerializationInfo info, StreamingContext context)
var ctor =
typeof(MySqlException).GetConstructors(BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.InvokeMethod).FirstOrDefault();
// MySqlErrorCode errorCode, string? sqlState, string message, Exception? innerException

var ctorLIst =
typeof(MySqlException).GetConstructors(
BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.InvokeMethod);
var ctor = ctorLIst.FirstOrDefault(p =>
p.ToString() == "Void .ctor(MySqlConnector.MySqlErrorCode, System.String, System.String, System.Exception)" );

var instance =
( MySqlException? ) ctor?.Invoke(new object[]
( MySqlException ) ctor?.Invoke(new object[]
{
info,
new StreamingContext(StreamingContextStates.All)
});
return instance!;
MySqlErrorCode.AccessDenied,
"test",
message,
new Exception()
})!;
return instance;
}

private static bool IsCalledMySqlSaveDbExceptionContext { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1102,9 +1102,11 @@ public void QueryFolder_Add_And_UpdateFolderCache_UpdateCacheItemTest()
public void CacheUpdateItem_Skip_ShouldSetItem()
{
var item1 = new FileIndexItem {Id = 400, Tags = "hi", FileName = "cache"};

// already verbose
_query.CacheUpdateItem(new List<FileIndexItem>{item1});

Assert.IsTrue(_logger.TrackedInformation.Any());
Assert.IsTrue(_logger.TrackedInformation.FirstOrDefault().Item2.Contains("[CacheUpdateItem]"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ public bool IsConceptualNull(IProperty property)
throw new NotImplementedException();
}

#pragma warning disable 8618
public DbContext Context { get; }

#pragma warning disable 8618
// ReSharper disable once UnassignedGetOnlyAutoProperty
public IEntityType EntityType { get; }
#pragma warning restore 8618

public EntityState EntityState { get; set; }
#pragma warning disable 8618

// ReSharper disable once UnassignedGetOnlyAutoProperty
public IUpdateEntry SharedIdentityEntry { get; }
#pragma warning restore 8618
Expand Down Expand Up @@ -310,31 +310,24 @@ public override Task<int> SaveChangesAsync(CancellationToken cancellationToken =

private static MySqlException CreateMySqlException(string message)
{
var info = new SerializationInfo(typeof(Exception),
new FormatterConverter());
info.AddValue("Number", 1);
info.AddValue("SqlState", "SqlState");
info.AddValue("Message", message);
info.AddValue("InnerException", new Exception());
info.AddValue("HelpURL", "");
info.AddValue("StackTraceString", "");
info.AddValue("RemoteStackTraceString", "");
info.AddValue("RemoteStackIndex", 1);
info.AddValue("HResult", 1);
info.AddValue("Source", "");
info.AddValue("WatsonBuckets", Array.Empty<byte>() );

// private MySqlException(SerializationInfo info, StreamingContext context)
var ctor =
typeof(MySqlException).GetConstructors(BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.InvokeMethod).FirstOrDefault();
// MySqlErrorCode errorCode, string? sqlState, string message, Exception? innerException

var ctorLIst =
typeof(MySqlException).GetConstructors(
BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.InvokeMethod);
var ctor = ctorLIst.FirstOrDefault(p =>
p.ToString() == "Void .ctor(MySqlConnector.MySqlErrorCode, System.String, System.String, System.Exception)" );

var instance =
( MySqlException? ) ctor?.Invoke(new object[]
( MySqlException ) ctor?.Invoke(new object[]
{
info,
new StreamingContext(StreamingContextStates.All)
});
return instance!;
MySqlErrorCode.AccessDenied,
"test",
message,
new Exception()
})!;
return instance;
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,31 +208,24 @@ public async Task ThumbnailQuery_3ConcurrencyException()

private static MySqlException CreateMySqlException(string message)
{
var info = new SerializationInfo(typeof(Exception),
new FormatterConverter());
info.AddValue("Number", 1);
info.AddValue("SqlState", "SqlState");
info.AddValue("Message", message);
info.AddValue("InnerException", new Exception());
info.AddValue("HelpURL", "");
info.AddValue("StackTraceString", "");
info.AddValue("RemoteStackTraceString", "");
info.AddValue("RemoteStackIndex", 1);
info.AddValue("HResult", 1);
info.AddValue("Source", "");
info.AddValue("WatsonBuckets", Array.Empty<byte>() );

// private MySqlException(SerializationInfo info, StreamingContext context)
var ctor =
typeof(MySqlException).GetConstructors(BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.InvokeMethod).FirstOrDefault();
// MySqlErrorCode errorCode, string? sqlState, string message, Exception? innerException

var ctorLIst =
typeof(MySqlException).GetConstructors(
BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.InvokeMethod);
var ctor = ctorLIst.FirstOrDefault(p =>
p.ToString() == "Void .ctor(MySqlConnector.MySqlErrorCode, System.String, System.String, System.Exception)" );

var instance =
( MySqlException? ) ctor?.Invoke(new object[]
( MySqlException ) ctor?.Invoke(new object[]
{
info,
new StreamingContext(StreamingContextStates.All)
});
return instance!;
MySqlErrorCode.AccessDenied,
"test",
message,
new Exception()
})!;
return instance;
}
private static bool IsCalledMySqlSaveDbExceptionContext { get; set; }

Expand Down
Loading

0 comments on commit 17272e7

Please sign in to comment.