Exemplo n.º 1
0
        public void Commit()
        {
            var batch = new StructuredDataBatchRequest
            {
                ReturnBox = _inbox.Name,
                Request   = Commands
            };

            _server.Enqueue(batch);
            _server.Send();

            var reply = _inbox.WaitForMessages <PutResult>(TimeSpan.FromMinutes(2.0));

            if (!reply.Any())
            {
                throw new TimeoutException();
            }

            _commands.Clear();

            if (reply.First().Code == PutResultCode.Ok)
            {
                return;
            }

            var innerExceptions = new List <Exception>();

            foreach (var result in reply)
            {
                var info = string.Format("For {0}: {1}", result.Key, result.Message);
                switch (result.Code)
                {
                case PutResultCode.Concurrency:
                    innerExceptions.Add(new DBConcurrencyException(info));
                    break;

                case PutResultCode.StorageCapacity:
                    innerExceptions.Add(new EndOfStreamException(info));
                    break;

                default:
                    innerExceptions.Add(new ApplicationException(info));
                    break;
                }
            }
            var badResult = new AggregateException(innerExceptions);

            throw badResult;
        }
Exemplo n.º 2
0
        internal IEnumerable <string> GetTableKeys(string tableName)
        {
            var batch = new StructuredDataBatchRequest
            {
                ReturnBox = _inbox.Name,
                Request   = EnumerableEx.OfOne(new StructuredDataRequest
                {
                    Table       = tableName,
                    RequestCode = StructuredDataRequestCode.ReadKeys
                })
            };

            _server.Enqueue(batch);
            _server.Send();

            var reply = _inbox.WaitForMessages <string>(TimeSpan.FromMinutes(2.0));

            return(reply);
        }
Exemplo n.º 3
0
        internal IEnumerable <ReadAtom <T> > Fetch <T>(string table, IEnumerable <string> keys)
        {
            var batch = new StructuredDataBatchRequest
            {
                ReturnBox = _inbox.Name,
                Request   = keys.Select(k =>
                                        new StructuredDataRequest
                {
                    Table       = table,
                    Key         = k,
                    RequestCode = StructuredDataRequestCode.Read
                })
            };

            _server.Enqueue(batch);
            _server.Send();

            var reply = _inbox.WaitForMessages <ReadAtomLarva>(TimeSpan.FromMinutes(2.0));

            if (!reply.Any())
            {
                throw new TimeoutException();
            }

            var firstItem = reply.First();

            if (null == firstItem || string.IsNullOrEmpty(firstItem.Key) || null == firstItem.Data)
            {
                return(Enumerable.Empty <ReadAtom <T> >());
            }

            return(reply.Select(item =>
            {
                var ms = new MemoryStream(item.Data);
                var reader = new BsonReader(ms);
                var serializer = new JsonSerializer();

                var data = serializer.Deserialize <T>(reader);
                return new ReadAtom <T> {
                    Data = data, ETag = item.ETag, Key = item.Key
                };
            }));
        }