Пример #1
0
        public string this[string key]
        {
            get
            {
                if (_metadata != null)
                {
                    return(_metadata[key]);
                }
                object value;
                if (_source.TryGetMember(key, out value))
                {
                    return(value.ToString());
                }
                throw new KeyNotFoundException(key + "is not in the metadata");
            }

            set
            {
                if (_metadata == null)
                {
                    Init();
                }
                _metadata[key] = value;
            }
        }
Пример #2
0
        public object this[string key]
        {
            get
            {
                if (_metadata != null)
                {
                    return(_metadata[key]);
                }
                if (_source.TryGetMember(key, out var value))
                {
                    return(ConvertValue(value));
                }

                throw new KeyNotFoundException(key + " is not in the metadata");
            }

            set
            {
                if (_metadata == null)
                {
                    Init();
                }
                Debug.Assert(_metadata != null);
                _metadata[key] = value;
            }
        }
Пример #3
0
        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);
        }
Пример #4
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);
        }
Пример #5
0
 private static void MigrateOpenDataInfo(BlittableJsonReaderObject doc, Record record)
 {
     if (doc.TryGetMember("Publication", out object publication))
     {
         if (publication != null)
         {
             if (publication is BlittableJsonReaderObject jsonObject)
             {
                 if (jsonObject.TryGet("OpenData", out object openData))
                 {
                     if (openData != null)
                     {
                         if (openData is BlittableJsonReaderObject openDataObject)
                         {
                             MigrateAssessmentInfo(openDataObject, record);
                             MigrateSignOffInfo(openDataObject, record);
                             MigratePublishableStatus(openDataObject, record);
                             MigrateResources(openDataObject, record);
                             MigrateLastAttempt(openDataObject, record);
                             MigrateLastSuccess(openDataObject, record);
                         }
                     }
                 }
             }
         }
     }
 }
Пример #6
0
        public object GetValue(BlittableJsonReaderObject queryParameters)
        {
            switch (Value)
            {
            case ValueTokenType.Parameter:
                queryParameters.TryGetMember(Token, out var r);
                return(r);

            case ValueTokenType.Long:
                return(QueryBuilder.ParseInt64WithSeparators(Token.Value));

            case ValueTokenType.Double:
                return(double.Parse(Token.AsSpan(), NumberStyles.AllowThousands | NumberStyles.Float, CultureInfo.InvariantCulture));

            case ValueTokenType.String:
                return(Token);

            case ValueTokenType.True:
                return(QueryExpressionExtensions.True);

            case ValueTokenType.False:
                return(QueryExpressionExtensions.False);

            case ValueTokenType.Null:
                return(null);

            default:
                throw new InvalidOperationException("Unknown ValueExpression value: " + Value);
            }
        }
        private IEnumerable <string> GetPropertiesRecursive(string parentPath, BlittableJsonReaderObject obj)
        {
            var inMetadata = Constants.Documents.Metadata.Key.Equals(parentPath);

            foreach (var propery in obj.GetPropertyNames())
            {
                //skip properties starting with '@' unless we are in the metadata and we need to export @metadata.@collection
                if (inMetadata && propery.Equals(Constants.Documents.Metadata.Collection) == false)
                {
                    continue;
                }
                if (propery.StartsWith('@') && propery.Equals(Constants.Documents.Metadata.Key) == false && parentPath.Equals(Constants.Documents.Metadata.Key) == false)
                {
                    continue;
                }
                var    path = IsNullOrEmpty(parentPath) ? propery : $"{parentPath}.{propery}";
                object res;
                if (obj.TryGetMember(propery, out res) && res is BlittableJsonReaderObject)
                {
                    foreach (var nested in GetPropertiesRecursive(path, res as BlittableJsonReaderObject))
                    {
                        yield return(nested);
                    }
                }
                else
                {
                    yield return(path);
                }
            }
        }
