Beispiel #1
0
        /// <summary>
        /// Create a new StreamAction for appending to an existing stream
        /// </summary>
        /// <param name="streamId"></param>
        /// <param name="events"></param>
        /// <returns></returns>
        public static StreamAction Append(EventGraph graph, Guid streamId, params object[] events)
        {
            var stream = new StreamAction(streamId, StreamActionType.Append);

            stream.AddEvents(events.Select(graph.BuildEvent).ToArray());
            return(stream);
        }
Beispiel #2
0
 public FetchLivePlan(EventGraph events, IEventIdentityStrategy <TId> identityStrategy, IDocumentStorage <TDoc, TId> documentStorage)
 {
     _events           = events;
     _identityStrategy = identityStrategy;
     _documentStorage  = documentStorage;
     _aggregator       = _events.Options.Projections.AggregatorFor <TDoc>();
 }
Beispiel #3
0
        /// <summary>
        /// Create a new StreamAction for appending to an existing stream
        /// </summary>
        /// <param name="streamKey"></param>
        /// <param name="events"></param>
        /// <returns></returns>
        public static StreamAction Append(EventGraph graph, string streamKey, params object[] events)
        {
            var stream = new StreamAction(streamKey, StreamActionType.Append);

            stream._events.AddRange(events.Select(graph.BuildEvent));
            return(stream);
        }
Beispiel #4
0
        public EventsTable(EventGraph events) : base(new DbObjectName(events.DatabaseSchemaName, "mt_events"))
        {
            var stringIdType = events.GetStreamIdDBType();

            AddPrimaryKey(new TableColumn("seq_id", "bigint"));
            AddColumn("id", "uuid", "NOT NULL");
            AddColumn("stream_id", stringIdType, (events.TenancyStyle != TenancyStyle.Conjoined) ? $"REFERENCES {events.DatabaseSchemaName}.mt_streams ON DELETE CASCADE" : null);
            AddColumn("version", "integer", "NOT NULL");
            AddColumn("data", "jsonb", "NOT NULL");
            AddColumn("type", "varchar(100)", "NOT NULL");
            AddColumn("timestamp", "timestamptz", "default (now()) NOT NULL");
            AddColumn <TenantIdColumn>();
            AddColumn(new DotNetTypeColumn {
                Directive = "NULL"
            });

            if (events.TenancyStyle == TenancyStyle.Conjoined)
            {
                Constraints.Add($"FOREIGN KEY(stream_id, {TenantIdColumn.Name}) REFERENCES {events.DatabaseSchemaName}.mt_streams(id, {TenantIdColumn.Name})");
                Constraints.Add($"CONSTRAINT pk_mt_events_stream_and_version UNIQUE(stream_id, {TenantIdColumn.Name}, version)");
            }
            else
            {
                Constraints.Add("CONSTRAINT pk_mt_events_stream_and_version UNIQUE(stream_id, version)");
            }

            Constraints.Add("CONSTRAINT pk_mt_events_id_unique UNIQUE(id)");
        }
Beispiel #5
0
        public StreamsTable(EventGraph events) : base(new DbObjectName(events.DatabaseSchemaName, "mt_streams"))
        {
            var idColumn = events.StreamIdentity == StreamIdentity.AsGuid
                ? new TableColumn("id", "uuid")
                : new TableColumn("id", "varchar");

            if (events.TenancyStyle == TenancyStyle.Conjoined)
            {
                AddPrimaryKeys(new List <TableColumn>
                {
                    idColumn,
                    new TenantIdColumn()
                });
            }
            else
            {
                AddPrimaryKey(idColumn);
            }

            AddColumn("type", "varchar", "NULL");
            AddColumn("version", "integer", "NOT NULL");
            AddColumn("timestamp", "timestamptz", "default (now()) NOT NULL");
            AddColumn("snapshot", "jsonb");
            AddColumn("snapshot_version", "integer");
            AddColumn("created", "timestamptz", "default (now()) NOT NULL").CanAdd = true;

            if (events.TenancyStyle != TenancyStyle.Conjoined)
            {
                AddColumn <TenantIdColumn>();
            }
        }
