Example #1
0
        public void WhenAssemblyEmitted_HasCorrectTypes()
        {
            //Act
            var result = Sut.GetContentTypes().ToList();

            Sut.CompileFurnaceContentTypes();

            //Assert
            foreach (var contentType in result)
            {
                var type =
                    TypeFinder.FindType(BuildTypeFullName(contentType));
                Assert.That(type, Is.Not.Null);
            }
        }
Example #2
0
        public void GivenOneFurnaceType_WhenCompileIsCalled_ThenCompilationHasSyntaxTree()
        {
            //Assign
            const string assemblyName = "TestAssemblyName";

            Sut.AddFurnaceType(BaseNamespace + "ClassOne");

            TypeFinder.FindType(BaseNamespace + "ClassOne").Returns(GetType());

            //Act
            var compilation = Sut.Compile(assemblyName);

            //Assert
            Assert.That(compilation.SyntaxTrees, Contains.Item(Spy.FurnaceTypes.First().SyntaxTree));
        }
Example #3
0
        public async Task <IList <FullyEvent <PrimaryKey> > > GetList(PrimaryKey stateId, long latestTimestamp, long startVersion, long endVersion)
        {
            var list = new List <FullyEvent <PrimaryKey> >((int)(endVersion - startVersion));
            await Task.Run(async() =>
            {
                var getTableListTask = config.GetSubTables();
                if (!getTableListTask.IsCompletedSuccessfully)
                {
                    await getTableListTask;
                }
                var stateIdStr = typeof(PrimaryKey) == typeof(long) ? stateId.ToString() : $"'{stateId.ToString()}'";
                using var conn = config.CreateConnection() as NpgsqlConnection;
                await conn.OpenAsync();
                foreach (var table in getTableListTask.Result.Where(t => t.EndTime >= latestTimestamp))
                {
                    var sql          = $"COPY (SELECT typecode,data,version,timestamp from {table.SubTable} WHERE stateid={stateIdStr} and version>={startVersion} and version<={endVersion} order by version asc) TO STDOUT (FORMAT BINARY)";
                    using var reader = conn.BeginBinaryExport(sql);
                    while (reader.StartRow() != -1)
                    {
                        var typeCode  = reader.Read <string>(NpgsqlDbType.Varchar);
                        var data      = reader.Read <string>(NpgsqlDbType.Json);
                        var version   = reader.Read <long>(NpgsqlDbType.Bigint);
                        var timestamp = reader.Read <long>(NpgsqlDbType.Bigint);
                        if (version <= endVersion && version >= startVersion)
                        {
                            if (serializer.Deserialize(Encoding.UTF8.GetBytes(data), typeFinder.FindType(typeCode)) is IEvent evt)
                            {
                                list.Add(new FullyEvent <PrimaryKey>
                                {
                                    StateId = stateId,
                                    Event   = evt,
                                    Base    = new EventBase(version, timestamp)
                                });
                            }
                        }
                    }
                }
            });

            return(list.OrderBy(e => e.Base.Version).ToList());
        }
        public object Map(JObject message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var fullType     = GetTypeOfObject(message);
            var typeName     = GetTypeName(fullType);
            var assemblyName = GetAssemblyName(fullType);
            var foundType    = _typeFinder.FindType(typeName, assemblyName);
            var msgBack      = CastParsedMessageToItsStrongType(message, foundType);

            return(msgBack);
        }
Example #5
0
        public async Task <IList <FullyEvent <PrimaryKey> > > GetList(PrimaryKey stateId, long latestTimestamp, long startVersion, long endVersion)
        {
            var list = new List <FullyEvent <PrimaryKey> >((int)(endVersion - startVersion));
            await Task.Run(async() =>
            {
                var getTableListTask = config.GetSubTables();
                if (!getTableListTask.IsCompletedSuccessfully)
                {
                    await getTableListTask;
                }
                var stateIdStr = typeof(PrimaryKey) == typeof(long) ? stateId.ToString() : $"'{stateId.ToString()}'";
                using var conn = config.CreateConnection();
                await conn.OpenAsync();
                foreach (var table in getTableListTask.Result.Where(t => t.EndTime >= latestTimestamp))
                {
                    var sql        = getListSqlDict.GetOrAdd(table.SubTable, key => $"SELECT TypeCode,Data,Version,Timestamp from {key} WHERE StateId=@StateId and Version>=@StartVersion and Version<=@EndVersion order by Version asc");
                    var originList = await conn.QueryAsync <EventModel>(sql, new
                    {
                        StateId      = stateId,
                        StartVersion = startVersion,
                        EndVersion   = endVersion
                    });
                    foreach (var item in originList)
                    {
                        if (serializer.Deserialize(Encoding.UTF8.GetBytes(item.Data), typeFinder.FindType(item.TypeCode)) is IEvent evt)
                        {
                            list.Add(new FullyEvent <PrimaryKey>
                            {
                                StateId = stateId,
                                Event   = evt,
                                Base    = new EventBase(item.Version, item.Timestamp)
                            });
                        }
                    }
                }
            });

            return(list.OrderBy(e => e.Base.Version).ToList());
        }
