Exemplo n.º 1
0
 public void SetUp()
 {
     Snapshot = new Snapshot(Guid.NewGuid(), 1, new TestSnapshot { Name = "TestName"});
     _foldername = Snapshot.EventSourceId.ToString().Substring(0, 2);
     _filename = Snapshot.EventSourceId.ToString().Substring(2) + ".ss";
     SnapshotStore.SaveShapshot(Snapshot);
 }
Exemplo n.º 2
0
 public void SaveSnapshot(Snapshot source)
 {
     FileInfo file = source.EventSourceId.GetSnapshotFileInfo(_path);
     if (!file.Exists && !file.Directory.Exists)
         file.Directory.Create();
     var jo = JObject.FromObject(source);
     WriteSnapshotTest(source, file.FullName, jo.ToString());
 }
Exemplo n.º 3
0
 private static void WriteSnapshotTest(Snapshot source, string path, string jsonData)
 {
     try
     {
         source.EventSourceId.GetWriteLock("snapshot");
         File.WriteAllText(path, source.GetType().AssemblyQualifiedName + "\n\r" + jsonData);
     } finally
     {
         source.EventSourceId.ReleaseWriteLock("snapshot");
     }
 }
Exemplo n.º 4
0
 public void SaveShapshot(Snapshot source)
 {
     using (var session = _documentStore.OpenSession())
     {
         session.Store(new StoredSnaphot
                           {
                               Id = source.EventSourceId.ToString(),
                               Data = source.Payload,
                               EventSourceId = source.EventSourceId,
                               Version = source.Version
                           });
         session.SaveChanges();
     }
 }
        public void Saving_snapshot_should_not_throw_an_exception_when_snapshot_is_valid()
        {
            var targetStore = new RavenDBSnapshotStore(_documentStore);

            var anId = Guid.NewGuid();
            var aVersion = 12;
            var snapshot = new Snapshot(anId, aVersion, new MySnapshot {Value = "Some value"});

            targetStore.SaveSnapshot(snapshot);

            var savedSnapshot = targetStore.GetSnapshot(anId, long.MaxValue);
            savedSnapshot.EventSourceId.Should().Be(anId);
            savedSnapshot.Version.Should().Be(aVersion);
            ((MySnapshot) savedSnapshot.Payload).Value.Should().Be("Some value");
        }
        /// <summary>Saves a snapshot of the specified event source.</summary>
        public void SaveSnapshot(Snapshot snapshot)
        {
            using (var connection = new NpgsqlConnection(_connectionString))
            {
                // Open connection and begin a transaction so we can
                // commit or rollback all the changes that has been made.
                connection.Open();
                using (NpgsqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        using (var dataStream = new MemoryStream())
                        {
                            var formatter = new BinaryFormatter();
                            formatter.Serialize(dataStream, snapshot.Payload);
                            byte[] data = dataStream.ToArray();

                            using (var command = new NpgsqlCommand(Queries.InsertSnapshot, transaction.Connection))
                            {
                                command.Transaction = transaction;
                                command.Parameters.AddWithValue("EventSourceId", snapshot.EventSourceId);
                                command.Parameters.AddWithValue("Version", snapshot.Version);
                                command.Parameters.AddWithValue("Type", snapshot.GetType().AssemblyQualifiedName);
                                command.Parameters.AddWithValue("Data", data);
                                command.ExecuteNonQuery();
                            }
                        }

                        // Everything is handled, commit transaction.
                        transaction.Commit();
                    }
                    catch
                    {
                        // Something went wrong, rollback transaction.
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
Exemplo n.º 7
0
 public void SaveShapshot(Snapshot source)
 {
 }
Exemplo n.º 8
0
 /// <summary>
 /// Saves a snapshot of the specified event source.
 /// </summary>
 public void SaveShapshot(Snapshot snapshot)
 {
     _snapshots[snapshot.EventSourceId] = snapshot;
 }
 public void SetUp()
 {
     Snapshot = new Snapshot(Guid.NewGuid(), 1, new TestSnapshot { Name = "Name" });
     SnapshotStore.SaveSnapshot(Snapshot);
     _retrieved = SnapshotStore.GetSnapshot(Snapshot.EventSourceId, long.MaxValue);
 }
Exemplo n.º 10
0
        public void Saving_snapshot_should_not_throw_an_exception_when_snapshot_is_valid()
        {
            var targetStore = new MsSqlServerEventStore(connectionString);

            var anId = Guid.NewGuid();
            var aVersion = 12;
            var snapshot = new Snapshot(anId, aVersion, new MySnapshot());

            targetStore.SaveShapshot(snapshot);

            var savedSnapshot = targetStore.GetSnapshot(anId, long.MaxValue);
            savedSnapshot.EventSourceId.Should().Be(anId);
            savedSnapshot.Version.Should().Be(aVersion);
        }
        /// <summary>Gets a snapshot of a particular event source, if one exists. Otherwise, returns <c>null</c>.</summary>
        /// <param name="eventSourceId">Indicates the event source to retrieve the snapshot for.</param>
        /// <param name="maxVersion">Indicates the maximum allowed version to be returned.</param>
        /// <returns>Returns the most recent <see cref="Snapshot"/> that exists in the store. If the store has a 
        /// snapshot that is more recent than the <paramref name="maxVersion"/>, then <c>null</c> will be returned.</returns>
        public Snapshot GetSnapshot(Guid eventSourceId, long maxVersion)
        {
            using (var connection = new SqlConnection(_connectionString))
            {
                // Open connection and begin a transaction so we can
                // commit or rollback all the changes that has been made.
                // QUESTION: Was this supposed to be done inside of a transaction?
                connection.Open();

                using (var command = new SqlCommand(Queries.SelectLatestSnapshot, connection))
                {
                    command.Parameters.AddWithValue("@EventSourceId", eventSourceId);

                    using (var reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            var snapshotData = (byte[])reader["Data"];

                            using (var buffer = new MemoryStream(snapshotData))
                            {
                                var formatter = new BinaryFormatter();
                                var payload = formatter.Deserialize(buffer);
                                var theSnapshot = new Snapshot(eventSourceId, (long)reader["Version"], payload);

                                // QUESTION: Does it make sense to have this check performed in the SQL Query that way
                                // an older snapshot could be returned if it does exist?
                                return theSnapshot.Version > maxVersion ? null : theSnapshot;
                            }
                        }

                        return null;
                    }
                }
            }
        }
Exemplo n.º 12
0
 public when_getting_the_snapshot()
 {
     Snapshot = new Snapshot(Guid.NewGuid(), 1, new TestSnapshot { Name = "Name" });
     SnapshotStore.SaveSnapshot(Snapshot);
     _retrieved = SnapshotStore.GetSnapshot(Snapshot.EventSourceId, long.MaxValue);
 }
Exemplo n.º 13
0
 public void SaveShapshot(Snapshot source)
 {
     Contract.Requires<ArgumentNullException>(source != null, "The source cannot be null.");
 }
Exemplo n.º 14
0
        public virtual void InitializeFromSnapshot(Snapshot snapshot)
        {
            Contract.Requires<ArgumentNullException>(snapshot != null, "The snapshot cannot be null.");
            Log.DebugFormat("Initializing event source {0} from snapshot (version {1}).", snapshot.EventSourceId, snapshot.Version);

            _eventSourceId = snapshot.EventSourceId;
            _initialVersion = _currentVersion = snapshot.Version;
        }
        /// <summary>
        /// Gets a snapshot of a particular event source, if one exists. Otherwise, returns <c>null</c>.
        /// </summary>
        public Snapshot GetSnapshot(Guid eventSourceId, long maxVersion)
        {
            using (var connection = new NpgsqlConnection(_connectionString))
            {
                // Open connection and begin a transaction so we can
                // commit or rollback all the changes that has been made.
                connection.Open();

                using (var command = new NpgsqlCommand(Queries.SelectLatestSnapshot, connection))
                {
                    command.Parameters.AddWithValue("@EventSourceId", eventSourceId);

                    using (var reader = command.ExecuteReader())
                    {
                        if (reader.FieldCount > 0)
                        {
                            var snapshotData = (byte[])reader["Data"];
                            using (var buffer = new MemoryStream(snapshotData))
                            {
                                var formatter = new BinaryFormatter();
                                object payload = formatter.Deserialize(buffer);
                                var theSnapshot = new Snapshot(eventSourceId, (long)reader["Version"], payload);
                                return theSnapshot.Version > maxVersion
                                           ? null
                                           : theSnapshot;
                            }
                        }
                        return null;
                    }
                }
            }
        }