Пример #1
0
        public IEnumerable <StreamRecord> EnumerateMessages(string key, long version, int count)
        {
            var records = _appendOnlyStore.ReadRecords(key, 0, int.MaxValue);

            foreach (var record in records)
            {
                using (var mem = new MemoryStream(record.Data))
                    using (var bin = new BinaryReader(mem))
                    {
                        var msgCount = bin.ReadInt32();
                        var objects  = new object[msgCount];
                        for (int i = 0; i < msgCount; i++)
                        {
                            var name = bin.ReadString();
                            try
                            {
                                var type = _contractToType[name];
                                var len  = bin.ReadInt32();
                                objects[i] = RuntimeTypeModel.Default.Deserialize(bin.BaseStream, null, type, len);
                            }
                            catch (KeyNotFoundException ex)
                            {
                                throw new KeyNotFoundException("Failed to find contract for " + name, ex);
                            }
                        }
                        yield return(new StreamRecord(record.StreamVersion, key, objects));
                    }
            }
        }
Пример #2
0
        public EventStream LoadEventStream(IIdentity id, long skip, int take)
        {
            var name    = IdentityToString(id);
            var records = _appendOnlyStore.ReadRecords(name, skip, take).ToList();
            var stream  = new EventStream();

            foreach (var tapeRecord in records)
            {
                stream.Events.AddRange(DeserializeEvent(tapeRecord.Data));
                stream.Version = tapeRecord.StreamVersion;
            }
            return(stream);
        }
Пример #3
0
        public EventStream LoadEventStream(Guid id, int skipEvents, int maxCount)
        {
            var records = _appendOnlyStore.ReadRecords(id, skipEvents, maxCount).ToList();

            var stream = new EventStream();

            foreach (var tapeRecord in records)
            {
                stream.Events.AddRange(DeserializeEvent(tapeRecord.Data));
                stream.Version = tapeRecord.Version;
            }

            return(stream);
        }
Пример #4
0
        /// <summary>
        /// Copy the content of one appender to another. The bytestreams will be identical.
        /// Use AddEvents(appender, events) to compress the bytestreams.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="to"></param>
        /// <param name="version"></param>
        public static void CopyAppender(this IAppendOnlyStore source, IAppendOnlyStore to, int version)
        {
            var serializer = new BinaryFormatterSerializer();

            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (to == null)
            {
                throw new ArgumentNullException("to");
            }

            var versions = new Dictionary <string, int>();

            foreach (var record in source.ReadRecords(0, version))
            {
                if (!versions.ContainsKey(record.Name))
                {
                    versions[record.Name] = 0;
                }

                var events = serializer.DeserializeEvent(record.Data);
                to.Append(record.Name, record.Data, versions[record.Name]);
                versions[record.Name]++;
            }
        }
Пример #5
0
        public List <StoredEvent> GetAllStoredForProcessId(Guid processId, int take = 1)
        {
            var records = _appendOnlyStore
                          .ReadRecords(processId, take)
                          .ToList();

            return(records);
        }
Пример #6
0
        public IEnumerable <Event> LoadAllEvents()
        {
            var records = appendOnlyStore.ReadRecords(0, long.MaxValue);

            var events = new List <Event>();

            foreach (var tapeRecord in records)
            {
                var @event = DesserializeEvent(tapeRecord.Data);

                @event.Version = tapeRecord.Version;

                @event.Date = tapeRecord.Date;

                events.Add(@event);
            }

            return(events);
        }
        public IEnumerable <StoreRecord> EnumerateMessages(string key, long afterVersion, int count)
        {
            var records = _appendOnlyStore.ReadRecords(key, afterVersion, count);

            foreach (var record in records)
            {
                using (var mem = new MemoryStream(record.Data))
                {
                    // drop attributes
                    var attribs    = _serializer.ReadAttributes(mem);
                    var eventCount = _serializer.ReadCompactInt(mem);
                    var objects    = new object[eventCount];
                    for (int i = 0; i < eventCount; i++)
                    {
                        objects[i] = _serializer.ReadMessage(mem);
                    }
                    yield return(new StoreRecord(key, objects, record.StoreVersion, record.StreamVersion, attribs));
                }
            }
        }
Пример #8
0
        public EventStream LoadEventStream(string streamName)
        {
            var records = _appendOnlyStore.ReadRecords(streamName);
            var stream  = new EventStream();

            foreach (var record in records)
            {
                stream.Events.AddRange(JsonConvert.DeserializeObject <ICollection <IEvent> >(record.JsonData, new JsonSerializerSettings {
                    TypeNameHandling = TypeNameHandling.Auto
                }));
                stream.Version = record.Version;
            }
            return(stream);
        }
Пример #9
0
        public void Run(CancellationToken token)
        {
            var connectFailure = 0;

            while (!token.IsCancellationRequested)
            {
                try {
                    var localStoreIsEmpty = _store.GetCurrentVersion() == 0;
                    if (localStoreIsEmpty)
                    {
                        token.WaitHandle.WaitOne(TimeSpan.FromSeconds(30));
                        continue;
                    }

                    _log.Information("Starting ES replication to {stream}", _streamName);

                    using (var conn = new CloudClient(_config.Host, _config.Login, _config.Password)) {
                        connectFailure = 0;

                        var lastReplicatedEvent = _checkpoint.GetOrInitPosition();

                        while (!token.IsCancellationRequested)
                        {
                            if (lastReplicatedEvent == _store.GetCurrentVersion())
                            {
                                // no work to do, so sleep and continue
                                token.WaitHandle.WaitOne(500);
                                continue;
                            }

                            var keys         = _store.ReadRecords(lastReplicatedEvent, 1000).ToList();
                            var remoteEvents = keys.Select(MessageToWrite).ToList();
                            conn.PostMessagesAsync(_streamName, remoteEvents).Wait(token);

                            lastReplicatedEvent = keys.Last().StoreVersion;
                            _checkpoint.Update(lastReplicatedEvent);
                        }
                    }
                }
                catch (Exception ex) {
                    if (connectFailure == 0)
                    {
                        _log.Error(ex, "Write connection failure");
                    }
                    connectFailure += 1;
                    token.WaitHandle.WaitOne(TimeSpan.FromMinutes(1));
                }
            }
        }
        public static async Task CopyAppender(this IAppendOnlyAsync to, IAppendOnlyStore @from)
        {
            var records = @from.ReadRecords(0, int.MaxValue);

            var versions = new Dictionary <string, int>();

            foreach (var record in records)
            {
                if (!versions.ContainsKey(record.Name))
                {
                    versions[record.Name] = 0;
                }

                await to.Append(record.Name, record.Data, versions[record.Name]);

                versions[record.Name]++;
            }
        }