protected override CorrelaterResult <T> InternalCorrelate(ICollectionWrapper <T> collection1, ICollectionWrapper <T> collection2, CancellationToken cancellationToken = default) { var dynamicTable = CreateDynamicTable(collection1, collection2, cancellationToken); var matchingArrays = GetMatchingArrays(dynamicTable, collection1, collection2); return(new CorrelaterResult <T>(dynamicTable[collection1.Length, collection2.Length], matchingArrays.Item1, matchingArrays.Item2)); }
private (T[], T[]) GetBestMatches(ICollectionWrapper <T> collection1, ICollectionWrapper <T> collection2, int longestSubsequentSize, TraceNode[] traceList) { var bestMatch1 = new List <T>(); var bestMatch2 = new List <T>(); var traceNode = traceList[longestSubsequentSize]; var locationInCollection1 = collection1.Length - 1; var locationInCollection2 = collection2.Length - 1; while (traceNode != null) { AddElementsFromCollection(collection1, bestMatch1, bestMatch2, locationInCollection1, traceNode.IndexInCollection1); AddElementsFromCollection(collection2, bestMatch2, bestMatch1, locationInCollection2, traceNode.IndexInCollection2); bestMatch1.Add(collection1[traceNode.IndexInCollection1]); bestMatch2.Add(collection2[traceNode.IndexInCollection2]); locationInCollection1 = traceNode.IndexInCollection1 - 1; locationInCollection2 = traceNode.IndexInCollection2 - 1; traceNode = traceNode.Previous; } AddElementsFromCollection(collection1, bestMatch1, bestMatch2, locationInCollection1, -1); AddElementsFromCollection(collection2, bestMatch2, bestMatch1, locationInCollection2, -1); bestMatch1.Reverse(); bestMatch2.Reverse(); return(bestMatch1.ToArray(), bestMatch2.ToArray()); }
public StreamProjection( ICollectionWrapper <StreamReadModel, Int64> streamReadModelCollection, IDocumentWriter documentWriter, IBlobStore blobStore, IQueueManager queueDispatcher, IReader <DocumentDescriptorReadModel, DocumentDescriptorId> documentDescriptorReadModel) { if (queueDispatcher is IObserveProjection) { Observe(queueDispatcher as IObserveProjection); } _streamReadModelCollection = streamReadModelCollection; _documentDescriptorReadModel = documentDescriptorReadModel; _documentWriter = documentWriter; _blobStore = blobStore; _streamReadModelCollection.Attach(this, false); if (_streamReadModelCollection.All.Any()) { _lastId = _streamReadModelCollection.All.Max(s => s.Id); } else { _lastId = 0; } }
/// <summary> /// Handles list properties. /// </summary> /// <param name="myItemProperty">The list property.</param> /// <param name="myHeader">The header.</param> /// <param name="myStringBuilder">The string builder.</param> private void HandleListProperties(ICollectionWrapper myItemProperty, String myHeader, ref StringBuilder myStringBuilder) { foreach (var item in myItemProperty) { myStringBuilder.AppendLine(myHeader + "\t " + item.ToString()); } }
private CorrelaterResult <T> GetResult(ICollectionWrapper <T> collection1, ICollectionWrapper <T> collection2, int[] thresholds, TraceNode[] traceList) { var longestSubsequentSize = GetLongestSubsequentSize(thresholds); var distance = collection1.Length + collection2.Length - 2 * longestSubsequentSize; var(bestMatch1, bestMatch2) = GetBestMatches(collection1, collection2, longestSubsequentSize, traceList); return(new CorrelaterResult <T>(distance, bestMatch1, bestMatch2)); }
public void Init() { _fakeDbWrapper = Substitute.For <INoSqlWrapper>(); _fakeCollection = Substitute.For <ICollectionWrapper <TestEntity> >(); _dummyLogger = Substitute.For <ILoggerExt>(); _sut = new NoSqlRepository <TestEntity>(_fakeDbWrapper, _dummyLogger); }
private CorrelaterResult <T> CreateResult(List <NegativeArray <int> > trace, ICollectionWrapper <T> collection1Wrapper, ICollectionWrapper <T> collection2Wrapper) { int x = collection1Wrapper.Length - 1, y = collection2Wrapper.Length - 1; var bestMatch1 = new LinkedList <T>(); var bestMatch2 = new LinkedList <T>(); for (var d = trace.Count - 1; d >= 0; d--) { var k = x - y; int prev_k = 0, prev_x = 0, prev_y = 0; if (d > 0) { if (k == -d || (k != d && trace[d - 1][k - 1] < trace[d - 1][k + 1])) { prev_k = k + 1; } else { prev_k = k - 1; } prev_x = trace[d - 1][prev_k]; prev_y = prev_x - prev_k; } else { prev_x = 0; prev_y = 0; } while (x >= prev_x && y >= prev_y) { bestMatch1.AddFirst(collection1Wrapper[x]); bestMatch2.AddFirst(collection2Wrapper[y]); x = x - 1; y = y - 1; } if (d > 0) { if (x == prev_x) { bestMatch1.AddFirst(collection1Wrapper[x]); bestMatch2.AddFirst(default(T)); x = x - 1; } else { bestMatch1.AddFirst(default(T)); bestMatch2.AddFirst(collection2Wrapper[y]); y = y - 1; } } } return(new CorrelaterResult <T>(trace.Count - 1, bestMatch1.ToArray(), bestMatch2.ToArray())); }
public DocumentProjection( IDocumentWriter writer, IReader <DocumentDescriptorReadModel, DocumentDescriptorId> documentDescriptorReader, ICollectionWrapper <DocumentDeletedReadModel, String> documentDeletedWrapper) { _writer = writer; _documentDescriptorReader = documentDescriptorReader; _documentDeletedWrapper = documentDeletedWrapper; _documentDeletedWrapper.Attach(this, false); }
/// <summary> /// Throws an exception if the collection contains a null element /// </summary> public static void CheckForNulls <T>(this ICollectionWrapper <T> collection, string collectionName) { for (int i = 0; i < collection.Length; i++) { if (collection[i] == null || collection[i].Equals(default(T))) { throw new NullElementException(collectionName, i); } } }
/// <summary> /// Handles list properties. /// </summary> /// <param name="myItemProperty">The list property.</param> /// <returns>An jarray contains the list items.</returns> private JArray HandleListProperties(ICollectionWrapper myItemProperty) { var values = new JArray(); foreach (var value in myItemProperty) { values.Add(new JArray(value.ToString())); } return(values); }
public OffsetCollectionWrapper(ICollectionWrapper <T> innerCollection, int start, int end) { if (start > end) { throw new InternalException($"Code 1000 (in {nameof(OffsetCollectionWrapper<T>)}: {nameof(start)} == {start} is smaller than {nameof(end)} == {end})"); } this.innerCollection = innerCollection; this.start = start; this.end = Math.Min(end, innerCollection.Length); }
private bool CanDoTransposition(ICollectionWrapper <T> collection1, ICollectionWrapper <T> collection2, int i, int j) { if (transpositionCalculator == null) { return(false); } if (i < 2 || j < 2) { return(false); } return(collection1[i - 1].Equals(collection2[j - 2]) && collection1[i - 2].Equals(collection2[j - 1])); }
public DocumentWorkflow( ICommandBus commandBus, IBlobStore blobStore, DeduplicationHelper deduplicationHelper, ICollectionWrapper <DocumentDescriptorReadModel, DocumentDescriptorId> documents, IHandleMapper handleMapper) { _commandBus = commandBus; _blobStore = blobStore; _deduplicationHelper = deduplicationHelper; _documents = documents; _handleMapper = handleMapper; }
public DocumentDescriptorProjection( ICollectionWrapper <DocumentDescriptorReadModel, DocumentDescriptorId> documents, IDocumentWriter handleWriter) { _documents = documents; _handleWriter = handleWriter; _documents.Attach(this, false); _documents.OnSave = (d, e) => { d.FormatsCount = d.Formats.Count; }; }
private List <Task <CorrelaterResult <T> > > Map(ICollectionWrapper <T> collection1, ICollectionWrapper <T> collection2, CancellationToken cancellationToken) { var resultTasks = new List <Task <CorrelaterResult <T> > >(); for (int i = 0; i < Math.Max(collection1.Length, collection2.Length); i = i + chunkSize) { var wrappedCollection1 = new OffsetCollectionWrapper <T>(collection1, Math.Min(collection1.Length, i), Math.Min(collection1.Length, i + chunkSize)); var wrappedCollection2 = new OffsetCollectionWrapper <T>(collection2, Math.Min(collection2.Length, i), Math.Min(collection2.Length, i + chunkSize)); resultTasks.Add(Task.Run(() => innerCorrelater.Correlate(wrappedCollection1, wrappedCollection2, cancellationToken))); } return(resultTasks); }
private List <NegativeArray <int> > CreateTraceTable(ICollectionWrapper <T> collection1Wrapper, ICollectionWrapper <T> collection2Wrapper, CancellationToken cancellationToken) { var maxDistance = collection1Wrapper.Length + collection2Wrapper.Length; var trace = new List <NegativeArray <int> >(); for (var distance = 0; distance <= maxDistance; distance++) { cancellationToken.ThrowIfCancellationRequested(); trace.Add(new NegativeArray <int>(maxDistance * 2)); // "k" is an arbitrary variable defined as: k = x - y // We're always comparing 2 elements. x is the location of the first element in collection1. y is the location of the second element in collection2 for (var k = -1 * distance; k <= distance; k += 2) { int x; if (distance == 0) { x = 0; } else if (k == -distance || (k != distance && trace[distance - 1][k - 1] < trace[distance - 1][k + 1])) { x = trace[distance - 1][k + 1]; } else { x = trace[distance - 1][k - 1] + 1; } var y = x - k; // This "while" is the greedy part of the algorithm - as long as the next elements are the same match them while (x < collection1Wrapper.Length && y < collection2Wrapper.Length && collection1Wrapper[x].Equals(collection2Wrapper[y])) { x = x + 1; y = y + 1; } trace[distance][k] = x; if (x == collection1Wrapper.Length && y == collection2Wrapper.Length) { return(trace); } } } throw new InternalException($"Code 1003 (reached the end of {nameof(MyersAlgorithmCorrelater<T>)}.{nameof(Correlate)})"); }
public static Task FindAndModifyAsync <TModel, TKey>( this ICollectionWrapper <TModel, TKey> collectionWrapper, DomainEvent e, TKey id, Action <TModel> action, bool notify = false) where TModel : IReadModelEx <TKey> { Func <TModel, Task> wrapper = m => { action(m); return(Task.CompletedTask); }; return(collectionWrapper.FindAndModifyAsync(e, id, wrapper, notify)); }
#pragma warning disable S2436 // Classes and methods should not have too many generic parameters public static Task FindByPropertyAsync <TModel, TKey, TProperty>( #pragma warning restore S2436 // Classes and methods should not have too many generic parameters this ICollectionWrapper <TModel, TKey> collectionWrapper, Expression <Func <TModel, TProperty> > propertySelector, TProperty propertyValue, Action <TModel> subscription) where TModel : IReadModelEx <TKey> { Func <TModel, Task> wrapper = m => { subscription(m); return(Task.CompletedTask); }; return(collectionWrapper.FindByPropertyAsync <TProperty>(propertySelector, propertyValue, wrapper)); }
/// <summary> /// Creates the dynamic table. /// Cell[i, j] defines the best match in which array1 contains all elements up to i (including), and array2 contains all elements up to j (including) /// </summary> private long[,] CreateDynamicTable(ICollectionWrapper <T> collection1, ICollectionWrapper <T> collection2, CancellationToken cancellationToken) { var dynamicTable = new long[collection1.Length + 1, collection2.Length + 1]; dynamicTable[0, 0] = 0; for (int i = 1; i < collection1.Length + 1; i++) { dynamicTable[i, 0] = dynamicTable[i - 1, 0] + removalCalculator.RemovalCost(collection1[i - 1]); } for (int i = 1; i < collection2.Length + 1; i++) { dynamicTable[0, i] = dynamicTable[0, i - 1] + insertionCalculator.InsertionCost(collection2[i - 1]); } OnProgressUpdate?.Invoke(1, collection1.Length + 1); for (int i = 1; i < collection1.Length + 1; i++) { cancellationToken.ThrowIfCancellationRequested(); for (int j = 1; j < collection2.Length + 1; j++) { if (collection1[i - 1].Equals(collection2[j - 1])) { dynamicTable[i, j] = dynamicTable[i - 1, j - 1]; continue; } var insertion = dynamicTable[i, j - 1] + insertionCalculator.InsertionCost(collection1[i - 1]); var removal = dynamicTable[i - 1, j] + removalCalculator.RemovalCost(collection2[j - 1]); var min = Min(insertion, removal); if (distanceCalculator != null) { var substitution = dynamicTable[i - 1, j - 1] + distanceCalculator.Distance(collection1[i - 1], collection2[j - 1]); min = Math.Min(min, substitution); } if (CanDoTransposition(collection1, collection2, i, j)) { var transposition = transpositionCalculator.TranspositionCost(collection1[i - 1], collection1[i - 2]) + dynamicTable[i - 2, j - 2]; min = Math.Min(min, transposition); } dynamicTable[i, j] = min; } OnProgressUpdate?.Invoke(i + 1, collection1.Length + 1); } return(dynamicTable); }
public static Task <TModel> UpsertAsync <TModel, TKey>( this ICollectionWrapper <TModel, TKey> collectionWrapper, DomainEvent e, TKey id, Func <TModel> insert, Action <TModel> update, bool notify = false) where TModel : IReadModelEx <TKey> { Func <TModel, Task> wrapper = m => { update(m); return(Task.CompletedTask); }; return(collectionWrapper.UpsertAsync(e, id, insert, wrapper, notify)); }
#pragma warning disable S2436 // Classes and methods should not have too many generic parameters public static Task FindAndModifyByPropertyAsync <TModel, TKey, TProperty>( #pragma warning restore S2436 // Classes and methods should not have too many generic parameters this ICollectionWrapper <TModel, TKey> collectionWrapper, DomainEvent e, Expression <Func <TModel, TProperty> > propertySelector, TProperty propertyValue, Action <TModel> action, bool notify = false) where TModel : IReadModelEx <TKey> { Func <TModel, Task> wrapper = m => { action(m); return(Task.CompletedTask); }; return(collectionWrapper.FindAndModifyByPropertyAsync <TProperty>(e, propertySelector, propertyValue, wrapper, notify)); }
public ICollection Read(XmlReader xmlReader) { if (xmlReader == null) { throw new ArgumentNullException(nameof(xmlReader)); } ICollectionWrapper result = null; if (xmlReader.MoveToContent() == XmlNodeType.Element) { while (xmlReader.NodeType != XmlNodeType.EndElement && xmlReader.NodeType != XmlNodeType.None) { if (xmlReader.IsStartElement("row")) { if (result == null) { result = CreateCollectionWrapper(); } if (xmlReader.IsEmptyElement) { xmlReader.Skip(); xmlReader.MoveToContent(); continue; } xmlReader.ReadStartElement(); xmlReader.MoveToContent(); var item = this.ElementObjectSerializer.Read(xmlReader); result.Add(item); xmlReader.ReadEndElement(); xmlReader.MoveToContent(); } else { throw new XmlSerializeException("错误的xml格式文档,集合的元素必须以“row”不根节点。"); } } } return(result == null ? null : result.Result); }
private Dictionary <T, TimesAndLocation> GetElemenetsInCollection(ICollectionWrapper <T> collection) { var elementsAppearingInCollection = new Dictionary <T, TimesAndLocation>(); for (var i = 0; i < collection.Length; i++) { var element = collection[i]; if (elementsAppearingInCollection.ContainsKey(element)) { elementsAppearingInCollection[element].Times++; } else { elementsAppearingInCollection[element] = new TimesAndLocation(i, 1); } } return(elementsAppearingInCollection); }
public void SetUp() { _collectionWrapper = Substitute.For<ICollectionWrapper<StreamReadModel, Int64>>(); rmStream = new List<StreamReadModel>(); rmDocuments = new List<DocumentDescriptorReadModel>(); _collectionWrapper.When(r => r.Insert( Arg.Any<DomainEvent>(), Arg.Any<StreamReadModel>())) .Do(cinfo => rmStream.Add((StreamReadModel)cinfo.Args()[1])); _collectionWrapper.All.Returns(rmStream.AsQueryable()); _readerDocumentReadModel = Substitute.For<IReader<DocumentDescriptorReadModel, DocumentDescriptorId>>(); _readerDocumentReadModel.AllUnsorted.Returns(rmDocuments.AsQueryable()); _readerDocumentReadModel.AllSortedById.Returns(rmDocuments.AsQueryable().OrderBy(r => r.Id)); _readerDocumentReadModel.FindOneById(Arg.Any<DocumentDescriptorId>()) .Returns(cinfo => rmDocuments.SingleOrDefault(d => d.Id == (DocumentDescriptorId)cinfo.Args()[0])); _handleWriter = Substitute.For<IDocumentWriter>(); _blobStore = Substitute.For<IBlobStore>(); }
public void SetUp() { _collectionWrapper = Substitute.For <ICollectionWrapper <StreamReadModel, Int64> >(); rmStream = new List <StreamReadModel>(); rmDocuments = new List <DocumentDescriptorReadModel>(); _collectionWrapper.When(r => r.Insert( Arg.Any <DomainEvent>(), Arg.Any <StreamReadModel>())) .Do(cinfo => rmStream.Add((StreamReadModel)cinfo.Args()[1])); _collectionWrapper.All.Returns(rmStream.AsQueryable()); _readerDocumentReadModel = Substitute.For <IReader <DocumentDescriptorReadModel, DocumentDescriptorId> >(); _readerDocumentReadModel.AllUnsorted.Returns(rmDocuments.AsQueryable()); _readerDocumentReadModel.AllSortedById.Returns(rmDocuments.AsQueryable().OrderBy(r => r.Id)); _readerDocumentReadModel.FindOneById(Arg.Any <DocumentDescriptorId>()) .Returns(cinfo => rmDocuments.SingleOrDefault(d => d.Id == (DocumentDescriptorId)cinfo.Args()[0])); _handleWriter = Substitute.For <IDocumentWriter>(); _blobStore = Substitute.For <IBlobStore>(); }
protected override CorrelaterResult <T> InternalCorrelate(ICollectionWrapper <T> collection1, ICollectionWrapper <T> collection2, CancellationToken cancellationToken = default) { var elementToLocationsInCollection2 = GetDictionaryFromElementToLocationsInCollection(collection2); var traceList = new TraceNode[collection1.Length + 1]; var thresholds = new int[collection1.Length + 1]; thresholds[0] = -1; for (var i = 1; i < thresholds.Length; i++) { thresholds[i] = thresholds.Length + 1; } for (var i = 0; i < collection1.Length; i++) { cancellationToken.ThrowIfCancellationRequested(); OnProgressUpdate?.Invoke(i + 1, collection1.Length); if (!elementToLocationsInCollection2.ContainsKey(collection1[i])) { continue; } foreach (var j in elementToLocationsInCollection2[collection1[i]]) { var k = BinarySearchFindK(j, thresholds); if (j < thresholds[k]) { thresholds[k] = j; traceList[k] = new TraceNode(i, j, k > 0 ? traceList[k - 1] : null); } } } return(GetResult(collection1, collection2, thresholds, traceList)); }
/// <summary> /// Handles list properties. /// </summary> /// <param name="myItemProperty">The property of the item(edge, hyperedge, vertex).</param> /// <param name="myPropertyToFill">The schema property which is to fill.</param> private void HandleListProperties(ICollectionWrapper myItemProperty, ref Property myPropertyToFill) { Type propertyElementType = typeof(Object); foreach (var value in myItemProperty) { myPropertyToFill.Value += "[" + value.ToString() + "],"; propertyElementType = value.GetType(); } var index = -1; if (myPropertyToFill.Value != null) { index = myPropertyToFill.Value.LastIndexOf(','); } if (index > -1) { myPropertyToFill.Value = myPropertyToFill.Value.Remove(index, 1); } myPropertyToFill.Type = myItemProperty.GetType().Name + "(" + propertyElementType.Name + ")"; }
/// <summary> /// Handles list properties. /// </summary> /// <param name="myItemProperty">The list property.</param> /// <param name="myHeader">The header.</param> /// <param name="myStringBuilder">The string builder.</param> private void HandleListProperties(ICollectionWrapper myItemProperty, String myHeader, ref StringBuilder myStringBuilder) { foreach (var item in myItemProperty) { myStringBuilder.AppendLine(myHeader + "\t " + item.ToString()); } }
public Projection2(ICollectionWrapper <SampleReadModel2, string> collection) { _collection = collection; _collection.Attach(this, false); }
public MyProjection(ICollectionWrapper <MyReadModel, string> collection) { _collection = collection; }
public ProjectionPollableReadmodel(ICollectionWrapper <SampleReadModelPollableTest, TestId> collection) { _collection = collection; _collection.Attach(this, false); }
public TypedIdProjection(ICollectionWrapper <SampleReadModelTest, String> collection) { _collection = collection; _collection.Attach(this, false); }
/// <summary> /// Handles list properties. /// </summary> /// <param name="myItemProperty">The list property.</param> /// <returns>An jarray contains the list items.</returns> private JArray HandleListProperties(ICollectionWrapper myItemProperty) { var values = new JArray(); foreach (var value in myItemProperty) { values.Add(new JArray(value.ToString())); } return values; }
/// <summary> /// Handles list properties. /// </summary> /// <param name="myItemProperty">The property of the item(edge, hyperedge, vertex).</param> /// <param name="myPropertyToFill">The schema property which is to fill.</param> private void HandleListProperties(ICollectionWrapper myItemProperty, ref Property myPropertyToFill) { Type propertyElementType = typeof(Object); foreach (var value in myItemProperty) { myPropertyToFill.Value += "[" + value.ToString() + "],"; propertyElementType = value.GetType(); } var index = -1; if (myPropertyToFill.Value != null) { index = myPropertyToFill.Value.LastIndexOf(','); } if (index > -1) { myPropertyToFill.Value = myPropertyToFill.Value.Remove(index, 1); } myPropertyToFill.Type = myItemProperty.GetType().Name + "(" + propertyElementType.Name + ")"; }
/// <summary> /// Creates a new collection literal using a set /// </summary> /// <param name="myCollection">The ISet collection</param> public CollectionLiteralExpression(ISet<IComparable> myCollection) { CollectionLiteral = new SetCollectionWrapper(myCollection); }