Created
October 30, 2019 23:18
-
-
Save Aaronontheweb/82f0d6fd6fcac177d59cdc9fd80944a6 to your computer and use it in GitHub Desktop.
Akka.Persistence.MongoDbReader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AkkaMongoReader | |
{ | |
public ActorSystem Sys { get; } | |
private readonly Akka.Serialization.Serialization _serialization; | |
public AkkaMongoReader(ActorSystem sys) | |
{ | |
Sys = sys; | |
_serialization = Sys.Serialization; | |
} | |
public Persistent ToPersistenceRepresentation(JournalEntry entry) | |
{ | |
int? serializerId = null; | |
Type type = null; | |
var legacy = entry.SerializerId.HasValue || !string.IsNullOrEmpty(entry.Manifest); | |
if (!legacy) | |
{ | |
var ser = _serialization.FindSerializerForType(typeof(Persistent)); | |
return ser.FromBinary<Persistent>((byte[])entry.Payload); | |
} | |
// legacy serialization | |
if (!entry.SerializerId.HasValue && !string.IsNullOrEmpty(entry.Manifest)) | |
type = Type.GetType(entry.Manifest, true); | |
else | |
serializerId = entry.SerializerId; | |
if (entry.Payload is byte[] bytes) | |
{ | |
object deserialized = null; | |
if (serializerId.HasValue) | |
{ | |
deserialized = _serialization.Deserialize(bytes, serializerId.Value, entry.Manifest); | |
} | |
else | |
{ | |
var deserializer = _serialization.FindSerializerForType(type); | |
deserialized = deserializer.FromBinary(bytes, type); | |
} | |
if (deserialized is Persistent p) | |
return p; | |
return new Persistent(deserialized, entry.SequenceNr, entry.PersistenceId, entry.Manifest, entry.IsDeleted, ActorRefs.NoSender); | |
} | |
else // backwards compat for object serialization - Payload was already deserialized by BSON | |
{ | |
return new Persistent(entry.Payload, entry.SequenceNr, entry.PersistenceId, entry.Manifest, | |
entry.IsDeleted, ActorRefs.NoSender); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment