Exemple #1
0
        ProjectionVersions GetProjectionVersions(string contractId)
        {
            try
            {
                if (string.IsNullOrEmpty(contractId))
                {
                    throw new ArgumentNullException(nameof(contractId));
                }

                var persistentVersionContractId = typeof(PersistentProjectionVersionHandler).GetContractId();
                if (string.Equals(persistentVersionContractId, contractId, StringComparison.OrdinalIgnoreCase))
                {
                    return(GetPersistentProjectionVersions(persistentVersionContractId));
                }

                ProjectionVersions versions = inMemoryVersionStore.Get(contractId);
                if (versions == null || versions.Count == 0)
                {
                    var queryResult = GetProjectionVersionsFromStore(contractId);
                    if (queryResult.Success)
                    {
                        if (queryResult.Projection.State.Live != null)
                        {
                            inMemoryVersionStore.Cache(queryResult.Projection.State.Live);
                        }
                        if (queryResult.Projection.State.Building != null)
                        {
                            inMemoryVersionStore.Cache(queryResult.Projection.State.Building);
                        }
                        versions = inMemoryVersionStore.Get(contractId);
                    }

                    if (versions == null || versions.Count == 0)
                    {
                        var initialVersion = new ProjectionVersion(contractId, ProjectionStatus.Building, 1, contractId.GetTypeByContract().GetProjectionHash());
                        inMemoryVersionStore.Cache(initialVersion);
                        versions = inMemoryVersionStore.Get(contractId);
                    }
                }

                return(versions ?? new ProjectionVersions());
            }
            catch (Exception ex)
            {
                log.WarnException($"Unable to load projection versions. ProjectionContractId:{contractId}", ex);
                return(new ProjectionVersions());
            }
        }
Exemple #2
0
        protected async virtual Task <ReadResult <ProjectionVersions> > GetProjectionVersionsAsync(string projectionName)
        {
            if (string.IsNullOrEmpty(projectionName))
            {
                throw new ArgumentNullException(nameof(projectionName));
            }

            var elapsed = new TimeSpan((long)(TimestampToTicks * (Stopwatch.GetTimestamp() - LastRefreshTimestamp)));

            ProjectionVersions versions = inMemoryVersionStore.Get(projectionName);

            //TODO: This optimization caused some problems
            //if (elapsed.TotalMinutes > 5 || versions is null || versions.Count == 0)
            {
                var queryResult = await GetProjectionVersionsFromStoreAsync(projectionName).ConfigureAwait(false);

                if (queryResult.IsSuccess)
                {
                    if (queryResult.Data.State.Live != null)
                    {
                        inMemoryVersionStore.Cache(queryResult.Data.State.Live);
                    }
                    foreach (var buildingVersion in queryResult.Data.State.AllVersions.GetBuildingVersions())
                    {
                        inMemoryVersionStore.Cache(buildingVersion);
                    }
                    versions             = inMemoryVersionStore.Get(projectionName);
                    LastRefreshTimestamp = Stopwatch.GetTimestamp();
                }

                if (queryResult.HasError)
                {
                    return(ReadResult <ProjectionVersions> .WithError(queryResult.Error));
                }
            }

            return(new ReadResult <ProjectionVersions>(versions));
        }
Exemple #3
0
        ProjectionVersions GetPersistentProjectionVersions(string contractId)
        {
            if (string.IsNullOrEmpty(contractId))
            {
                throw new ArgumentNullException(nameof(contractId));
            }

            ProjectionVersions versions = inMemoryVersionStore.Get(contractId);

            if (versions == null || versions.Count == 0)
            {
                var queryResult = GetProjectionVersionsFromStore(contractId);
                if (queryResult.Success)
                {
                    if (queryResult.Projection.State.Live != null)
                    {
                        inMemoryVersionStore.Cache(queryResult.Projection.State.Live);
                    }
                    if (queryResult.Projection.State.Building != null)
                    {
                        inMemoryVersionStore.Cache(queryResult.Projection.State.Building.WithStatus(ProjectionStatus.Live));
                    }
                    versions = inMemoryVersionStore.Get(contractId);
                }

                // inception
                if (versions == null || versions.Count == 0)
                {
                    var initialVersion = new ProjectionVersion(contractId, ProjectionStatus.Live, 1, typeof(PersistentProjectionVersionHandler).GetProjectionHash());

                    inMemoryVersionStore.Cache(initialVersion);
                    versions = inMemoryVersionStore.Get(contractId);
                }
            }

            return(versions ?? new ProjectionVersions());
        }
Exemple #4
0
        protected virtual ReadResult <ProjectionVersions> GetProjectionVersions(string projectionName)
        {
            if (string.IsNullOrEmpty(projectionName))
            {
                throw new ArgumentNullException(nameof(projectionName));
            }

            var elapsed = new TimeSpan((long)(TimestampToTicks * (Stopwatch.GetTimestamp() - LastRefreshTimestamp)));

            ProjectionVersions versions = inMemoryVersionStore.Get(projectionName);

            if (elapsed.TotalMinutes > 5 || versions is null || versions.Count == 0)
            {
                var queryResult = GetProjectionVersionsFromStore(projectionName);
                if (queryResult.IsSuccess)
                {
                    if (queryResult.Data.State.Live != null)
                    {
                        inMemoryVersionStore.Cache(queryResult.Data.State.Live);
                    }
                    foreach (var buildingVersion in queryResult.Data.State.AllVersions.Where(x => x.Status == ProjectionStatus.Building))
                    {
                        inMemoryVersionStore.Cache(buildingVersion.WithStatus(ProjectionStatus.Live));
                    }
                    versions             = inMemoryVersionStore.Get(projectionName);
                    LastRefreshTimestamp = Stopwatch.GetTimestamp();
                }

                if (queryResult.NotFound)
                {
                    versions.Add(new ProjectionVersion(projectionName, ProjectionStatus.NotPresent, 0, projectionHasher.CalculateHash(projectionName)));
                }
            }

            return(new ReadResult <ProjectionVersions>(versions));
        }