Beispiel #6
0
 /// <summary>
 /// Create a new StreamAction for starting a new stream
 /// </summary>
 /// <param name="streamKey"></param>
 /// <param name="events"></param>
 /// <returns></returns>
 /// <exception cref="EmptyEventStreamException"></exception>
 public static StreamAction Start(EventGraph graph, string streamKey, params object[] events)
 {
     if (!events.Any())
     {
         throw new EmptyEventStreamException(streamKey);
     }
     return(new StreamAction(streamKey, StreamActionType.Start).AddEvents(events.Select(graph.BuildEvent).ToArray()));
 }
Beispiel #7
0
 public StreamStateByIdHandler(EventGraph events, T streamKey, string tenantId = null)
 {
     if (events.TenancyStyle == TenancyStyle.Conjoined && tenantId == null)
     {
         throw new ArgumentNullException(nameof(tenantId), $"{nameof(tenantId)} cannot be null for {TenancyStyle.Conjoined}");
     }
     _streamKey = streamKey;
     _events    = events;
     _tenantId  = tenantId;
 }
Beispiel #8
0
        protected EventMapping(EventGraph parent, Type eventType)
        {
            _parent      = parent;
            DocumentType = eventType;

            EventTypeName = eventType.IsGenericType ? eventType.ShortNameInCode() : DocumentType.Name.ToTableAlias();
            IdMember      = DocumentType.GetProperty(nameof(IEvent.Id));

            _inner = new DocumentMapping(eventType, parent.Options);
        }
Beispiel #9
0
        protected EventMapping(EventGraph parent, Type eventType)
        {
            _parent      = parent;
            DocumentType = eventType;

            EventTypeName = Alias = ToEventTypeName(DocumentType);
            IdMember      = DocumentType.GetProperty(nameof(IEvent.Id));

            // TODO -- may need to pull StoreOptions through here.
            _inner = new DocumentMapping(eventType);
        }
Beispiel #10
0
        protected EventMapping(EventGraph parent, Type eventType)
        {
            _options     = parent.Options;
            _parent      = parent;
            DocumentType = eventType;

            EventTypeName = Alias = DocumentType.Name.ToTableAlias();
            IdMember      = DocumentType.GetProperty(nameof(IEvent.Id));

            _inner = new DocumentMapping(eventType, parent.Options);
        }
        public static EventGraph UseAggregatorLookup(this EventGraph eventGraph, AggregationLookupStrategy strategy)
        {
            if (strategy == AggregationLookupStrategy.UsePublicApply)
            {
                eventGraph.UseAggregatorLookup(new AggregatorLookup(type => typeof(Aggregator <>).CloseAndBuildAs <IAggregator>(type)));
            }
            else if (strategy == AggregationLookupStrategy.UsePrivateApply)
            {
                eventGraph.UseAggregatorLookup(new AggregatorLookup(type => typeof(AggregatorApplyPrivate <>).CloseAndBuildAs <IAggregator>(type)));
            }

            return(eventGraph);
        }
Beispiel #12
0
        public StreamsTable(EventGraph events) : base(new DbObjectName(events.DatabaseSchemaName, "mt_streams"))
        {
            var idColumn = events.StreamIdentity == StreamIdentity.AsGuid
                ? new StreamTableColumn("id", x => x.Id)
                : new StreamTableColumn("id", x => x.Key);

            if (events.TenancyStyle == TenancyStyle.Conjoined)
            {
                AddPrimaryKeys(new List <TableColumn>
                {
                    idColumn,
                    new TenantIdColumn()
                });
            }
            else
            {
                AddPrimaryKey(idColumn);
            }

            AddColumn(new StreamTableColumn("type", x => x.AggregateTypeName)
            {
                Directive = "NULL"
            });

            AddColumn(new StreamTableColumn("version", x => x.Version)
            {
                Directive = "NOT NULL"
            });

            AddColumn(new StreamTableColumn("timestamp", x => x.Timestamp)
            {
                Type      = "timestamptz",
                Directive = "default (now()) NOT NULL",
                Writes    = false
            });


            AddColumn("snapshot", "jsonb");
            AddColumn("snapshot_version", "integer");

            AddColumn(new StreamTableColumn("created", x => x.Created)
            {
                Directive = "default (now()) NOT NULL", Writes = false, Type = "timestamptz"
            });

            if (events.TenancyStyle != TenancyStyle.Conjoined)
            {
                AddColumn <TenantIdColumn>();
            }
        }
