Exemple #1
0
        public T[] Complete <T>()
        {
            for (var i = 0; i < includeResults.Length; i++)
            {
                var include    = includeResults[i];
                var entityType = includes.Length > i ? includes[i].Value : typeof(object);
                sessionOperations.TrackEntity(entityType, include);
            }

            var finalResults = SelectResults()
                               .Select(document => document == null ? default(T) : sessionOperations.TrackEntity <T>(document))
                               .ToArray();

            for (var i = 0; i < finalResults.Length; i++)
            {
                if (ReferenceEquals(finalResults[i], null))
                {
                    sessionOperations.RegisterMissing(ids[i]);
                }
            }

            var includePaths = includes != null?includes.Select(x => x.Key).ToArray() : null;

            sessionOperations.RegisterMissingIncludes(results.Where(x => x != null).Select(x => x.DataAsJson), includePaths);

            return(finalResults);
        }
Exemple #2
0
        public IList <T> Complete <T>()
        {
            var queryResult = currentQueryResults.CreateSnapshot();

            foreach (var include in queryResult.Includes)
            {
                var metadata = include.Value <RavenJObject>("@metadata");

                sessionOperations.TrackEntity <object>(metadata.Value <string>("@id"),
                                                       include,
                                                       metadata, disableEntitiesTracking);
            }
            var list = queryResult.Results
                       .Select(Deserialize <T>)
                       .ToList();

            sessionOperations.RegisterMissingIncludes(queryResult.Results, includes);

            if (transformResults == null)
            {
                return(list);
            }

            return(transformResults(indexQuery, list.Cast <object>()).Cast <T>().ToList());
        }
Exemple #3
0
        private T GetDocument <T>(string id)
        {
            if (id == null)
            {
                return(default(T));
            }

            if (_session.IsDeleted(id))
            {
                return(default(T));
            }

            DocumentInfo doc;

            if (_session.DocumentsById.TryGetValue(id, out doc))
            {
                return(_session.TrackEntity <T>(doc));
            }

            if (_session.IncludedDocumentsById.TryGetValue(id, out doc))
            {
                return(_session.TrackEntity <T>(doc));
            }

            return(default(T));
        }
        public T[] Complete <T>()
        {
            foreach (var include in includeResults)
            {
                sessionOperations.TrackEntity <object>(include);
            }

            return(results
                   .Select(document => document == null ? default(T) : sessionOperations.TrackEntity <T>(document))
                   .ToArray());
        }
        private T[] ReturnResults <T>()
        {
            var finalResults = new T[results.Length];

            for (int i = 0; i < results.Length; i++)
            {
                if (results[i] != null)
                {
                    finalResults[i] = sessionOperations.TrackEntity <T>(results[i]);
                }
            }
            return(finalResults);
        }
        public void HandleResponse(GetResponse response)
        {
            if (response.ForceRetry)
            {
                Result        = null;
                RequiresRetry = true;
                return;
            }

            switch (response.StatusCode)
            {
            case HttpStatusCode.NotModified:
                Result = (default(T), _changeVector);     // value not changed
                return;

            case HttpStatusCode.NotFound:
                _session.RegisterMissing(_id);
                Result = default((T, string));
                return;
            }

            if (response.Result != null)
            {
                var etag         = response.Headers[Constants.Headers.Etag];
                var res          = JsonDeserializationClient.ConditionalGetResult((BlittableJsonReaderObject)response.Result);
                var documentInfo = DocumentInfo.GetNewDocumentInfo((BlittableJsonReaderObject)res.Results[0]);
                var r            = _session.TrackEntity <T>(documentInfo);

                Result = (r, etag);
                return;
            }

            Result = null;
            _session.RegisterMissing(_id);
        }
        public T[] GetDocuments <T>()
        {
            var i = 0;

            T[] finalResults;

            if (_session.NoTracking)
            {
                if (_resultsSet == false)
                {
                    throw new InvalidOperationException($"Cannot execute '{nameof(GetDocuments)}' before operation execution.");
                }

                if (_results == null || _results.Results == null || _results.Results.Length == 0)
                {
                    return(Array.Empty <T>());
                }

                finalResults = new T[_results.Results.Length];
                foreach (var document in GetDocumentsFromResult(_results))
                {
                    finalResults[i++] = _session.TrackEntity <T>(document);
                }
            }
            else
            {
                finalResults = new T[_returnedIds.Count];
                foreach (var id in _returnedIds)
                {
                    finalResults[i++] = GetDocument <T>(id);
                }
            }

            return(finalResults);
        }
        public void HandleResponse(GetResponse response)
        {
            var getDocumentResult = JsonDeserializationClient.GetDocumentsResult((BlittableJsonReaderObject)response.Result);

            var finalResults = new Dictionary <string, T>(StringComparer.OrdinalIgnoreCase);

            foreach (BlittableJsonReaderObject document in getDocumentResult.Results)
            {
                var newDocumentInfo = DocumentInfo.GetNewDocumentInfo(document);
                _sessionOperations.DocumentsById.Add(newDocumentInfo);
                if (newDocumentInfo.Id == null)
                {
                    continue; // is this possible?
                }
                if (_sessionOperations.IsDeleted(newDocumentInfo.Id))
                {
                    finalResults[newDocumentInfo.Id] = default(T);
                    continue;
                }
                DocumentInfo doc;
                if (_sessionOperations.DocumentsById.TryGetValue(newDocumentInfo.Id, out doc))
                {
                    finalResults[newDocumentInfo.Id] = _sessionOperations.TrackEntity <T>(doc);
                    continue;
                }

                finalResults[newDocumentInfo.Id] = default(T);
            }

            Result = finalResults;
        }
