Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Akka.Persistence.MongoDbReader
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
You can’t perform that action at this time.