private VersionedDocumentReadResult <TDocument> ReadDocumentAsync <TDocument>(
            string id,
            DocumentTypeMapping <TDocument> mapping,
            IList <VersionedDbDocument> documents,
            VersionedDocumentReadOptions readOptions)
        {
            VersionedDbDocument document;
            DateTime            createdTime;
            DateTime            modifiedTime;

            if (!TryGetLatestDocument(documents, readOptions, out document, out createdTime, out modifiedTime))
            {
                return(null);
            }

            var metadata = new VersionedDocumentMetadata(document.Version, document.Deleted, createdTime, modifiedTime, document.Actor);

            TDocument content;
            DocumentReadFailureDetails failure;

            if (!TryGetDocumentContent(document, mapping, out content, out failure))
            {
                return(VersionedDocumentReadResult <TDocument> .CreateFailure(id, metadata, failure));
            }

            return(VersionedDocumentReadResult <TDocument> .CreateOkay(id, metadata, content));
        }
        /// <inheritdoc />
        public async Task <VersionedDocumentQueryResult <TDocument> > GetDocumentsAsync <TDocument>(
            string query,
            IEnumerable <DbParameter> parameters,
            DocumentTypeMapping <TDocument> mapping,
            VersionedDocumentReadOptions options)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }
            if (mapping == null)
            {
                throw new ArgumentNullException(nameof(mapping));
            }

            parameters = parameters ?? new DbParameter[0];

            var builtQuery = QueryClient.CreateQueryByLatest(mapping, query, parameters);
            var documents  = await ExecuteQueryAsync(builtQuery);

            if (documents.Count == 0)
            {
                return(VersionedDocumentQueryResult <TDocument> .Empty);
            }

            options = options ?? new VersionedDocumentReadOptions();

            List <VersionedDocumentReadResult <TDocument> > loaded = new List <VersionedDocumentReadResult <TDocument> >();
            List <VersionedDocumentReadResult <TDocument> > failed = new List <VersionedDocumentReadResult <TDocument> >();

            foreach (var docs in documents.GroupBy(x => x.DocumentId))
            {
                VersionedDbDocument doc;
                DateTime            createdTime;
                DateTime            modifiedTime;

                if (!TryGetLatestDocument(docs, options, out doc, out createdTime, out modifiedTime))
                {
                    // Document exists but the latest version is deleted.
                    continue;
                }

                var metadata = new VersionedDocumentMetadata(doc.Version, doc.Deleted, createdTime, modifiedTime, doc.Actor);

                TDocument content;
                DocumentReadFailureDetails failure;

                if (TryGetDocumentContent(doc, mapping, out content, out failure))
                {
                    loaded.Add(VersionedDocumentReadResult <TDocument> .CreateOkay(doc.DocumentId, metadata, content));
                }
                else
                {
                    failed.Add(VersionedDocumentReadResult <TDocument> .CreateFailure(doc.DocumentId, metadata, failure));
                }
            }

            return(new VersionedDocumentQueryResult <TDocument>(ImmutableList.CreateRange(loaded), ImmutableList.CreateRange(failed)));
        }
        /// <summary>
        /// Initialises a new instance of the <see cref="VersionedDocumentWithMetadata{TDocument}"/> class.
        /// </summary>
        /// <param name="metadata">The metadata.</param>
        /// <param name="document">The document.</param>
        public VersionedDocumentWithMetadata(VersionedDocumentMetadata metadata, TDocument document)
        {
            if (metadata == null)
            {
                throw new ArgumentNullException(nameof(metadata));
            }
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            Metadata = metadata;
            Document = document;
        }
