public bool ProcessEvent(
            string partition, CheckpointTag eventPosition, string category1, ResolvedEvent data,
            out string newState, out EmittedEventEnvelope[] emittedEvents)
        {
            emittedEvents = null;
            newState = null;
            if (data.PositionStreamId.StartsWith("$"))
                return false;
            var lastSlashPos = data.PositionStreamId.LastIndexOf(_separator);
            if (lastSlashPos < 0)
                return true; // handled but not interesting to us

            var category = data.PositionStreamId.Substring(0, lastSlashPos);

            string linkTarget;
            if (data.EventType == SystemEventTypes.LinkTo) 
                linkTarget = data.Data;
            else 
                linkTarget = data.EventSequenceNumber + "@" + data.EventStreamId;

            emittedEvents = new[]
            {
                new EmittedEventEnvelope(
                    new EmittedLinkToWithRecategorization(
                        _categoryStreamPrefix + category, Guid.NewGuid(), linkTarget, eventPosition, expectedTag: null,
                        originalStreamId: data.PositionStreamId))
            };

            return true;
        }
        public bool ProcessEvent(
            string partition, CheckpointTag eventPosition, string category1, ResolvedEvent data,
            out string newState, out EmittedEventEnvelope[] emittedEvents)
        {
            emittedEvents = null;
            newState = null;
            if (data.PositionSequenceNumber != 0)
                return false; // not our event
            if (data.EventStreamId.StartsWith("$"))
                return false;
            var lastSlashPos = data.EventStreamId.LastIndexOf(_separator);
            if (lastSlashPos < 0)
                return true; // handled but not interesting to us

            var category = data.EventStreamId.Substring(0, lastSlashPos);

            emittedEvents = new[]
            {
                new EmittedEventEnvelope(
                    new EmittedDataEvent(
                        "$category" + _separator + category, Guid.NewGuid(), SystemEventTypes.StreamReference, false,
                        data.EventStreamId, null, eventPosition, expectedTag: null))
            };

            return true;
        }
        public bool ProcessEvent(
            string partition, CheckpointTag eventPosition, string category1, ResolvedEvent data,
            out string newState, out string newSharedState, out EmittedEventEnvelope[] emittedEvents)
        {
            newSharedState = null;
            emittedEvents = null;
            newState = null;
            var category = _streamCategoryExtractor.GetCategoryByStreamId(data.PositionStreamId);
            if (category == null)
                return true; // handled but not interesting

            string linkTarget;
            if (data.EventType == SystemEventTypes.LinkTo) 
                linkTarget = data.Data;
            else 
                linkTarget = data.EventSequenceNumber + "@" + data.EventStreamId;

            emittedEvents = new[]
            {
                new EmittedEventEnvelope(
                    new EmittedLinkToWithRecategorization(
                        _categoryStreamPrefix + category, Guid.NewGuid(), linkTarget, eventPosition, expectedTag: null,
                        originalStreamId: data.PositionStreamId))
            };

            return true;
        }
 private EmittedEventEnvelope[] CreateResultUpdatedEvents(string partition, string projectionResult, CheckpointTag at)
 {
     var streamId = _namesBuilder.MakePartitionResultStreamName(partition);
     var allResultsStreamId = _namesBuilder.GetResultStreamName();
     if (string.IsNullOrEmpty(partition))
     {
         var result =
             new EmittedEventEnvelope(
                 projectionResult == null
                     ? new EmittedDataEvent(
                         streamId, Guid.NewGuid(), "ResultRemoved", true, null, null, at, null)
                     : new EmittedDataEvent(
                         streamId, Guid.NewGuid(), "Result", true, projectionResult, null, at, null),
                 _resultStreamMetadata);
         
         return new[] {result};
     }
     else
     {
         var linkTo = new EmittedLinkTo(allResultsStreamId, Guid.NewGuid(), streamId, at, null);
         var linkToEnvelope = new EmittedEventEnvelope(linkTo, _resultStreamMetadata);
         var result =
             new EmittedEventEnvelope(
                 projectionResult == null
                     ? new EmittedDataEvent(
                         streamId, Guid.NewGuid(), "ResultRemoved", true, null, null, at, null,
                         linkTo.SetTargetEventNumber)
                     : new EmittedDataEvent(
                         streamId, Guid.NewGuid(), "Result", true, projectionResult, null, at, null,
                         linkTo.SetTargetEventNumber), _resultStreamMetadata);
         return new[] {result, linkToEnvelope};
     }
 }
        public bool ProcessEvent(
            string partition, CheckpointTag eventPosition, string category, ResolvedEvent data, out string newState,
            out string newSharedState, out EmittedEventEnvelope[] emittedEvents)
        {
            newSharedState = null;
            if (!data.EventStreamId.StartsWith(UserStreamPrefix))
                throw new InvalidOperationException(
                    string.Format(
                        "Invalid stream name: '{0}' The IndexUsersProjectionHandler cannot handle events from other streams than named after the '$user-' pattern",
                        data.EventStreamId));

            var loginName = data.EventStreamId.Substring(UserStreamPrefix.Length);

            var userData = data.Data.ParseJson<UserData>();
            if (userData.LoginName != loginName)
                throw new InvalidOperationException(
                    string.Format(
                        "Invalid $UserCreated event found.  '{0}' login name expected, but '{1}' found", loginName,
                        userData.LoginName));

            emittedEvents = new[]
            {
                new EmittedEventEnvelope(
                    new EmittedDataEvent(
                        UsersStream, Guid.NewGuid(), UserEventType, false, loginName, null, eventPosition, null))
            };
            newState = "";
            return true;
        }