Exemple #9
0
 public T Complete <T>()
 {
     if (documentFound == null)
     {
         return(default(T));
     }
     return(sessionOperations.TrackEntity <T>(documentFound));
 }
Exemple #10
0
 public virtual T Complete <T>()
 {
     if (documentFound == null)
     {
         sessionOperations.RegisterMissing(id);
         return(default(T));
     }
     return(sessionOperations.TrackEntity <T>(documentFound));
 }
Exemple #11
0
        private T ApplyTrackingIfNeeded <T>(JsonDocument document)
        {
            if (document != null)
            {
                return(sessionOperations.TrackEntity <T>(document));
            }

            return(default(T));
        }
Exemple #12
0
        public IList <T> Complete <T>()
        {
            var queryResult = currentQueryResults.CreateSnapshot();

            foreach (var include in queryResult.Includes)
            {
                var metadata = include.Value <RavenJObject>("@metadata");

                sessionOperations.TrackEntity <object>(metadata.Value <string>("@id"),
                                                       include,
                                                       metadata);
            }
            var list = queryResult.Results
                       .Select(Deserialize <T>)
                       .ToList();

            return(list);
        }
        internal static T Deserialize <T>(string id, BlittableJsonReaderObject document, BlittableJsonReaderObject metadata, FieldsToFetchToken fieldsToFetch, bool disableEntitiesTracking, InMemoryDocumentSessionOperations session)
        {
            if (metadata.TryGetProjection(out var projection) == false || projection == false)
            {
                return(session.TrackEntity <T>(id, document, metadata, disableEntitiesTracking));
            }

            if (fieldsToFetch?.Projections != null && fieldsToFetch.Projections.Length == 1) // we only select a single field
            {
                var type     = typeof(T);
                var typeInfo = type.GetTypeInfo();
                if (type == typeof(string) || typeInfo.IsValueType || typeInfo.IsEnum)
                {
                    var projectionField = fieldsToFetch.Projections[0];
                    T   value;

                    if (fieldsToFetch.SourceAlias != null)
                    {
                        // remove source-alias from projection name
                        projectionField = projectionField.Substring(fieldsToFetch.SourceAlias.Length + 1);
                    }

                    return(document.TryGet(projectionField, out value) == false
                        ? default(T)
                        : value);
                }

                if (document.TryGetMember(fieldsToFetch.Projections[0], out object inner) == false)
                {
                    return(default(T));
                }

                if (fieldsToFetch.FieldsToFetch != null && fieldsToFetch.FieldsToFetch[0] == fieldsToFetch.Projections[0])
                {
                    if (inner is BlittableJsonReaderObject innerJson) //extraction from original type
                    {
                        document = innerJson;
                    }
                }
            }

            var result = (T)session.Conventions.DeserializeEntityFromBlittable(typeof(T), document);

            if (string.IsNullOrEmpty(id) == false)
            {
                // we need to make an additional check, since it is possible that a value was explicitly stated
                // for the identity property, in which case we don't want to override it.
                object value;
                var    identityProperty = session.Conventions.GetIdentityProperty(typeof(T));
                if (identityProperty != null && (document.TryGetMember(identityProperty.Name, out value) == false || value == null))
                {
                    session.GenerateEntityIdOnTheClient.TrySetIdentity(result, id);
                }
            }

            return(result);
        }