Beispiel #13
0
        protected EventMapping(EventGraph parent, Type eventType)
        {
            TenancyStyle = parent.TenancyStyle;

            _parent      = parent;
            DocumentType = eventType;

            EventTypeName = eventType.IsGenericType ? eventType.ShortNameInCode() : DocumentType.Name.ToTableAlias();
            IdMember      = DocumentType.GetProperty(nameof(IEvent.Id));

            _inner = new DocumentMapping(eventType, parent.Options);

            DotNetTypeName = $"{eventType.FullName}, {eventType.Assembly.GetName().Name}";
        }
Beispiel #14
0
        public StreamsTable(EventGraph events) : base(new DbObjectName(events.DatabaseSchemaName, "mt_streams"))
        {
            AddPrimaryKey(events.StreamIdentity == StreamIdentity.AsGuid
                ? new TableColumn("id", "uuid")
                : new TableColumn("id", "varchar"));


            AddColumn("type", "varchar", "NULL");
            AddColumn("version", "integer", "NOT NULL");
            AddColumn("timestamp", "timestamptz", "default (now()) NOT NULL");
            AddColumn("snapshot", "jsonb");
            AddColumn("snapshot_version", "integer");
            AddColumn("created", "timestamptz", "default (now()) NOT NULL").CanAdd = true;
            AddColumn <TenantIdColumn>();
        }
Beispiel #15
0
        public void PrepareEvents(int currentVersion, EventGraph graph, Queue <long> sequences, IMartenSession session)
        {
            var timestamp = DateTimeOffset.UtcNow;

            if (AggregateType != null)
            {
                AggregateTypeName = graph.AggregateAliasFor(AggregateType);
            }

            var i = currentVersion;

            if (currentVersion != 0)
            {
                // Guard logic for optimistic concurrency
                if (ExpectedVersionOnServer.HasValue)
                {
                    if (currentVersion != ExpectedVersionOnServer.Value)
                    {
                        throw new EventStreamUnexpectedMaxEventIdException((object)Key ?? Id, AggregateType, ExpectedVersionOnServer.Value, currentVersion);
                    }
                }

                ExpectedVersionOnServer = currentVersion;
            }



            foreach (var @event in _events)
            {
                @event.Version        = ++i;
                @event.DotNetTypeName = graph.DotnetTypeNameFor(@event.EventType);

                var mapping = graph.EventMappingFor(@event.EventType);
                @event.EventTypeName = mapping.EventTypeName;
                if (@event.Id == Guid.Empty)
                {
                    @event.Id = CombGuidIdGeneration.NewGuid();
                }
                @event.Sequence = sequences.Dequeue();
                @event.TenantId ??= session.Tenant.TenantId;
                @event.Timestamp = timestamp;
            }

            Version = Events.Last().Version;
        }
Beispiel #16
0
        public EventDocumentStorage(EventGraph graph, EventQueryMapping mapping, ISerializer serializer)
        {
            _graph   = graph;
            _mapping = mapping;

            FromObject = _mapping.Table.QualifiedName;
            Fields     = mapping;

            if (graph.StreamIdentity == StreamIdentity.AsGuid)
            {
                IdType    = typeof(Guid);
                _selector = new EventSelector(graph, serializer);
            }
            else
            {
                IdType    = typeof(string);
                _selector = new StringIdentifiedEventSelector(graph, serializer);
            }
        }
Beispiel #17
0
        public EventStreamStorage(EventGraph graph)
        {
            _graph = graph;

            if (_graph.JavascriptProjectionsEnabled)
            {
                throw new NotSupportedException("Marten does not yet support Javascript projections");
            }


            if (_graph.AsyncProjectionsEnabled)
            {
                AppendEventFunction = new FunctionName(_graph.DatabaseSchemaName, "mt_append_event_with_buffering");
            }
            else
            {
                AppendEventFunction = new FunctionName(_graph.DatabaseSchemaName, "mt_append_event");
            }
        }
