private async Task WriteAsync(NetCore.Blobs.Blob blob, Stream sourceStream, CancellationToken cancellationToken)
        {
            string fullPath = ToFullPath(blob);

            byte[] value = sourceStream.ToByteArray();

            using (ServiceFabricTransaction tx = GetTransaction())
            {
                IReliableDictionary <string, byte[]> coll = await OpenCollectionAsync().ConfigureAwait(false);

                IReliableDictionary <string, BlobMetaTag> metaColl = await OpenMetaCollectionAsync().ConfigureAwait(false);

                var meta = new BlobMetaTag
                {
                    LastModificationTime = DateTimeOffset.UtcNow,
                    Length = value.LongLength,
                    Md     = value.GetHash(HashType.Md5).ToHexString()
                };

                await metaColl.AddOrUpdateAsync(tx.Tx, fullPath, meta, (k, v) => meta).ConfigureAwait(false);

                await coll.AddOrUpdateAsync(tx.Tx, fullPath, value, (k, v) => value).ConfigureAwait(false);

                await tx.CommitAsync().ConfigureAwait(false);
            }
        }
        private async Task <IEnumerable <QueueMessage> > ReceiveMessagesAsync(int count)
        {
            IReliableQueue <byte[]> collection = await _stateManager.GetOrAddAsync <IReliableQueue <byte[]> >(_queueName);

            var result = new List <QueueMessage>();

            using (var tx = new ServiceFabricTransaction(_stateManager, null))
            {
                while (result.Count < count)
                {
                    ConditionalValue <byte[]> message = await collection.TryDequeueAsync(tx.Tx);

                    if (message.HasValue)
                    {
                        QueueMessage qm = QueueMessage.FromByteArray(message.Value);

                        result.Add(qm);
                    }
                    else
                    {
                        break;
                    }
                }

                await tx.CommitAsync();
            }

            return(result.Count == 0 ? null : result);
        }
        private async Task AppendAsync(string fullPath, Stream sourceStream, CancellationToken cancellationToken)
        {
            fullPath = ToFullPath(fullPath);

            using (ServiceFabricTransaction tx = GetTransaction())
            {
                IReliableDictionary <string, byte[]> coll = await OpenCollectionAsync().ConfigureAwait(false);

                //create a new byte array with
                byte[] extra = sourceStream.ToByteArray();
                ConditionalValue <byte[]> value = await coll.TryGetValueAsync(tx.Tx, fullPath).ConfigureAwait(false);

                int    oldLength = value.HasValue ? value.Value.Length : 0;
                byte[] newData   = new byte[oldLength + extra.Length];
                if (value.HasValue)
                {
                    Array.Copy(value.Value, newData, oldLength);
                }
                Array.Copy(extra, 0, newData, oldLength, extra.Length);

                //put new array into the key
                await coll.AddOrUpdateAsync(tx.Tx, fullPath, extra, (k, v) => extra).ConfigureAwait(false);

                //commit the transaction
                await tx.CommitAsync().ConfigureAwait(false);
            }
        }