Exemple #14
0
        public IList <T> Complete <T>()
        {
            var queryResult = _currentQueryResults.CreateSnapshot();

            foreach (BlittableJsonReaderObject include in queryResult.Includes)
            {
                var newDocumentInfo = DocumentInfo.GetNewDocumentInfo(include);
                _session.includedDocumentsByKey[newDocumentInfo.Id] = newDocumentInfo;
            }

            var usedTransformer = string.IsNullOrEmpty(_indexQuery.Transformer) == false;
            var list            = new List <T>();

            foreach (BlittableJsonReaderObject document in queryResult.Results)
            {
                if (usedTransformer)
                {
                    BlittableJsonReaderArray values;
                    if (document.TryGet("$values", out values) == false)
                    {
                        throw new InvalidOperationException("Transformed document must have a $values property");
                    }

                    list.AddRange(TransformerHelpers.ParseValuesFromBlittableArray <T>(_session, values));

                    continue;
                }

                BlittableJsonReaderObject metadata;
                string id;
                if (document.TryGet(Constants.Metadata.Key, out metadata) == false)
                {
                    throw new InvalidOperationException("Document must have a metadata");
                }
                if (metadata.TryGet(Constants.Metadata.Id, out id) == false)
                {
                    throw new InvalidOperationException("Document must have an id");
                }

                list.Add(_session.TrackEntity <T>(id, document, metadata, _disableEntitiesTracking));
            }

            if (_disableEntitiesTracking == false)
            {
                _session.RegisterMissingIncludes(queryResult.Results, _includes);
            }

            if (_transformResults == null)
            {
                return(list);
            }

            return(_transformResults(_indexQuery, list.Cast <object>()).Cast <T>().ToList());
        }
Exemple #15
0
        public T[] Complete <T>()
        {
            foreach (var include in includeResults)
            {
                sessionOperations.TrackEntity <object>(include);
            }

            return(results
                   .Select(sessionOperations.TrackEntity <T>)
                   .ToArray());
        }
Exemple #16
0
        public T Deserialize <T>(RavenJObject result)
        {
            var metadata = result.Value <RavenJObject>("@metadata");

            if ((projectionFields == null || projectionFields.Length <= 0) &&
                (metadata != null && string.IsNullOrEmpty(metadata.Value <string>("@id")) == false))
            {
                return(sessionOperations.TrackEntity <T>(metadata.Value <string>("@id"),
                                                         result,
                                                         metadata, disableEntitiesTracking));
            }

            if (typeof(T) == typeof(RavenJObject))
            {
                return((T)(object)result);
            }

            if (typeof(T) == typeof(object) && string.IsNullOrEmpty(result.Value <string>("$type")))
            {
                return((T)(object)new DynamicJsonObject(result));
            }

            var documentId = result.Value <string>(Constants.DocumentIdFieldName);  //check if the result contain the reserved name

            if (!string.IsNullOrEmpty(documentId) && typeof(T) == typeof(string) && // __document_id is present, and result type is a string
                                                                                    // We are projecting one field only (although that could be derived from the
                                                                                    // previous check, one could never be too careful
                projectionFields != null && projectionFields.Length == 1 &&
                HasSingleValidProperty(result, metadata)                            // there are no more props in the result object
                )
            {
                return((T)(object)documentId);
            }

            sessionOperations.HandleInternalMetadata(result);

            var deserializedResult = DeserializedResult <T>(result);

            if (string.IsNullOrEmpty(documentId) == false)
            {
                // we need to make an additional check, since it is possible that a value was explicitly stated
                // for the identity property, in which case we don't want to override it.
                var identityProperty = sessionOperations.Conventions.GetIdentityProperty(typeof(T));
                if (identityProperty != null &&
                    (result[identityProperty.Name] == null ||
                     result[identityProperty.Name].Type == JTokenType.Null))
                {
                    sessionOperations.GenerateEntityIdOnTheClient.TrySetIdentity(deserializedResult, documentId);
                }
            }

            return(deserializedResult);
        }