Example #6
0
        public async Task <IList <FullyEvent <PrimaryKey> > > GetList(PrimaryKey stateId, long latestTimestamp, long startVersion, long endVersion)
        {
            var collectionListTask = grainConfig.GetCollectionList();

            if (!collectionListTask.IsCompletedSuccessfully)
            {
                await collectionListTask;
            }
            var list = new List <FullyEvent <PrimaryKey> >();

            foreach (var collection in collectionListTask.Result.Where(c => c.EndTime >= latestTimestamp))
            {
                var filterBuilder = Builders <BsonDocument> .Filter;
                var filter        = filterBuilder.Eq("StateId", stateId) & filterBuilder.Lte("Version", endVersion) & filterBuilder.Gte("Version", startVersion);
                var cursor        = await grainConfig.Client.GetCollection <BsonDocument>(grainConfig.DataBase, collection.SubTable).FindAsync <BsonDocument>(filter, cancellationToken: new CancellationTokenSource(10000).Token);

                foreach (var document in cursor.ToEnumerable())
                {
                    var typeCode  = document["TypeCode"].AsString;
                    var data      = document["Data"].AsString;
                    var timestamp = document["Timestamp"].AsInt64;
                    var version   = document["Version"].AsInt64;
                    if (version <= endVersion && version >= startVersion)
                    {
                        if (serializer.Deserialize(Encoding.UTF8.GetBytes(data), typeFinder.FindType(typeCode)) is IEvent evt)
                        {
                            list.Add(new FullyEvent <PrimaryKey>
                            {
                                StateId = stateId,
                                Event   = evt,
                                Base    = new EventBase(version, timestamp)
                            });
                        }
                    }
                }
            }
            return(list);
        }
Example #7
0
        public ObserverUnit <PrimaryKey> UnreliableObserver(
            string group,
            Func <IServiceProvider,
                  FullyEvent <PrimaryKey>, ValueTask> handler)
        {
            GetEventHandlers(group).Add(EventHandler);
            GetBatchEventHandlers(group).Add(BatchEventHandler);
            eventHandlers.Add(EventHandler);
            batchEventHandlers.Add(BatchEventHandler);
            return(this);

            //内部函数
            Task EventHandler(byte[] bytes)
            {
                var(success, transport) = EventBytesTransport.FromBytes <PrimaryKey>(bytes);
                if (success)
                {
                    var data = serializer.Deserialize(transport.EventBytes, typeFinder.FindType(transport.EventTypeCode));
                    if (data is IEvent @event && transport.GrainId is PrimaryKey actorId)
                    {
                        var eventBase = EventBase.FromBytes(transport.BaseBytes);
                        var tellTask  = handler(serviceProvider, new FullyEvent <PrimaryKey>
                        {
                            StateId = actorId,
                            Base    = eventBase,
                            Event   = @event
                        });
                        if (!tellTask.IsCompletedSuccessfully)
                        {
                            return(tellTask.AsTask());
                        }
                    }
                }
                return(Task.CompletedTask);
            }

            Task BatchEventHandler(List <byte[]> list)
            {
                var groups =
                    list.Select(b => EventBytesTransport.FromBytes <PrimaryKey>(b))
                    .Where(o => o.success)
                    .Select(o => o.transport)
                    .GroupBy(o => o.GrainId);

                return(Task.WhenAll(groups.Select(async kv =>
                {
                    foreach (var transport in kv)
                    {
                        var data = serializer.Deserialize(transport.EventBytes, typeFinder.FindType(transport.EventTypeCode));
                        if (data is IEvent @event && transport.GrainId is PrimaryKey actorId)
                        {
                            var eventBase = EventBase.FromBytes(transport.BaseBytes);
                            var tellTask = handler(serviceProvider, new FullyEvent <PrimaryKey>
                            {
                                StateId = actorId,
                                Base = eventBase,
                                Event = @event
                            });
                            if (!tellTask.IsCompletedSuccessfully)
                            {
                                await tellTask;
                            }
                        }
                    }
                })));
            }
        }