Esempio n. 4
0
        private VersionedDocumentReadResult(
            string documentId,
            VersionedDocumentMetadata metadata,
            TDocument document,
            DocumentReadFailureDetails failureDetails,
            DocumentReadResultType resultType)
        {
            DocumentId     = documentId;
            FailureDetails = failureDetails;
            ResultType     = resultType;

            // Both may be null.
            Metadata = metadata;
            Document = document;
        }
        private VersionedDocumentReadResult <TDocument> CreateReadResult <TDocument>(
            string id,
            VersionedDbDocument document,
            DocumentTypeMapping <TDocument> mapping,
            DateTime createdTime,
            DateTime modifiedTime)
        {
            var metadata = new VersionedDocumentMetadata(document.Version, document.Deleted, createdTime, modifiedTime, document.Actor);

            TDocument content;
            DocumentReadFailureDetails failure;

            if (!TryGetDocumentContent(document, mapping, out content, out failure))
            {
                return(VersionedDocumentReadResult <TDocument> .CreateFailure(id, metadata, failure));
            }

            return(VersionedDocumentReadResult <TDocument> .CreateOkay(id, metadata, content));
        }
        /// <summary>
        /// Initialises a new instance of the <see cref="VersionedDocumentUpsertResult{TDocument}"/> class.
        /// </summary>
        /// <param name="documentId">The document id.</param>
        /// <param name="metadata">The document metadata.</param>
        /// <param name="document">The document that was stored.</param>
        public VersionedDocumentUpsertResult(string documentId, VersionedDocumentMetadata metadata, TDocument document)
        {
            if (documentId == null)
            {
                throw new ArgumentNullException(nameof(documentId));
            }
            if (metadata == null)
            {
                throw new ArgumentNullException(nameof(metadata));
            }
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            DocumentId           = documentId;
            DocumentVersion      = metadata.Version;
            Document             = document;
            DocumentWithMetadata = new VersionedDocumentWithMetadata <TDocument>(metadata, document);
        }
