Exemple #1
0
        public async Task <Result> Write(string collectionId, HttpRequest request)
        {
            try
            {
                var payload = new MemoryStream();

                await request.Body.CopyToAsync(payload);

                if (request.ContentLength.Value != payload.Length)
                {
                    throw new DataMisalignedException();
                }

                var messageBuf = payload.ToArray();

                var responseStream = await _data.Write(collectionId.ToHash(), messageBuf);

                return(new Result {
                    Data = responseStream, MediaType = "application/octet-stream"
                });
            }
            catch (Exception ex)
            {
                _log.WriteLine(ex);

                throw;
            }
        }
Exemple #2
0
        public async Task <ResponseModel> Write(string collectionName, HttpRequest request)
        {
            try
            {
                var collectionId = collectionName.ToHash();
                var timer        = Stopwatch.StartNew();

                var payload = new MemoryStream();

                await request.Body.CopyToAsync(payload);

                if (request.ContentLength.Value != payload.Length)
                {
                    throw new DataMisalignedException();
                }

                var compressed = payload.ToArray();
                var messageBuf = QuickLZ.decompress(compressed);

                // A write request is either a request to write new data
                // or a request to concat two or more existing pages.

                this.Log(string.Format("serialized {0} bytes in {1}", messageBuf.Length, timer.Elapsed));

                timer.Restart();

                MemoryStream responseStream;

                lock (Sync)
                {
                    this.Log("waited for synchronization for {0}", timer.Elapsed);

                    timer.Restart();

                    responseStream = _data.Write(collectionId, messageBuf);

                    timer.Stop();

                    var t = timer.ElapsedMilliseconds > 0 ? timer.ElapsedMilliseconds : 1;

                    this.Log(string.Format(
                                 "wrote {0} bytes in {1}: {2} bytes/ms",
                                 messageBuf.Length, timer.Elapsed, messageBuf.Length / t));
                }

                return(new ResponseModel {
                    Stream = responseStream, MediaType = "application/octet-stream"
                });
            }
            catch (Exception ex)
            {
                this.Log(ex);

                throw;
            }
        }