Beispiel #18
0
        public EventsTable(EventGraph events) : base(new DbObjectName(events.DatabaseSchemaName, "mt_events"))
        {
            AddPrimaryKey(new EventTableColumn("seq_id", x => x.Sequence));
            AddColumn(new EventTableColumn("id", x => x.Id)
            {
                Directive = "NOT NULL"
            });
            AddColumn(new StreamIdColumn(events));
            AddColumn(new EventTableColumn("version", x => x.Version)
            {
                Directive = "NOT NULL"
            });
            AddColumn <EventJsonDataColumn>();
            AddColumn <EventTypeColumn>();
            AddColumn(new EventTableColumn("timestamp", x => x.Timestamp)
            {
                Directive = "default (now()) NOT NULL", Type = "timestamptz"
            });

            AddColumn <TenantIdColumn>();
            AddColumn(new DotNetTypeColumn {
                Directive = "NULL"
            });

            if (events.TenancyStyle == TenancyStyle.Conjoined)
            {
                Constraints.Add(
                    $"FOREIGN KEY(stream_id, {TenantIdColumn.Name}) REFERENCES {events.DatabaseSchemaName}.mt_streams(id, {TenantIdColumn.Name})");
                Constraints.Add(
                    $"CONSTRAINT pk_mt_events_stream_and_version UNIQUE(stream_id, {TenantIdColumn.Name}, version)");
            }
            else
            {
                Constraints.Add("CONSTRAINT pk_mt_events_stream_and_version UNIQUE(stream_id, version)");
            }

            Constraints.Add("CONSTRAINT pk_mt_events_id_unique UNIQUE(id)");
        }
Beispiel #19
0
        protected EventMapping(EventGraph parent, Type eventType)
        {
            TenancyStyle = parent.TenancyStyle;

            _parent      = parent;
            DocumentType = eventType;

            EventTypeName = eventType.IsGenericType ? eventType.ShortNameInCode() : DocumentType.Name.ToTableAlias();
            IdMember      = DocumentType.GetProperty(nameof(IEvent.Id));

            _inner = new DocumentMapping(eventType, parent.Options);

            DotNetTypeName = $"{eventType.FullName}, {eventType.Assembly.GetName().Name}";
            ISqlFragment filter = new WhereFragment($"d.type = '{EventTypeName}'");

            filter = filter.CombineAnd(IsNotArchivedFilter.Instance);
            if (parent.TenancyStyle == TenancyStyle.Conjoined)
            {
                filter = filter.CombineAnd(CurrentTenantFilter.Instance);
            }


            _defaultWhereFragment = filter;
        }
 internal StringIdentifiedEventSelector(EventGraph events, ISerializer serializer)
 {
     Events      = events;
     _serializer = serializer;
 }
 public EventStoreDatabaseObjects(EventGraph parent)
 {
     _parent = parent;
 }
        public EventStreamAppender(EventGraph graph)
        {
            _graph = graph;

            AppendEventFunction = new FunctionName(_graph.DatabaseSchemaName, "mt_append_event");
        }
 public EventStoreDatabaseObjects(EventGraph parent)
 {
     _parent = parent;
 }
Beispiel #24
0
        public EventStreamAppender(EventGraph graph)
        {
            _graph = graph;

            AppendEventFunction = new DbObjectName(_graph.DatabaseSchemaName, "mt_append_event");
        }
 public EventSequenceFetcher(EventGraph graph, int number)
 {
     _sql = $"select nextval('{graph.DatabaseSchemaName}.mt_events_sequence') from generate_series(1,{number})";
 }
Beispiel #26
0
 internal EventSelector(EventGraph events, ISerializer serializer)
 {
     Events      = events;
     _serializer = serializer;
 }
Beispiel #27
0
 public AppendEventsOperation(EventStream stream, EventGraph eventGraph)
 {
     Stream      = stream;
     _eventGraph = eventGraph;
 }
Beispiel #28
0
 public AppendEventFunction(EventGraph events) : base(new DbObjectName(events.DatabaseSchemaName, "mt_append_event"))
 {
     _events = events;
 }
Beispiel #29
0
 public StreamStateByGuidHandler(EventGraph events, Guid streamId, string tenantId = null) : base(events, streamId, tenantId)
 {
 }
Beispiel #30
0
 public StreamStateByStringHandler(EventGraph events, string streamKey, string tenantId = null) : base(events, streamKey, tenantId)
 {
 }
Beispiel #31
0
 public abstract IStorageOperation AppendEvent(EventGraph events, IMartenSession session, StreamAction stream, IEvent e);
Beispiel #32
0
 public EventStreamStorage(EventGraph graph)
 {
     _graph = graph;
 }