Пример #8
0
        public static T GetOptions <T>(string optionsAsStringOrParameterName, ValueTokenType optionsType, BlittableJsonReaderObject parameters, JsonOperationContext context)
        {
            BlittableJsonReaderObject optionsJson;

            if (optionsType == ValueTokenType.Parameter)
            {
                if (parameters == null)
                {
                    throw new ArgumentNullException(nameof(parameters));
                }

                if (parameters.TryGetMember(optionsAsStringOrParameterName, out var optionsObject) == false)
                {
                    throw new InvalidOperationException($"Parameter '{optionsAsStringOrParameterName}' containing '{typeof(T).Name}' was not present in the list of parameters.");
                }

                optionsJson = optionsObject as BlittableJsonReaderObject;

                if (optionsJson == null)
                {
                    throw new InvalidOperationException($"Parameter '{optionsAsStringOrParameterName}' should contain JSON object.");
                }
            }
            else if (optionsType == ValueTokenType.String)
            {
                optionsJson = IndexReadOperation.ParseJsonStringIntoBlittable(optionsAsStringOrParameterName, context);
            }
            else
            {
                throw new InvalidOperationException($"Unknown options type '{optionsType}'.");
            }

            return(DocumentConventions.DefaultForServer.Serialization.DefaultConverter.FromBlittable <T>(optionsJson, "options"));
        }
Пример #9
0
        public List <string> GetTerms(JsonOperationContext context, BlittableJsonReaderObject parameters)
        {
            if (_terms != null)
            {
                return(_terms);
            }

            if (_termAsStringOrParameterName == null)
            {
                return(null);
            }

            var terms = new List <string>();

            if (_termType == AST.ValueTokenType.Parameter)
            {
                if (parameters == null)
                {
                    throw new ArgumentNullException(nameof(parameters));
                }

                if (parameters.TryGetMember(_termAsStringOrParameterName, out var termsJson) == false)
                {
                    throw new InvalidOperationException($"Parameter '{_termAsStringOrParameterName}' containing terms was not present in the list of parameters.");
                }

                if (termsJson is BlittableJsonReaderArray termsArray)
                {
                    foreach (var item in termsArray)
                    {
                        terms.Add(item.ToString());
                    }
                }

                if (termsJson is LazyStringValue lsv)
                {
                    terms.Add(lsv);
                }

                if (termsJson is LazyCompressedStringValue lcsv)
                {
                    terms.Add(lcsv);
                }

                return(terms);
            }

            if (_termType == AST.ValueTokenType.String)
            {
                terms.Add(_termAsStringOrParameterName);
                _terms = terms;

                return(terms);
            }

            throw new InvalidOperationException($"Unknown options type '{_optionsType}'.");
        }
Пример #10
0
        private void UnprotectSecuredSettingsOfDatabaseDocument(BlittableJsonReaderObject obj)
        {
            //TODO: implement this
            object securedSettings;

            if (obj.TryGetMember("SecuredSettings", out securedSettings) == false)
            {
            }
        }
Пример #11
0
        public bool PrepareSqlReplicationConfig(BlittableJsonReaderObject connections, bool writeToLog = true)
        {
            if (string.IsNullOrWhiteSpace(Configuration.ConnectionStringName) == false)
            {
                object connection;
                if (connections.TryGetMember(Configuration.ConnectionStringName, out connection))
                {
                    _predefinedSqlConnection = JsonDeserializationServer.PredefinedSqlConnection(connection as BlittableJsonReaderObject);
                    if (_predefinedSqlConnection != null)
                    {
                        return(true);
                    }
                }

                if (writeToLog)
                {
                    if (_logger.IsInfoEnabled)
                    {
                        _logger.Info("Could not find connection string named '" + Configuration.ConnectionStringName
                                     + "' for sql replication config: " + Configuration.Name + ", ignoring sql replication setting.");
                    }
                }
                Statistics.LastAlert = new Alert
                {
                    CreatedAt = SystemTime.UtcNow,
                    Type      = AlertType.SqlReplicationConnectionStringMissing,
                    Severity  = AlertSeverity.Error,
                    Message   = "Could not start replication",
                    Content   = new ExceptionAlertContent
                    {
                        Message = $"Could not find connection string named '{Configuration.ConnectionStringName}' for sql replication config: {Configuration.Name}, ignoring sql replication setting.",
                    }
                };
                return(false);
            }

            if (writeToLog)
            {
                if (_logger.IsInfoEnabled)
                {
                    _logger.Info("Connection string name cannot be empty for sql replication config: " + Configuration.ConnectionStringName + ", ignoring sql replication setting.");
                }
            }
            Statistics.LastAlert = new Alert
            {
                Type      = AlertType.SqlReplicationConnectionStringMissing,
                CreatedAt = SystemTime.UtcNow,
                Severity  = AlertSeverity.Error,
                Message   = "Could not start replication",
                Content   = new ExceptionAlertContent
                {
                    Message = $"Connection string name cannot be empty for sql replication config: {Configuration.Name}, ignoring sql replication setting."
                }
            };
            return(false);
        }
