This class holds the information stored in the snapshot map. It identifies the snapshot and the location it is stored in the snapshot file
 public bool Equals(SnapshotMapEntry sme)
 {
     if (!Metadata.Equals(sme.Metadata)) return false;
     if (Position != sme.Position) return false;
     if (Length != sme.Length) return false;
     if (Deleted != sme.Deleted) return false;
     return true;
 }
예제 #2
0
        private void WriteSME(FileStream stream, SnapshotMapEntry sme)
        {
            //lock (_smeLock) {

            try {
                var pos = stream.Position;

                // Convert the PersistenceId to bytes and store them in the buffer, leaving space at the beginning to store its length
                var temp   = Encoding.ASCII.GetBytes(sme.Metadata.PersistenceId);
                var length = temp.Length;
                var buffer = new byte[length + SIZE_OF_PERSISTENCE_ID_LENGTH + SIZE_OF_SEQ_NUM + SIZE_OF_DATE_TIME +
                                      SIZE_OF_SNAPSHOT_LENGTH + SIZE_OF_SNAPSHOT_POSITION + SIZE_OF_DELETED];

                // Convert and store the length of the persistence ID
                var bits = BitConverter.GetBytes(length);
                bits.CopyTo(buffer, 0);

                // This is slower than the original code that placed the bytes from the persistence Id straight into the buffer
                // Copy the bytes into the main buffer
                temp.CopyTo(buffer, SIZE_OF_PERSISTENCE_ID_LENGTH);

                // Convert the sequence number of the snapshot
                var offset = length + SIZE_OF_PERSISTENCE_ID_LENGTH;
                var bits1  = BitConverter.GetBytes(sme.Metadata.SequenceNr);
                bits1.CopyTo(buffer, offset);

                // Convert and store the timestamp of the snapshot
                var datetime = sme.Metadata.Timestamp.ToBinary();
                offset += SIZE_OF_SEQ_NUM;
                var bits2 = BitConverter.GetBytes(datetime);
                bits2.CopyTo(buffer, offset);

                // Convert and store the position of the snapshot in the snapshot file
                var position = sme.Position;
                offset += SIZE_OF_DATE_TIME;
                var bits3 = BitConverter.GetBytes(position);
                bits3.CopyTo(buffer, offset);

                // Convert and store the length of the snapshot
                var snapLength = sme.Length;
                offset += SIZE_OF_SNAPSHOT_POSITION;
                var bits4 = BitConverter.GetBytes(snapLength);
                bits4.CopyTo(buffer, offset);

                // Convert and store the deleted marker that denotes if this snapshot is deleted
                offset        += SIZE_OF_SNAPSHOT_LENGTH;
                buffer[offset] = (byte)(sme.Deleted ? 1 : 0);

                // Write to stream
                stream.Write(buffer, 0, offset + 1);
            } catch (Exception e) {
                _log.Error("Error writing SME, msg = {0}, location = {1}", e.Message, e.StackTrace);
                throw e;
            }
            //}
        }
        /// <summary>
        ///     Saves the snapshot to the end of the file.
        /// </summary>
        /// <param name="metadata">TBD</param>
        /// <param name="snapshot">TBD</param>
        //        [MethodImpl(MethodImplOptions.Synchronized)]
        protected virtual void Save(SnapshotMetadata metadata, object snapshot)
        {
            _save++;
            if (_save % 10000 == 0)
            {
                _log.Info("Save() - count of calls={0}", _saveasync);
            }

            lock (_smeLock)
            {
                try
                {
                    // Serialize the object that describes the snapshot
                    var serializer = _serialization.FindSerializerFor(snapshot, _defaultSerializer);
                    var bytes      = serializer.ToBinary(snapshot);

                    // Get the current location of the file stream so we know where the object is stored on the disk
                    var pos = _writeStream.Position;

                    // Write the Snapshot to disk
                    _writeStream.Write(bytes, 0, bytes.Length);

                    // Save the information about the snapshot and where it is located in the file to the map
                    // Create a snapshot map entry to describe the snapshot
                    var sme = new SnapshotMapEntry(metadata, pos, bytes.Length, false);

                    //                _log.Debug("Save() - persitenceId={0}\tposition={1}\tlength={2}", metadata.PersistenceId, pos, bytes.Length);

                    // Write the SME to disk
                    WriteSME(_writeSMEStream, sme);

                    // Save the SME in the map
                    if (!SnapshotMap.TryGetValue(metadata.PersistenceId, out var currentValue))
                    {
                        SnapshotMap.TryAdd(sme.Metadata.PersistenceId, sme);
                    }
                    else
                    {
                        SnapshotMap.TryUpdate(sme.Metadata.PersistenceId, sme, currentValue);
                    }
                }
                catch (SerializationException e)
                {
                    _log.Error("Failed to serialize. Reason: {0}\n{1}", e.Message, e.StackTrace);
                    throw e;
                }
                catch (Exception e)
                {
                    _log.Error("ERROR in Save. Message={0}\n StackTrace={1}", e.Message, e.StackTrace);
                    throw e;
                }
            }
        }
예제 #4
0
        /// <summary>
        ///     Saves the snapshot to the end of the file.
        /// </summary>
        /// <param name="metadata">TBD</param>
        /// <param name="snapshot">TBD</param>
        //        [MethodImpl(MethodImplOptions.Synchronized)]
        protected void Save(SnapshotMetadata metadata, object original_snapshot)
        {
            if (!(original_snapshot is ICloneable))
            {
                throw new ObjectDoesNotImplementICloneable($"Type {original_snapshot.GetType()} does not implement ICloneable. It Must to be snapshotted.");
            }

            lock (_smeLock) {
                try {
                    // Serialize the object that describes the snapshot
                    object snapshot   = original_snapshot; //relegated fix to snapshot called
                    var    serializer = _serialization.FindSerializerFor(snapshot, _defaultSerializer);
                    byte[] bytes      = null;
                    try {
                        bytes = serializer.ToBinary(snapshot);
                    } catch (Exception x) {
                        _log.Error("ERROR Failed to serialize. {0}\n{1}", x.Message, x.StackTrace);
                        throw x; //debugging here
                    }

                    // Get the current location of the file stream so we know where the object is stored on the disk
                    var pos = _writeStream.Position;

                    // Write the Snapshot to disk
                    _writeStream.Write(bytes, 0, bytes.Length);

                    // Save the information about the snapshot and where it is located in the file to the map
                    // Create a snapshot map entry to describe the snapshot
                    var sme = new SnapshotMapEntry(metadata, pos, bytes.Length, false);

                    // Write the SME to disk
                    WriteSME(_writeSMEStream, sme);

                    // Save the SME in the map
                    if (SnapshotMap.TryGetValue(sme.Metadata.PersistenceId, out var currentValue))
                    {
                        SnapshotMap.TryUpdate(sme.Metadata.PersistenceId, sme, currentValue);
                    }
                    else
                    {
                        SnapshotMap.TryAdd(sme.Metadata.PersistenceId, sme);
                    }
                } catch (SerializationException e) {
                    _log.Error("Failed to serialize. Reason: {0}\n{1}", e.Message, e.StackTrace);
                    throw e;
                } catch (Exception e) {
                    _log.Error("ERROR in Save. Message={0}\n StackTrace={1}", e.Message, e.StackTrace);
                    throw e;
                }
            }
        }