Class used for storing intermediate result of the IPersistentRepresentation as BsonDocument into the MongoDB-Collection
 private Persistent ToPersistanceRepresentation(JournalEntry entry, IActorRef sender)
 {
     return(new Persistent(entry.Payload, entry.SequenceNr, entry.Manifest, entry.PersistenceId, entry.IsDeleted, sender));
 }
Exemplo n.º 2
0
        private Persistent ToPersistenceRepresentation(JournalEntry entry, IActorRef sender)
        {
            if (_settings.LegacySerialization)
            {
                var manifest = string.IsNullOrEmpty(entry.Manifest) ? entry.Payload.GetType().TypeQualifiedName() : entry.Manifest;

                return(new Persistent(
                           entry.Payload,
                           entry.SequenceNr,
                           entry.PersistenceId,
                           manifest,
                           entry.IsDeleted,
                           sender,
                           timestamp: ToTicks(entry.Ordering))); // MongoDb timestamps are stored as Unix Epoch
            }

            var legacy = entry.SerializerId.HasValue || !string.IsNullOrEmpty(entry.Manifest);

            if (!legacy)
            {
                var ser    = _serialization.FindSerializerForType(typeof(Persistent));
                var output = ser.FromBinary <Persistent>((byte[])entry.Payload);

                // backwards compatibility for https://github.com/akkadotnet/akka.net/pull/4680
                // it the timestamp is not defined in the binary payload
                if (output.Timestamp == 0L)
                {
                    output = (Persistent)output.WithTimestamp(ToTicks(entry.Ordering));
                }

                return(output);
            }

            int? serializerId = null;
            Type type         = null;

            // 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((Persistent)p.WithTimestamp(ToTicks(entry.Ordering)));
                }

                return(new Persistent(deserialized, entry.SequenceNr, entry.PersistenceId, entry.Manifest, entry.IsDeleted, sender, timestamp: ToTicks(entry.Ordering)));
            }
            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, sender, timestamp: ToTicks(entry.Ordering)));
            }
        }