Пример #12
0
        public static (object Value, ValueTokenType Type) GetValue(string fieldName, Query query, QueryMetadata metadata, BlittableJsonReaderObject parameters,
                                                                   QueryExpression expr)
        {
            var value = expr as ValueExpression;

            if (value == null)
            {
                throw new InvalidQueryException("Expected value, but got: " + expr, query.QueryText, parameters);
            }

            if (value.Value == ValueTokenType.Parameter)
            {
                var parameterName = value.Token.Value;

                if (parameters == null)
                {
                    ThrowParametersWereNotProvided(metadata.QueryText);
                }

                if (parameters.TryGetMember(parameterName, out var parameterValue) == false)
                {
                    ThrowParameterValueWasNotProvided(parameterName, metadata.QueryText, parameters);
                }

                var parameterValueType = GetValueTokenType(parameterValue, metadata.QueryText, parameters);

                return(UnwrapParameter(parameterValue, parameterValueType), parameterValueType);
            }

            switch (value.Value)
            {
            case ValueTokenType.String:
                return(value.Token, ValueTokenType.String);

            case ValueTokenType.Long:
                var valueAsLong = long.Parse(value.Token);
                return(valueAsLong, ValueTokenType.Long);

            case ValueTokenType.Double:
                var valueAsDouble = double.Parse(value.Token, CultureInfo.InvariantCulture);
                return(valueAsDouble, ValueTokenType.Double);

            case ValueTokenType.True:
                return(LuceneDocumentConverterBase.TrueString, ValueTokenType.String);

            case ValueTokenType.False:
                return(LuceneDocumentConverterBase.FalseString, ValueTokenType.String);

            case ValueTokenType.Null:
                return(null, ValueTokenType.String);

            default:
                throw new ArgumentOutOfRangeException(nameof(value.Type), value.Type, null);
            }
        }
Пример #13
0
        public static bool TryGetChangeVector(this BlittableJsonReaderObject metadata, out string changeVector)
        {
            if (metadata.TryGetMember(Constants.Documents.Metadata.ChangeVector, out object changeVectorAsObject) == false)
            {
                changeVector = null;
                return(false);
            }

            changeVector = changeVectorAsObject as string;
            return(true);
        }
