Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongodb implementation #4127

Merged
merged 31 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f29eff6
Implemented MongoDb
gurkanguran May 30, 2023
5e10320
Refactor
gurkanguran May 30, 2023
6c811b3
Refactor
gurkanguran May 30, 2023
252a2bb
Use main entity models in mongodb
gurkanguran Jun 1, 2023
2b69729
Fixed object serialization issue
gurkanguran Jun 9, 2023
839acc8
Refactor
gurkanguran Jun 9, 2023
6f15926
Fixed issues caused by duplicate collection names
gurkanguran Jun 9, 2023
0bd2002
Optimized count query
gurkanguran Jun 12, 2023
c2e0dec
Merge branch 'v3' into mongodb
gurkanguran Jun 12, 2023
5568af6
Fixed the issues after merge
gurkanguran Jun 12, 2023
0c69308
Merge branch 'v3' into mongodb
gurkanguran Jun 12, 2023
d1cffa2
Revert "Fixed the issues after merge"
gurkanguran Jun 12, 2023
1b30f42
Use expression helper to solve mongodb query issue
gurkanguran Jun 12, 2023
28b02b5
Merge branch 'v3' into mongodb
gurkanguran Jun 12, 2023
0018e71
Handle null values during deserialization
gurkanguran Jun 12, 2023
9fe1b9f
Moved MongoDb project to persistence folder
gurkanguran Jun 12, 2023
703be36
Merge branch 'v3' into mongodb
gurkanguran Jun 14, 2023
8b88d70
Refactor
gurkanguran Jun 14, 2023
461e887
Did some refactor and added type serializer for variable fix
gurkanguran Jun 15, 2023
0d57717
Modularize mongodb indices creation
gurkanguran Jun 17, 2023
1c67677
Refactor
gurkanguran Jun 17, 2023
14c56d3
Merge branch 'v3' into mongodb
gurkanguran Jun 17, 2023
ceda7e2
Fixed the build
gurkanguran Jun 17, 2023
0b8d919
Refactor
gurkanguran Jun 18, 2023
0949fda
Merge branch 'v3' into mongodb
gurkanguran Jun 18, 2023
6f25915
Merge branch 'v3' into mongodb
gurkanguran Jun 18, 2023
4258026
Merge branch 'v3' into mongodb
sfmskywalker Jun 28, 2023
7f22b71
Remove unused imports
sfmskywalker Jun 28, 2023
2a1b960
Import necessary usings
sfmskywalker Jun 28, 2023
501b8a3
Implement variable serializer
sfmskywalker Jun 28, 2023
94eabd5
Update SQLite migrations
sfmskywalker Jun 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed object serialization issue
  • Loading branch information
gurkanguran committed Jun 9, 2023
commit 2b69729bf3f18b6567f629f43bfe0ce469334737
28 changes: 9 additions & 19 deletions src/modules/Elsa.MongoDB/Features/MongoDbFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Elsa.MongoDB.Modules.Runtime;
using Elsa.MongoDB.Options;
using Elsa.MongoDB.Serializers;
using Elsa.MongoDB.Stores.Management;
using Elsa.Workflows.Core.Services;
using Elsa.Workflows.Core.State;
using Elsa.Workflows.Management.Contracts;
Expand Down Expand Up @@ -53,17 +52,17 @@ public override void Apply()

Services.AddSingleton(CreateDatabase);

RegisterClassMaps();
RegisterSerializers();

AddMongoCollection<Application>(Services, "applications");
AddMongoCollection<User>(Services, "users");
AddMongoCollection<Role>(Services, "roles");
AddMongoCollection<Label>(Services, "labels");
AddMongoCollection<WorkflowDefinitionLabel>(Services, "workflow-definition-labels");
AddMongoCollection<WorkflowDefinition>(Services, "workflow-definitions");
AddMongoCollection<WorkflowInstance>(Services, "workflow-instances");
AddMongoCollection<WorkflowState>(Services, "workflow-definitions");
AddMongoCollection<WorkflowExecutionLogRecord>(Services, "workflow-instances");
AddMongoCollection<WorkflowDefinitionLabel>(Services, "workflow_definition_labels");
AddMongoCollection<WorkflowDefinition>(Services, "workflow_definitions");
AddMongoCollection<WorkflowInstance>(Services, "workflow_instances");
AddMongoCollection<WorkflowState>(Services, "workflow_definitions");
AddMongoCollection<WorkflowExecutionLogRecord>(Services, "workflow_instances");
AddMongoCollection<StoredTrigger>(Services, "triggers");
AddMongoCollection<StoredBookmark>(Services, "bookmarks");

