Skip to content

Commit

Permalink
#1833 exists IQuery, SelectorStorage to IServiceScope
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Jan 14, 2025
1 parent 658cd88 commit c18df69
Show file tree
Hide file tree
Showing 7 changed files with 426 additions and 293 deletions.
51 changes: 26 additions & 25 deletions starsky/starsky.foundation.database/Interfaces/IQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ namespace starsky.foundation.database.Interfaces;
public interface IQuery
{
/// <summary>
/// Get a list of all files inside an folder (NOT recursive)
/// But this uses a database as source
/// Get a list of all files inside an folder (NOT recursive)
/// But this uses a database as source
/// </summary>
/// <param name="filePaths">relative database path</param>
/// <param name="timeout"></param>
/// <returns>list of FileIndex-objects</returns>
Task<List<FileIndexItem>> GetAllFilesAsync(List<string> filePaths, int timeout = 1000);

/// <summary>
/// Get a list of all files inside an folder (NOT recursive)
/// But this uses a database as source
/// Get a list of all files inside a folder (NOT recursive)
/// But this uses a database as source
/// </summary>
/// <param name="subPath">relative database path</param>
/// <returns>list of FileIndex-objects</returns>
Expand All @@ -32,7 +32,7 @@ public interface IQuery
Task<List<FileIndexItem>> GetAllRecursiveAsync(List<string> filePathList);

/// <summary>
/// to do the query and return object
/// to do the query and return object
/// </summary>
/// <param name="subPath">subPath style</param>
/// <param name="colorClassActiveList">filter the colorClass</param>
Expand All @@ -53,7 +53,7 @@ IEnumerable<FileIndexItem> DisplayFileFolders(
bool hideDeleted = true);

/// <summary>
/// to do the query and return object
/// to do the query and return object
/// </summary>
/// <param name="singleItemDbPath"></param>
/// <param name="colorClassActiveList"></param>
Expand All @@ -69,7 +69,7 @@ IEnumerable<FileIndexItem> DisplayFileFolders(
SortType? sort = SortType.FileName);

/// <summary>
/// To make an object without any query
/// To make an object without any query
/// </summary>
/// <param name="fileIndexItemsList"></param>
/// <param name="singleItemDbPath"></param>
Expand All @@ -87,22 +87,21 @@ IEnumerable<FileIndexItem> DisplayFileFolders(
SortType? sort = SortType.FileName);

/// <summary>
/// Get FirstOrDefault for that filePath
/// Get FirstOrDefault for that filePath
/// </summary>
/// <param name="filePath">subPath style</param>
/// <returns>item</returns>
FileIndexItem? GetObjectByFilePath(string filePath);

/// <summary>
/// Get FirstOrDefault for that filePath
/// Get FirstOrDefault for that filePath
/// </summary>
/// <param name="filePath">subPath style</param>
/// <param name="cacheTime">time of cache </param>
/// <returns>item</returns>
Task<FileIndexItem?> GetObjectByFilePathAsync(string filePath, TimeSpan? cacheTime = null);

/// <summary>
///
/// </summary>
/// <param name="inputFilePath"></param>
/// <param name="collections"></param>
Expand All @@ -111,7 +110,7 @@ Task<List<FileIndexItem>> GetObjectsByFilePathAsync(string inputFilePath,
bool collections);

/// <summary>
/// Cached result that contain values
/// Cached result that contain values
/// </summary>
/// <param name="inputFilePaths">List of filePaths</param>
/// <param name="collections">enable implicit raw files with the same base name</param>
Expand All @@ -120,7 +119,7 @@ Task<List<FileIndexItem>> GetObjectsByFilePathAsync(List<string> inputFilePaths,
bool collections);

/// <summary>
/// Query direct by filePaths (without cache)
/// Query direct by filePaths (without cache)
/// </summary>
/// <param name="filePathList">List of filePaths</param>
/// <returns></returns>
Expand All @@ -133,7 +132,7 @@ Task<List<FileIndexItem>> RemoveItemAsync(
List<FileIndexItem> updateStatusContentList);

/// <summary>
/// Clear the directory name from the cache
/// Clear the directory name from the cache
/// </summary>
/// <param name="directoryName">the path of the directory (there is no parent generation)</param>
bool RemoveCacheParentItem(string directoryName);
Expand All @@ -154,6 +153,8 @@ Task<List<FileIndexItem>> GetAllObjectsAsync(List<string> filePaths,

Task<FileIndexItem> AddItemAsync(FileIndexItem fileIndexItem);

Task<bool> ExistsAsync(string filePath);

Task<List<FileIndexItem>> AddRangeAsync(List<FileIndexItem> fileIndexItemList);

Task<FileIndexItem> UpdateItemAsync(FileIndexItem updateStatusContent);
Expand All @@ -163,7 +164,7 @@ Task<List<FileIndexItem>> GetAllObjectsAsync(List<string> filePaths,


/// <summary>
/// Update parent item with all data from child items
/// Update parent item with all data from child items
/// </summary>
/// <param name="directoryName">parent directory</param>
/// <param name="items">all items that are in this folder</param>
Expand All @@ -172,14 +173,14 @@ bool AddCacheParentItem(string directoryName,
List<FileIndexItem> items);

/// <summary>
/// Cache API within Query to update cached items
/// For DisplayFileFolders and SingleItem
/// Cache API within Query to update cached items
/// For DisplayFileFolders and SingleItem
/// </summary>
/// <param name="updateStatusContent">items to update</param>
void CacheUpdateItem(List<FileIndexItem> updateStatusContent);

/// <summary>
/// And remove content from cache
/// And remove content from cache
/// </summary>
/// <param name="updateStatusContent">list of items</param>
void RemoveCacheItem(List<FileIndexItem> updateStatusContent);
Expand All @@ -188,14 +189,14 @@ bool AddCacheParentItem(string directoryName,


/// <summary>
/// Add Sub Path Folder - Parent Folders
/// root(/)
/// /2017 *= index only this folder
/// /2018
/// If you use the cmd: $ starskycli -s "/2017"
/// the folder '2017' it self is not added
/// and all parent paths are not included
/// this class does add those parent folders
/// Add Sub Path Folder - Parent Folders
/// root(/)
/// /2017 *= index only this folder
/// /2018
/// If you use the cmd: $ starskycli -s "/2017"
/// the folder '2017' it self is not added
/// and all parent paths are not included
/// this class does add those parent folders
/// </summary>
/// <param name="subPath">subPath as input</param>
/// <returns>void</returns>
Expand Down
37 changes: 37 additions & 0 deletions starsky/starsky.foundation.database/Query/QueryExistsAsync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using starsky.foundation.database.Data;
using starsky.foundation.database.Interfaces;
using starsky.foundation.platform.Helpers;

namespace starsky.foundation.database.Query;

/// <summary>
/// QueryExistsAsync
/// </summary>
public partial class Query : IQuery
{
public async Task<bool> ExistsAsync(string filePath)
{
if ( filePath != "/" )
{
filePath = PathHelper.RemoveLatestSlash(filePath);
}

async Task<bool> LocalQuery(ApplicationDbContext context)
{
return await context.FileIndex.AnyAsync(p => p.FilePath == filePath);
}

try
{
return await LocalQuery(_context);
}
catch ( ObjectDisposedException e )
{
_logger.LogInformation("[ExistsAsync] catch-ed ObjectDisposedException", e);
return await LocalQuery(new InjectServiceScope(_scopeFactory).Context());
}
}
}
19 changes: 14 additions & 5 deletions starsky/starsky/Controllers/DiskController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,16 @@ public async Task<IActionResult> Mkdir(string f)
FilePath = subPath, Status = FileIndexItem.ExifStatus.Ok
};

// if it exists make sure the item is added to the database
await AddIfExistItemAsync(subPath);

if ( _iStorage.ExistFolder(subPath) )
{
toAddStatus.Status = FileIndexItem.ExifStatus.OperationNotSupported;
syncResultsList.Add(toAddStatus);
continue;
}

await _query.AddItemAsync(new FileIndexItem(subPath)
{
IsDirectory = true, ImageFormat = ExtensionRolesHelper.ImageFormat.directory
});

// add to fs
_iStorage.CreateDirectory(subPath);

Expand All @@ -100,6 +98,17 @@ await _query.AddItemAsync(new FileIndexItem(subPath)
await SyncMessageToSocket(syncResultsList, ApiNotificationType.Mkdir);

return Json(syncResultsList);

async Task AddIfExistItemAsync(string subPath)
{
if ( !await _query.ExistsAsync(subPath) )
{
await _query.AddItemAsync(new FileIndexItem(subPath)
{
IsDirectory = true, ImageFormat = ExtensionRolesHelper.ImageFormat.directory
});
}
}
}

/// <summary>
Expand Down
15 changes: 10 additions & 5 deletions starsky/starskytest/FakeMocks/FakeIQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,6 @@ public void ResetItemByHash(string fileHash)
throw new NotImplementedException();
}

public List<FileIndexItem> GetAllFolders()
{
return _content.Where(p => p.IsDirectory == true).ToList();
}

public Task<List<FileIndexItem>> GetFoldersAsync(string subPath)
{
return Task.FromResult(_content
Expand Down Expand Up @@ -294,6 +289,11 @@ public async Task<FileIndexItem> AddItemAsync(FileIndexItem fileIndexItem)
return fileIndexItem;
}

public Task<bool> ExistsAsync(string filePath)
{
throw new NotImplementedException();
}

public async Task<List<FileIndexItem>> AddRangeAsync(List<FileIndexItem> fileIndexItemList)
{
foreach ( var fileIndexItem in fileIndexItemList )
Expand Down Expand Up @@ -407,6 +407,11 @@ public Task<int> CountAsync(Expression<Func<FileIndexItem, bool>>? expression =
return Task.FromResult(expression == null ? _content.Count : _content.Count(func!));
}

public List<FileIndexItem> GetAllFolders()
{
return _content.Where(p => p.IsDirectory == true).ToList();
}

[SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract")]
public List<FileIndexItem> GetAllFiles(string subPath)
{
Expand Down
Loading

0 comments on commit c18df69

Please sign in to comment.