string GetSanitizedApplicationResourceIdentifier(IApplicationResourceIdentifier identifier) { var identifierAsString = _applicationResourceIdentifierConverter.AsString(identifier); identifierAsString = identifierAsString.Replace('#', '|'); return(identifierAsString); }
/// <inheritdoc/> public Type Resolve(IApplicationResourceIdentifier identifier) { _logger.Trace($"Trying to resolve : {identifier.Resource.Name} - with type {identifier.Resource.Type.Identifier}"); var typeIdentifier = identifier.Resource.Type.Identifier; if (_resolversByType.ContainsKey(typeIdentifier)) { return(_resolversByType[typeIdentifier].Resolve(identifier)); } var resourceType = _types.GetFor(typeIdentifier); var types = _typeDiscoverer.FindMultiple(resourceType.Type); var typesMatchingName = types.Where(t => t.Name == identifier.Resource.Name); ThrowIfAmbiguousTypes(identifier, typesMatchingName); var formats = _application.Structure.GetStructureFormatsForArea(resourceType.Area); var type = typesMatchingName.Where(t => formats.Any(f => f.Match(t.Namespace).HasMatches)).FirstOrDefault(); if (type != null) { return(type); } _logger.Error($"Unknown application resurce type : {identifier.Resource.Type.Identifier}"); throw new UnknownApplicationResourceType(identifier.Resource.Type.Identifier); }
/// <summary> /// Initializes a new instance of <see cref="EventEnvelope"/> /// </summary> /// <param name="correlationId"><see cref="TransactionCorrelationId"/> the <see cref="IEvent"/> is part of</param> /// <param name="eventId"><see cref="EventId"/> for the <see cref="IEvent"/></param> /// <param name="sequenceNumber"></param> /// <param name="sequenceNumberForEventType"></param> /// <param name="generation"><see cref="EventGeneration"/> for the <see cref="IEvent"/> </param> /// <param name="event"><see cref="IApplicationResourceIdentifier"/> representing the <see cref="IEvent"/></param> /// <param name="eventSourceId"><see cref="EventSourceId"/> for the <see cref="IEventSource"/></param> /// <param name="eventSource"><see cref="IApplicationResourceIdentifier"/> representing the <see cref="IEventSource"/></param> /// <param name="version"><see cref="EventSourceVersion">Version</see> of the event related to the <see cref="IEventSource"/></param> /// <param name="causedBy"><see cref="string"/> representing which person or what system caused the event</param> /// <param name="occurred"><see cref="DateTime">When</see> the event occured</param> public EventEnvelope( TransactionCorrelationId correlationId, EventId eventId, EventSequenceNumber sequenceNumber, EventSequenceNumber sequenceNumberForEventType, EventGeneration generation, IApplicationResourceIdentifier @event, EventSourceId eventSourceId, IApplicationResourceIdentifier eventSource, EventSourceVersion version, CausedBy causedBy, DateTimeOffset occurred) { CorrelationId = correlationId; EventId = eventId; SequenceNumber = sequenceNumber; SequenceNumberForEventType = sequenceNumberForEventType; Generation = generation; Event = @event; EventSourceId = eventSourceId; EventSource = eventSource; Version = version; CausedBy = causedBy; Occurred = occurred; }
string GetPartitionKeyFor(IApplicationResourceIdentifier identifier, EventSourceId id) { var identifierAsString = GetSanitizedApplicationResourceIdentifier(identifier); var partitionKey = $"{identifierAsString}-{id}"; return(partitionKey); }
/// <inheritdoc/> public EventSourceVersion GetVersionFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var partitionKeyFilter = GetPartitionKeyFilterFor(eventSource, eventSourceId); var query = new TableQuery <DynamicTableEntity>().Select(new[] { "Version" }).Where(partitionKeyFilter); var events = new List <DynamicTableEntity>(); TableContinuationToken continuationToken = null; do { _table.ExecuteQuerySegmentedAsync(query, continuationToken).ContinueWith(e => { events.AddRange(e.Result.Results); continuationToken = e.Result.ContinuationToken; }).Wait(); } while (continuationToken != null); var result = events.OrderByDescending(e => long.Parse(e.RowKey)).FirstOrDefault(); if (result == null) { return(EventSourceVersion.Zero); } var value = result.Properties["Version"].DoubleValue.Value; var version = EventSourceVersion.FromCombined(value); return(version); }
public AuthorizationResult GetForCommand(IApplicationResourceIdentifier commandType) { var command = new CommandRequest(TransactionCorrelationId.NotSet, commandType, new Dictionary <string, object>()); var result = _commandSecurityManager.Authorize(command); return(result); }
void ThrowIfAmbiguousTypes(IApplicationResourceIdentifier identifier, IEnumerable <Type> typesMatchingName) { if (typesMatchingName.Count() > 1) { throw new AmbiguousTypes(identifier); } }
string GetPartitionKeyFilterFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var partitionKey = GetPartitionKeyFor(eventSource, eventSourceId); var partitionKeyFilter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey); return(partitionKeyFilter); }
/// <inheritdoc/> public EventSourceVersion GetFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var version = EventSourceVersion.Zero; version = _eventStore.GetVersionFor(eventSource, eventSourceId); return(version); }
/// <inheritdoc/> public bool Equals(IApplicationResourceIdentifier other) { if (LocationSegments.Count() != other.LocationSegments.Count()) { return(false); } if (((string)Application.Name) != ((string)other.Application.Name)) { return(false); } if (((string)Resource.Name) != ((string)other.Resource.Name)) { return(false); } var locationSegmentsA = LocationSegments.ToArray(); var locationSegmentsB = other.LocationSegments.ToArray(); for (var i = 0; i < locationSegmentsA.Length; i++) { if (locationSegmentsA[i].Name.AsString() != locationSegmentsB[i].Name.AsString()) { return(false); } } return(true); }
/// <inheritdoc/> public EventSequenceNumber NextForType(IApplicationResourceIdentifier identifier) { var key = $"{EventSequenceNumberForTypePrefix}-{_applicationResourceIdentifierConverter.AsString(identifier)}"; long sequenceNumber = _database.StringIncrement(key); return(sequenceNumber); }
/// <inheritdoc/> public IEnumerable <EventAndEnvelope> GetFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var eventSourceIdentifier = _applicationResourceIdentifierConverter.AsString(eventSource); var eventPath = GetPathFor(eventSourceIdentifier, eventSourceId); var files = _files.GetFilesIn(eventPath).OrderBy(f => f); var eventFiles = files.Where(f => f.EndsWith(".event")).ToArray(); var envelopeFiles = files.Where(f => f.EndsWith(".envelope")).ToArray(); if (eventFiles.Length != envelopeFiles.Length) { throw new Exception($"There is a problem with event files for {eventSourceIdentifier} with Id {eventSourceId}"); } var events = new List <EventAndEnvelope>(); for (var eventIndex = 0; eventIndex < eventFiles.Length; eventIndex++) { var envelopeFile = envelopeFiles[eventIndex]; var eventFile = eventFiles[eventIndex]; var envelopeAsJson = _files.ReadString(Path.GetDirectoryName(envelopeFile), Path.GetFileName(envelopeFile)); var eventAsJson = _files.ReadString(Path.GetDirectoryName(eventFile), Path.GetFileName(eventFile)); var envelopeValues = _serializer.GetKeyValuesFromJson(envelopeAsJson); var _correllationId = Guid.Parse((string)envelopeValues["CorrelationId"]); var _eventId = Guid.Parse((string)envelopeValues["EventId"]); var _sequenceNumber = (long)envelopeValues["SequenceNumber"]; var _sequenceNumberForEventType = (long)envelopeValues["SequenceNumberForEventType"]; var _generation = (long)envelopeValues["Generation"]; var _event = _applicationResourceIdentifierConverter.FromString((string)envelopeValues["Event"]); var _eventSourceId = Guid.Parse((string)envelopeValues["EventSourceId"]); var _eventSource = _applicationResourceIdentifierConverter.FromString((string)envelopeValues["EventSource"]); var _eventSourceVersion = EventSourceVersion.FromCombined(double.Parse(envelopeValues["Version"].ToString())); var _causedBy = (string)envelopeValues["CausedBy"]; var _occurred = (DateTime)envelopeValues["Occurred"]; var envelope = new EventEnvelope( _correllationId, _eventId, _sequenceNumber, _sequenceNumberForEventType, (int)_generation, _event, _eventSourceId, _eventSource, _eventSourceVersion, _causedBy, _occurred ); var eventType = _applicationResourceResolver.Resolve(envelope.Event); var @event = Activator.CreateInstance(eventType, eventSourceId) as IEvent; _serializer.FromJson(@event, eventAsJson); events.Add(new EventAndEnvelope(envelope, @event)); } return(events); }
/// <inheritdoc/> public bool HasEventsFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var eventSourceIdentifier = _applicationResourceIdentifierConverter.AsString(eventSource); var eventPath = GetPathFor(eventSourceIdentifier, eventSourceId); var files = _files.GetFilesIn(eventPath, "*.event"); return(files.Count() > 0); }
/// <inheritdoc/> public IEnumerable <EventAndEnvelope> GetFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var filter = Builders <BsonDocument> .Filter.Eq("EventSourceId", eventSourceId); var result = _collection.Find(filter).SortBy(bson => bson["Version"]); return(new EventAndEnvelope[0]); }
void ThrowIfAmbiguousTypes(IApplicationResourceIdentifier identifier, IEnumerable <Type> typesMatchingName) { _logger.Error($"Ambiguous types found for {identifier.Resource.Name}"); if (typesMatchingName.Count() > 1) { throw new AmbiguousTypes(identifier); } }
/// <inheritdoc/> public bool HasEventsFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var applicationResourceIdentifier = _applicationResources.Identify(eventSource); var eventSourceIdentifier = _applicationResourceIdentifierConverter.AsString(applicationResourceIdentifier); var eventPath = GetPathFor(eventSourceIdentifier, eventSourceId); var files = Directory.GetFiles(eventPath, "*.event"); return(files.Length > 0); }
string GetKeyFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { _logger.Trace($"Getting key for eventSource '{eventSource?.ToString()??"<unknown eventsource"}' with Id '{eventSourceId?.Value.ToString()??"<unknown eventsource id>"}'"); var identifier = _applicationResourceIdentifierConverter.AsString(eventSource); var key = $"{VersionForPrefix}-{identifier}-{eventSourceId.Value}"; return(key); }
string GetPartitionKeyFor(IApplicationResourceIdentifier identifier, EventSourceId id) { var identifierAsString = _applicationResourceIdentifierConverter.AsString(identifier); identifierAsString = identifierAsString.Replace('#', '|'); var partitionKey = $"{identifierAsString}-{id}"; return(partitionKey); }
/// <inheritdoc/> public EventSourceVersion GetVersionFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var filter = Builders <BsonDocument> .Filter.Eq("EventSourceId", eventSourceId); var result = _collection.Find(filter).SortByDescending(bson => bson["Version"]).Single(); var value = result["Version"].AsDouble; var version = EventSourceVersion.FromCombined(value); return(version); }
/// <inheritdoc/> public bool HasEventsFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var partitionKeyFilter = GetPartitionKeyFilterFor(eventSource, eventSourceId); var query = new TableQuery <DynamicTableEntity>().Where(partitionKeyFilter); var hasEvents = false; TableContinuationToken continuationToken = null; _table.ExecuteQuerySegmentedAsync(query, continuationToken).ContinueWith(e => hasEvents = e.Result.Results.Count() > 0 ).Wait(); return(hasEvents); }
/// <summary> /// Initializes a new instance of <see cref="ProcessMethodEventProcessor"/> /// </summary> /// <param name="container"><see cref="IContainer"/> to use for getting instances of <see cref="IProcessEvents"/> implementation</param> /// <param name="systemClock"><see cref="ISystemClock"/> for timing purposes</param> /// <param name="identifier"><see cref="EventProcessorIdentifier"/> that uniquely identifies the <see cref="ProcessMethodEventProcessor"/></param> /// <param name="event"><see cref="IApplicationResourceIdentifier">Identifier</see> for identifying the <see cref="IEvent"/></param> /// <param name="methodInfo"><see cref="MethodInfo"/> for the actual process method</param> public ProcessMethodEventProcessor( IContainer container, ISystemClock systemClock, EventProcessorIdentifier identifier, IApplicationResourceIdentifier @event, MethodInfo methodInfo) { Identifier = identifier; Event = @event; _container = container; _systemClock = systemClock; _methodInfo = methodInfo; }
/// <inheritdoc/> public EventSourceVersion GetVersionFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var eventSourceIdentifier = _applicationResourceIdentifierConverter.AsString(eventSource); var eventPath = GetPathFor(eventSourceIdentifier, eventSourceId); var first = _files.GetFilesIn(eventPath, "*.event").OrderByDescending(f => f).FirstOrDefault(); if (first == null) { return(EventSourceVersion.Zero); } var versionAsString = Path.GetFileNameWithoutExtension(first); var versionAsDouble = double.Parse(versionAsString, CultureInfo.InvariantCulture); return(EventSourceVersion.FromCombined(versionAsDouble)); }
/// <inheritdoc/> public EventSourceVersion GetFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var fileName = GetFileNameFor(eventSource, eventSourceId); var version = EventSourceVersion.Zero; if (_files.Exists(_path, fileName)) { var versionAsString = _files.ReadString(_path, fileName); version = EventSourceVersion.FromCombined(double.Parse(versionAsString)); } else { version = _eventStore.GetVersionFor(eventSource, eventSourceId); } return(version); }
/// <inheritdoc/> public override Type Resolve(IApplicationResourceIdentifier identifier) { var formats = _application.Structure.GetStructureFormatsForArea(ApplicationAreas.Events); var types = _eventTypes.Where(t => t.Name == identifier.Resource.Name); foreach (var type in types) { foreach (var format in formats) { if (format.Match(type.Namespace).HasMatches) { return(type); } } } throw new UnknownApplicationResourceType(identifier.Resource.Type.Identifier); }
/// <inheritdoc/> public EventSourceVersion GetFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var key = GetKeyFor(eventSource, eventSourceId); var version = EventSourceVersion.Zero; var value = _database.StringGet(key); if (value.IsNull) { version = _eventStore.GetVersionFor(eventSource, eventSourceId); SetFor(eventSource, eventSourceId, version); } else { version = EventSourceVersion.FromCombined(double.Parse(value.ToString())); } return(version); }
/// <inheritdoc/> public IEnumerable <EventAndEnvelope> GetFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var partitionKeyFilter = GetPartitionKeyFilterFor(eventSource, eventSourceId); var query = new TableQuery <DynamicTableEntity>().Where(partitionKeyFilter); var events = new List <DynamicTableEntity>(); TableContinuationToken continuationToken = null; do { _table.ExecuteQuerySegmentedAsync(query, continuationToken).ContinueWith(e => { events.AddRange(e.Result.Results); continuationToken = e.Result.ContinuationToken; }).Wait(); } while (continuationToken != null); var eventsAndEnvelopes = events.OrderBy(e => long.Parse(e.RowKey)).Select(entity => { var eventResource = GetApplicationResourceIdentifierFromSanitizedString(entity.Properties[PropertiesFor <EventEnvelope> .GetPropertyName(e => e.Event)].StringValue); var eventSourceResource = GetApplicationResourceIdentifierFromSanitizedString(entity.Properties[PropertiesFor <EventEnvelope> .GetPropertyName(e => e.EventSource)].StringValue); var envelope = new EventEnvelope( PropertiesFor <EventEnvelope> .GetValue(entity, e => e.CorrelationId), PropertiesFor <EventEnvelope> .GetValue(entity, e => e.EventId), PropertiesFor <EventEnvelope> .GetValue(entity, e => e.SequenceNumber), PropertiesFor <EventEnvelope> .GetValue(entity, e => e.SequenceNumberForEventType), PropertiesFor <EventEnvelope> .GetValue(entity, e => e.Generation), eventResource, PropertiesFor <EventEnvelope> .GetValue(entity, e => e.EventSourceId), eventSourceResource, PropertiesFor <EventEnvelope> .GetValue(entity, e => e.Version), PropertiesFor <EventEnvelope> .GetValue(entity, e => e.CausedBy), PropertiesFor <EventEnvelope> .GetValue(entity, e => e.Occurred) ); var eventType = _applicationResourceResolver.Resolve(envelope.Event); var @event = Activator.CreateInstance(eventType, envelope.EventSourceId) as IEvent; eventType.GetProperties().Where(p => p.CanWrite).ForEach(p => p.SetValue(@event, PropertyHelper.GetValue(entity, p))); return(new EventAndEnvelope(envelope, @event)); }); return(eventsAndEnvelopes); }
/// <inheritdoc/> public string AsString(IApplicationResourceIdentifier identifier) { var stringBuilder = new StringBuilder(); stringBuilder.Append($"{identifier.Application.Name}{ApplicationSeparator}"); var first = true; identifier.LocationSegments.ForEach(l => { if (!first) { stringBuilder.Append(ApplicationLocationSeparator); } first = false; stringBuilder.Append($"{l.Name}"); }); stringBuilder.Append($"{ApplicationResourceSeparator}{identifier.Resource.Name}"); stringBuilder.Append($"{ApplicationResourceTypeSeparator}{identifier.Resource.Type.Identifier}"); return(stringBuilder.ToString()); }
/// <inheritdoc/> public EventSequenceNumber NextForType(IApplicationResourceIdentifier identifier) { var hashCode = identifier.GetHashCode(); lock ( _sequenceLocksPerType ) { if (!_sequenceLocksPerType.ContainsKey(hashCode)) { _sequenceLocksPerType[hashCode] = new object(); } } lock (_sequenceLocksPerType[hashCode]) { var identifierAsString = _applicationResourceIdentifierConverter.AsString(identifier); var file = $"{SequenceForPrefix}{identifierAsString}"; var sequence = GetNextInSequenceFromFile(file); _files.WriteString(_configuration.Path, file, sequence.ToString()); return(sequence); } }
/// <inheritdoc/> public EventSourceVersion GetVersionFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { return(EventSourceVersion.Zero); }
/// <inheritdoc/> public bool HasEventsFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { return(false); }