Exemple #6
0
        private EmittedEventEnvelope[] CreateResultUpdatedEvents(string partition, string projectionResult, CheckpointTag at)
        {
            var streamId           = _namesBuilder.MakePartitionResultStreamName(partition);
            var allResultsStreamId = _namesBuilder.GetResultStreamName();

            if (string.IsNullOrEmpty(partition))
            {
                var result =
                    new EmittedEventEnvelope(
                        projectionResult == null
                            ? new EmittedDataEvent(
                            streamId, Guid.NewGuid(), "ResultRemoved", true, null, null, at, null)
                            : new EmittedDataEvent(
                            streamId, Guid.NewGuid(), "Result", true, projectionResult, null, at, null),
                        _resultStreamMetadata);

                return(new[] { result });
            }
            else
            {
                var linkTo         = new EmittedLinkTo(allResultsStreamId, Guid.NewGuid(), streamId, at, null);
                var linkToEnvelope = new EmittedEventEnvelope(linkTo, _resultStreamMetadata);
                var result         =
                    new EmittedEventEnvelope(
                        projectionResult == null
                            ? new EmittedDataEvent(
                            streamId, Guid.NewGuid(), "ResultRemoved", true, null, null, at, null,
                            linkTo.SetTargetEventNumber)
                            : new EmittedDataEvent(
                            streamId, Guid.NewGuid(), "Result", true, projectionResult, null, at, null,
                            linkTo.SetTargetEventNumber), _resultStreamMetadata);
                return(new[] { result, linkToEnvelope });
            }
        }
        public bool ProcessEvent(
            string partition, CheckpointTag eventPosition, string category1, ResolvedEvent data,
            out string newState, out string newSharedState, out EmittedEventEnvelope[] emittedEvents)
        {
            newSharedState = null;
            emittedEvents = null;
            newState = null;
            if (data.EventStreamId != data.PositionStreamId)
                return false;
            var indexedEventType = data.EventType;
            if (indexedEventType == "$>")
                return false;

            string positionStreamId;
            var isStreamDeletedEvent = StreamDeletedHelper.IsStreamDeletedEvent(
                data.PositionStreamId, data.EventType, data.Data, out positionStreamId);
            if (isStreamDeletedEvent)
                indexedEventType = "$deleted";

            emittedEvents = new[]
            {
                new EmittedEventEnvelope(
                    new EmittedDataEvent(
                        _indexStreamPrefix + indexedEventType, Guid.NewGuid(), "$>", false,
                        data.EventSequenceNumber + "@" + positionStreamId,
                        isStreamDeletedEvent
                            ? new ExtraMetaData(new Dictionary<string, JRaw> {{"$deleted", new JRaw(-1)}})
                            : null, eventPosition, expectedTag: null))
            };

            return true;
        }
        public bool ProcessEvent(
            string partition, CheckpointTag eventPosition, string category1, ResolvedEvent data,
            out string newState, out string newSharedState, out EmittedEventEnvelope[] emittedEvents)
        {
            newSharedState = null;
            emittedEvents = null;
            newState = null;

            if (data.PositionSequenceNumber != 0)
                return false; // not our event

            var category = _streamCategoryExtractor.GetCategoryByStreamId(data.PositionStreamId);
            if (category == null)
                return true; // handled but not interesting

            emittedEvents = new[]
            {
                new EmittedEventEnvelope(
                    new EmittedDataEvent(
                        "$category" + "-" + category, Guid.NewGuid(), SystemEventTypes.StreamReference, false,
                        data.PositionStreamId, null, eventPosition, expectedTag: null))
            };

            return true;
        }
 private void UpdateLastPosition(EmittedEventEnvelope[] events)
 {
     foreach (var emittedEvent in events)
     {
         if (emittedEvent.Event.CausedByTag > _last)
             _last = emittedEvent.Event.CausedByTag;
     }
 }
 public bool ProcessEvent(
     string partition, CheckpointTag eventPosition, string category1, ResolvedEvent data,
     out string newState, out EmittedEventEnvelope[] emittedEvents)
 {
     emittedEvents = null;
     newState = null;
     return true;
 }
 public bool ProcessEvent(
     string partition, CheckpointTag eventPosition, string category1, ResolvedEvent data,
     out string newState, out EmittedEventEnvelope[] emittedEvents)
 {
     if (data.EventType == "fail" || _query == "fail")
         throw new Exception("failed");
     _logger("ProcessEvent(" + "..." + ")");
     newState = "{\"data\": " + _state + data + "}";
     emittedEvents = null;
     return true;
 }
        public void ValidateOrderAndEmitEvents(EmittedEventEnvelope[] events)
        {
            UpdateLastPosition(events);
            EnsureCheckpointNotRequested();

            var groupedEvents = events.GroupBy(v => v.Event.StreamId);
            foreach (var eventGroup in groupedEvents)
            {
                EmitEventsToStream(eventGroup.Key, eventGroup.ToArray());
            }
        }
 public static bool ProcessEvent(
     this IProjectionStateHandler self, string partition, CheckpointTag eventPosition, string streamId,
     string eventType, string category, Guid eventId, int eventSequenceNumber, string metadata, string data,
     out string state, out EmittedEventEnvelope[] emittedEvents, bool isJson = true)
 {
     return self.ProcessEvent(
         partition, eventPosition, category,
         new ResolvedEvent(
             streamId, eventSequenceNumber, streamId, eventSequenceNumber, false, new TFPos(0, -1),
             eventId, eventType, isJson, data, metadata), out state, out emittedEvents);
 }
 public EventProcessedResult(
     string partition, CheckpointTag checkpointTag, PartitionState oldState, PartitionState newState,
     EmittedEventEnvelope[] emittedEvents, Guid causedBy, string correlationId)
 {
     if (partition == null) throw new ArgumentNullException("partition");
     if (checkpointTag == null) throw new ArgumentNullException("checkpointTag");
     _emittedEvents = emittedEvents;
     _causedBy = causedBy;
     _correlationId = correlationId;
     _oldState = oldState;
     _newState = newState;
     _partition = partition;
     _checkpointTag = checkpointTag;
 }
        public bool ProcessEvent(
            string partition, CheckpointTag eventPosition, string category1, ResolvedEvent data,
            out string newState, out EmittedEventEnvelope[] emittedEvents)
        {
            emittedEvents = null;
            newState = null;
            if (data.EventSequenceNumber != 0 || data.ResolvedLinkTo)
                return false; // not our event

            emittedEvents = new[]
            {
                new EmittedEventEnvelope(
                    new EmittedDataEvent(
                        SystemStreams.StreamsStream, Guid.NewGuid(), SystemEventTypes.LinkTo, false,
                        data.EventSequenceNumber + "@" + data.EventStreamId, null, eventPosition, expectedTag: null))
            };

            return true;
        }
        public bool ProcessEvent(
            string partition, CheckpointTag eventPosition, string category1, ResolvedEvent data,
            out string newState, out EmittedEventEnvelope[] emittedEvents)
        {
            emittedEvents = null;
            newState = null;
            if (data.EventStreamId != data.PositionStreamId)
                return false;
            if (data.EventType == "$>")
                return false;

            emittedEvents = new[]
            {
                new EmittedEventEnvelope(
                    new EmittedDataEvent(
                        _indexStreamPrefix + data.EventType, Guid.NewGuid(), "$>", false,
                        data.EventSequenceNumber + "@" + data.EventStreamId, null, eventPosition, expectedTag: null))
            };

            return true;
        }
 public void EventsEmitted(
     EmittedEventEnvelope[] scheduledWrites, Guid causedBy, string correlationId)
 {
     _coreProjectionCheckpointManager.EventsEmitted(
         scheduledWrites, causedBy, correlationId);
 }
 public bool ProcessPartitionCreated(string partition, CheckpointTag createPosition, ResolvedEvent data, out EmittedEventEnvelope[] emittedEvents)
 {
     emittedEvents = null;
     return false;
 }
 public void ValidateOrderAndEmitEvents(EmittedEventEnvelope[] events)
 {
     Writes.Add(events.Select(v => v.Event).ToArray());
 }
 public void EventsEmitted(EmittedEventEnvelope[] scheduledWrites, Guid causedBy, string correlationId)
 {
     if (_stopped)
         return;
     EnsureStarted();
     if (_stopping)
         throw new InvalidOperationException("Stopping");
     if (scheduledWrites != null)
     {
         foreach (var @event in scheduledWrites)
         {
             var emittedEvent = @event.Event;
             emittedEvent.SetCausedBy(causedBy);
             emittedEvent.SetCorrelationId(correlationId);
         }
         _currentCheckpoint.ValidateOrderAndEmitEvents(scheduledWrites);
     }
 }
 public void EventsEmitted(
     EmittedEventEnvelope[] scheduledWrites, Guid causedBy, string correlationId)
 {
     throw new NotSupportedException();
 }
 public void ProcessNewCheckpoint(CheckpointTag checkpointPosition, out EmittedEventEnvelope[] emittedEvents)
 {
     emittedEvents = new[]
     {
         new EmittedEventEnvelope(
             new EmittedDataEvent(
                 _indexCheckpointStream, Guid.NewGuid(), ProjectionNamesBuilder.EventType_PartitionCheckpoint,
                 true, checkpointPosition.ToJsonString(), null, checkpointPosition, expectedTag: null))
     };
 }
        private void EmitEventsToStream(string streamId, EmittedEventEnvelope[] emittedEvents)
        {
            if (streamId == null)
                throw new ArgumentNullException("streamId");
            EmittedStream stream;
            if (!_emittedStreams.TryGetValue(streamId, out stream))
            {
                var streamMetadata = emittedEvents.Length > 0 ? emittedEvents[0].StreamMetadata : null;

                var writerConfiguration = new EmittedStream.WriterConfiguration(
                    streamMetadata, _runAs, maxWriteBatchLength: _maxWriteBatchLength, logger: _logger);

                stream = new EmittedStream(
                    streamId, writerConfiguration, _projectionVersion, _positionTagger, _from, _ioDispatcher, this);

                if (_started)
                    stream.Start();
                _emittedStreams.Add(streamId, stream);
            }
            stream.EmitEvents(emittedEvents.Select(v => v.Event).ToArray());
        }
 public void ValidateOrderAndEmitEvents(EmittedEventEnvelope[] events)
 {
     Assert.Fail("Should not write any events");
 }
        public bool ProcessEvent(
            string partition, CheckpointTag eventPosition, string category1, ResolvedEvent data,
            out string newState, out string newSharedState, out EmittedEventEnvelope[] emittedEvents)
        {
            newSharedState = null;
            if (_failOnProcessEvent)
                throw new Exception("PROCESS_EVENT_FAILED");
            _lastProcessedStreamId = data.EventStreamId;
            _lastProcessedEventType = data.EventType;
            _lastProcessedEventId = data.EventId;
            _lastProcessedSequencenumber = data.EventSequenceNumber;
            _lastProcessedMetadata = data.Metadata;
            _lastProcessedData = data.Data;
            _lastPartition = partition;

            _eventsProcessed++;
            switch (data.EventType)
            {
                case "skip_this_type":
                    newState = null;
                    emittedEvents = null;
                    return false;
                case "handle_this_type":
                    _loadedState = newState = data.Data;
                    emittedEvents = null;
                    return true;
                case "append":
                    _loadedState = newState = _loadedState + data.Data;
                    emittedEvents = null;
                    return true;
                case "no_state_emit1_type":
                    _loadedState = newState = "";
                    emittedEvents = new[]
                    {
                        new EmittedEventEnvelope(
                            new EmittedDataEvent(
                                _emit1StreamId, Guid.NewGuid(), _emit1EventType, true, _emit1Data, null, eventPosition, null)),
                    };
                    return true;
                case "emit1_type":
                    _loadedState = newState = data.Data;
                    emittedEvents = new[]
                    {
                        new EmittedEventEnvelope(
                            new EmittedDataEvent(
                                _emit1StreamId, Guid.NewGuid(), _emit1EventType, true, _emit1Data, null, eventPosition, null)),
                    };
                    return true;
                case "emit22_type":
                    _loadedState = newState = data.Data;
                    emittedEvents = new[]
                    {
                        new EmittedEventEnvelope(
                            new EmittedDataEvent(
                                _emit2StreamId, Guid.NewGuid(), _emit2EventType, true, _emit1Data, null, eventPosition, null)),
                        new EmittedEventEnvelope(
                            new EmittedDataEvent(
                                _emit2StreamId, Guid.NewGuid(), _emit2EventType, true, _emit2Data, null, eventPosition, null)),
                    };
                    return true;
                case "emit212_type":
                    _loadedState = newState = data.Data;
                    emittedEvents = new[]
                    {
                        new EmittedEventEnvelope(
                            new EmittedDataEvent(
                                _emit2StreamId, Guid.NewGuid(), _emit2EventType, true, _emit1Data, null, eventPosition, null)),
                        new EmittedEventEnvelope(
                            new EmittedDataEvent(
                                _emit1StreamId, Guid.NewGuid(), _emit1EventType, true, _emit2Data, null, eventPosition, null)),
                        new EmittedEventEnvelope(
                            new EmittedDataEvent(
                                _emit2StreamId, Guid.NewGuid(), _emit2EventType, true, _emit3Data, null, eventPosition, null)),
                    };
                    return true;
                case "emit12_type":
                    _loadedState = newState = data.Data;
                    emittedEvents = new[]
                    {
                        new EmittedEventEnvelope(
                            new EmittedDataEvent(
                                _emit1StreamId, Guid.NewGuid(), _emit1EventType, true, _emit1Data, null, eventPosition, null)),
                        new EmittedEventEnvelope(
                            new EmittedDataEvent(
                                _emit2StreamId, Guid.NewGuid(), _emit2EventType, true, _emit2Data, null, eventPosition, null)),
                    };
                    return true;
                case "just_emit":
                    newState = _loadedState;
                    emittedEvents = new[]
                    {
                        new EmittedEventEnvelope(
                            new EmittedDataEvent(
                                _emit1StreamId, Guid.NewGuid(), _emit1EventType, true, _emit1Data, null, eventPosition, null)),
                    };
                    return true;
                default:
                    throw new NotSupportedException();
            }
        }
 public void EventsEmitted(
     EmittedEventEnvelope[] scheduledWrites, Guid causedBy, string correlationId)
 {
     EmittedEvents.AddRange(scheduledWrites);
 }
 public bool ProcessPartitionCreated(string partition, CheckpointTag createPosition, ResolvedEvent data, out EmittedEventEnvelope[] emittedEvents)
 {
     throw new NotImplementedException();
 }
        public bool ProcessEvent(
            string partition, CheckpointTag eventPosition, string category, ResolvedEvent data, out string newState,
            out string newSharedState, out EmittedEventEnvelope[] emittedEvents)
        {
            CheckDisposed();
            if (data == null)
                throw new ArgumentNullException("data");
            _eventPosition = eventPosition;
            _emittedEvents = null;
            var newStates = _query.Push(
                data.Data.Trim(), // trimming data passed to a JS 
                new[]
                {
                    data.IsJson ? "1" : "", data.EventStreamId, data.EventType, category ?? "",
                    data.EventSequenceNumber.ToString(CultureInfo.InvariantCulture), data.Metadata ?? "",
                    data.PositionMetadata ?? "", partition, ""
                });
            newState = newStates.Item1;
            newSharedState = newStates.Item2;
/*            try
            {
                if (!string.IsNullOrEmpty(newState))
                {
                    var jo = newState.ParseJson<JObject>();
                }

            }
            catch (InvalidCastException)
            {
                Console.Error.WriteLine(newState);
            }
            catch (JsonException)
            {
                Console.Error.WriteLine(newState);
            }*/
            emittedEvents = _emittedEvents == null ? null : _emittedEvents.ToArray();
            return true;
        }
 public bool ProcessPartitionCreated(string partition, CheckpointTag createPosition, ResolvedEvent data, out EmittedEventEnvelope[] emittedEvents)
 {
     CheckDisposed();
     _eventPosition = createPosition;
     _emittedEvents = null;
     var newStates = _query.NotifyCreated(
         data.Data.Trim(), // trimming data passed to a JS 
         new[]
         {
             data.IsJson ? "1" : "", data.EventStreamId, data.EventType, "",
             data.EventSequenceNumber.ToString(CultureInfo.InvariantCulture), data.Metadata ?? "",
             data.PositionMetadata ?? "", partition, ""
         });
     emittedEvents = _emittedEvents == null ? null : _emittedEvents.ToArray();
     return true;
 }