示例#4
0
        private async Task WriteAsync(string id, Stream sourceStream)
        {
            id = ToId(id);

            byte[] value = sourceStream.ToByteArray();

            using (ServiceFabricTransaction tx = GetTransaction())
            {
                IReliableDictionary <string, byte[]> coll = await OpenCollectionAsync();

                IReliableDictionary <string, BlobMetaTag> metaColl = await OpenMetaCollectionAsync();

                var meta = new BlobMetaTag
                {
                    LastModificationTime = DateTimeOffset.UtcNow,
                    Length = value.LongLength,
                    Md     = value.GetHash(HashType.Md5).ToHexString()
                };

                await metaColl.AddOrUpdateAsync(tx.Tx, id, meta, (k, v) => meta);

                await coll.AddOrUpdateAsync(tx.Tx, id, value, (k, v) => value);

                await tx.CommitAsync();
            }
        }
        public async Task PutMessagesAsync(IReadOnlyCollection <QueueMessage> messages, CancellationToken cancellationToken)
        {
            IReliableQueue <byte[]> collection = await _stateManager.GetOrAddAsync <IReliableQueue <byte[]> >(_queueName);

            using (var tx = new ServiceFabricTransaction(_stateManager, null))
            {
                foreach (QueueMessage message in messages)
                {
                    byte[] data = message.ToByteArray();
                    await collection.EnqueueAsync(tx.Tx, data, _timeout, cancellationToken);
                }

                await tx.CommitAsync();
            }
        }
        private async Task WriteAsync(string id, Stream sourceStream, CancellationToken cancellationToken)
        {
            id = ToId(id);

            byte[] value = sourceStream.ToByteArray();

            using (ServiceFabricTransaction tx = GetTransaction())
            {
                IReliableDictionary <string, byte[]> coll = await OpenCollectionAsync();

                await coll.AddOrUpdateAsync(tx.Tx, id, value, (k, v) => value);

                await tx.CommitAsync();
            }
        }
        public async Task DeleteAsync(IEnumerable <string> fullPaths, CancellationToken cancellationToken)
        {
            GenericValidation.CheckBlobFullPaths(fullPaths);

            using (ServiceFabricTransaction tx = GetTransaction())
            {
                IReliableDictionary <string, byte[]> coll = await OpenCollectionAsync().ConfigureAwait(false);

                foreach (string fullPath in fullPaths)
                {
                    await coll.TryRemoveAsync(tx.Tx, ToFullPath(fullPath)).ConfigureAwait(false);
                }

                await tx.CommitAsync().ConfigureAwait(false);
            }
        }
        public async Task DeleteAsync(IEnumerable <string> ids, CancellationToken cancellationToken)
        {
            GenericValidation.CheckBlobId(ids);

            using (ServiceFabricTransaction tx = GetTransaction())
            {
                IReliableDictionary <string, byte[]> coll = await OpenCollectionAsync();

                foreach (string id in ids)
                {
                    await coll.TryRemoveAsync(tx.Tx, ToId(id));
                }

                await tx.CommitAsync();
            }
        }
示例#9
0
        private async Task ReceiveMessagesAsync(Func <IEnumerable <QueueMessage>, Task> onMessage, int maxBatchSize, CancellationToken cancellationToken)
        {
            var messages = new List <QueueMessage>();

            while (!cancellationToken.IsCancellationRequested && !_disposed)
            {
                try
                {
                    using (var tx = new ServiceFabricTransaction(_stateManager, null))
                    {
                        IReliableQueue <byte[]> collection = await GetCollectionAsync();

                        while (messages.Count < maxBatchSize)
                        {
                            ConditionalValue <byte[]> message = await collection.TryDequeueAsync(tx.Tx, TimeSpan.FromSeconds(4), cancellationToken);

                            if (message.HasValue)
                            {
                                QueueMessage qm = QueueMessage.FromByteArray(message.Value);

                                messages.Add(qm);
                            }
                            else
                            {
                                break;
                            }
                        }

                        //make the call before committing the transaction
                        if (messages.Count > 0)
                        {
                            await onMessage(messages);

                            messages.Clear();
                        }

                        await tx.CommitAsync();
                    }
                }
                catch (Exception ex)
                {
                    Trace.Fail($"failed to listen to messages on queue '{_queueName}'", ex.ToString());
                }

                await Task.Delay(_scanInterval);
            }
        }
        private async Task ReceiveMessagesAsync(Func <IReadOnlyCollection <QueueMessage>, CancellationToken, Task> onMessage, int maxBatchSize, CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested && !_disposed)
            {
                try
                {
                    using (var tx = new ServiceFabricTransaction(_stateManager, null))
                    {
                        IReliableState collection = await GetCollectionAsync().ConfigureAwait(false);

                        var messages = new List <QueueMessage>();

                        while (messages.Count < maxBatchSize)
                        {
                            ConditionalValue <byte[]> message = await TryDequeueAsync(tx, collection, cancellationToken).ConfigureAwait(false);

                            if (message.HasValue)
                            {
                                QueueMessage qm = QueueMessage.FromByteArray(message.Value);

                                messages.Add(qm);
                            }
                            else
                            {
                                break;
                            }
                        }

                        //make the call before committing the transaction
                        if (messages.Count > 0)
                        {
                            await onMessage(messages, cancellationToken).ConfigureAwait(false);
                        }

                        await tx.CommitAsync().ConfigureAwait(false);
                    }
                }
                catch (Exception ex)
                {
                    Trace.Fail($"failed to listen to messages on queue '{_queueName}'", ex.ToString());
                }

                await Task.Delay(_scanInterval).ConfigureAwait(false);
            }

            Trace.TraceInformation("queue '{0}' scanner exited", _queueName);
        }