Expand All @@ -83,18 +82,9 @@ public override void Apply()
.AddHealthChecks();
}

private void RegisterClassMaps()
private void RegisterSerializers()
{
BsonClassMap.RegisterClassMap<HttpEndpointBookmarkPayload>(cm =>
{
cm.AutoMap();
cm.SetDiscriminator("HttpEndpointBookmarkPayload");
});

BsonClassMap.RegisterClassMap<StoredTrigger>(cm => {
cm.AutoMap();
cm.GetMemberMap(c => c.Payload).SetSerializer(new PolymorphicSerializer());
});
BsonSerializer.RegisterSerializer(typeof(object), new PolymorphicSerializer());
}

private static IMongoDatabase CreateDatabase(IServiceProvider sp)
Expand All @@ -103,7 +93,7 @@ private static IMongoDatabase CreateDatabase(IServiceProvider sp)
var settings = MongoClientSettings.FromConnectionString(options.ConnectionString);

settings.ClusterConfigurator = cb => cb.Subscribe(new DiagnosticsActivityEventSubscriber());
gurkanguran marked this conversation as resolved.
Show resolved Hide resolved
settings.ApplicationName = "elsa-workflows";
settings.ApplicationName = "elsa_workflows";
gurkanguran marked this conversation as resolved.
Show resolved Hide resolved
settings.WriteConcern = WriteConcern.WMajority;
settings.ReadConcern = ReadConcern.Available;
settings.ReadPreference = ReadPreference.Nearest;
Expand Down

This file was deleted.

54 changes: 27 additions & 27 deletions src/modules/Elsa.MongoDB/Serializers/PolymorphicSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
using Elsa.Http.Models;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;

namespace Elsa.MongoDB.Serializers;

public class PolymorphicSerializer : IBsonSerializer<object>
{
private readonly Dictionary<string, IBsonSerializer> _serializers = new Dictionary<string, IBsonSerializer>();

public PolymorphicSerializer()
{
_serializers.Add(typeof(HttpEndpointBookmarkPayload).FullName, new HttpEndpointBookmarkPayloadSerializer());
}

public Type ValueType => typeof(object);

public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var typeValue = context.Reader.ReadString();
if (_serializers.TryGetValue(typeValue, out var serializer))
var reader = context.Reader;
var bookmark = reader.GetBookmark();
var bsonType = reader.GetCurrentBsonType();
if (bsonType == BsonType.Document)
{
return serializer.Deserialize(context, args);
var document = BsonDocumentSerializer.Instance.Deserialize(context);
if (document.Contains("$type") && document.Contains("$value"))
{
var typeValue = document["$type"].AsString;
var type = Type.GetType(typeValue);
var value = BsonSerializer.Deserialize(document["$value"].AsBsonDocument, type);
return value;
}
}
throw new BsonSerializationException($"No serializer found for type {typeValue}");
reader.ReturnToBookmark(bookmark);
return BsonValueSerializer.Instance.Deserialize(context);
}

public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object? value)
{
if (_serializers.TryGetValue(value.GetType().FullName, out var serializer))
var writer = context.Writer;
if (value == null)
{
context.Writer.WriteString(value.GetType().FullName);
serializer.Serialize(context, args, value);
writer.WriteNull();
}
else
{
throw new BsonSerializationException($"No serializer found for type {value.GetType().FullName}");
var type = value.GetType();
var serializer = BsonSerializer.LookupSerializer(type);
writer.WriteStartDocument();
writer.WriteName("$type");
writer.WriteString(type.AssemblyQualifiedName);
writer.WriteName("$value");
serializer.Serialize(context, value);
writer.WriteEndDocument();
}
}

object IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
return Deserialize(context, args);
}

void IBsonSerializer.Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
{
Serialize(context, args, value);
}
}