Esempio n. 7
0
        /// <summary>
        /// Creates a unsuccessful read result.
        /// </summary>
        /// <param name="documentId">The document id.</param>
        /// <param name="metadata">The document metadata.</param>
        /// <param name="failureDetails">The failure details.</param>
        /// <returns>A document read result indicating failure.</returns>
        public static VersionedDocumentReadResult <TDocument> CreateFailure(
            string documentId,
            VersionedDocumentMetadata metadata,
            DocumentReadFailureDetails failureDetails)
        {
            if (documentId == null)
            {
                throw new ArgumentNullException(nameof(documentId));
            }
            if (metadata == null)
            {
                throw new ArgumentNullException(nameof(metadata));
            }
            if (failureDetails == null)
            {
                throw new ArgumentNullException(nameof(failureDetails));
            }

            return(new VersionedDocumentReadResult <TDocument>(documentId, metadata, default(TDocument), failureDetails, DocumentReadResultType.Failed));
        }
        public async Task <IList <VersionedDocumentWithMetadata <TDocument> > > GetDocumentsAsync <TDocument>(
            DocumentTypeMapping <TDocument> mapping,
            VersionedDocumentReadOptions options)
        {
            if (mapping == null)
            {
                throw new ArgumentNullException(nameof(mapping));
            }

            options = options ?? new VersionedDocumentReadOptions();

            var query     = QueryClient.CreateQueryByLatest(mapping, null);
            var documents = await ExecuteQueryAsync(query);

            List <VersionedDocumentWithMetadata <TDocument> > result = new List <VersionedDocumentWithMetadata <TDocument> >();

            foreach (var docs in documents.GroupBy(x => x.DocumentId))
            {
                VersionedDbDocument doc;
                DateTime            createdTime;
                DateTime            modifiedTime;

                if (!TryGetLatestDocument(docs, options, out doc, out createdTime, out modifiedTime))
                {
                    // Document exists but the latest version is deleted.
                    continue;
                }

                TDocument content;
                if (TryGetDocumentContent(doc, mapping, out content, out _))
                {
                    var metadata = new VersionedDocumentMetadata(doc.Version, doc.Deleted, createdTime, modifiedTime, doc.Actor);
                    result.Add(new VersionedDocumentWithMetadata <TDocument>(metadata, content));
                }
            }

            return(result);
        }
        /// <inheritdoc />
        public async Task <VersionedDocumentMetadataReadResult> GetDocumentMetadataAsync <TDocument>(
            string id,
            DocumentTypeMapping <TDocument> mapping)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (mapping == null)
            {
                throw new ArgumentNullException(nameof(mapping));
            }

            var query     = QueryClient.CreateQueryAllVersionsById(id, mapping);
            var documents = await ExecuteQueryAsync(query);

            var ordered = documents.OrderBy(d => d.Version).ToArray();

            if (ordered.Length == 0)
            {
                // Document not found.
                return(null);
            }

            var records = new List <VersionedDocumentMetadata>();

            var createdTime = ordered[0].Timestamp;

            foreach (var document in ordered)
            {
                var metadata = new VersionedDocumentMetadata(
                    document.Version, document.Deleted, createdTime, document.Timestamp, document.Actor);

                records.Add(metadata);
            }

            return(new VersionedDocumentMetadataReadResult(id, ImmutableList.CreateRange(records)));
        }
        /// <inheritdoc />
        public async Task <VersionedDocumentBatchReadResult <TDocument> > GetDocumentsAsync <TDocument>(
            IEnumerable <string> ids,
            DocumentTypeMapping <TDocument> mapping,
            VersionedDocumentReadOptions options)
        {
            if (ids == null)
            {
                throw new ArgumentNullException(nameof(ids));
            }
            if (mapping == null)
            {
                throw new ArgumentNullException(nameof(mapping));
            }

            var idSet = new HashSet <string>(ids);

            if (idSet.Count == 0)
            {
                return(VersionedDocumentBatchReadResult <TDocument> .Empty);
            }

            options = options ?? new VersionedDocumentReadOptions();

            var query     = QueryClient.CreateQueryByIds(idSet, mapping);
            var documents = await ExecuteQueriesAsync(query);

            List <VersionedDocumentReadResult <TDocument> > loaded = new List <VersionedDocumentReadResult <TDocument> >();
            List <VersionedDocumentReadResult <TDocument> > failed = new List <VersionedDocumentReadResult <TDocument> >();

            foreach (var docs in documents.GroupBy(x => x.DocumentId))
            {
                VersionedDbDocument doc;
                DateTime            createdTime;
                DateTime            modifiedTime;

                if (!TryGetLatestDocument(docs, options, out doc, out createdTime, out modifiedTime))
                {
                    // Document exists but the latest version is deleted.
                    continue;
                }

                var metadata = new VersionedDocumentMetadata(doc.Version, doc.Deleted, createdTime, modifiedTime, doc.Actor);

                TDocument content;
                DocumentReadFailureDetails failure;

                if (TryGetDocumentContent(doc, mapping, out content, out failure))
                {
                    loaded.Add(VersionedDocumentReadResult <TDocument> .CreateOkay(doc.DocumentId, metadata, content));
                }
                else
                {
                    failed.Add(VersionedDocumentReadResult <TDocument> .CreateFailure(doc.DocumentId, metadata, failure));
                }

                idSet.Remove(doc.DocumentId);
            }

            var missing = ImmutableList.CreateRange(idSet);

            return(new VersionedDocumentBatchReadResult <TDocument>(ImmutableList.CreateRange(loaded), missing, ImmutableList.CreateRange(failed)));
        }
Esempio n. 11
0
        /// <summary>
        /// Creates a successful read result.
        /// </summary>
        /// <param name="documentId">The document id.</param>
        /// <param name="metadata">The document metadata.</param>
        /// <param name="document">The document.</param>
        /// <returns>A document read result indicating success.</returns>
        public static VersionedDocumentReadResult <TDocument> CreateOkay(string documentId, VersionedDocumentMetadata metadata, TDocument document)
        {
            if (documentId == null)
            {
                throw new ArgumentNullException(nameof(documentId));
            }
            if (metadata == null)
            {
                throw new ArgumentNullException(nameof(metadata));
            }
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            return(new VersionedDocumentReadResult <TDocument>(documentId, metadata, document, null, DocumentReadResultType.Loaded));
        }