public void Append(CreateStreamEvent createStreamEvent)
 {
     _writeMutex.WaitOne(TimeSpan.FromSeconds(1));
     try
     {
         var @event = WriteEventToStream(createStreamEvent);
         RunProjections(@event);
     }
     finally
     {
         _writeMutex.ReleaseMutex();
     }
 }
        private StreamEvent WriteEventToStream(CreateStreamEvent createStreamEvent)
        {
            var streamPosition = GetStreamPosition(createStreamEvent);

            if (streamPosition == StreamPositions.DoesNotExist)
            {
                _streams.Add(createStreamEvent.StreamName, new List <StreamEvent>());
                streamPosition = 0;
            }

            if (createStreamEvent.StreamPosition != StreamPositions.Any)
            {
                CheckStreamPosition(createStreamEvent, streamPosition);
            }

            var @event = new StreamEvent(createStreamEvent, streamPosition, _allEvents.Count);

            _streams[createStreamEvent.StreamName].Add(@event);
            _allEvents.Add(@event);

            ExecuteSubscriptions(@event);
            return(@event);
        }
        private static void CheckStreamPosition(CreateStreamEvent streamEvent, long currentPosition)
        {
            switch (streamEvent.StreamPosition)
            {
            case StreamPositions.CreateNewStream:
            {
                if (currentPosition != 0)
                {
                    throw new IEventStore.InvalidStreamPosition(
                              streamEvent.StreamName,
                              streamEvent.StreamPosition,
                              currentPosition);
                }

                break;
            }

            case StreamPositions.Any:
            {
                break;
            }

            default:
            {
                if (streamEvent.StreamPosition != currentPosition)
                {
                    throw new IEventStore.InvalidStreamPosition(
                              streamEvent.StreamName,
                              streamEvent.StreamPosition,
                              currentPosition);
                }

                break;
            }
            }
        }