Skip to content

Commit

Permalink
Refactor: rewrite to async + cleanup obsoletes (#2011)
Browse files Browse the repository at this point in the history
* Refactor: async.

* async await

* Fix test names (scandallum: _MsSql_ in inmem tests).

* Move Content's obsolete methods to a new partial class.

* RebuildIndex --> RebuildIndexAsync

* SaveSameVersionAsync(bool validOnly... and SaveExplicitVersionAsync

* SaveSameVersion --> SaveSameVersionAsync

* OData.Delete is async.

* Use async overloads.

* Use async version.

* Obsolete(..., true): Delete(bool)

* Use new internal TypePermission enum instead of public TypeAllow.

* ForceDelete(*) is strongly obsolete

* Node.Delete() is strictly obsolete.

* Some "IsIn..." methods are strictly obsolete.

* ContentTypeInstaller.RemoveContentType is strictly obsolete.

* Node.Delete(int|string) are strictly obsolete.

* User.GetRoles() and IsInRole() are strictly obsolete.

* TrashBin.DeleteNode(GenericContent) is strictly obsolete.
  • Loading branch information
kavics committed Jan 15, 2024
1 parent 7328bb8 commit 4cc38ae
Show file tree
Hide file tree
Showing 71 changed files with 695 additions and 501 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,27 @@ public Task<BlobStorageContext> GetBlobStorageContextAsync(int fileId, bool clea
return STT.Task.FromResult(result);
}

public STT.Task InsertBinaryPropertyAsync(IBlobProvider blobProvider, BinaryDataValue value, int versionId, int propertyTypeId,
public async STT.Task InsertBinaryPropertyAsync(IBlobProvider blobProvider, BinaryDataValue value, int versionId, int propertyTypeId,
bool isNewNode, SnDataContext dataContext)
{
var streamLength = value.Stream?.Length ?? 0;
var ctx = new BlobStorageContext(blobProvider) { VersionId = versionId, PropertyTypeId = propertyTypeId, FileId = 0, Length = streamLength };

// blob operation

blobProvider.AllocateAsync(ctx, CancellationToken.None).GetAwaiter().GetResult();
await blobProvider.AllocateAsync(ctx, dataContext.CancellationToken).ConfigureAwait(false);

using (var stream = blobProvider.GetStreamForWrite(ctx))
value.Stream?.CopyTo(stream);
if (value.Stream != null)
await value.Stream.CopyToAsync(stream);

value.BlobProviderName = ctx.Provider.GetType().FullName;
value.BlobProviderData = BlobStorageContext.SerializeBlobProviderData(ctx.BlobProviderData);

// metadata operation
var db = DataProvider.DB;
if (!isNewNode)
DeleteBinaryPropertyAsync(versionId, propertyTypeId, dataContext).GetAwaiter().GetResult();
await DeleteBinaryPropertyAsync(versionId, propertyTypeId, dataContext).ConfigureAwait(false);

var fileId = db.Files.GetNextId();
db.Files.Insert(new FileDoc
Expand All @@ -96,16 +97,14 @@ public STT.Task InsertBinaryPropertyAsync(IBlobProvider blobProvider, BinaryData
value.Id = binaryPropertyId;
value.FileId = fileId;
value.Timestamp = 0L; //TODO: file row timestamp

return STT.Task.CompletedTask;
}

public STT.Task InsertBinaryPropertyWithFileIdAsync(BinaryDataValue value, int versionId, int propertyTypeId, bool isNewNode,
public async STT.Task InsertBinaryPropertyWithFileIdAsync(BinaryDataValue value, int versionId, int propertyTypeId, bool isNewNode,
SnDataContext dataContext)
{
var db = DataProvider.DB;
if (!isNewNode)
DeleteBinaryPropertyAsync(versionId, propertyTypeId, dataContext).GetAwaiter().GetResult();
await DeleteBinaryPropertyAsync(versionId, propertyTypeId, dataContext).ConfigureAwait(false);

var binaryPropertyId = db.BinaryProperties.GetNextId();
db.BinaryProperties.Insert(new BinaryPropertyDoc
Expand All @@ -117,11 +116,9 @@ public STT.Task InsertBinaryPropertyWithFileIdAsync(BinaryDataValue value, int v
});

value.Id = binaryPropertyId;

return STT.Task.CompletedTask;
}

public STT.Task UpdateBinaryPropertyAsync(IBlobProvider blobProvider, BinaryDataValue value, SnDataContext dataContext)
public async STT.Task UpdateBinaryPropertyAsync(IBlobProvider blobProvider, BinaryDataValue value, SnDataContext dataContext)
{
var streamLength = value.Stream?.Length ?? 0;
var isExternal = false;
Expand All @@ -136,7 +133,7 @@ public STT.Task UpdateBinaryPropertyAsync(IBlobProvider blobProvider, BinaryData
Length = streamLength,
};

blobProvider.AllocateAsync(ctx, CancellationToken.None).GetAwaiter().GetResult();
await blobProvider.AllocateAsync(ctx, dataContext.CancellationToken).ConfigureAwait(false);
isExternal = true;

value.BlobProviderName = ctx.Provider.GetType().FullName;
Expand All @@ -147,7 +144,7 @@ public STT.Task UpdateBinaryPropertyAsync(IBlobProvider blobProvider, BinaryData
var hasStream = isRepositoryStream || value.Stream is MemoryStream;
if (!isExternal && !hasStream)
// do not do any database operation if the stream is not modified
return STT.Task.CompletedTask;
return;

var db = DataProvider.DB;
var fileId = db.Files.GetNextId();
Expand Down Expand Up @@ -179,16 +176,14 @@ public STT.Task UpdateBinaryPropertyAsync(IBlobProvider blobProvider, BinaryData

if(streamLength == 0)
{
blobProvider.ClearAsync(newCtx, dataContext.CancellationToken)
.ConfigureAwait(false).GetAwaiter().GetResult();
await blobProvider.ClearAsync(newCtx, dataContext.CancellationToken).ConfigureAwait(false);
}
else
{
using (var stream = blobProvider.GetStreamForWrite(newCtx))
value.Stream?.CopyTo(stream);
if (value.Stream != null)
await value.Stream.CopyToAsync(stream);
}

return STT.Task.CompletedTask;
}

public STT.Task DeleteBinaryPropertyAsync(int versionId, int propertyTypeId, SnDataContext dataContext)
Expand Down Expand Up @@ -428,13 +423,13 @@ private void CleanupFilesSetDeleteFlag(bool immediately)
item.IsDeleted = true;
}

public virtual Task<bool> CleanupFilesAsync(CancellationToken cancel)
public virtual async Task<bool> CleanupFilesAsync(CancellationToken cancel)
{
var db = DataProvider.DB;

var file = db.Files.FirstOrDefault(x => x.IsDeleted);
if (file == null)
return STT.Task.FromResult(false);
return false;
db.Files.Remove(file);

// delete bytes from the blob storage
Expand All @@ -443,9 +438,9 @@ public virtual Task<bool> CleanupFilesAsync(CancellationToken cancel)
{
VersionId = 0, PropertyTypeId = 0, FileId = file.FileId, Length = file.Size
};
ctx.Provider.DeleteAsync(ctx, cancel).ConfigureAwait(false).GetAwaiter().GetResult();
await ctx.Provider.DeleteAsync(ctx, cancel).ConfigureAwait(false);

return STT.Task.FromResult(true);
return true;
}

// Do not increase this value int he production scenario. It is only used in tests.
Expand Down
10 changes: 4 additions & 6 deletions src/ContentRepository.InMemory/InMemoryDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ public override Task<IEnumerable<NodeData>> LoadNodesAsync(int[] versionIdArray,
}
}

public override STT.Task DeleteNodeAsync(NodeHeadData nodeHeadData, CancellationToken cancellationToken)
public override async STT.Task DeleteNodeAsync(NodeHeadData nodeHeadData, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var needToCleanupFiles = false;
Expand All @@ -417,7 +417,7 @@ public override STT.Task DeleteNodeAsync(NodeHeadData nodeHeadData, Cancellation

var nodeDoc = DB.Nodes.FirstOrDefault(x => x.NodeId == nodeId);
if (nodeDoc == null)
return STT.Task.CompletedTask;
return;

if (nodeDoc.Timestamp != timestamp)
throw new NodeIsOutOfDateException(
Expand All @@ -443,7 +443,7 @@ public override STT.Task DeleteNodeAsync(NodeHeadData nodeHeadData, Cancellation
.Select(r => r.ReferencePropertyId)
.ToArray();

BlobStorage.DeleteBinaryPropertiesAsync(versionIds, dataContext).GetAwaiter().GetResult();
await BlobStorage.DeleteBinaryPropertiesAsync(versionIds, dataContext).ConfigureAwait(false);

//foreach (var refPropPropId in refPropPropIds) ...
foreach (var refPropId in refPropIds)
Expand Down Expand Up @@ -475,9 +475,7 @@ public override STT.Task DeleteNodeAsync(NodeHeadData nodeHeadData, Cancellation
}

if (needToCleanupFiles)
BlobStorage.DeleteOrphanedFilesAsync(cancellationToken).ConfigureAwait(false).GetAwaiter().GetResult();

return STT.Task.CompletedTask;
await BlobStorage.DeleteOrphanedFilesAsync(cancellationToken).ConfigureAwait(false);
}

public override STT.Task MoveNodeAsync(NodeHeadData sourceNodeHeadData, int targetNodeId,
Expand Down
1 change: 1 addition & 0 deletions src/ContentRepository.InMemory/InMemoryQueryEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public InMemoryQueryEngine(InMemorySearchEngine searchEngine)
_searchEngine = searchEngine;
}

[Obsolete("Use async version instead", false)]
public QueryResult<int> ExecuteQuery(SnQuery query, IPermissionFilter filter, IQueryContext context)
{
return ExecuteQueryAsync(query, filter, context, CancellationToken.None)
Expand Down
7 changes: 3 additions & 4 deletions src/ContentRepository/ApplicationModel/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,18 @@ public override async Task SaveAsync(NodeSaveSettings settings, CancellationToke
DeviceManager.Reset();
}

[Obsolete("Use async version instead", false)]
[Obsolete("Use async version instead", true)]
public override void Delete(bool bypassTrash)
{
base.Delete(bypassTrash);
DeviceManager.Reset();
DeleteAsync(bypassTrash, CancellationToken.None).GetAwaiter().GetResult();
}
public override async Task DeleteAsync(bool bypassTrash, CancellationToken cancel)
{
await base.DeleteAsync(bypassTrash, cancel);
DeviceManager.Reset();
}

[Obsolete("Use async version instead", false)]
[Obsolete("Use async version instead", true)]
public override void ForceDelete()
{
ForceDeleteAsync(CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult();
Expand Down
2 changes: 1 addition & 1 deletion src/ContentRepository/Aspect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ public override async System.Threading.Tasks.Task SaveAsync(NodeSaveSettings set
/// The logged-in user need to have ManageListsAndWorkspaces permission,
/// otherwise <see cref="SenseNetSecurityException"/> will be thrown.
/// </summary>
[Obsolete("Use async version instead", false)]
[Obsolete("Use async version instead", true)]
public override void ForceDelete()
{
ForceDeleteAsync(CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult();
Expand Down
Loading

0 comments on commit 4cc38ae

Please sign in to comment.