Ejemplo n.º 1
0
        /// <summary>Retrive immutable objects.</summary>
        /// <typeparam name="T">Type of presisted value.</typeparam>
        /// <param name="checksum">Checksum of value to get.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public async Task <StoreResult <T> > TryGetAsync <T>(Checksum checksum)
        {
            if (checksum == null)
            {
                throw new ArgumentNullException(nameof(checksum));
            }

            if (this.cache.TryGet <T>(checksum, out var cacheValue))
            {
                return(cacheValue);
            }

            foreach (var store in this.storages)
            {
                var result = await store.TryGetAsync(checksum).ConfigureAwait(false);

                if (result.Contains)
                {
                    var             value       = (T)this.formatter.Deserialize(result.GetStream());
                    StoreResult <T> storeResult = new Persisted <T>(value, this, checksum);
                    return(this.cache.AddOrGet(storeResult));
                }
            }

            throw new DoesNotContainValueException(checksum);
        }
Ejemplo n.º 2
0
        #pragma warning restore CA2225

        /// <inheritdoc/>
        public bool Equals(Persisted <T> other)
        {
            if (other == null)
            {
                return(false);
            }

            return(this.Checksum.Equals(other.Checksum));
        }
        /// <summary>Initializes a new instance of the <see cref="StoreResult{T}"/> class.</summary>
        /// <param name="contains">True if contains value.</param>
        /// <param name="presistedValue">Valus that was presisted.</param>
        /// <exception cref="ArgumentNullException">Throws <see cref="ArgumentNullException"/> if contains is true and value is null.</exception>
        public StoreResult(bool contains, Persisted <T> presistedValue)
        {
            if (contains && presistedValue == null)
            {
                throw new ArgumentNullException(nameof(presistedValue));
            }

            this.contains       = contains;
            this.presistedValue = presistedValue;
        }
Ejemplo n.º 4
0
        /// <summary>Presists immutable objects, and returns link to object.</summary>
        /// <typeparam name="T">Type of presisted value.</typeparam>
        /// <param name="value">Value to ensure is presisted in store.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public async Task <Persisted <T> > EnsurePresistAsync <T>(T value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            this.ImmutableValidator.EnsureImmutable(value);
            this.SerializableValidator.EnsureSerializable(value);

            var memoryStream = new MemoryStream();

            this.formatter.Serialize(memoryStream, value);
            var bytes    = memoryStream.ToArray();
            var checksum = Checksum.CalculateChecksum(bytes);

            if (this.cache.TryGet <T>(checksum, out var cacheValue))
            {
                return(cacheValue.GetPresistedValue());
            }

            var persistTasks = new List <Task>(this.storages.Length);

            foreach (var store in this.storages)
            {
                persistTasks.Add(store.EnsurePresistAsync(checksum, bytes));
            }

            StoreResult <T> persistedValue = new Persisted <T>(value, this, checksum);

            foreach (var task in persistTasks)
            {
                await task.ConfigureAwait(false);
            }

            var cachedValue = this.cache.AddOrGet(persistedValue);

            return(cachedValue.GetPresistedValue());
        }