Exemple #17
0
        public T[] Complete <T>()
        {
            foreach (var include in includeResults)
            {
                sessionOperations.TrackEntity <object>(include);
            }


            var finalResults = SelectResults()
                               .Select(document => document == null ? default(T) : sessionOperations.TrackEntity <T>(document))
                               .ToArray();

            for (var i = 0; i < finalResults.Length; i++)
            {
                if (ReferenceEquals(finalResults[i], null))
                {
                    sessionOperations.RegisterMissing(ids[i]);
                }
            }

            sessionOperations.RegisterMissingIncludes(results.Where(x => x != null).Select(x => x.DataAsJson), includes);

            return(finalResults);
        }
Exemple #18
0
        internal static T Deserialize <T>(string id, BlittableJsonReaderObject document, BlittableJsonReaderObject metadata, string[] projectionFields, bool disableEntitiesTracking, InMemoryDocumentSessionOperations session)
        {
            if (projectionFields == null || projectionFields.Length == 0)
            {
                return(session.TrackEntity <T>(id, document, metadata, disableEntitiesTracking));
            }

            if (projectionFields.Length == 1) // we only select a single field
            {
                var type     = typeof(T);
                var typeInfo = type.GetTypeInfo();
                if (type == typeof(string) || typeInfo.IsValueType || typeInfo.IsEnum)
                {
                    var projectionField = projectionFields[0];
                    T   value;
                    return(document.TryGet(projectionField, out value) == false
                        ? default(T)
                        : value);
                }

                if (document.TryGetMember(projectionFields[0], out object inner) == false)
                {
                    return(default(T));
                }

                var innerJson = inner as BlittableJsonReaderObject;
                if (innerJson != null)
                {
                    document = innerJson;
                }
            }

            var result = (T)session.Conventions.DeserializeEntityFromBlittable(typeof(T), document);

            if (string.IsNullOrEmpty(id) == false)
            {
                // we need to make an additional check, since it is possible that a value was explicitly stated
                // for the identity property, in which case we don't want to override it.
                object value;
                var    identityProperty = session.Conventions.GetIdentityProperty(typeof(T));
                if (identityProperty != null && (document.TryGetMember(identityProperty.Name, out value) == false || value == null))
                {
                    session.GenerateEntityIdOnTheClient.TrySetIdentity(result, id);
                }
            }

            return(result);
        }
Exemple #19
0
        internal static T Deserialize <T>(string id, BlittableJsonReaderObject document, BlittableJsonReaderObject metadata, FieldsToFetchToken fieldsToFetch, bool disableEntitiesTracking, InMemoryDocumentSessionOperations session, bool isProjectInto)
        {
            if (metadata.TryGetProjection(out var projection) == false || projection == false)
            {
                return(session.TrackEntity <T>(id, document, metadata, disableEntitiesTracking));
            }

            var type = typeof(T);

            if (fieldsToFetch?.Projections != null && fieldsToFetch.Projections.Length == 1) // we only select a single field
            {
                var typeInfo        = type;
                var projectionField = fieldsToFetch.Projections[0];

                if (fieldsToFetch.SourceAlias != null)
                {
                    if (projectionField.StartsWith(fieldsToFetch.SourceAlias))
                    {
                        // remove source-alias from projection name
                        projectionField = projectionField.Substring(fieldsToFetch.SourceAlias.Length + 1);
                    }

                    if (Regex.IsMatch(projectionField, "'([^']*)"))
                    {
                        // projection field is quoted, remove quotes
                        projectionField = projectionField.Substring(1, projectionField.Length - 2);
                    }
                }

                if (type == typeof(string) || typeInfo.IsValueType || typeInfo.IsEnum)
                {
                    return(document.TryGet(projectionField, out T value) == false
                        ? default
                        : value);
                }

                var isTimeSeriesField = fieldsToFetch.Projections[0].StartsWith(Constants.TimeSeries.QueryFunction);

                if (isProjectInto == false || isTimeSeriesField)
                {
                    if (document.TryGetMember(projectionField, out object inner) == false)
                    {
                        return(default);