Esempio n. 1
0
        private RavenJObject ReadDocument(Tuple <MemoryStream, RavenJObject, int> stream, JsonDocumentMetadata metadata)
        {
            if (stream.Item2 != null)
            {
                return(stream.Item2);
            }

            RavenJObject result;
            Stream       docDataStream = stream.Item1;

            if (documentCodecs.Any())
            {
                var metadataCopy = (RavenJObject)metadata.Metadata.CloneToken();
                using (docDataStream = documentCodecs
                                       .Aggregate(docDataStream, (dataStream, codec) => codec.Decode(metadata.Key, metadataCopy, dataStream)))
                    result = docDataStream.ToJObject();
            }
            else
            {
                result = docDataStream.ToJObject();
            }

            Debug.Assert(metadata.Etag != null);
            documentCacher.SetCachedDocument(metadata.Key, metadata.Etag.Value, result, metadata.Metadata, stream.Item3);

            return(result);
        }
        private RavenJObject ReadDocumentData(string key, Etag existingEtag, RavenJObject metadata)
        {
            var loweredKey = CreateKey(key);

            var existingCachedDocument = documentCacher.GetCachedDocument(loweredKey, existingEtag);

            if (existingCachedDocument != null)
            {
                return(existingCachedDocument.Document);
            }

            var documentReadResult = tableStorage.Documents.Read(Snapshot, loweredKey, writeBatch.Value);

            if (documentReadResult == null)             //non existing document
            {
                return(null);
            }

            using (var stream = documentReadResult.Reader.AsStream())
            {
                using (var decodedDocumentStream = documentCodecs.Aggregate(stream,
                                                                            (current, codec) => codec.Value.Decode(loweredKey, metadata, current)))
                {
                    var documentData = decodedDocumentStream.ToJObject();

                    documentCacher.SetCachedDocument(loweredKey, existingEtag, documentData, metadata, (int)stream.Length);

                    return(documentData);
                }
            }
        }
        private RavenJObject ReadDocument(Tuple <MemoryStream, RavenJObject> stream, JsonDocumentMetadata metadata)
        {
            if (stream.Item2 != null)
            {
                return(stream.Item2);
            }

            var memoryStream = stream.Item1;

            if (documentCodecs.Count() > 0)
            {
                byte[] buffer       = memoryStream.GetBuffer();
                var    metadataCopy = (RavenJObject)metadata.Metadata.CloneToken();
                var    dataBuffer   = new byte[memoryStream.Length - memoryStream.Position];
                Buffer.BlockCopy(buffer, (int)memoryStream.Position, dataBuffer, 0,
                                 dataBuffer.Length);
                documentCodecs.Aggregate(dataBuffer, (bytes, codec) => codec.Value.Decode(metadata.Key, metadataCopy, bytes));
                memoryStream = new MemoryStream(dataBuffer);
            }

            var result = memoryStream.ToJObject();

            Debug.Assert(metadata.Etag != null);
            documentCacher.SetCachedDocument(metadata.Key, metadata.Etag.Value, result, metadata.Metadata);

            return(result);
        }
Esempio n. 4
0
        private RavenJObject ReadDocumentData(string key, Etag existingEtag, RavenJObject metadata, out int size)
        {
            var loweredKey = CreateKey(key);

            size = -1;

            var existingCachedDocument = documentCacher.GetCachedDocument(loweredKey, existingEtag);

            if (existingCachedDocument != null)
            {
                size = existingCachedDocument.Size;
                return(existingCachedDocument.Document);
            }

            var documentReadResult = tableStorage.Documents.Read(Snapshot, loweredKey, writeBatch.Value);

            if (documentReadResult == null)             //non existing document
            {
                return(null);
            }

            using (var stream = documentReadResult.Reader.AsStream())
            {
                using (var decodedDocumentStream = documentCodecs.Aggregate(stream,
                                                                            (current, codec) => codec.Value.Decode(loweredKey, metadata, current)))
                {
                    var streamToUse = decodedDocumentStream;
                    if (stream != decodedDocumentStream)
                    {
                        streamToUse = new CountingStream(decodedDocumentStream);
                    }

                    var documentData = decodedDocumentStream.ToJObject();

                    size = (int)Math.Max(stream.Position, streamToUse.Position);

                    documentCacher.SetCachedDocument(loweredKey, existingEtag, documentData, metadata, size);
                    return(documentData);
                }
            }
        }
Esempio n. 5
0
        private RavenJObject ReadDocumentData(string normalizedKey, Etag existingEtag, RavenJObject metadata, out int size)
        {
            var normalizedKeySlice = (Slice)normalizedKey;

            try
            {
                size = -1;

                var existingCachedDocument = documentCacher.GetCachedDocument(normalizedKey, existingEtag);
                if (existingCachedDocument != null)
                {
                    size = existingCachedDocument.Size;
                    return(existingCachedDocument.Document);
                }

                var documentReadResult = tableStorage.Documents.Read(Snapshot, normalizedKeySlice, writeBatch.Value);
                if (documentReadResult == null)                 //non existing document
                {
                    return(null);
                }

                using (var stream = documentReadResult.Reader.AsStream())
                {
                    using (var decodedDocumentStream = documentCodecs.Aggregate(stream,
                                                                                (current, codec) => codec.Value.Decode(normalizedKey, metadata, current)))
                    {
                        var streamToUse = decodedDocumentStream;
                        if (stream != decodedDocumentStream)
                        {
                            streamToUse = new CountingStream(decodedDocumentStream);
                        }

                        var documentData = decodedDocumentStream.ToJObject();

                        size = (int)Math.Max(stream.Position, streamToUse.Position);
                        documentCacher.SetCachedDocument(normalizedKey, existingEtag, documentData, metadata, size);

                        return(documentData);
                    }
                }
            }
            catch (Exception e)
            {
                InvalidDataException invalidDataException = null;
                try
                {
                    size = -1;
                    var documentReadResult = tableStorage.Documents.Read(Snapshot, normalizedKeySlice, writeBatch.Value);
                    if (documentReadResult == null) //non existing document
                    {
                        return(null);
                    }

                    using (var stream = documentReadResult.Reader.AsStream())
                    {
                        using (var reader = new BinaryReader(stream))
                        {
                            if (reader.ReadUInt32() == DocumentCompression.CompressFileMagic)
                            {
                                invalidDataException = new InvalidDataException(string.Format("Document '{0}' is compressed, but the compression bundle is not enabled.\r\n" +
                                                                                              "You have to enable the compression bundle when dealing with compressed documents.", normalizedKey), e);
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    // we are already in error handling mode, just ignore this
                }
                if (invalidDataException != null)
                {
                    throw invalidDataException;
                }

                throw new InvalidDataException("Failed to de-serialize a document: " + normalizedKey, e);
            }
        }