Пример #14
0
        private static void MigrateResources(BlittableJsonReaderObject openDataObject, Record record)
        {
            if (openDataObject.TryGetMember("Resources", out object resources))
            {
                if (resources != null)
                {
                    if (resources is BlittableJsonReaderArray resourcesArray)
                    {
                        Logger.Info("Migrating resources");

                        if (resourcesArray.Length > 0)
                        {
                            if (record.Resources == null)
                            {
                                record.Resources = new List <Resource>();
                            }

                            foreach (var resource in resourcesArray)
                            {
                                if (resource is BlittableJsonReaderObject resourceObject)
                                {
                                    if (resourceObject.TryGet("Path", out string path))
                                    {
                                        if (!record.Resources.Any(r => r.Path.Equals(path)))
                                        {
                                            var newResource = new Resource();
                                            newResource.Path = path;

                                            if (resourceObject.TryGet("Name", out string name))
                                            {
                                                newResource.Name = name;
                                            }
                                            else
                                            {
                                                newResource.Name = Path.GetFileName(path);
                                            }

                                            if (resourceObject.TryGet("PublishedUrl", out string publishedUrl))
                                            {
                                                newResource.PublishedUrl = publishedUrl;
                                            }

                                            record.Resources.Add(newResource);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #15
0
        public SuggestionOptions GetOptions(JsonOperationContext context, BlittableJsonReaderObject parameters)
        {
            if (_options != null)
            {
                return(_options);
            }

            if (_optionsAsStringOrParameterName == null)
            {
                return(null);
            }

            BlittableJsonReaderObject optionsJson;

            if (_optionsType == AST.ValueTokenType.Parameter)
            {
                if (parameters == null)
                {
                    throw new ArgumentNullException(nameof(parameters));
                }

                if (parameters.TryGetMember(_optionsAsStringOrParameterName, out var optionsObject) == false)
                {
                    throw new InvalidOperationException($"Parameter '{_optionsAsStringOrParameterName}' containing '{nameof(SuggestionOptions)}' was not present in the list of parameters.");
                }

                optionsJson = optionsObject as BlittableJsonReaderObject;

                if (optionsJson == null)
                {
                    throw new InvalidOperationException($"Parameter '{_optionsAsStringOrParameterName}' should contain JSON object.");
                }
            }
            else if (_optionsType == AST.ValueTokenType.String)
            {
                optionsJson = IndexReadOperation.ParseJsonStringIntoBlittable(_optionsAsStringOrParameterName, context);
            }
            else
            {
                throw new InvalidOperationException($"Unknown options type '{_optionsType}'.");
            }

            var options = (SuggestionOptions)EntityToBlittable.ConvertToEntity(typeof(SuggestionOptions), "suggestion/options", optionsJson, DocumentConventions.Default);

            if (_optionsType == AST.ValueTokenType.String)
            {
                _options = options;
            }

            return(options);
        }
Пример #16
0
        private static void MigrateAssessmentInfo(BlittableJsonReaderObject openDataObject, Record record)
        {
            if (openDataObject.TryGetMember("Assessment", out object assessment))
            {
                if (assessment != null)
                {
                    Logger.Info("Migrating assessment info");
                    if (assessment is BlittableJsonReaderObject assessmentObject)
                    {
                        record.Publication.Assessment = new AssessmentInfo();

                        if (assessmentObject.TryGet("Completed", out bool completed))
                        {
                            record.Publication.Assessment.Completed = completed;
                        }

                        if (assessmentObject.TryGetMember("CompletedByUser", out object user))
                        {
                            if (user != null)
                            {
                                if (user is BlittableJsonReaderObject userObject)
                                {
                                    record.Publication.Assessment.CompletedByUser = new UserInfo();

                                    if (userObject.TryGet("DisplayName", out string name))
                                    {
                                        record.Publication.Assessment.CompletedByUser.DisplayName = name;
                                    }

                                    if (userObject.TryGet("Email", out string email))
                                    {
                                        record.Publication.Assessment.CompletedByUser.Email = email;
                                    }
                                }
                            }
                        }

                        if (assessmentObject.TryGet("CompletedOnUtc", out DateTime datetime))
                        {
                            record.Publication.Assessment.CompletedOnUtc = datetime;
                        }

                        if (assessmentObject.TryGet("InitialAssessmentWasDoneOnSpreadsheet", out bool spreadsheetAssessment))
                        {
                            record.Publication.Assessment.InitialAssessmentWasDoneOnSpreadsheet = spreadsheetAssessment;
                        }
                    }
                }
            }
        }
Пример #17
0
 private static unsafe void BlitIndexing(List <BlittableJsonReaderObject> blitCache)
 {
     using (var blittableContext = JsonOperationContext.ShortTermSingleUse())
     {
         foreach (var tuple in blitCache)
         {
             var    doc = new BlittableJsonReaderObject(tuple.BasePointer, tuple.Size, blittableContext);
             object result;
             if (doc.TryGetMember("name", out result) == false)
             {
                 throw new InvalidOperationException();
             }
             if (doc.TryGetMember("overview", out result) == false)
             {
                 throw new InvalidOperationException();
             }
             if (doc.TryGetMember("video_embeds", out result) == false)
             {
                 throw new InvalidOperationException();
             }
         }
     }
 }
Пример #18
0
        private static void MigrateLastSuccess(BlittableJsonReaderObject openDataObject, Record record)
        {
            if (openDataObject.TryGetMember("LastSuccess", out object lastSuccess))
            {
                if (lastSuccess != null)
                {
                    Logger.Info("Migrating last success info");

                    if (record.Publication.Data == null)
                    {
                        record.Publication.Data = new DataInfo();
                    }

                    if (record.Publication.Data.LastSuccess == null)
                    {
                        record.Publication.Data.LastSuccess = new PublicationAttempt();
                    }

                    if (record.Publication.Target == null)
                    {
                        record.Publication.Target = new TargetInfo();
                    }

                    if (record.Publication.Target.Gov == null)
                    {
                        record.Publication.Target.Gov = new GovPublicationInfo();
                    }

                    if (record.Publication.Target.Gov.LastSuccess == null)
                    {
                        record.Publication.Target.Gov.LastSuccess = new PublicationAttempt();
                    }

                    if (lastSuccess is BlittableJsonReaderObject lastSuccessObject)
                    {
                        if (lastSuccessObject.TryGet("DateUtc", out DateTime datetime))
                        {
                            record.Publication.Data.LastSuccess.DateUtc       = datetime;
                            record.Publication.Target.Gov.LastSuccess.DateUtc = datetime;
                        }

                        if (lastSuccessObject.TryGet("Message", out string message))
                        {
                            record.Publication.Data.LastSuccess.Message       = message;
                            record.Publication.Target.Gov.LastSuccess.Message = message;
                        }
                    }
                }
            }
        }
Пример #19
0
        private static void MigrateResourceLocator(BlittableJsonReaderObject doc, Record record)
        {
            if (doc.TryGetMember("Gemini", out object gemini))
            {
                if (gemini is BlittableJsonReaderObject jsonObject)
                {
                    if (jsonObject.TryGet("ResourceLocator", out string resourceLocator))
                    {
                        if (!string.IsNullOrWhiteSpace(resourceLocator))
                        {
                            Logger.Info($"Migrating resource locator {resourceLocator}");

                            if (record.Resources == null)
                            {
                                record.Resources = new List <Resource>();
                            }

                            if (!record.Resources.Any(r => r.Path.Equals(resourceLocator)))
                            {
                                if (resourceLocator.Contains("http://data.jncc.gov.uk/data/"))
                                {
                                    var friendlyFilename = resourceLocator.Replace(
                                        "http://data.jncc.gov.uk/data/" + Helpers.RemoveCollection(record.Id) + "-",
                                        "");
                                    record.Resources.Add(new Resource
                                    {
                                        Name         = friendlyFilename,
                                        Path         = resourceLocator,
                                        PublishedUrl = resourceLocator
                                    });
                                }
                                else
                                {
                                    record.Resources.Add(new Resource
                                    {
                                        Name = "Published location for online access",
                                        Path = resourceLocator
                                    });
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #20
0
        private static void MigrateSignOffInfo(BlittableJsonReaderObject openDataObject, Record record)
        {
            if (openDataObject.TryGetMember("SignOff", out object signOff))
            {
                if (signOff != null)
                {
                    Logger.Info("Migrating sign off info");
                    if (signOff is BlittableJsonReaderObject signOffObject)
                    {
                        record.Publication.SignOff = new SignOffInfo();

                        if (signOffObject.TryGetMember("User", out object user))
                        {
                            if (user != null)
                            {
                                if (user is BlittableJsonReaderObject userObject)
                                {
                                    record.Publication.SignOff.User = new UserInfo();

                                    if (userObject.TryGet("DisplayName", out string name))
                                    {
                                        record.Publication.SignOff.User.DisplayName = name;
                                    }

                                    if (userObject.TryGet("Email", out string email))
                                    {
                                        record.Publication.SignOff.User.Email = email;
                                    }
                                }
                            }
                        }

                        if (signOffObject.TryGet("DateUtc", out DateTime datetime))
                        {
                            record.Publication.SignOff.DateUtc = datetime;
                        }

                        if (signOffObject.TryGet("Comment", out string comment))
                        {
                            record.Publication.SignOff.Comment = comment;
                        }
                    }
                }
            }
        }
 private IEnumerable <string> GetPropertiesRecursive(string parentPath, BlittableJsonReaderObject obj)
 {
     foreach (var propery in obj.GetPropertyNames())
     {
         var    path = IsNullOrEmpty(parentPath) ? propery : $"{parentPath}.{propery}";
         object res;
         if (obj.TryGetMember(propery, out res) && res is BlittableJsonReaderObject)
         {
             foreach (var nested in GetPropertiesRecursive(path, res as BlittableJsonReaderObject))
             {
                 yield return(nested);
             }
         }
         else
         {
             yield return(path);
         }
     }
 }
Пример #22
0
        private bool TryGetParameter(string key, out TransformerParameter parameter)
        {
            if (_parameters == null)
            {
                parameter = null;
                return(false);
            }

            object value;

            if (_parameters.TryGetMember(key, out value) == false)
            {
                parameter = null;
                return(false);
            }

            parameter = new TransformerParameter(value);
            return(true);
        }
Пример #23
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);
Пример #24
0
        protected ValueTokenType GetValueTokenType(BlittableJsonReaderObject parameters, ValueExpression value, bool unwrapArrays)
        {
            if (value.Value == ValueTokenType.Parameter)
            {
                if (parameters == null)
                {
                    QueryBuilder.ThrowParametersWereNotProvided(QueryText);
                    return(ValueTokenType.Null); // never hit
                }

                if (parameters.TryGetMember(value.Token, out var parameterValue) == false)
                {
                    QueryBuilder.ThrowParameterValueWasNotProvided(value.Token, QueryText, parameters);
                }

                return(QueryBuilder.GetValueTokenType(parameterValue, QueryText, parameters, unwrapArrays));
            }

            return(value.Value);
        }
Пример #25
0
        private static object True = true, False = false; // to avoid constant heap allocs
        private static object GetValue(QueryExpression qe, BlittableJsonReaderObject value, BlittableJsonReaderObject queryParameters)
        {
            switch (qe)
            {
            case ValueExpression ve:
                switch (ve.Value)
                {
                case ValueTokenType.Parameter:
                    queryParameters.TryGetMember(ve.Token, out var r);
                    return(r);

                case ValueTokenType.Long:
                    return(QueryBuilder.ParseInt64WithSeparators(ve.Token.Value));

                case ValueTokenType.Double:
                    return(double.Parse(ve.Token.AsSpan(), NumberStyles.AllowThousands | NumberStyles.Float, CultureInfo.InvariantCulture));

                case ValueTokenType.String:
                    return(ve.Token);

                case ValueTokenType.True:
                    return(True);

                case ValueTokenType.False:
                    return(False);

                case ValueTokenType.Null:
                    return(null);

                default:
                    throw new InvalidOperationException("Unknown ValueExpression value: " + ve.Value);
                }

            case FieldExpression fe:
                BlittableJsonTraverser.Default.TryRead(value, fe.FieldValue, out var result, out var leftPath);
                return(result);

            default:
                throw new NotSupportedException("Cannot get value of " + qe.Type + ", " + qe);
            }
        }
        public static bool TryGetExpires(BlittableJsonReaderObject value, out long ticks)
        {
            ticks = default;
            if (value.TryGetMember(Constants.Documents.Metadata.Key, out var metadata) == false || metadata is not BlittableJsonReaderObject metadataReader)
            {
                return(false);
            }

            if (metadataReader.TryGet(Constants.Documents.Metadata.Expires, out LazyStringValue expirationDate) == false)
            {
                return(false);
            }

            if (DateTime.TryParseExact(expirationDate, DefaultFormat.DateTimeFormatsToRead, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var date) == false)
            {
                throw new FormatException($"{Constants.Documents.Metadata.Expires} should contain date but has {expirationDate}': {value}");
            }

            ticks = date.ToUniversalTime().Ticks;
            return(true);
        }
Пример #27
0
        protected ValueTokenType GetValueTokenType(BlittableJsonReaderObject parameters, ValueToken value, bool unwrapArrays)
        {
            var valueType = value.Type;

            if (valueType == ValueTokenType.Parameter)
            {
                var parameterName = QueryExpression.Extract(QueryText, value);

                if (parameters == null)
                {
                    QueryBuilder.ThrowParametersWereNotProvided(QueryText);
                }

                if (parameters.TryGetMember(parameterName, out var parameterValue) == false)
                {
                    QueryBuilder.ThrowParameterValueWasNotProvided(parameterName, QueryText, parameters);
                }

                valueType = QueryBuilder.GetValueTokenType(parameterValue, QueryText, parameters, unwrapArrays);
            }

            return(valueType);
        }
Пример #28
0
        private static bool IsEqualTo(HashSet <string> excludedShallowProperties, BlittableJsonReaderObject myMetadata,
                                      BlittableJsonReaderObject objMetadata)
        {
            var properties = new HashSet <string>(myMetadata.GetPropertyNames());

            foreach (var propertyName in objMetadata.GetPropertyNames())
            {
                properties.Add(propertyName);
            }

            foreach (var property in properties)
            {
                if (excludedShallowProperties.Contains(property))
                {
                    continue;
                }

                object myProperty;
                object objProperty;

                if (myMetadata.TryGetMember(property, out myProperty) == false)
                {
                    return(false);
                }

                if (objMetadata.TryGetMember(property, out objProperty) == false)
                {
                    return(false);
                }

                if (Equals(myProperty, objProperty) == false)
                {
                    return(false);
                }
            }
            return(true);
        }
        public static bool HasExpiredMetadata(BlittableJsonReaderObject value, out long ticks, Slice keySlice, string storageKey = null)
        {
            ticks = default;
            if (value.TryGetMember(Constants.Documents.Metadata.Key, out var metadata) == false || metadata == null)
            {
                return(false);
            }

            if (metadata is BlittableJsonReaderObject bjro && bjro.TryGet(Constants.Documents.Metadata.Expires, out object obj))
            {
                if (obj is LazyStringValue expirationDate)
                {
                    if (DateTime.TryParseExact(expirationDate, DefaultFormat.DateTimeFormatsToRead, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind,
                                               out DateTime date) == false)
                    {
                        storageKey ??= keySlice.ToString();

                        var inner = new InvalidOperationException(
                            $"The expiration date format for compare exchange '{CompareExchangeKey.SplitStorageKey(storageKey).Key}' is not valid: '{expirationDate}'. Use the following format: {DateTime.UtcNow:O}");
                        throw new RachisApplyException("Could not apply command.", inner);
                    }

                    var expiry = date.ToUniversalTime();
                    ticks = expiry.Ticks;
                    return(true);
                }
                else
                {
                    storageKey ??= keySlice.ToString();
                    var inner = new InvalidOperationException(
                        $"The type of {Constants.Documents.Metadata.Expires} metadata for compare exchange '{CompareExchangeKey.SplitStorageKey(storageKey).Key}' is not valid. Use the following type: {nameof(DateTime)}");
                    throw new RachisApplyException("Could not apply command.", inner);
                }
            }

            return(false);
        }
Пример #30
0
        private static DocumentCompareResult ComparePropertiesExceptStartingWithAt(BlittableJsonReaderObject current, BlittableJsonReaderObject modified,
                                                                                   bool isMetadata = false, bool tryMergeAttachmentsConflict = false)
        {
            var resolvedAttachmetConflict = false;

            var properties = new HashSet <string>(current.GetPropertyNames());

            foreach (var propertyName in modified.GetPropertyNames())
            {
                properties.Add(propertyName);
            }

            foreach (var property in properties)
            {
                if (property[0] == '@')
                {
                    if (isMetadata)
                    {
                        if (property.Equals(Constants.Documents.Metadata.Attachments, StringComparison.OrdinalIgnoreCase))
                        {
                            if (tryMergeAttachmentsConflict)
                            {
                                if (current.TryGetMember(property, out object _) == false ||
                                    modified.TryGetMember(property, out object _) == false)
                                {
                                    // Resolve when just 1 document have attachments
                                    resolvedAttachmetConflict = true;
                                    continue;
                                }

                                resolvedAttachmetConflict = ShouldResolveAttachmentsConflict(current, modified);
                                if (resolvedAttachmetConflict)
                                {
                                    continue;
                                }

                                return(DocumentCompareResult.NotEqual);
                            }
                        }
                        else if (property.Equals(Constants.Documents.Metadata.Collection, StringComparison.OrdinalIgnoreCase) == false)
                        {
                            continue;
                        }
                    }
                    else if (property.Equals(Constants.Documents.Metadata.Key, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }
                }

                if (current.TryGetMember(property, out object currentProperty) == false ||
                    modified.TryGetMember(property, out object modifiedPropery) == false)
                {
                    return(DocumentCompareResult.NotEqual);
                }

                if (Equals(currentProperty, modifiedPropery) == false)
                {
                    return(DocumentCompareResult.NotEqual);
                }
            }

            return(DocumentCompareResult.Equal | (resolvedAttachmetConflict ? DocumentCompareResult.ShouldRecreateDocument : DocumentCompareResult.None));
        }